浏览代码

AK+LibWeb: Encode ' ' as '+' in application/x-www-form-urlencoded

This matches what the URL and HTML specifications ask us to do.
Andreas Kling 3 年之前
父节点
当前提交
3724ce765e
共有 3 个文件被更改,包括 12 次插入5 次删除
  1. 5 2
      AK/URL.cpp
  2. 5 1
      AK/URL.h
  3. 2 2
      Userland/Libraries/LibWeb/URL/URLSearchParams.cpp

+ 5 - 2
AK/URL.cpp

@@ -403,11 +403,14 @@ void URL::append_percent_encoded_if_necessary(StringBuilder& builder, u32 code_p
         builder.append_code_point(code_point);
         builder.append_code_point(code_point);
 }
 }
 
 
-String URL::percent_encode(StringView input, URL::PercentEncodeSet set)
+String URL::percent_encode(StringView input, URL::PercentEncodeSet set, SpaceAsPlus space_as_plus)
 {
 {
     StringBuilder builder;
     StringBuilder builder;
     for (auto code_point : Utf8View(input)) {
     for (auto code_point : Utf8View(input)) {
-        append_percent_encoded_if_necessary(builder, code_point, set);
+        if (space_as_plus == SpaceAsPlus::Yes && code_point == ' ')
+            builder.append('+');
+        else
+            append_percent_encoded_if_necessary(builder, code_point, set);
     }
     }
     return builder.to_string();
     return builder.to_string();
 }
 }

+ 5 - 1
AK/URL.h

@@ -104,7 +104,11 @@ public:
     static u16 default_port_for_scheme(StringView);
     static u16 default_port_for_scheme(StringView);
     static bool is_special_scheme(StringView);
     static bool is_special_scheme(StringView);
 
 
-    static String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo);
+    enum class SpaceAsPlus {
+        No,
+        Yes,
+    };
+    static String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
     static String percent_decode(StringView input);
     static String percent_decode(StringView input);
 
 
     bool operator==(URL const& other) const { return equals(other, ExcludeFragment::No); }
     bool operator==(URL const& other) const { return equals(other, ExcludeFragment::No); }

+ 2 - 2
Userland/Libraries/LibWeb/URL/URLSearchParams.cpp

@@ -16,9 +16,9 @@ String url_encode(Vector<QueryParam> const& pairs, AK::URL::PercentEncodeSet per
 {
 {
     StringBuilder builder;
     StringBuilder builder;
     for (size_t i = 0; i < pairs.size(); ++i) {
     for (size_t i = 0; i < pairs.size(); ++i) {
-        builder.append(AK::URL::percent_encode(pairs[i].name, percent_encode_set));
+        builder.append(AK::URL::percent_encode(pairs[i].name, percent_encode_set, AK::URL::SpaceAsPlus::Yes));
         builder.append('=');
         builder.append('=');
-        builder.append(AK::URL::percent_encode(pairs[i].value, percent_encode_set));
+        builder.append(AK::URL::percent_encode(pairs[i].value, percent_encode_set, AK::URL::SpaceAsPlus::Yes));
         if (i != pairs.size() - 1)
         if (i != pairs.size() - 1)
             builder.append('&');
             builder.append('&');
     }
     }