Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1455,18 +1455,22 @@ template <class Fn> void split(const char *b, const char *e, char d, Fn fn) {
int i = 0;
int beg = 0;

while (e ? (b + i != e) : (b[i] != '\0')) {
while (e ? (b + i < e) : (b[i] != '\0')) {
if (b[i] == d) {
auto r = trim(b, e, beg, i);
fn(&b[r.first], &b[r.second]);
if (r.first < r.second) {
fn(&b[r.first], &b[r.second]);
}
beg = i + 1;
}
i++;
}

if (i) {
auto r = trim(b, e, beg, i);
fn(&b[r.first], &b[r.second]);
if (r.first < r.second) {
fn(&b[r.first], &b[r.second]);
}
}
}

Expand Down Expand Up @@ -2832,7 +2836,6 @@ inline std::string params_to_query_str(const Params &params) {
query += "=";
query += encode_url(it->second);
}

return query;
}

Expand All @@ -2847,7 +2850,10 @@ inline void parse_query_text(const std::string &s, Params &params) {
val.assign(b2, e2);
}
});
params.emplace(decode_url(key, true), decode_url(val, true));

if(!key.empty()) {
params.emplace(decode_url(key, true), decode_url(val, true));
}
});
}

Expand Down
18 changes: 18 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ TEST(SplitTest, ParseQueryString) {
EXPECT_EQ("val3", dic.find("key3")->second);
}

TEST(SplitTest, ParseInvalidQueryTests) {

{
string s = " ";
Params dict;
detail::parse_query_text(s, dict);
EXPECT_TRUE(dict.empty());
}

{
string s = " = =";
Params dict;
detail::parse_query_text(s, dict);
EXPECT_TRUE(dict.empty());
}
}


TEST(ParseQueryTest, ParseQueryString) {
string s = "key1=val1&key2=val2&key3=val3";
Params dic;
Expand Down