From 286bf219580806cbc5c0ae31b24bba62bc2c39aa Mon Sep 17 00:00:00 2001 From: discollizard Date: Tue, 29 Jul 2025 22:26:32 -0300 Subject: [PATCH 1/3] implementation --- lib/inc/drogon/HttpRequest.h | 7 +++++++ lib/src/HttpRequestImpl.h | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 24d8c7579d..ecdffd31dd 100644 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -159,6 +159,13 @@ class DROGON_EXPORT HttpRequest */ virtual void removeHeader(std::string key) = 0; + /** + * @brief Clears the value in the header identified by the key parameter. + * + * @param key The key is case insensitive + */ + virtual void clearHeader(std::string key) = 0; + /// Get the cookie string identified by the field parameter virtual const std::string &getCookie(const std::string &field) const = 0; diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 966ef92b78..68c82a9079 100644 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -351,6 +351,23 @@ class HttpRequestImpl : public HttpRequest headers_.erase(lowerKey); } + void clearHeader(std::string key) override + { + transform(key.begin(), key.end(), key.begin(), [](unsigned char c) { + return tolower(c); + }); + clearHeaderBy(key); + } + + void clearHeaderBy(const std::string &lowerKey) + { + auto it = headers_.find(lowerKey); + if (it != headers_.end()) + { + it->second = ""; + } + } + const std::string &getHeader(std::string field) const override { std::transform(field.begin(), From 9f4b0e86e30be5ef789d7d09ce66ea17d5d76b90 Mon Sep 17 00:00:00 2001 From: discollizard Date: Tue, 29 Jul 2025 22:27:12 -0300 Subject: [PATCH 2/3] tests --- lib/tests/unittests/HttpHeaderTest.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/tests/unittests/HttpHeaderTest.cc b/lib/tests/unittests/HttpHeaderTest.cc index 4a46bb193a..c819400e93 100644 --- a/lib/tests/unittests/HttpHeaderTest.cc +++ b/lib/tests/unittests/HttpHeaderTest.cc @@ -12,8 +12,20 @@ DROGON_TEST(HttpHeaderRequest) CHECK(req->getHeader("Abc") == "abc"); CHECK(req->getHeader("abc") == "abc"); + // removing header req->removeHeader("Abc"); CHECK(req->getHeader("abc") == ""); + + req->addHeader("Def", "def"); + CHECK(req->getHeader("Def") == "def"); + + auto it = req->headers().find("def"); + const std::string *original_ptr = &it->second; + + //clearing header, but reusing memory + req->clearHeader("Def"); + CHECK(req->getHeader("def") == ""); + CHECK(&req->getHeader("def") == original_ptr); } DROGON_TEST(HttpHeaderResponse) From a48ecfe8663874cb3b6adf920a7dcb2078dabc37 Mon Sep 17 00:00:00 2001 From: discollizard Date: Tue, 29 Jul 2025 22:35:43 -0300 Subject: [PATCH 3/3] ran clang-format --- lib/tests/unittests/HttpHeaderTest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tests/unittests/HttpHeaderTest.cc b/lib/tests/unittests/HttpHeaderTest.cc index c819400e93..b693a25c57 100644 --- a/lib/tests/unittests/HttpHeaderTest.cc +++ b/lib/tests/unittests/HttpHeaderTest.cc @@ -22,7 +22,7 @@ DROGON_TEST(HttpHeaderRequest) auto it = req->headers().find("def"); const std::string *original_ptr = &it->second; - //clearing header, but reusing memory + // clearing header, but reusing memory req->clearHeader("Def"); CHECK(req->getHeader("def") == ""); CHECK(&req->getHeader("def") == original_ptr);