diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-04-13 22:20:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-13 22:20:35 +0200 |
commit | e7e9d9a0ea87c246610e27fbace47b02e06d0d14 (patch) | |
tree | 732f2bc4a712fd27213d2465d4cd057ecb43a1f9 /core | |
parent | 1d15c5d726600047805ab10d53ff2a7efdc41d8e (diff) | |
parent | 00e98458ba3585f4e5e027e24b3362d4ae31a146 (diff) |
Merge pull request #18156 from RandomShaper/fix-messed-pr
Revert "Unify http- and percent- encode/decode"
Diffstat (limited to 'core')
-rw-r--r-- | core/io/http_client.cpp | 6 | ||||
-rw-r--r-- | core/ustring.cpp | 66 | ||||
-rw-r--r-- | core/ustring.h | 7 |
3 files changed, 72 insertions, 7 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index c787b7ec4c..9e301ccac5 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -618,14 +618,14 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { String query = ""; Array keys = p_dict.keys(); for (int i = 0; i < keys.size(); ++i) { - String encoded_key = String(keys[i]).percent_encode(); + String encoded_key = String(keys[i]).http_escape(); Variant value = p_dict[keys[i]]; switch (value.get_type()) { case Variant::ARRAY: { // Repeat the key with every values Array values = value; for (int j = 0; j < values.size(); ++j) { - query += "&" + encoded_key + "=" + String(values[j]).percent_encode(); + query += "&" + encoded_key + "=" + String(values[j]).http_escape(); } break; } @@ -636,7 +636,7 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { } default: { // Add the key-value pair - query += "&" + encoded_key + "=" + String(value).percent_encode(); + query += "&" + encoded_key + "=" + String(value).http_escape(); } } } diff --git a/core/ustring.cpp b/core/ustring.cpp index d749146998..954f590218 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -3165,7 +3165,7 @@ String String::word_wrap(int p_chars_per_line) const { return ret; } -String String::percent_encode() const { +String String::http_escape() const { const CharString temp = utf8(); String res; for (int i = 0; i < temp.length(); ++i) { @@ -3189,7 +3189,7 @@ String String::percent_encode() const { return res; } -String String::percent_decode() const { +String String::http_unescape() const { String res; for (int i = 0; i < length(); ++i) { if (ord_at(i) == '%' && i + 2 < length()) { @@ -3727,6 +3727,68 @@ String String::plus_file(const String &p_file) const { return *this + "/" + p_file; } +String String::percent_encode() const { + + CharString cs = utf8(); + String encoded; + for (int i = 0; i < cs.length(); i++) { + uint8_t c = cs[i]; + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '~' || c == '.') { + + char p[2] = { (char)c, 0 }; + encoded += p; + } else { + char p[4] = { '%', 0, 0, 0 }; + static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + + p[1] = hex[c >> 4]; + p[2] = hex[c & 0xF]; + encoded += p; + } + } + + return encoded; +} +String String::percent_decode() const { + + CharString pe; + + CharString cs = utf8(); + for (int i = 0; i < cs.length(); i++) { + + uint8_t c = cs[i]; + if (c == '%' && i < length() - 2) { + + uint8_t a = LOWERCASE(cs[i + 1]); + uint8_t b = LOWERCASE(cs[i + 2]); + + c = 0; + if (a >= '0' && a <= '9') + c = (a - '0') << 4; + else if (a >= 'a' && a <= 'f') + c = (a - 'a' + 10) << 4; + else + continue; + + uint8_t d = 0; + + if (b >= '0' && b <= '9') + d = (b - '0'); + else if (b >= 'a' && b <= 'f') + d = (b - 'a' + 10); + else + continue; + c += d; + i += 2; + } + pe.push_back(c); + } + + pe.push_back(0); + + return String::utf8(pe.ptr()); +} + String String::get_basename() const { int pos = find_last("."); diff --git a/core/ustring.h b/core/ustring.h index 8023c9b95d..bb676ce623 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -226,14 +226,17 @@ public: String xml_escape(bool p_escape_quotes = false) const; String xml_unescape() const; - String percent_encode() const; - String percent_decode() const; + String http_escape() const; + String http_unescape() const; String c_escape() const; String c_escape_multiline() const; String c_unescape() const; String json_escape() const; String word_wrap(int p_chars_per_line) const; + String percent_encode() const; + String percent_decode() const; + bool is_valid_identifier() const; bool is_valid_integer() const; bool is_valid_float() const; |