Commit f4f6217e authored by Adam Burgess's avatar Adam Burgess

Make Cookie/Set-Cookie headers case-insensitive

parent d72ee8c7
...@@ -28,6 +28,8 @@ struct LowercaseHash { ...@@ -28,6 +28,8 @@ struct LowercaseHash {
} }
}; };
bool LowercaseEqualStatic(const std::string& dynamic, const std::string& statik);
struct LowercaseEqual { struct LowercaseEqual {
bool operator()(const std::string& left, const std::string& right) const { bool operator()(const std::string& left, const std::string& right) const {
return std::equal(left.begin(), left.end(), right.begin(), right.end(), return std::equal(left.begin(), left.end(), right.begin(), right.end(),
......
...@@ -290,11 +290,11 @@ namespace Private { ...@@ -290,11 +290,11 @@ namespace Private {
if (!cursor.advance(1)) return State::Again; if (!cursor.advance(1)) return State::Again;
} }
if (name == "Cookie") { if (Header::LowercaseEqualStatic(name, "cookie")) {
message->cookies_.removeAllCookies(); // removing existing cookies before re-adding them. message->cookies_.removeAllCookies(); // removing existing cookies before re-adding them.
message->cookies_.addFromRaw(cursor.offset(start), cursor.diff(start)); message->cookies_.addFromRaw(cursor.offset(start), cursor.diff(start));
} }
else if (name == "Set-Cookie") { else if (Header::LowercaseEqualStatic(name, "set-cookie")) {
message->cookies_.add(Cookie::fromRaw(cursor.offset(start), cursor.diff(start))); message->cookies_.add(Cookie::fromRaw(cursor.offset(start), cursor.diff(start)));
} }
......
...@@ -41,6 +41,14 @@ toLowercase(std::string str) { ...@@ -41,6 +41,14 @@ toLowercase(std::string str) {
return str; return str;
} }
bool
LowercaseEqualStatic(const std::string& dynamic, const std::string& statik) {
return std::equal(dynamic.begin(), dynamic.end(), statik.begin(), statik.end(),
[] (const char& a, const char& b) {
return std::tolower(a) == b;
});
}
Registry& Registry::instance() { Registry& Registry::instance() {
static Registry instance; static Registry instance;
......
...@@ -713,3 +713,29 @@ TEST(headers_test, raw_headers_are_case_insensitive) ...@@ -713,3 +713,29 @@ TEST(headers_test, raw_headers_are_case_insensitive)
ASSERT_FALSE(request.headers().tryGetRaw("Custom-Header").isEmpty()); ASSERT_FALSE(request.headers().tryGetRaw("Custom-Header").isEmpty());
ASSERT_FALSE(request.headers().tryGetRaw("custom-header").isEmpty()); ASSERT_FALSE(request.headers().tryGetRaw("custom-header").isEmpty());
} }
TEST(headers_test, cookie_headers_are_case_insensitive)
{
{
std::string line = "cookie: x=y\r\n";
Pistache::RawStreamBuf<> buf(&line[0], line.size());
Pistache::StreamCursor cursor(&buf);
Pistache::Http::Request request;
Pistache::Http::Private::HeadersStep step(&request);
step.apply(cursor);
ASSERT_TRUE(request.cookies().has("x"));
}
{
std::string line = "set-cookie: x=y\r\n";
Pistache::RawStreamBuf<> buf(&line[0], line.size());
Pistache::StreamCursor cursor(&buf);
Pistache::Http::Request request;
Pistache::Http::Private::HeadersStep step(&request);
step.apply(cursor);
ASSERT_TRUE(request.cookies().has("x"));
}
}
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