fix:set_file_content with range request return 416. (#2010)

Co-authored-by: fenlog <bakurise@qq.com>
This commit is contained in:
sinnren 2024-12-24 22:38:59 +08:00 committed by GitHub
parent 8794792baa
commit d647f484a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 8 deletions

View file

@ -7183,14 +7183,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
: StatusCode::PartialContent_206; : StatusCode::PartialContent_206;
} }
if (detail::range_error(req, res)) {
res.body.clear();
res.content_length_ = 0;
res.content_provider_ = nullptr;
res.status = StatusCode::RangeNotSatisfiable_416;
return write_response(strm, close_connection, req, res);
}
// Serve file content by using a content provider // Serve file content by using a content provider
if (!res.file_content_path_.empty()) { if (!res.file_content_path_.empty()) {
const auto &path = res.file_content_path_; const auto &path = res.file_content_path_;
@ -7217,6 +7209,14 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
}); });
} }
if (detail::range_error(req, res)) {
res.body.clear();
res.content_length_ = 0;
res.content_provider_ = nullptr;
res.status = StatusCode::RangeNotSatisfiable_416;
return write_response(strm, close_connection, req, res);
}
return write_response_with_content(strm, close_connection, req, res); return write_response_with_content(strm, close_connection, req, res);
} else { } else {
if (res.status == -1) { res.status = StatusCode::NotFound_404; } if (res.status == -1) { res.status = StatusCode::NotFound_404; }

View file

@ -3036,6 +3036,16 @@ TEST_F(ServerTest, GetFileContent) {
EXPECT_EQ("test.html", res->body); EXPECT_EQ("test.html", res->body);
} }
TEST_F(ServerTest, GetFileContentWithRange) {
auto res = cli_.Get("/file_content", {{make_range_header({{1, 3}})}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("bytes 1-3/9", res->get_header_value("Content-Range"));
EXPECT_EQ(3, std::stoi(res->get_header_value("Content-Length")));
EXPECT_EQ("est", res->body);
}
TEST_F(ServerTest, GetFileContentWithContentType) { TEST_F(ServerTest, GetFileContentWithContentType) {
auto res = cli_.Get("/file_content_with_content_type"); auto res = cli_.Get("/file_content_with_content_type");
ASSERT_TRUE(res); ASSERT_TRUE(res);