added automatic HEAD method handling

This commit is contained in:
The-EDev 2021-03-13 13:51:27 +03:00
parent 51a8a977d9
commit 6f5514743a
3 changed files with 13 additions and 0 deletions

View File

@ -357,6 +357,11 @@ namespace crow
(*middlewares_, ctx_, req_, res);
}
if (res.head)
{
res.body = "";
}
std::string accept_encoding = req_.get_header_value("Accept-Encoding");
if (!accept_encoding.empty() && res.compressed)
{

View File

@ -29,6 +29,7 @@ namespace crow
std::string body; ///< The actual payload containing the response data.
ci_map headers; ///< HTTP headers.
bool compressed = true; ///< If compression is enabled and this is false, the individual response will not be compressed.
bool head = false; ///< Whether this is a response to a HEAD request
/// Set the value of an existing header in the response.
void set_header(std::string key, std::string value)

View File

@ -1075,8 +1075,15 @@ public:
void handle(const request& req, response& res)
{
HTTPMethod method_actual = req.method;
if (req.method >= HTTPMethod::InternalMethodCount)
return;
else if (req.method == HTTPMethod::HEAD)
{
method_actual = HTTPMethod::GET;
res.head = true;
}
auto& per_method = per_methods_[(int)req.method];
auto& trie = per_method.trie;
auto& rules = per_method.rules;