Skip to content

feat(s3): honor the Range header on GetObject and HeadObject (#396) - #405

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/396-s3-ranged-getobject
Jul 31, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/396-s3-ranged-getobject

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #396.

docs/services.md has claimed | GetObject | Supports Range header | since the
operation was first documented, but the header was never read — Content-Range,
StatusPartialContent and InvalidRange appeared nowhere in non-test source. A
consumer whose every read is a ranged GET (the reporter is building a FUSE
filesystem; a columnar reader seeking within a Parquet footer is the same shape)
silently received whole objects, so its read-path tests passed while verifying
nothing. That is the failure mode this repo exists to prevent, and the false doc
claim is what made it invisible.

The edge cases are the substance

S3 does not report an error for most bad ranges, so a caller cannot use a 416 to
detect one. Verified against the AWS GetObject API reference and RFC 9110 §14
rather than from memory:

Range (1000-byte object) Result
bytes=0-99 206, Content-Range: bytes 0-99/1000
bytes=900- 206, bytes 900-999/1000
bytes=-100 206, bytes 900-999/1000 (suffix)
bytes=0-99999 206, clamped to bytes 0-999/1000 — past EOF is not an error
bytes=1000-1099 416 InvalidRange + Content-Range: bytes */1000
bytes=-0 416 — a zero-length suffix is unsatisfiable
bytes=abc, bytes=500-100 200, whole object — malformed ranges are ignored
bytes=0-99,200-299 200, whole object — S3 serves one range per GET
items=0-99 200, whole object — non-bytes units ignored

byteRangeSpec therefore carries three states rather than a
(start, end, error) triple, which cannot distinguish "ignore, serve 200" from
"416". HEAD honors Range identically, minus the body. Ranges compose with
versionId, and every range against a zero-byte object is unsatisfiable.

Two supporting changes

The error builder can now carry child elements and headers. The
InvalidRange body needs <ActualObjectSize>/<RangeRequested>, and #400
(MinSizeAllowed) and #398 (StorageClass) will need the same. The existing
extras ...string parameter was an unused stub (_ = extras) with zero call
sites, so it is replaced by an s3Error options struct rather than given a
meaning — one flat variadic cannot express two independent variable-length things
without a magic sentinel. All 61 s3ErrorResponse call sites compile untouched,
and s3DeleteMarkerResponse now shares the builder instead of re-declaring its
own XML type. The dynamic element names rely on encoding/xml resolving a name
from the XMLName field's value ahead of the parent field's tag, so a test
asserts the exact XML bytes.

The GET/HEAD header block was duplicated 3× and had already drifted. It moves
to objectResponseHeaders, and resolveTaskCompletion returns the object and
body it synthesized rather than a finished response. Its clock-gate branch
returned a NoSuchKey byte-identical to getObject's own, so that case collapses
into handled=false, and the synthesized completion record becomes rangeable like
any other object. Two behaviour changes fall out, logged under Fixed: HEAD now
reports x-amz-version-id (GET always did, so a caller had to download a body to
learn which version it had just described), and both now advertise
Accept-Ranges: bytes.

The trap worth flagging in review

Content-Length is overwritten on a 206 rather than left to the server.
server.writeResponse only defaults Content-Length when the plugin supplied
none, and the object header block always supplies obj.Size — so a short body
would ship under a full-object length and hang SDK clients. A test asserts
Content-Length equals the bytes actually written on every 206.

The body slice is clamped against the body actually read, because getObject's
fallback path can return fewer bytes than the recorded Size. That pre-existing
drift already made Content-Length lie; the clamp keeps it from panicking too,
rather than papering over it.

Out of scope

Retrieving a part by ?partNumber=N is unrouted today and needs per-part byte
boundaries persisted on S3Object — which completeMultipartUpload currently
deletes. Noted in the docs as unimplemented; separate issue if wanted.

Verification

go build, go vet, golangci-lint run ./... (0 issues), make test (race),
test/e2e, and make docs-reference-check all pass locally. New
emulator/s3_range_test.go covers all 15 table rows above plus the
Content-Length desync guard, ETag/Last-Modified identity between 200 and 206,
range+versionId, zero-byte objects, HEAD+Range, case-folded header lookup, a
ranged read of a synthesized completion record, and exact-bytes assertions on the
new error XML.

docs/services.md has claimed "GetObject | Supports Range header" since the
operation was first documented, but the header was never read: Content-Range,
StatusPartialContent and InvalidRange appeared nowhere in non-test source. A
consumer whose every read is a ranged GET — a FUSE filesystem, a columnar
reader seeking within a Parquet footer — silently received whole objects, so
its read-path tests passed while verifying nothing.

The edge cases are the substance of the fix, because S3 does not report an
error for most bad ranges and a caller cannot use a 416 to detect one. A range
past the end of the object is clamped, not rejected. A malformed range, a
multi-range request, and a unit other than bytes are all *ignored* — 200 with
the whole object. Only a range starting at or beyond the object's end is a 416
InvalidRange, and every range against a zero-byte object is unsatisfiable.
byteRangeSpec therefore carries three states rather than a (start, end, error)
triple, which cannot distinguish "ignore, serve 200" from "416".

Two supporting changes, behavior-preserving except where noted:

The error-response builder can now carry error-specific child elements and
response headers, which the InvalidRange body needs (<ActualObjectSize>,
<RangeRequested>, Content-Range: bytes */<size>) and which MinSizeAllowed
(#400) and StorageClass (#398) will need. Its extras ...string parameter was
an unused stub with zero call sites, so it is replaced by an s3Error options
struct rather than given a meaning; one flat variadic cannot express two
independent variable-length things. s3DeleteMarkerResponse shares the builder
instead of re-declaring its own XML type.

The GET/HEAD header block was duplicated three times and had already drifted,
so it moves to objectResponseHeaders and resolveTaskCompletion now returns the
object and body it synthesized rather than a finished response. Its clock-gate
branch returned a NoSuchKey byte-identical to getObject's own, so that case
collapses into handled=false, and a synthesized completion record is rangeable
like any other object. Two consequences of de-duplicating, logged as fixes:
HEAD now reports x-amz-version-id (GET always did, so a caller had to download
a body to learn which version it had just described), and both now advertise
Accept-Ranges: bytes.

Content-Length is overwritten on a 206 rather than left to the server, which
is required and not cosmetic: writeResponse only defaults it when the plugin
supplied none, and the header block always supplies the full object size, so a
short body would ship under a full-object length and hang SDK clients. A test
asserts Content-Length equals the bytes actually written on every 206. The
body slice is clamped against the body actually read, because getObject's
fallback path can return fewer bytes than the recorded size — that drift
already made Content-Length lie, and the clamp keeps it from panicking too.

Retrieving a part by ?partNumber=N stays unimplemented; it needs per-part byte
boundaries persisted on S3Object, which completeMultipartUpload deletes.

Refs #396
@scttfrdmn scttfrdmn added this to the v0.79.0 milestone Jul 31, 2026
@scttfrdmn scttfrdmn added type: feature New feature or improvement area: s3 Issues relating to the s3 component priority: high Must be done for this milestone labels Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.92806% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_plugin.go 89.70% 5 Missing and 2 partials ⚠️
emulator/s3_range.go 93.10% 2 Missing and 2 partials ⚠️
emulator/spawn_control.go 76.92% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: s3 Issues relating to the s3 component priority: high Must be done for this milestone type: feature New feature or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S3: GetObject ignores the Range header (no 206 / Content-Range / 416); docs claim it works

1 participant