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(), diff --git a/lib/tests/unittests/HttpHeaderTest.cc b/lib/tests/unittests/HttpHeaderTest.cc index 4a46bb193a..b693a25c57 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)