Skip unsatisfiable ranges instead of rejecting the whole Range set (#2703)

RFC 9110 14.1.1: a byte-range-set is satisfiable when any of its ranges is,
and 15.5.17 keeps 416 for when none of them are. range_error() answered 416
as soon as one range started past the end of the body. Leave such ranges
out and serve the rest.
This commit is contained in:
Leng Niu authored and GitHub committed 2026-11-30 15:41:19 -05:00
1 parent 22a57ffeba
commit a95f17cffa
2 files changed
+52 -10

No files matched your search

+13 -8
View File
@@ -10994,7 +10994,7 @@ inline bool range_error(Request &req, Response &res) {
ssize_t content_len = static_cast<ssize_t>( ssize_t content_len = static_cast<ssize_t>(
res.content_length_ ? res.content_length_ : res.body.size()); res.content_length_ ? res.content_length_ : res.body.size());
std::vector<std::pair<ssize_t, ssize_t>> processed_ranges; Ranges satisfiable_ranges;
size_t overwrapping_count = 0; size_t overwrapping_count = 0;
// NOTE: The following Range check is based on '14.2. Range' in RFC 9110 // NOTE: The following Range check is based on '14.2. Range' in RFC 9110
@@ -11007,7 +11007,9 @@ inline bool range_error(Request &req, Response &res) {
return true; return true;
} }
for (auto &r : req.ranges) { satisfiable_ranges.reserve(req.ranges.size());
for (auto r : req.ranges) {
auto &first_pos = r.first; auto &first_pos = r.first;
auto &last_pos = r.second; auto &last_pos = r.second;
@@ -11036,25 +11038,28 @@ inline bool range_error(Request &req, Response &res) {
last_pos = content_len - 1; last_pos = content_len - 1;
} }
// Range must be within content length // Skip an unsatisfiable range (RFC 9110 14.1.1)
if (!(0 <= first_pos && first_pos <= last_pos && if (!(0 <= first_pos && first_pos <= last_pos &&
last_pos <= content_len - 1)) { last_pos <= content_len - 1)) {
return true; continue;
} }
// Request must not have more than two overlapping ranges // Request must not have more than two overlapping ranges
for (const auto &processed_range : processed_ranges) { for (const auto &prev : satisfiable_ranges) {
if (!(last_pos < processed_range.first || if (!(last_pos < prev.first || first_pos > prev.second)) {
first_pos > processed_range.second)) {
overwrapping_count++; overwrapping_count++;
if (overwrapping_count > 2) { return true; } if (overwrapping_count > 2) { return true; }
break; // Only count once per range break; // Only count once per range
} }
} }
processed_ranges.emplace_back(first_pos, last_pos); satisfiable_ranges.push_back(r);
} }
// 416 only when no range is satisfiable (RFC 9110 15.5.17)
if (satisfiable_ranges.empty()) { return true; }
req.ranges = std::move(satisfiable_ranges);
// After validation, coalesce overlapping ranges as per RFC 9110 // After validation, coalesce overlapping ranges as per RFC 9110
coalesce_ranges(req.ranges, static_cast<size_t>(content_len)); coalesce_ranges(req.ranges, static_cast<size_t>(content_len));
} }
+39 -2
View File
@@ -9410,10 +9410,47 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
} }
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
// RFC 9110 14.1.1: the set is satisfiable when any of its ranges is, and the
// unsatisfiable ones are left out of the response.
auto res = cli_.Get("/with-range", auto res = cli_.Get("/with-range",
Headers{{make_range_header({{-1, 2}, {10000, 30000}})}}); Headers{make_range_header({{-1, 2}, {10000, 30000}}),
{"Accept-Encoding", ""}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("2", res->get_header_value("Content-Length"));
EXPECT_EQ("bytes 5-6/7", res->get_header_value("Content-Range"));
EXPECT_EQ(std::string("fg"), res->body);
}
TEST_F(ServerTest, GetWithRangeMultipartSkipsUnsatisfiableRange) {
auto res =
cli_.Get("/with-range",
Headers{{make_range_header({{1, 2}, {10000, 30000}, {4, 5}})}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("267", res->get_header_value("Content-Length"));
EXPECT_EQ(false, res->has_header("Content-Range"));
EXPECT_EQ(267U, res->body.size());
EXPECT_TRUE(res->body.find("bc\r\n") != std::string::npos);
EXPECT_TRUE(res->body.find("ef\r\n") != std::string::npos);
}
TEST_F(ServerTest, GetWithRangeMultipartNoneSatisfiable) {
for (auto range : {"bytes=7-, -0", "bytes=10000-30000, 7-8"}) {
auto res = cli_.Get("/with-range", Headers{{"Range", range}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status) << range;
}
}
TEST_F(ServerTest, GetStreamedWithRangeSkipsUnsatisfiableRange) {
auto res =
cli_.Get("/streamed-with-range", Headers{{"Range", "bytes=7-, 2-3"}});
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("2", res->get_header_value("Content-Length"));
EXPECT_EQ("bytes 2-3/7", res->get_header_value("Content-Range"));
EXPECT_EQ(std::string("cd"), res->body);
} }
TEST_F(ServerTest, GetWithRangeCustomizedResponse) { TEST_F(ServerTest, GetWithRangeCustomizedResponse) {