Skip to content

Clear http header, but don't delete it #2361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/inc/drogon/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions lib/src/HttpRequestImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions lib/tests/unittests/HttpHeaderTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading