Repository navigation
fix: clamp range last-pos to EOF per RFC 9110 - #738
Open
Huang-404-Q wants to merge 1 commit into
Open
Huang-404-Q wants to merge 1 commit into
Huang-404-Q wants to merge 1 commit into
Conversation
A range whose last-pos exceeds the file size was rejected with 416, but per RFC 9110 §14.1.2 it is satisfiable and last-pos must be clamped to size - 1. Clamp in parse_range() so such ranges return 206 with the clamped end position (e.g. bytes=0-9 on a 5-byte file now returns 206, content-range: bytes 0-4/5). Ranges with first-pos >= size are still rejected with 416, as before. Fixes sigoden#735
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
Rangeheader whoselast-poslies beyond the end of the file — e.g.bytes=0-999against a 500-byte file, or the exact-EOF casebytes=0-5against a 5-byte file (#735) — is answered416 Range Not Satisfiable, although the first part of the range is fully satisfiable.RFC 9110 §14.1.2 requires the opposite: when
last-posis present and greater than or equal to the current length, the valid range isfirst-pos .. length-1and the server responds206 Partial Contentwith the clamped range.416is only forfirst-pos >= length.Root cause
parse_rangeinsrc/utils.rsrejected any range whoseend >= sizeinstead of clamping it:The
NonemadeGET /file(src/server.rs) set416+Content-Range: bytes */{size}.Fix
Clamp
endtosize - 1inparse_range(single and multipart ranges alike);first-pos >= sizestill returns unsatisfiable. The second caller,parse_upload_offset(WebDAV PATCH), only uses the start position and is unaffected.Tests
src/utils.rs): 6 new assertions — exact EOF, beyond-EOF, mid-range beyond EOF, multipart with an out-of-range part, the two exact-EOF cases from the issue, andfirst-pos == sizestill unsatisfiable.tests/range.rs): 6 new tests against a real server —206with clampedContent-Rangeand the full body, the boundarybytes=0-<len-1>, mid-range clamping,HEAD, multipart where both parts are clamped, and mixed satisfiable + unsatisfiable parts still416(regression guard). The existingget_file_range_beyondexpectation was corrected from416to the RFC-correct206bytes 12-17/18.Verification
cargo test --all— all 21 suites pass.cargo clippy --all --all-targets— no new warnings;cargo fmt --all --checkclean.Fixes #735