Commit befdba10 authored by 's avatar

Fix Request Cookies When Receiving Multiple

Previously, when parsing the "Cookie" header for an HTTP request, the header
was parsed as if it was the "Set-Cookie" header of an HTTP response. As a
result, the CookieJar object of a Request object would never contain more than
a single cookie, with any additional cookies being stored in Cookie.ext of the
first cookie. This commit fixes this issue by implementing a
CookieJar.addFromRaw method which adds multiple Cookies to a CookieJar from a
string. HeadersStep::apply now uses CookieJar.addFromRaw instead of
Cookie::fromRaw.
parent 52321e5a
......@@ -78,6 +78,7 @@ public:
CookieJar();
void add(const Cookie& cookie);
void addFromRaw(const char *str, size_t len);
Cookie get(const std::string& name) const;
bool has(const std::string& name) const;
......
......@@ -210,6 +210,34 @@ CookieJar::add(const Cookie& cookie) {
cookies.insert(std::make_pair(cookie.name, cookie));
}
void
CookieJar::addFromRaw(const char *str, size_t len) {
RawStreamBuf<> buf(const_cast<char *>(str), len);
StreamCursor cursor(&buf);
while (!cursor.eof()) {
StreamCursor::Token nameToken(cursor);
if (!match_until('=', cursor))
throw std::runtime_error("Invalid cookie, missing value");
auto name = nameToken.text();
if (!cursor.advance(1))
throw std::runtime_error("Invalid cookie, missing value");
StreamCursor::Token valueToken(cursor);
match_until(';', cursor);
auto value = valueToken.text();
Cookie cookie(std::move(name), std::move(value));
add(cookie);
cursor.advance(2);
}
}
Cookie
CookieJar::get(const std::string& name) const {
auto it = cookies.find(name);
......
......@@ -294,9 +294,7 @@ namespace Private {
}
if (name == "Cookie") {
message->cookies_.add(
Cookie::fromRaw(cursor.offset(start), cursor.diff(start))
);
message->cookies_.addFromRaw(cursor.offset(start), cursor.diff(start));
}
else if (Header::Registry::isRegistered(name)) {
......
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