Repository navigation
Conversation
The static file handler read Range with sscanf("bytes=%ld-%ld") and
treated a last position of 0 as "to the end of the file", so
"bytes=0-0" returned the whole file as "bytes 0-999/1000". Suffix ranges
("bytes=-500") were always answered with 416, "bytes=-0" got the whole
file, and a value sscanf could not match left `from` uninitialized.
The handler now parses the first range itself (RFC 9110 section
14.1.2): "first-last", "first-" and "-length", with a last position or a
suffix past the end clamped to the file. A Range header with another
unit or an invalid range is ignored and the whole file is sent; a range
starting past the end still gets 416. The small-range path reads exactly
the selected bytes, since HFile::readrange also treats to = 0 as "to the
end".
unittest/http_static_range_test serves a 10 byte file and checks 0-0,
5-, -3, -20, 10- and a non-bytes unit.
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.
The static file handler reads
Rangewithsscanf("bytes=%ld-%ld")(HttpRequest::GetRange) and treats a last position of 0 as "to the end of the file". Withhttpdserving a 1000-byte file:Range: bytes=0-0returns the whole file as206withContent-Range: bytes 0-999/1000. Clients often sendbytes=0-0to probe for range support and the file size, and they get 1000 bytes instead of 1.bytes=-10,bytes=-2000) always get416, whilebytes=-0returns the whole file.sscanfdoesn't match (for exampleitems=0-5orbytes=abc),fromis left uninitialized, and the response depends on whatever value it held.defaultStaticHandlernow parses the first range itself, following RFC 9110 section 14.1.2:first-last,first-and-length.416withContent-Range: bytes */size.Rangeheader with another unit or an invalid range (for example5-2) is ignored, and the whole file is sent with200.The small-range path now reads exactly the selected bytes, because
HFile::readrangetreatsto = 0as "to the end" as well.HttpRequest::GetRangeandHFile::readrangeare unchanged.The new
unittest/http_static_range_test(added to the Makefile, CMake andscripts/unittest.sh) serves a 10-byte file and checks0-0,5-,-3,-20,10-anditems=0-1. Without the change it fails on0-0(bytes 0-9/10). With the change it passes. Results withhttpdfor a 1000-byte file:bytes=0-0bytes=-10bytes=-2000bytes=-0items=0-5Bytes=0-1