fix(compress): do not compress 206 Partial Content responses by dr4gan0x · Pull Request #5020 · honojs/hono · GitHub
Skip to content

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

Open
dr4gan0x wants to merge 1 commit into
honojs:mainfrom
dr4gan0x:fix/compress-skip-206
Open

fix(compress): do not compress 206 Partial Content responses#5020
dr4gan0x wants to merge 1 commit into
honojs:mainfrom
dr4gan0x:fix/compress-skip-206

Conversation

@dr4gan0x

@dr4gan0x dr4gan0x commented Jun 11, 2026

Copy link
Copy Markdown

This addresses the second of the three problems reported in #5010 (compress encodes 206 Partial Content).

The middleware bails out for already-encoded, chunked, HEAD, small, non-compressible and no-transform responses, but it never checks the status code. A 206 ends up gzipped while its Content-Range header still refers to the uncompressed byte range, and the Content-Length that matched the partial body gets dropped. A range client stitching parts together from such responses gets corrupted data. This is easy to hit with a handler that does its own range handling, or when proxying an upstream that serves ranges, since the proxy helper passes the upstream status through.

The fix skips compression when the status is 206.

The new test serves a 1024 byte text/plain part with Content-Range and asserts it comes back uncompressed with status, Content-Length and Content-Range intact. It fails without the guard.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

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 honojs#5010.
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant