mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-30 21:28:26 +00:00
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:
1 parent
22a57ffeba
commit
a95f17cffa
2 files changed
+52
-10
No files matched your search
@@ -10994,7 +10994,7 @@ inline bool range_error(Request &req, Response &res) {
|
||||
ssize_t content_len = static_cast<ssize_t>(
|
||||
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;
|
||||
|
||||
// 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<size_t>(content_len));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user