Skip to content

Commit 8f10aaa

Browse files
committed
Apply range header base on response status code
1 parent 44e2d91 commit 8f10aaa

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

httplib.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ struct Response {
590590
Headers headers;
591591
std::string body;
592592
std::string location; // Redirect location
593-
bool ignore_range = false;
594593

595594
bool has_header(const std::string &key) const;
596595
std::string get_header_value(const std::string &key, size_t id = 0) const;
@@ -6503,7 +6502,7 @@ inline bool Server::dispatch_request(Request &req, Response &res,
65036502
inline void Server::apply_ranges(const Request &req, Response &res,
65046503
std::string &content_type,
65056504
std::string &boundary) const {
6506-
if (req.ranges.size() > 1 && !res.ignore_range) {
6505+
if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) {
65076506
auto it = res.headers.find("Content-Type");
65086507
if (it != res.headers.end()) {
65096508
content_type = it->second;
@@ -6521,7 +6520,7 @@ inline void Server::apply_ranges(const Request &req, Response &res,
65216520
if (res.body.empty()) {
65226521
if (res.content_length_ > 0) {
65236522
size_t length = 0;
6524-
if (req.ranges.empty() || res.ignore_range) {
6523+
if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
65256524
length = res.content_length_;
65266525
} else if (req.ranges.size() == 1) {
65276526
auto offset_and_length = detail::get_range_offset_and_length(
@@ -6550,7 +6549,7 @@ inline void Server::apply_ranges(const Request &req, Response &res,
65506549
}
65516550
}
65526551
} else {
6553-
if (req.ranges.empty() || res.ignore_range) {
6552+
if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
65546553
;
65556554
} else if (req.ranges.size() == 1) {
65566555
auto offset_and_length =

test/test.cc

-1
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,6 @@ class ServerTest : public ::testing::Test {
18731873
})
18741874
.Get("/with-range-customized-response",
18751875
[&](const Request & /*req*/, Response &res) {
1876-
res.ignore_range = true;
18771876
res.status = StatusCode::BadRequest_400;
18781877
res.set_content(JSON_DATA, "application/json");
18791878
})

0 commit comments

Comments
 (0)