Added isHead() method to HttpRequest, to preserve information about the original method for use in the controller (#1743)

This commit is contained in:
Greisberger Christophe 2023-08-24 06:41:48 +02:00 committed by GitHub
parent 81bf767d89
commit 40579ae308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -123,6 +123,16 @@ class DROGON_EXPORT HttpRequest
return method();
}
/**
* @brief Check if the method is or was HttpMethod::Head
* @details Allows to know that an incoming request is a HEAD request, since
* drogon sets the method to HttpMethod::Get before calling the
* controller
* @return true if method() returns HttpMethod::Head, or HttpMethod::Get but
* was previously HttpMethod::Head
*/
virtual bool isHead() const = 0;
/// Get the header string identified by the key parameter.
/**
* @note

View File

@ -55,6 +55,7 @@ class HttpRequestImpl : public HttpRequest
void reset()
{
method_ = Invalid;
previousMethod_ = Invalid;
version_ = Version::kUnknown;
flagForParsingJson_ = false;
headers_.clear();
@ -110,6 +111,7 @@ class HttpRequestImpl : public HttpRequest
void setMethod(const HttpMethod method) override
{
previousMethod_ = method_;
method_ = method;
return;
}
@ -119,6 +121,13 @@ class HttpRequestImpl : public HttpRequest
return method_;
}
bool isHead() const override
{
return (method_ == HttpMethod::Head) ||
((method_ == HttpMethod::Get) &&
(previousMethod_ == HttpMethod::Head));
}
const char *methodString() const override;
void setPath(const char *start, const char *end)
@ -576,6 +585,7 @@ class HttpRequestImpl : public HttpRequest
mutable bool flagForParsingParameters_{false};
mutable bool flagForParsingJson_{false};
HttpMethod method_{Invalid};
HttpMethod previousMethod_{Invalid};
Version version_{Version::kUnknown};
std::string path_;
std::string originalPath_;