Skip to content

Commit

Permalink
Fix Request Cookies When Receiving Multiple
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yisaj committed Aug 15, 2018
1 parent 52321e5 commit befdba1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/pistache/cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class CookieJar {
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;
Expand Down
28 changes: 28 additions & 0 deletions src/common/cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/common/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit befdba1

Please sign in to comment.