Serve HTTP Range requests from the static-file path#46
Merged
Conversation
Answer a single byte range (bytes=A-B, bytes=A-, bytes=-N) with 206 Partial Content and a Content-Range header, driving the transfer by seeding the fd's sendfile offset/size so the existing loop sends exactly the requested window. Unsatisfiable ranges get 416; absent, malformed, and multi-range specs fall back to the full 200. Every sendfile response now advertises Accept-Ranges: bytes, and HEAD gets the same status and headers with no body.
There was a problem hiding this comment.
Build & Tests
Checked out claude/range-requests (37db046, based on current main) and ran it on armhf via carp -x (carp -b emits no binary here):
test/web.carp→ 236/0test/websocket.carp→ 128/0 (unchanged — refactor preserved)- CHANGELOG entry lands correctly under
## Unreleased→### Added(branch is on current main, so no mis-filing). - CI:
test (macos-latest)green — the real signal, since web's matrix is macOS-only.
Findings
No blocking findings. I went past the suite to try to break it:
- Non-vacuity, independently. Beyond your end-clamp mutation, I mutated the both-bounds boundary
(>= a total)→(> a total)and the suite caught exactlystart equal to size is unsatisfiable(235/1). Harness is real. - Overflow-safe parse holds.
web-parse-boundonly computesacc*10on the branch whereacc ≤ total/10(theorshort-circuits before the second disjunct), so oversized specs saturate attotal+1with no 32-bitLongoverflow. Probedbytes=999999999999-→UNSAT,bytes=-999999999999→ whole file. Good. - End-to-end GET seek verified. Ran the smoke server (
carp -x) + curl:bytes=5-9→ 206Content-Range: bytes 5-9/11, bodyindex— the mid-filesendfileoffset seed works, not just the pure/HEAD path.bytes=-3→bytes 8-10/11;bytes=100-200→ 416bytes */11CL 0; plain GET → 200 +Accept-Ranges;[0-4]+[5-10]reassembles to the full file; server shut down clean (no orphan fd). - Range header is case-insensitive.
range:/RANGE:/Range:all yield 206 (via http@0.2.0'sheader-lookup) — confirmed at the wire level with-H 'range: ...'. - No internal-header leak. The
X-Sendfile-Rangemarker is stripped in every GET/HEAD branch, andweb-not-modifiedrebuilds a clean 304 (onlyETag), so aRange+ matchingIf-None-Matchcan't leak the marker. - Malformed / multi-range / leading-space specs all fall back to
200(spec-allows-MAY-ignore);bytes=-0and any range on an empty file → unsatisfiable. All correct.
Nice reuse of the existing X-Sendfile marker convention to carry the raw spec to file-open time — no new transfer-loop code, async hot path untouched.
Verdict: merge
Correct, overflow-safe, and verified end-to-end including the mid-file seek; CI green. The draft state is intentional (yours to un-draft) — leaving it as-is.
hellerve
marked this pull request as ready for review
July 23, 2026 16:04
hellerve
approved these changes
Jul 23, 2026
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.
What
Adds single-range HTTP
Rangesupport to the static-file (sendfile) serving path. The server now answers206 Partial ContentforRange: bytes=…requests, turning the per-fd transfer machinery (sf-offsets/sf-sizes) — which already existed for partial transfers — into a real feature.Supported forms (single range only):
bytes=A-B— inclusive[A, B],Bclamped to EOFbytes=A-— fromAto EOFbytes=-N— lastNbytesResponses:
206withContent-Range: bytes A-B/total,Accept-Ranges: bytes, andContent-Length= range length; the fd's sendfile offset is seeded toAand its size cap toB+1, so the existing transfer loop sends exactly[A, B].416 Range Not SatisfiablewithContent-Range: bytes */total, no transfer.200(a server MAY ignoreRange; keeps scope tight and is spec-compliant).Every
sendfile200now advertisesAccept-Ranges: bytes.HEADrequests get the same status line and headers with an empty body.How
The response is built (
web-build-response) before the file size is known, but the206/Content-Rangedecision needs it. The size is only learned when the server opens the file — at GET transfer setup, and (forHEAD) inweb-strip-head-body. So the rawRangevalue is carried from the parsed request to those points as an internalX-Sendfile-Rangemarker header (mirroring the existingX-Sendfileconvention) and stripped before serialization. Resolution against the real size happens once, where the file is opened.web-resolve-rangeis the pure core:(spec, total) -> Full | Partial [start end] | Unsatisfiable. Bound parsing saturates attotal+1once a value exceeds the file size, so oversized/malicious specs (e.g.bytes=999999999999-) can't overflowLong(which is 32-bit on some targets) — resolved bounds always stay within[0, total-1], so there is no bad-offset path intosendfile.The GET setup that opened the file and registered transfer state inline is extracted into
App.setup-sendfile, which now also resolves the range.Testing
test/web.carp: 236/0 (was 206). Adds pureweb-resolve-rangecases (bytes=0-499,500-,-500,0-, clamp past EOF,start≥size,bytes=abc/bytes=/missing-bytes=, multiple ranges,-0, empty file, overflow) plus HEAD integration tests throughweb-build-responseagainst the 11-bytestatic-fixtures/index.htmlfixture (206 +Content-Range+ range-lengthContent-Length+ empty body; 416;Accept-Rangeson the plain 200).test/websocket.carp: 128/0 (unchanged).min(b, total-1)flipsbytes=0-100000 clamps end to EOFto failing (235/1); reverting restores 236/0.test/smoke.sh(runs in CI): drives a real server withcurl -rfor a 206 body, an open-ended range that reassembles to the whole file, and a 416. Verified locally end-to-end (including a mid-filebytes=5-9→index, confirming the offset seek).carp-fmt --checkandanglerclean on changed files (no new findings).carp -bdoesn't emit a binary on the armhf dev box, so the smoke script itself was validated by running the same server viacarp -xand exercising the endpoints withcurl.Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.