Skip to content

fix(cf-workers): send Accept-Encoding on backend reads so stored encodings pass through - #165

Merged
alukach merged 3 commits into
mainfrom
fix/forward-content-encoding
Oct 26, 2026
Merged

alukach merged 3 commits into
mainfrom
fix/forward-content-encoding

Conversation

@alukach

@alukach alukach commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

What I'm changing

An object stored with a Content-Encoding loses it when served through a deployed Worker. For example, source/metadata-catalog/catalog.jsonl is now uploaded brotli-compressed (Content-Encoding: br, 789 KB) and served by data.source.coop.

  • Full GETs: every client gets the 6.1 MB decoded body, with no Content-Encoding and a weakened ETag, even clients that send Accept-Encoding: br.
  • Ranged GETs: they return raw brotli slices with no header.

The cause is Cloudflare's network, not the proxy code. A Worker subrequest that doesn't send Accept-Encoding has an encoded backend body decoded before the Worker sees it. wrangler dev doesn'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 mirrors forward(): fetch the S3 object, then re-wrap its body stream and headers.

Worker variant Client Accept-Encoding: br Client identity / gzip
as today (no upstream Accept-Encoding) 6,075,430 B plain, no Content-Encoding, weak ETag same
+ Cache-Control: no-transform same same
+ encodeResponseBody/encodeBody: "manual" same same
+ upstream Accept-Encoding: br, gzip 788,699 B stored brotli, Content-Encoding: br, strong ETag 6,075,430 B plain, decoded by the edge
+ upstream Accept-Encoding + no-transform identical to the row above identical

no-transform stays as it is: it doesn't interfere with the passthrough.

How I did it

  • crates/core/src/route_handler.rs: new ForwardRequest::accept_encoding() returns Some("br, gzip") for non-ranged GET/HEAD and None otherwise. Ranged reads get none: Cloudflare strips Content-Encoding from 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 to should_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 in default-members.
  • crates/cf-workers/src/backend.rs: WorkerBackend::forward sets that header on the subrequest. Object stores ignore it (S3 serves the object as stored), and backend URLs are presigned over host only, so an unsigned standard header is fine.
  • tests/integration/test_integration.py: TestStoredContentEncoding stores a gzip-encoded object directly in RustFS. It asserts that a full GET through the proxy returns the stored bytes with Content-Encoding. This passes under wrangler dev with or without the fix, as noted in its docstring. It guards the proxy's own handling, not the Cloudflare-only behaviour.
  • Docs: docs/deployment/cloudflare-workers.md (Response Handling: covers br/gzip only) and docs/architecture/request-lifecycle.md. The latter describes accept_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-Encoding from 206 responses for encoded objects, under every variant tested. Range reads of pre-compressed objects aren't fixed by this change.

Review notes:

  • HEAD vs GET size: for an encoded object, HEAD reports Content-Length of the stored (encoded) bytes, while a client that doesn't accept the encoding gets a larger decoded body from the edge. That's standard Content-Encoding semantics, and it's what S3 itself returns.
  • Test-first: no test in this repo fails without the fix, because only Cloudflare's network decodes the body; wrangler dev passes 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.
  • Plain objects are unaffected: checked through #264's preview deploy, which runs this branch on Cloudflare's network. A Markdown file and a CSV come back exactly as on staging without the fix, under every client Accept-Encoding: no Content-Encoding, same Content-Length, Accept-Ranges: bytes, strong ETag.

Test plan

  • cargo test: all workspace tests pass, including the new accept_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-unknown
  • make test-integration: 38 passed, 7 skipped (the OIDC tests, which need GitHub Actions)
  • Edge behaviour: probe Worker on source-coop.workers.dev, results in the table above
  • After release: bump in data.source.coop and confirm curl -H 'Accept-Encoding: br' -sI https://data.source.coop/source/metadata-catalog/catalog.jsonl shows content-encoding: br

🤖 Generated with Claude Code

alukach and others added 2 commits October 25, 2026 22:32
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

claude Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Claude finished @alukach's task in 13s —— View job


✅ No blocking issues — safe to merge.

I read the diff for crates/core/src/route_handler.rs, crates/cf-workers/src/backend.rs and the integration test. I found no correctness or security problems.

  • accept_encoding() returns a header only for un-ranged GET/HEAD. backend.rs sets it with headers.set, so it overwrites any client Accept-Encoding that was forwarded. The backend URL is presigned over host only, so signing isn't affected.
  • Non-blocking: the PR body says the new test covers "full and ranged GETs", but TestStoredContentEncoding has only test_full_get_keeps_encoding. Either add a ranged case or reword the body. The ranged case is one more request and it asserts on the 206 body.
  • Non-blocking: backend.rs discards the Result from headers.set with let _ =. That's fine for a static valid header name and value, and it matches how the rest of the code handles web_sys calls.

Simplify (ponytail)

  • crates/core/src/route_handler.rs accept_encoding(): the 12-line doc comment restates the PR description. Trim it to 3–4 lines (why Cloudflare needs the header, and why ranged reads get none).
  • crates/core/src/route_handler.rs test accept_encoding_on_reads_only: the HEAD and POST assertions repeat the GET and PUT cases. Keep GET, ranged GET and PUT.

💰 Estimated review cost: $0.10 · 0m12s · 4 turns

@github-actions github-actions Bot added the fix label Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

📖 Docs preview deployed to https://multistore-docs-pr-165.development-seed.workers.dev

  • Date: 2026-10-26T06:14:11Z
  • Commit: 497ce98

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🚀 Latest commit deployed to https://multistore-proxy-pr-165.development-seed.workers.dev

  • Date: 2026-10-26T06:14:11Z
  • Commit: 497ce98

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>
@alukach
alukach marked this pull request as ready for review October 26, 2026 22:11
@alukach
alukach merged commit d857803 into main Oct 26, 2026
20 of 21 checks passed
@alukach
alukach deleted the fix/forward-content-encoding branch October 26, 2026 22:12

This branch was successfully deployed

1 active deployment
preview — e9b304f5 Deployed Oct 26, 2026 by alukach via Deploy & Test / Deploy #482
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant