From dffd9f7360213a8fff758a6b3f593004e6f9b426 Mon Sep 17 00:00:00 2001 From: Alexey Ozeritskiy Date: Wed, 11 Nov 2026 20:37:44 +0000 Subject: [PATCH] Compare HTTP header names case-insensitively --- coroio/http/httpd.cpp | 2 +- coroio/http/httpd.hpp | 28 ++++++++++++++++--- tests/test_http.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/coroio/http/httpd.cpp b/coroio/http/httpd.cpp index 6e29013..d3da65c 100644 --- a/coroio/http/httpd.cpp +++ b/coroio/http/httpd.cpp @@ -169,7 +169,7 @@ void TRequest::ParseHeaders() { } } -const std::map& TRequest::Headers() const { +const THeaders& TRequest::Headers() const { return Headers_; } diff --git a/coroio/http/httpd.hpp b/coroio/http/httpd.hpp index 2477d3d..6786a78 100644 --- a/coroio/http/httpd.hpp +++ b/coroio/http/httpd.hpp @@ -11,12 +11,34 @@ #include #include #include +#include +#include +#include #include #include namespace NNet { +/// ASCII case-insensitive "less" for header names (RFC 9110 section 5.1). +struct THeaderNameLess { + using is_transparent = void; + bool operator()(std::string_view a, std::string_view b) const { + return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](char x, char y) { + return Lower(x) < Lower(y); + }); + } +private: + static char Lower(char c) { + return (c >= 'A' && c <= 'Z') ? char(c - 'A' + 'a') : c; + } +}; + +/// Request headers: names compare case-insensitively; for duplicates the last value wins. +using THeaders = std::map; +/// Response headers: names compare case-insensitively; the first-seen spelling of a name is kept. +using TResponseHeaders = std::map; + /** * @brief Parsed HTTP request URI: `/path?key=value&...#fragment`. * @@ -86,7 +108,7 @@ class TRequest { /// Returns `true` if the client sent `Connection: close` or is HTTP/1.0. bool RequireConnectionClose() const; /// All request headers as a `name → value` map (views into the header buffer). - const std::map& Headers() const; + const THeaders& Headers() const; private: void ParseRequestLine(); @@ -97,7 +119,7 @@ class TRequest { std::string Header_; size_t HeaderStartPos_ = 0; - std::map Headers_; + THeaders Headers_; size_t ContentLength_ = 0; bool HasBody_ = false; bool Chunked_ = false; @@ -172,7 +194,7 @@ class TResponse { TFuture CompleteWrite(const char* data, size_t size); int StatusCode_ = 200; - std::map Headers_; + TResponseHeaders Headers_; bool HeadersSent_ = false; bool Chunked_ = false; bool IsClosed_ = false; diff --git a/tests/test_http.cpp b/tests/test_http.cpp index 5c782d0..10e3798 100644 --- a/tests/test_http.cpp +++ b/tests/test_http.cpp @@ -292,6 +292,66 @@ void test_http_response_handling_chunked(void**) { assert_string_equal(responseData.c_str(), expectedResponse.c_str()); } +void test_http_headers_case_insensitive(void**) { + std::string headerPart = + "POST /x HTTP/1.1\r\n" + "host: localhost\r\n" + "CONTENT-LENGTH: 5\r\n" + "transfer-Encoding: identity\r\n" + "cOnNeCtIoN: close\r\n" + "\r\n"; + auto bodyReader = [](char* buffer, size_t size) -> TFuture { + size_t n = std::min(size, 5); + std::memcpy(buffer, "Hello", n); + co_return n; + }; + TRequest request(std::move(headerPart), bodyReader); + + const auto& headers = request.Headers(); + assert_true(headers.find("Host") != headers.end()); + assert_true(headers.find("HOST") != headers.end()); + assert_true(headers.find("content-length")->second == "5"); + assert_true(headers.find("Transfer-Encoding")->second == "identity"); + assert_true(headers.find("Connection")->second == "close"); + assert_true(request.RequireConnectionClose()); + assert_true(headers.size() == 4); + + std::string body; + [](TRequest& request, std::string& body) -> TVoidTask { + body = co_await request.ReadBodyFull(); + } (request, body); + assert_string_equal(body.c_str(), "Hello"); +} + +void test_http_response_headers_case_insensitive(void**) { + std::string responseData; + auto writer = [&](const void* data, size_t size) -> TFuture { + responseData.append(static_cast(data), size); + co_return size; + }; + + TResponse response(writer); + response.SetHeader("Content-Type", "text/plain"); + response.SetHeader("content-type", "text/html"); + response.SetHeader("CONNECTION", "close"); + + [](TResponse& response) -> TVoidTask { + co_await response.WriteBodyFull("Hi"); + } (response); + + // One entry per name, first spelling kept, last value wins; + // Content-Length is not added twice and Connection: close is recognized. + std::string expectedResponse = + "HTTP/1.1 200 OK\r\n" + "CONNECTION: close\r\n" + "Content-Length: 2\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "Hi"; + assert_string_equal(responseData.c_str(), expectedResponse.c_str()); + assert_true(response.IsClosed()); +} + namespace { // Delivers the given chunks one per ReadSome; reports a closed connection @@ -564,6 +624,8 @@ int main(int argc, char** argv) { ADD_TEST(cmocka_unit_test, test_http_request_handling_advanced); ADD_TEST(cmocka_unit_test, test_http_request_handling_2requests_in_1buffer); ADD_TEST(cmocka_unit_test, test_http_request_handling_chunked_body); + ADD_TEST(cmocka_unit_test, test_http_headers_case_insensitive); + ADD_TEST(cmocka_unit_test, test_http_response_headers_case_insensitive); ADD_TEST(cmocka_unit_test, test_http_response_handling_basic); ADD_TEST(cmocka_unit_test, test_http_response_handling_chunked); ADD_TEST(cmocka_unit_test, test_byte_reader_read_until_split_two_chunks);