Unverified Commit 46f4ba58 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #266 from yisaj/fix-req-cookies

Fix Request Cookies Parsing
parents 52321e5a 5761ab06
...@@ -78,6 +78,7 @@ public: ...@@ -78,6 +78,7 @@ public:
CookieJar(); CookieJar();
void add(const Cookie& cookie); void add(const Cookie& cookie);
void addFromRaw(const char *str, size_t len);
Cookie get(const std::string& name) const; Cookie get(const std::string& name) const;
bool has(const std::string& name) const; bool has(const std::string& name) const;
......
...@@ -210,6 +210,35 @@ CookieJar::add(const Cookie& cookie) { ...@@ -210,6 +210,35 @@ CookieJar::add(const Cookie& cookie) {
cookies.insert(std::make_pair(cookie.name, 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(1);
skip_whitespaces(cursor);
}
}
Cookie Cookie
CookieJar::get(const std::string& name) const { CookieJar::get(const std::string& name) const {
auto it = cookies.find(name); auto it = cookies.find(name);
......
...@@ -294,9 +294,7 @@ namespace Private { ...@@ -294,9 +294,7 @@ namespace Private {
} }
if (name == "Cookie") { if (name == "Cookie") {
message->cookies_.add( message->cookies_.addFromRaw(cursor.offset(start), cursor.diff(start));
Cookie::fromRaw(cursor.offset(start), cursor.diff(start))
);
} }
else if (Header::Registry::isRegistered(name)) { else if (Header::Registry::isRegistered(name)) {
......
...@@ -129,3 +129,24 @@ TEST(cookie_test, invalid_test) { ...@@ -129,3 +129,24 @@ TEST(cookie_test, invalid_test) {
ASSERT_THROW(Cookie::fromString("lang=en-US; Max-Age=12ab"), std::invalid_argument); ASSERT_THROW(Cookie::fromString("lang=en-US; Max-Age=12ab"), std::invalid_argument);
} }
void addCookies(const char* str, std::function<void (const CookieJar&)> testFunc) {
CookieJar jar;
jar.addFromRaw(str, strlen(str));
testFunc(jar);
}
TEST(cookie_test, cookiejar_test) {
addCookies("key1=value1", [](const CookieJar& jar) {
ASSERT_EQ(jar.get("key1").value, "value1");
});
addCookies("key2=value2; key3=value3; key4=; key5=foo=bar", [](const CookieJar& jar) {
ASSERT_EQ(jar.get("key2").value, "value2");
ASSERT_EQ(jar.get("key3").value, "value3");
ASSERT_EQ(jar.get("key4").value, "");
ASSERT_EQ(jar.get("key5").value, "foo=bar");
});
CookieJar jar;
ASSERT_THROW(jar.addFromRaw("key4", strlen("key4")), std::runtime_error);
}
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