Skip to content

Fix incorrect status lines in HTTPResponseEncoder fast paths - #3631

Merged
glbrntt merged 3 commits into
apple:mainfrom
tkshsbcue:fix-http-response-encoder-status-lines
Jun 23, 2026
Merged

glbrntt merged 3 commits into
apple:mainfrom
tkshsbcue:fix-http-response-encoder-status-lines

Conversation

@tkshsbcue

Copy link
Copy Markdown
Contributor

Motivation

HTTPResponseEncoder writes hand-optimised status lines for the well-known HTTPResponseStatus values instead of going through the general default path that formats HTTP/<version> <code> <reasonPhrase>. Several of those fast paths had drifted out of sync with HTTPResponseStatus.reasonPhrase, so the exact bytes on the wire depended on whether a status happened to have a fast path:

  • HTTP/1.0 + 511 emitted the wrong HTTP version (HTTP/1.1 511 ...) β€” a wire-format correctness bug, not just cosmetic.
  • HTTP/1.1 + 431 emitted the reason phrase Range Not Satisfiable instead of Request Header Fields Too Large.
  • HTTP/1.1 + 416 emitted Request Range Not Satisified (also misspelled) instead of Range Not Satisfiable.
  • HTTP/1.0 and HTTP/1.1 + 307 emitted Tempory Redirect (misspelled) instead of Temporary Redirect.

Resolves #3630.

Modifications

  • Correct the five diverging status lines in HTTPResponseEncoder so they match HTTPResponseStatus.reasonPhrase and carry the correct HTTP version.
  • Add a regression test that, for every well-known status at both HTTP/1.0 and HTTP/1.1, asserts the encoded status line equals the canonical HTTP/<version> <code> <reasonPhrase> form, keeping the fast paths in lockstep with reasonPhrase.

Result

The encoder now produces correct, consistent status lines for all well-known statuses regardless of whether they use a fast path. The new test fails on the pre-fix code (the 511 case fails on the version, the 431/416/307 cases on the reason phrase) and passes after the fix.

Motivation:

`HTTPResponseEncoder` writes hand-optimised status lines for the
well-known `HTTPResponseStatus` values instead of going through the
general `default` path that formats `HTTP/<version> <code>
<reasonPhrase>`. Several of those fast paths had drifted out of sync
with `HTTPResponseStatus.reasonPhrase`, so the exact bytes on the wire
depended on whether a status happened to have a fast path:

* HTTP/1.0 + 511 emitted the wrong HTTP version (`HTTP/1.1 511 ...`),
  which is a wire-format correctness bug, not just cosmetic.
* HTTP/1.1 + 431 emitted the reason phrase `Range Not Satisfiable`
  instead of `Request Header Fields Too Large`.
* HTTP/1.1 + 416 emitted `Request Range Not Satisified` (also
  misspelled) instead of `Range Not Satisfiable`.
* HTTP/1.0 and HTTP/1.1 + 307 emitted `Tempory Redirect` (misspelled)
  instead of `Temporary Redirect`.

Modifications:

* Correct the five diverging status lines in `HTTPResponseEncoder` so
  they match `HTTPResponseStatus.reasonPhrase` and carry the correct
  HTTP version.
* Add a regression test that, for every well-known status at both
  HTTP/1.0 and HTTP/1.1, asserts the encoded status line equals the
  canonical `HTTP/<version> <code> <reasonPhrase>` form, keeping the
  fast paths in lockstep with `reasonPhrase`.

Result:

The encoder now produces correct, consistent status lines for all
well-known statuses regardless of whether they use a fast path.

@glbrntt glbrntt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this look great. One tiny thing in the tests to fixup.

andHeaders: HTTPHeaders(),
version: version
)
let bytes = written.getString(at: written.readerIndex, length: written.readableBytes)!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the direct init rather than using get* APIs. I think this is the API:

Suggested change
let bytes = written.getString(at: written.readerIndex, length: written.readableBytes)!
let bytes = String(buffer: written)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it!

Address review feedback to prefer the direct String(buffer:) initializer
over the getString get* API.
@tkshsbcue
tkshsbcue force-pushed the fix-http-response-encoder-status-lines branch from 60cdfb0 to 55127fd Compare June 23, 2026 11:40
@tkshsbcue

Copy link
Copy Markdown
Contributor Author

hey @glbrntt just made those changes you suggest please have a look
Thanks!

@glbrntt glbrntt added the πŸ”¨ semver/patch No public API change. label Jun 23, 2026

@glbrntt glbrntt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@glbrntt
glbrntt enabled auto-merge (squash) June 23, 2026 12:28
@glbrntt
glbrntt merged commit a8e036c into apple:main Jun 23, 2026
50 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

πŸ”¨ semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTPResponseEncoder emits wrong version / reason phrase for some status lines (511, 431, 416, 307)

2 participants