Commit 725e53ea authored by octal's avatar octal

Improved URI query parameter parsing

parent a9217a15
......@@ -28,14 +28,21 @@ class MyHandler : public Net::Http::Handler {
using namespace Net::Http;
response.headers()
.add<Header::Server>("lys")
.add<Header::ContentType>(MIME(Text, Plain));
auto stream = response.stream(Net::Http::Code::Ok);
stream << "PO";
stream << "NG";
stream << ends;
timeout.arm(std::chrono::seconds(2));
auto query = req.query();
if (query.has("chunked")) {
std::cout << "Using chunked encoding" << std::endl;
response.headers()
.add<Header::Server>("lys")
.add<Header::ContentType>(MIME(Text, Plain));
auto stream = response.stream(Net::Http::Code::Ok);
stream << "PO";
stream << "NG";
stream << ends;
}
}
}
......
......@@ -93,21 +93,30 @@ namespace Private {
while ((n = cursor.current()) != ' ') {
StreamCursor::Token keyToken(cursor);
if (!match_until('=', cursor))
if (!match_until({ '=', ' ', '&' }, cursor))
return State::Again;
std::string key = keyToken.text();
if (!cursor.advance(1)) return State::Again;
auto c = cursor.current();
if (c == ' ') {
request->query_.add(std::move(key), "");
} else if (c == '&') {
request->query_.add(std::move(key), "");
if (!cursor.advance(1)) return State::Again;
}
else if (c == '=') {
if (!cursor.advance(1)) return State::Again;
StreamCursor::Token valueToken(cursor);
if (!match_until({ ' ', '&' }, cursor))
return State::Again;
StreamCursor::Token valueToken(cursor);
if (!match_until({ ' ', '&' }, cursor))
return State::Again;
std::string value = valueToken.text();
request->query_.add(std::move(key), std::move(value));
if (cursor.current() == '&') {
if (!cursor.advance(1)) return State::Again;
std::string value = valueToken.text();
request->query_.add(std::move(key), std::move(value));
if (cursor.current() == '&') {
if (!cursor.advance(1)) return State::Again;
}
}
}
}
......@@ -305,6 +314,11 @@ namespace Uri {
return Some(it->second);
}
bool
Query::has(const std::string& name) const {
return params.find(name) != std::end(params);
}
} // namespace Uri
Request::Request()
......
......@@ -65,6 +65,7 @@ namespace Uri {
void add(std::string name, std::string value);
Optional<std::string> get(const std::string& name) const;
bool has(const std::string& name) const;
private:
std::unordered_map<std::string, std::string> params;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment