Jelajahi Sumber

Encoding all non-ASCII characters in URLs to allow posting bug report and searching for message using web browsers that do not default to UTF-8 encoding for URLs (like Internet Explorer).

Source commit: 0b409c77ec9a763417687a7e7085e5213199b09a
Martin Prikryl 10 tahun lalu
induk
melakukan
7e79e8c4f3
1 mengubah file dengan 18 tambahan dan 33 penghapusan
  1. 18 33
      source/core/Common.cpp

+ 18 - 33
source/core/Common.cpp

@@ -2289,39 +2289,30 @@ UnicodeString __fastcall DecodeUrlChars(UnicodeString S)
   return S;
 }
 //---------------------------------------------------------------------------
-UnicodeString __fastcall DoEncodeUrl(UnicodeString S, UnicodeString Chars)
+UnicodeString __fastcall DoEncodeUrl(UnicodeString S, bool EncodeSlash)
 {
-  int i = 1;
-  while (i <= S.Length())
-  {
-    if (Chars.Pos(S[i]) > 0)
-    {
-      // We decode as UTF-8 so we should encode as UTF-8 too
-      UnicodeString H = ByteToHex(AnsiString(UnicodeString(S[i]))[1]);
-      S.Insert(H, i + 1);
-      S[i] = '%';
-      i += H.Length();
-    }
-    i++;
-  }
-  return S;
-}
-//---------------------------------------------------------------------------
-UnicodeString __fastcall NonUrlChars()
-{
-  UnicodeString S;
-  for (unsigned int I = 0; I <= 127; I++)
+  int Index = 1;
+  while (Index <= S.Length())
   {
-    wchar_t C = static_cast<wchar_t>(I);
+    wchar_t C = S[Index];
     if (IsLetter(C) ||
         IsDigit(C) ||
-        (C == L'_') || (C == L'-') || (C == L'.'))
+        (C == L'_') || (C == L'-') || (C == L'.') ||
+        ((C == L'/') && !EncodeSlash))
     {
-      // noop
+      Index++;
     }
     else
     {
-      S += C;
+      UTF8String UtfS(S.SubString(Index, 1));
+      UnicodeString H;
+      for (int Index2 = 1; Index2 <= UtfS.Length(); Index2++)
+      {
+        H += L"%" + ByteToHex(static_cast<unsigned char>(UtfS[Index2]));
+      }
+      S.Delete(Index, 1);
+      S.Insert(H, Index);
+      Index += H.Length();
     }
   }
   return S;
@@ -2329,18 +2320,12 @@ UnicodeString __fastcall NonUrlChars()
 //---------------------------------------------------------------------------
 UnicodeString __fastcall EncodeUrlString(UnicodeString S)
 {
-  return DoEncodeUrl(S, NonUrlChars());
+  return DoEncodeUrl(S, true);
 }
 //---------------------------------------------------------------------------
 UnicodeString __fastcall EncodeUrlPath(UnicodeString S)
 {
-  UnicodeString Ignore = NonUrlChars();
-  int P = Ignore.Pos(L"/");
-  if (DebugAlwaysTrue(P > 0))
-  {
-    Ignore.Delete(P, 1);
-  }
-  return DoEncodeUrl(S, Ignore);
+  return DoEncodeUrl(S, false);
 }
 //---------------------------------------------------------------------------
 UnicodeString __fastcall AppendUrlParams(UnicodeString AURL, UnicodeString Params)