Repository navigation
fix(cf-workers): send Accept-Encoding on backend reads so stored encodings pass through - #165
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dings pass through On Cloudflare, a Worker subrequest without Accept-Encoding has an object stored with Content-Encoding (e.g. uploaded brotli-compressed) decoded before the Worker sees it, so every client got the uncompressed body with the header dropped. Reads now send `Accept-Encoding: br, gzip` (ForwardRequest:: accept_encoding); the encoded body passes through and the edge decodes it only for clients that can't take it. Object stores ignore the header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Claude finished @alukach's task in 13s —— View job ✅ No blocking issues — safe to merge. I read the diff for
Simplify (ponytail)
💰 Estimated review cost: $0.10 · 0m12s · 4 turns |
|
📖 Docs preview deployed to https://multistore-docs-pr-165.development-seed.workers.dev
|
|
🚀 Latest commit deployed to https://multistore-proxy-pr-165.development-seed.workers.dev
|
4 of 5 tasks
Ranged reads gain nothing from it (Cloudflare strips Content-Encoding from 206s) and it would invite a backend that compresses on request to return a compressed slice. Docs now say runtimes opt in (only the Workers backend does) and that the passthrough covers br and gzip. Drops the integration test that asserted a 206 keeps Content-Encoding, which only holds under wrangler dev, and closes the remaining test's streamed response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch was successfully deployed
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 I'm changing
An object stored with a
Content-Encodingloses it when served through a deployed Worker. For example,source/metadata-catalog/catalog.jsonlis now uploaded brotli-compressed (Content-Encoding: br, 789 KB) and served by data.source.coop.Content-Encodingand a weakened ETag, even clients that sendAccept-Encoding: br.The cause is Cloudflare's network, not the proxy code. A Worker subrequest that doesn't send
Accept-Encodinghas an encoded backend body decoded before the Worker sees it.wrangler devdoesn't do this, so the existing integration setup can't reproduce it. Cloudflare's fetch docs describe the passthrough: "the Accept-Encoding allows the worker to directly receive the compressed data stream from the server without it being decompressed beforehand."Verified on Cloudflare's real network with a throwaway Worker on
source-coop.workers.dev. It mirrorsforward(): fetch the S3 object, then re-wrap its body stream and headers.Accept-Encoding: bridentity/gzipAccept-Encoding)Content-Encoding, weak ETagCache-Control: no-transformencodeResponseBody/encodeBody: "manual"Accept-Encoding: br, gzipContent-Encoding: br, strong ETagAccept-Encoding+no-transformno-transformstays as it is: it doesn't interfere with the passthrough.How I did it
crates/core/src/route_handler.rs: newForwardRequest::accept_encoding()returnsSome("br, gzip")for non-ranged GET/HEAD andNoneotherwise. Ranged reads get none: Cloudflare stripsContent-Encodingfrom 206s anyway, so the header would gain nothing and would only invite a backend that compresses on request to return a compressed slice. It sits next toshould_bypass_cache, the other read policy driven by edge behaviour, so the native unit tests cover it. The cf-workers crate's tests don't compile natively and aren't indefault-members.crates/cf-workers/src/backend.rs:WorkerBackend::forwardsets that header on the subrequest. Object stores ignore it (S3 serves the object as stored), and backend URLs are presigned overhostonly, so an unsigned standard header is fine.tests/integration/test_integration.py:TestStoredContentEncodingstores a gzip-encoded object directly in RustFS. It asserts that a full GET through the proxy returns the stored bytes withContent-Encoding. This passes underwrangler devwith or without the fix, as noted in its docstring. It guards the proxy's own handling, not the Cloudflare-only behaviour.docs/deployment/cloudflare-workers.md(Response Handling: coversbr/gziponly) anddocs/architecture/request-lifecycle.md. The latter describesaccept_encoding()as opt-in: only the Workers backend sends it, and the server/lambda examples don't need it, because their HTTP clients don't decode bodies.Known limit: Cloudflare's edge still removes
Content-Encodingfrom 206 responses for encoded objects, under every variant tested. Range reads of pre-compressed objects aren't fixed by this change.Review notes:
Content-Lengthof the stored (encoded) bytes, while a client that doesn't accept the encoding gets a larger decoded body from the edge. That's standardContent-Encodingsemantics, and it's what S3 itself returns.wrangler devpasses it through either way. The regression guard is fix: serve stored Content-Encoding objects encoded to clients that accept it source-cooperative/data.source.coop#264's production deploy check, which fails against today's production and passes with the fix.Accept-Encoding: noContent-Encoding, sameContent-Length,Accept-Ranges: bytes, strong ETag.Test plan
cargo test: all workspace tests pass, including the newaccept_encoding_on_reads_only(written first; it failed to compile before the method existed)cargo clippy -- -D warnings,cargo fmt --check,cargo check -p multistore-cf-workers --target wasm32-unknown-unknownmake test-integration: 38 passed, 7 skipped (the OIDC tests, which need GitHub Actions)source-coop.workers.dev, results in the table abovecurl -H 'Accept-Encoding: br' -sI https://data.source.coop/source/metadata-catalog/catalog.jsonlshowscontent-encoding: br🤖 Generated with Claude Code