Fix Url Query formatting

fixes #12883
This commit is contained in:
Jean Flach 2016-10-10 16:16:52 +02:00
parent 53f7481a5f
commit d264a0dab8
2 changed files with 13 additions and 3 deletions

View File

@ -257,16 +257,25 @@ String Url::Format(bool print_credentials) const
else else
param += "&"; param += "&";
// Just one (or one empty) value
if (kv.second.size() == 1) {
param += key;
param += kv.second[0].IsEmpty() ?
String() : "=" + Utility::EscapeString(kv.second[0], ACQUERY_ENCODE, false);
continue;
}
// Array
String temp; String temp;
for (const String s : kv.second) { for (const String s : kv.second) {
if (!temp.IsEmpty()) if (!temp.IsEmpty())
temp += "&"; temp += "&";
temp += key; temp += key;
if (kv.second.size() > 1) if (kv.second.size() > 1)
temp += "[]"; temp += "[]";
if (!s.IsEmpty())
temp += "=" + Utility::EscapeString(s, ACQUERY_ENCODE, false); temp += "=" + Utility::EscapeString(s, ACQUERY_ENCODE, false);
} }
param += temp; param += temp;
@ -375,7 +384,7 @@ bool Url::ParseQuery(const String& query)
String key = token.SubStr(0, pHelper); String key = token.SubStr(0, pHelper);
String value = Empty; String value = Empty;
if (pHelper != token.GetLength() - 1) if (pHelper != String::NPos && pHelper != token.GetLength() - 1)
value = token.SubStr(pHelper+1); value = token.SubStr(pHelper+1);
if (!ValidateToken(value, ACQUERY)) if (!ValidateToken(value, ACQUERY))

View File

@ -91,6 +91,7 @@ BOOST_AUTO_TEST_CASE(format)
url = new Url("/foo/bar/index.php?blaka"); url = new Url("/foo/bar/index.php?blaka");
BOOST_CHECK(new Url(url->Format())); BOOST_CHECK(new Url(url->Format()));
BOOST_CHECK(url->Format() == "/foo/bar/index.php?blaka");
url = new Url("/"); url = new Url("/");
BOOST_CHECK(url->Format() == "/"); BOOST_CHECK(url->Format() == "/");