diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 62d8a91eb..8ec2008d3 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -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) { diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 4421d83d2..c3b39ae6d 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -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) diff --git a/include/crow/routing.h b/include/crow/routing.h index 7a8eaded1..a1b831c02 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -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;