From 3d425f44de521bd9ba22d1696d59369bcade12bc Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 24 Jul 2020 12:30:32 +0400 Subject: [PATCH] Request header case is ignored Originally reported against case sensitivity of the Range header (see issue #387), this fix applies to all request headers (since according to RFC 7230 all header fields are case-insensitive, see https://tools.ietf.org/html/rfc7230#section-3.2). However, a corresponding unit-test was added only for the Range header. --- src/server/request_context.cpp | 6 ++++-- test/server.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/server/request_context.cpp b/src/server/request_context.cpp index 93fc60cdf..cad39eefc 100644 --- a/src/server/request_context.cpp +++ b/src/server/request_context.cpp @@ -26,6 +26,8 @@ #include #include +#include "tools/stringTools.h" + namespace kiwix { static std::atomic_ullong s_requestIndex(0); @@ -96,7 +98,7 @@ MHD_Result RequestContext::fill_header(void *__this, enum MHD_ValueKind kind, const char *key, const char *value) { RequestContext *_this = static_cast(__this); - _this->headers[key] = value; + _this->headers[lcAll(key)] = value; return MHD_YES; } @@ -178,7 +180,7 @@ std::string RequestContext::get_argument(const std::string& name) const { } std::string RequestContext::get_header(const std::string& name) const { - return headers.at(name); + return headers.at(lcAll(name)); } } diff --git a/test/server.cpp b/test/server.cpp index 36f2a5637..0f6e801f7 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -523,3 +523,17 @@ TEST_F(ServerTest, RangeHasPrecedenceOverCompression) EXPECT_EQ(invariantHeaders(p1->headers), invariantHeaders(p2->headers)); EXPECT_EQ(p1->body, p2->body); } + +TEST_F(ServerTest, RangeHeaderIsCaseInsensitive) +{ + const char url[] = "/zimfile/I/m/Ray_Charles_classic_piano_pose.jpg"; + const auto r0 = zfs1_->GET(url, { {"Range", "bytes=100-200"} } ); + + const char* header_variations[] = { "RANGE", "range", "rAnGe", "RaNgE" }; + for ( const char* header : header_variations ) { + const auto r = zfs1_->GET(url, { {header, "bytes=100-200"} } ); + EXPECT_EQ(206, r->status); + EXPECT_EQ("bytes 100-200/20077", r->get_header_value("Content-Range")); + EXPECT_EQ(r0->body, r->body); + } +}