From a95f17cffab24cca2b6b47d0054c810712a2535c Mon Sep 17 00:00:00 2001 From: Leng Niu <344562942+vtxe02023274@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:41:19 +0800 Subject: [PATCH] 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. --- httplib.h | 21 +++++++++++++-------- test/test.cc | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index 91b7beec..4f8d931e 100644 --- a/httplib.h +++ b/httplib.h @@ -10994,7 +10994,7 @@ inline bool range_error(Request &req, Response &res) { ssize_t content_len = static_cast( res.content_length_ ? res.content_length_ : res.body.size()); - std::vector> processed_ranges; + Ranges satisfiable_ranges; size_t overwrapping_count = 0; // 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; } - for (auto &r : req.ranges) { + satisfiable_ranges.reserve(req.ranges.size()); + + for (auto r : req.ranges) { auto &first_pos = r.first; auto &last_pos = r.second; @@ -11036,25 +11038,28 @@ inline bool range_error(Request &req, Response &res) { 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 && last_pos <= content_len - 1)) { - return true; + continue; } // Request must not have more than two overlapping ranges - for (const auto &processed_range : processed_ranges) { - if (!(last_pos < processed_range.first || - first_pos > processed_range.second)) { + for (const auto &prev : satisfiable_ranges) { + if (!(last_pos < prev.first || first_pos > prev.second)) { overwrapping_count++; if (overwrapping_count > 2) { return true; } 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 coalesce_ranges(req.ranges, static_cast(content_len)); } diff --git a/test/test.cc b/test/test.cc index 640965cc..e9fe968c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9410,10 +9410,47 @@ TEST_F(ServerTest, GetWithRangeMultipart) { } 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", - 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()); - 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) {