Merge pull request #18157 from RandomShaper/improve-uri-utils-3.0
Improve/fix URI utils (3.0)
This commit is contained in:
commit
bcb38393a5
|
@ -618,7 +618,27 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) {
|
||||||
String query = "";
|
String query = "";
|
||||||
Array keys = p_dict.keys();
|
Array keys = p_dict.keys();
|
||||||
for (int i = 0; i < keys.size(); ++i) {
|
for (int i = 0; i < keys.size(); ++i) {
|
||||||
query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape();
|
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]).http_escape();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Variant::NIL: {
|
||||||
|
// Add the key with no value
|
||||||
|
query += "&" + encoded_key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
// Add the key-value pair
|
||||||
|
query += "&" + encoded_key + "=" + String(value).http_escape();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
query.erase(0, 1);
|
query.erase(0, 1);
|
||||||
return query;
|
return query;
|
||||||
|
|
|
@ -3168,8 +3168,8 @@ String String::word_wrap(int p_chars_per_line) const {
|
||||||
String String::http_escape() const {
|
String String::http_escape() const {
|
||||||
const CharString temp = utf8();
|
const CharString temp = utf8();
|
||||||
String res;
|
String res;
|
||||||
for (int i = 0; i < length(); ++i) {
|
for (int i = 0; i < temp.length(); ++i) {
|
||||||
CharType ord = temp[i];
|
char ord = temp[i];
|
||||||
if (ord == '.' || ord == '-' || ord == '_' || ord == '~' ||
|
if (ord == '.' || ord == '-' || ord == '_' || ord == '~' ||
|
||||||
(ord >= 'a' && ord <= 'z') ||
|
(ord >= 'a' && ord <= 'z') ||
|
||||||
(ord >= 'A' && ord <= 'Z') ||
|
(ord >= 'A' && ord <= 'Z') ||
|
||||||
|
@ -3178,9 +3178,9 @@ String String::http_escape() const {
|
||||||
} else {
|
} else {
|
||||||
char h_Val[3];
|
char h_Val[3];
|
||||||
#if defined(__GNUC__) || defined(_MSC_VER)
|
#if defined(__GNUC__) || defined(_MSC_VER)
|
||||||
snprintf(h_Val, 3, "%.2X", ord);
|
snprintf(h_Val, 3, "%hhX", ord);
|
||||||
#else
|
#else
|
||||||
sprintf(h_Val, "%.2X", ord);
|
sprintf(h_Val, "%hhX", ord);
|
||||||
#endif
|
#endif
|
||||||
res += "%";
|
res += "%";
|
||||||
res += h_Val;
|
res += h_Val;
|
||||||
|
|
|
@ -111,6 +111,12 @@
|
||||||
String queryString = httpClient.query_string_from_dict(fields)
|
String queryString = httpClient.query_string_from_dict(fields)
|
||||||
returns:= "username=user&password=pass"
|
returns:= "username=user&password=pass"
|
||||||
[/codeblock]
|
[/codeblock]
|
||||||
|
Furthermore, if a key has a null value, only the key itself is added, without equal sign and value. If the value is an array, for each value in it a pair with the same key is added.
|
||||||
|
[codeblock]
|
||||||
|
var fields = {"single": 123, "not_valued": null, "multiple": [22, 33, 44]}
|
||||||
|
String queryString = httpClient.query_string_from_dict(fields)
|
||||||
|
returns:= "single=123&not_valued&multiple=22&multiple=33&multiple=44"
|
||||||
|
[/codeblock]
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="read_response_body_chunk">
|
<method name="read_response_body_chunk">
|
||||||
|
|
Loading…
Reference in New Issue