@harris But that’s exact issue I’m having. If worker returns gzipped content from cache or other source like lambda response that requires Content-Encoding: gzip
header, then Cloudflare double-gunzips it or something. Here’s worker to reproduce:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const hello = new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])
const compressedHello = new Uint8Array([
0x61, 0x0d, 0x0a, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x13, 0x0d, 0x0a, 0x66, 0x0d, 0x0a, 0xcb, 0x48, 0xcd, 0xc9,
0xc9, 0x07, 0x00, 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00,
0x0d, 0x0a, 0x30, 0x0d, 0x0a, 0x0d, 0x0a
])
async function handleRequest(request) {
const pathname = new URL(request.url).pathname
if (pathname === '/compressed') {
const response = new Response(compressedHello)
response.headers.set('Cache-Control', 'no-transform')
response.headers.set('Transfer-Encoding', 'chunked')
response.headers.set('Content-Encoding', 'gzip')
response.headers.set('X-Compressed', 'yes')
return response
}
if (pathname === '/plain') {
const response = new Response(hello)
response.headers.set('X-Compressed', 'no')
response.headers.set('Cache-Control', 'no-transform')
return response
}
return new Response('ok')
}
Both hello
and compressedHello
could be stored in cache. hello
is plain response and compressedHello
is “hello” as gzip compressed and chunked response. compressedHello
response must have Content-Encoding: gzip for curl to interpret such response properly, so I cannot just skip it.
As you see binary data of plain “hello” is returned 1:1:
curl https://test.sheerun.workers.dev/plain --http1.1 --raw --silent | xxd -p -l 50 | fold -w2 | while read b; do echo 0x$b,; done | tr "\n" " "
0x68, 0x65, 0x6c, 0x6c, 0x6f
But cached compressed response is modified by Cloudflare…
curl https://test.sheerun.workers.dev/compressed --http1.1 --raw --silent | xxd -p -l 50 | fold -w2 | while read b; do echo 0x$b,; done | tr "\n" " "
0x32, 0x38, 0x0d, 0x0a, 0x61, 0x0d, 0x0a, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x0d, 0x0a, 0x66, 0x0d, 0x0a, 0xcb, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00, 0x0d, 0x0a, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x0d, 0x0a, 0x30, 0x0d, 0x0a, 0x0d