Skip to content
Merged
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
3 changes: 3 additions & 0 deletions lib/inc/drogon/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class DROGON_EXPORT HttpRequest
*/
virtual void removeHeader(std::string key) = 0;

// Clear all HTTP headers
virtual void clearHeaders() = 0;

/// Get the cookie string identified by the field parameter
virtual const std::string &getCookie(const std::string &field) const = 0;

Expand Down
5 changes: 5 additions & 0 deletions lib/src/HttpRequestImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ class HttpRequestImpl : public HttpRequest
headers_.erase(lowerKey);
}

void clearHeaders() override
{
headers_.clear();
}

const std::string &getHeader(std::string field) const override
{
std::transform(field.begin(),
Expand Down
19 changes: 19 additions & 0 deletions lib/tests/unittests/HttpHeaderTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,22 @@ DROGON_TEST(AddHttpCorsHeaders)
resp->addCorsHeaders(req);
CHECK(resp->getHeader("Access-Control-Allow-Credentials") == "true");
}

DROGON_TEST(ClearHeaders)
{
auto req = HttpRequest::newHttpRequest();
// set a custom path to ensure it is not cleared
req->setPath("/api/test");
req->addHeader("X-Test", "value");
req->addHeader("Authorization", "Bearer token");

CHECK(req->headers().size() == 2);
CHECK(req->getHeader("X-Test") == "value");
CHECK(req->getHeader("Authorization") == "Bearer token");

req->clearHeaders();

CHECK(req->headers().empty());
// verify path unchanged
CHECK(req->path() == "/api/test");
}
Loading