Skip to content

Commit fccfae5

Browse files
committed
Add HttpRequest::clearHeaders() to clear all headers efficiently (#2046)
1 parent b1af64a commit fccfae5

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/inc/drogon/HttpRequest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ class DROGON_EXPORT HttpRequest
159159
*/
160160
virtual void removeHeader(std::string key) = 0;
161161

162+
// Clear all HTTP headers
163+
virtual void clearHeaders() = 0;
164+
162165
/// Get the cookie string identified by the field parameter
163166
virtual const std::string &getCookie(const std::string &field) const = 0;
164167

lib/src/HttpRequestImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ class HttpRequestImpl : public HttpRequest
351351
headers_.erase(lowerKey);
352352
}
353353

354+
void clearHeaders() override
355+
{
356+
headers_.clear();
357+
}
358+
354359
const std::string &getHeader(std::string field) const override
355360
{
356361
std::transform(field.begin(),

lib/tests/unittests/HttpHeaderTest.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,22 @@ DROGON_TEST(AddHttpCorsHeaders)
239239
resp->addCorsHeaders(req);
240240
CHECK(resp->getHeader("Access-Control-Allow-Credentials") == "true");
241241
}
242+
243+
DROGON_TEST(ClearHeaders)
244+
{
245+
auto req = HttpRequest::newHttpRequest();
246+
// set a custom path to ensure it is not cleared
247+
req->setPath("/api/test");
248+
req->addHeader("X-Test", "value");
249+
req->addHeader("Authorization", "Bearer token");
250+
251+
CHECK(req->headers().size() == 2);
252+
CHECK(req->getHeader("X-Test") == "value");
253+
CHECK(req->getHeader("Authorization") == "Bearer token");
254+
255+
req->clearHeaders();
256+
257+
CHECK(req->headers().empty());
258+
// verify path unchanged
259+
CHECK(req->path() == "/api/test");
260+
}

0 commit comments

Comments
 (0)