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

+39 -2
View File
@@ -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) {