Skip to content
Myr

fix(compress): do not compress 206 Partial Content responses (#5020)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

compress() never looked at the response status, so a 206 was gzipped while its Content-Range header still described the uncompressed byte range, and the Content-Length matching the partial body was dropped. Range clients reassembling parts from such responses end up with corrupted data. Skip compression when the status is 206. Reported as the second issue in #5010.

authored by Rıdvan Bahçeci · committed by GitHub · parent a48c8bf · Browse files · 2 files+15 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−0 removed

  • tests:+14 added−0 removed
  • other:+1 added−0 removed

2 files+15 added−0 removed

modified:src/middleware/compress/index.test.ts+14 added
56 unmodified lines
57 c.header('Transfer-Encoding', 'chunked')58 c.header('Content-Length', '1024')59 return c.body(new Uint8Array(1024)) // Simulated chunked data60 })61 app.get('/partial', (c) => {62 c.header('Content-Type', 'text/plain')63 c.header('Content-Length', '1024')64 c.header('Content-Range', 'bytes 0-1023/2048')65 return c.body('a'.repeat(1024), 206)66 })67 app.get('/stream', (c) =>68 stream(c, async (stream) => {69 c.header('Content-Type', 'text/plain')70 // 60000 bytes
91 unmodified lines
162 const res = await testCompression('/chunked', 'gzip', null)163 expect(res.headers.get('Content-Length')).toBe('1024')164 expect(res.headers.get('Transfer-Encoding')).toBe('chunked')165 })166167 it('should not compress 206 Partial Content responses', async () => {168 const res = await testCompression('/partial', 'gzip', null)169 expect(res.status).toBe(206)170 expect(res.headers.get('Content-Length')).toBe('1024')171 expect(res.headers.get('Content-Range')).toBe('bytes 0-1023/2048')172 expect(await res.text()).toBe('a'.repeat(1024))173 })174 })175176 describe('JSON Handling', () => {177 it('should not compress small JSON responses', async () => {
271 unmodified lines
modified:src/middleware/compress/index.ts+1 added