Skip to content

Commit d647f48

Browse files
sinnrenbakurise
andauthored
fix:set_file_content with range request return 416. (#2010)
Co-authored-by: fenlog <[email protected]>
1 parent 8794792 commit d647f48

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

httplib.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -7183,14 +7183,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
71837183
: StatusCode::PartialContent_206;
71847184
}
71857185

7186-
if (detail::range_error(req, res)) {
7187-
res.body.clear();
7188-
res.content_length_ = 0;
7189-
res.content_provider_ = nullptr;
7190-
res.status = StatusCode::RangeNotSatisfiable_416;
7191-
return write_response(strm, close_connection, req, res);
7192-
}
7193-
71947186
// Serve file content by using a content provider
71957187
if (!res.file_content_path_.empty()) {
71967188
const auto &path = res.file_content_path_;
@@ -7217,6 +7209,14 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
72177209
});
72187210
}
72197211

7212+
if (detail::range_error(req, res)) {
7213+
res.body.clear();
7214+
res.content_length_ = 0;
7215+
res.content_provider_ = nullptr;
7216+
res.status = StatusCode::RangeNotSatisfiable_416;
7217+
return write_response(strm, close_connection, req, res);
7218+
}
7219+
72207220
return write_response_with_content(strm, close_connection, req, res);
72217221
} else {
72227222
if (res.status == -1) { res.status = StatusCode::NotFound_404; }

test/test.cc

+10
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,16 @@ TEST_F(ServerTest, GetFileContent) {
30363036
EXPECT_EQ("test.html", res->body);
30373037
}
30383038

3039+
TEST_F(ServerTest, GetFileContentWithRange) {
3040+
auto res = cli_.Get("/file_content", {{make_range_header({{1, 3}})}});
3041+
ASSERT_TRUE(res);
3042+
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
3043+
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
3044+
EXPECT_EQ("bytes 1-3/9", res->get_header_value("Content-Range"));
3045+
EXPECT_EQ(3, std::stoi(res->get_header_value("Content-Length")));
3046+
EXPECT_EQ("est", res->body);
3047+
}
3048+
30393049
TEST_F(ServerTest, GetFileContentWithContentType) {
30403050
auto res = cli_.Get("/file_content_with_content_type");
30413051
ASSERT_TRUE(res);

0 commit comments

Comments
 (0)