Skip to content

Commit 560854a

Browse files
authored
Apply range header base on response status code (yhirose#1806)
* Enable ignoring range header to generate customized response * Apply range header base on response status code
1 parent 2064462 commit 560854a

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

httplib.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6521,7 +6521,7 @@ inline bool Server::dispatch_request(Request &req, Response &res,
65216521
inline void Server::apply_ranges(const Request &req, Response &res,
65226522
std::string &content_type,
65236523
std::string &boundary) const {
6524-
if (req.ranges.size() > 1) {
6524+
if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) {
65256525
auto it = res.headers.find("Content-Type");
65266526
if (it != res.headers.end()) {
65276527
content_type = it->second;
@@ -6539,7 +6539,7 @@ inline void Server::apply_ranges(const Request &req, Response &res,
65396539
if (res.body.empty()) {
65406540
if (res.content_length_ > 0) {
65416541
size_t length = 0;
6542-
if (req.ranges.empty()) {
6542+
if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
65436543
length = res.content_length_;
65446544
} else if (req.ranges.size() == 1) {
65456545
auto offset_and_length = detail::get_range_offset_and_length(
@@ -6568,7 +6568,7 @@ inline void Server::apply_ranges(const Request &req, Response &res,
65686568
}
65696569
}
65706570
} else {
6571-
if (req.ranges.empty()) {
6571+
if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
65726572
;
65736573
} else if (req.ranges.size() == 1) {
65746574
auto offset_and_length =

test/test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,11 @@ class ServerTest : public ::testing::Test {
18901890
[&](const Request & /*req*/, Response &res) {
18911891
res.set_content("abcdefg", "text/plain");
18921892
})
1893+
.Get("/with-range-customized-response",
1894+
[&](const Request & /*req*/, Response &res) {
1895+
res.status = StatusCode::BadRequest_400;
1896+
res.set_content(JSON_DATA, "application/json");
1897+
})
18931898
.Post("/chunked",
18941899
[&](const Request &req, Response & /*res*/) {
18951900
EXPECT_EQ(req.body, "dechunked post body");
@@ -3166,6 +3171,24 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
31663171
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
31673172
}
31683173

3174+
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
3175+
auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}})}});
3176+
ASSERT_TRUE(res);
3177+
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
3178+
EXPECT_EQ(true, res->has_header("Content-Length"));
3179+
EXPECT_EQ(false, res->has_header("Content-Range"));
3180+
EXPECT_EQ(JSON_DATA, res->body);
3181+
}
3182+
3183+
TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
3184+
auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}, {4, 5}})}});
3185+
ASSERT_TRUE(res);
3186+
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
3187+
EXPECT_EQ(true, res->has_header("Content-Length"));
3188+
EXPECT_EQ(false, res->has_header("Content-Range"));
3189+
EXPECT_EQ(JSON_DATA, res->body);
3190+
}
3191+
31693192
TEST_F(ServerTest, Issue1772) {
31703193
auto res = cli_.Get("/issue1772", {{make_range_header({{1000, -1}})}});
31713194
ASSERT_TRUE(res);

0 commit comments

Comments
 (0)