backing out bug 393246 for final to resolve bug 432836, a=schrep
authormconnor@steelgryphon.com
Fri, 09 May 2008 17:53:37 -0700
changeset 15098 c3a5c2cbeba853332a0afc38c230c1990fb2bd7e
parent 15097 40bfaf37b5dbed0dc3968ae0955ef3ef24d7a478
child 15099 7c49d2ac0beb8135e7e0837e6f32ccb56c87e869
push idunknown
push userunknown
push dateunknown
reviewersschrep
bugs393246, 432836
milestone1.9pre
backing out bug 393246 for final to resolve bug 432836, a=schrep
docshell/base/nsDefaultURIFixup.cpp
docshell/base/nsDefaultURIFixup.h
--- a/docshell/base/nsDefaultURIFixup.cpp
+++ b/docshell/base/nsDefaultURIFixup.cpp
@@ -41,16 +41,17 @@
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "nsNetUtil.h"
 #include "nsEscape.h"
 #include "nsCRT.h"
 
 #include "nsIPrefService.h"
 #include "nsIPrefLocalizedString.h"
+#include "nsIPlatformCharset.h"
 #include "nsILocalFile.h"
 
 #include "nsIURIFixup.h"
 #include "nsDefaultURIFixup.h"
 
 /* Implementation file */
 NS_IMPL_ISUPPORTS1(nsDefaultURIFixup, nsIURIFixup)
 
@@ -226,26 +227,38 @@ nsDefaultURIFixup::CreateFixupURI(const 
                 if (*start == '\\')
                     *start = '/';
                 ++start;
             }
         }
 #endif
     }
 
+    // For these protocols, use system charset instead of the default UTF-8,
+    // if the URI is non ASCII.
+    PRBool bAsciiURI = IsASCII(uriString);
+    PRBool bUseNonDefaultCharsetForURI =
+                        !bAsciiURI &&
+                        (scheme.IsEmpty() ||
+                         scheme.LowerCaseEqualsLiteral("http") ||
+                         scheme.LowerCaseEqualsLiteral("https") ||
+                         scheme.LowerCaseEqualsLiteral("ftp") ||
+                         scheme.LowerCaseEqualsLiteral("file"));
+
     // Now we need to check whether "scheme" is something we don't
     // really know about.
     nsCOMPtr<nsIProtocolHandler> ourHandler, extHandler;
     
     ioService->GetProtocolHandler(scheme.get(), getter_AddRefs(ourHandler));
     extHandler = do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"default");
     
     if (ourHandler != extHandler || !PossiblyHostPortUrl(uriString)) {
         // Just try to create an URL out of it
-        rv = NS_NewURI(aURI, uriString, nsnull);
+        rv = NS_NewURI(aURI, uriString,
+                       bUseNonDefaultCharsetForURI ? GetCharsetForUrlBar() : nsnull);
 
         if (!*aURI && rv != NS_ERROR_MALFORMED_URI) {
             return rv;
         }
     }
     
     if (*aURI) {
         if (aFixupFlags & FIXUP_FLAGS_MAKE_ALTERNATE_URI)
@@ -305,19 +318,23 @@ nsDefaultURIFixup::CreateFixupURI(const 
         nsCAutoString hostSpec;
         uriString.Left(hostSpec, hostPos);
 
         // insert url spec corresponding to host name
         if (IsLikelyFTP(hostSpec))
             uriString.Assign(NS_LITERAL_CSTRING("ftp://") + uriString);
         else 
             uriString.Assign(NS_LITERAL_CSTRING("http://") + uriString);
+
+        // For ftp & http, we want to use system charset.
+        if (!bAsciiURI)
+          bUseNonDefaultCharsetForURI = PR_TRUE;
     } // end if checkprotocol
 
-    rv = NS_NewURI(aURI, uriString, nsnull);
+    rv = NS_NewURI(aURI, uriString, bUseNonDefaultCharsetForURI ? GetCharsetForUrlBar() : nsnull);
 
     // Did the caller want us to try an alternative URI?
     // If so, attempt to fixup http://foo into http://www.foo.com
 
     if (*aURI && aFixupFlags & FIXUP_FLAGS_MAKE_ALTERNATE_URI) {
         MakeAlternateURI(*aURI);
     }
 
@@ -734,16 +751,51 @@ PRBool nsDefaultURIFixup::PossiblyByteEx
     {
         if (*iter >= 0x0080 && *iter <= 0x00FF)
             return PR_TRUE;
         ++iter;
     }
     return PR_FALSE;
 }
 
+const char * nsDefaultURIFixup::GetFileSystemCharset()
+{
+  if (mFsCharset.IsEmpty())
+  {
+    nsresult rv;
+    nsCAutoString charset;
+    nsCOMPtr<nsIPlatformCharset> plat(do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv));
+    if (NS_SUCCEEDED(rv))
+      rv = plat->GetCharset(kPlatformCharsetSel_FileName, charset);
+
+    if (charset.IsEmpty())
+      mFsCharset.AssignLiteral("ISO-8859-1");
+    else
+      mFsCharset.Assign(charset);
+  }
+
+  return mFsCharset.get();
+}
+
+const char * nsDefaultURIFixup::GetCharsetForUrlBar()
+{
+  const char *charset = GetFileSystemCharset();
+#ifdef XP_MAC
+  // check for "x-mac-" prefix
+  if ((strlen(charset) >= 6) && charset[0] == 'x' && charset[2] == 'm')
+  {
+    if (!strcmp("x-mac-roman", charset))
+      return "ISO-8859-1";
+    // we can do more x-mac-xxxx mapping here
+    // or somewhere in intl code like nsIPlatformCharset.
+  }
+#endif
+  return charset;
+}
+
 nsresult nsDefaultURIFixup::KeywordURIFixup(const nsACString & aURIString, 
                                             nsIURI** aURI)
 {
     // These are keyword formatted strings
     // "what is mozilla"
     // "what is mozilla?"
     // "docshell site:mozilla.org" - has no dot/colon in the first space-separated substring
     // "?mozilla" - anything that begins with a question mark
--- a/docshell/base/nsDefaultURIFixup.h
+++ b/docshell/base/nsDefaultURIFixup.h
@@ -63,13 +63,16 @@ private:
     /* additional members */
     nsresult FileURIFixup(const nsACString &aStringURI, nsIURI** aURI);
     nsresult ConvertFileToStringURI(const nsACString& aIn, nsCString& aOut);
     nsresult KeywordURIFixup(const nsACString &aStringURI, nsIURI** aURI);
     PRBool PossiblyByteExpandedFileName(const nsAString& aIn);
     PRBool PossiblyHostPortUrl(const nsACString& aUrl);
     PRBool MakeAlternateURI(nsIURI *aURI);
     PRBool IsLikelyFTP(const nsCString& aHostSpec);
+    const char * GetFileSystemCharset();
+    const char * GetCharsetForUrlBar();
 
     nsCOMPtr<nsIPrefBranch> mPrefBranch;
+    nsCAutoString mFsCharset;
 };
 
 #endif