mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
HEAD support for static file server
This commit is contained in:
parent
79df842cc5
commit
5e43680486
2 changed files with 14 additions and 4 deletions
|
@ -509,7 +509,7 @@ private:
|
|||
bool listen_internal();
|
||||
|
||||
bool routing(Request &req, Response &res, Stream &strm, bool last_connection);
|
||||
bool handle_file_request(Request &req, Response &res);
|
||||
bool handle_file_request(Request &req, Response &res, bool head = false);
|
||||
bool dispatch_request(Request &req, Response &res, Handlers &handlers);
|
||||
bool dispatch_request_for_content_reader(Request &req, Response &res,
|
||||
ContentReader content_reader,
|
||||
|
@ -3212,7 +3212,7 @@ inline bool Server::read_content_core(Stream &strm, bool last_connection,
|
|||
return true;
|
||||
}
|
||||
|
||||
inline bool Server::handle_file_request(Request &req, Response &res) {
|
||||
inline bool Server::handle_file_request(Request &req, Response &res, bool head) {
|
||||
for (const auto &kv : base_dirs_) {
|
||||
const auto &mount_point = kv.first;
|
||||
const auto &base_dir = kv.second;
|
||||
|
@ -3230,7 +3230,7 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
|
|||
detail::find_content_type(path, file_extension_and_mimetype_map_);
|
||||
if (type) { res.set_header("Content-Type", type); }
|
||||
res.status = 200;
|
||||
if (file_request_handler_) { file_request_handler_(req, res); }
|
||||
if (!head && file_request_handler_) { file_request_handler_(req, res); }
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -3331,7 +3331,8 @@ inline bool Server::listen_internal() {
|
|||
inline bool Server::routing(Request &req, Response &res, Stream &strm,
|
||||
bool last_connection) {
|
||||
// File handler
|
||||
if (req.method == "GET" && handle_file_request(req, res)) { return true; }
|
||||
bool is_head_request = req.method == "HEAD";
|
||||
if ((req.method == "GET" || is_head_request) && handle_file_request(req, res, is_head_request)) { return true; }
|
||||
|
||||
if (detail::expect_content(req)) {
|
||||
// Content reader handler
|
||||
|
|
|
@ -1051,6 +1051,15 @@ TEST_F(ServerTest, HeadMethod200) {
|
|||
EXPECT_EQ("", res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, HeadMethod200Static) {
|
||||
auto res = cli_.Head("/mount/dir/index.html");
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ(104, std::stoi(res->get_header_value("Content-Length")));
|
||||
EXPECT_EQ("", res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, HeadMethod404) {
|
||||
auto res = cli_.Head("/invalid");
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
|
|
Loading…
Reference in a new issue