Fix incorrect status lines in HTTPResponseEncoder fast paths (#3631)

### 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.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
This commit is contained in:
Kumar TanayandGeorge Barnett authored and GitHub committed 2026-06-23 12:33:49 +00:00
1 parent a863732fbc
commit a8e036cb86
2 files changed
+48 -6

No files matched your search

+5 -5
View File
@@ -387,7 +387,7 @@ extension ByteBuffer {
case (1, 0, .useProxy):
self.writeStaticString("HTTP/1.0 305 Use Proxy\r\n")
case (1, 0, .temporaryRedirect):
self.writeStaticString("HTTP/1.0 307 Tempory Redirect\r\n")
self.writeStaticString("HTTP/1.0 307 Temporary Redirect\r\n")
case (1, 0, .permanentRedirect):
self.writeStaticString("HTTP/1.0 308 Permanent Redirect\r\n")
case (1, 0, .badRequest):
@@ -465,7 +465,7 @@ extension ByteBuffer {
case (1, 0, .notExtended):
self.writeStaticString("HTTP/1.0 510 Not Extended\r\n")
case (1, 0, .networkAuthenticationRequired):
self.writeStaticString("HTTP/1.1 511 Network Authentication Required\r\n")
self.writeStaticString("HTTP/1.0 511 Network Authentication Required\r\n")
// Optimization for HTTP/1.1
case (1, 1, .custom(_, _)):
@@ -511,7 +511,7 @@ extension ByteBuffer {
case (1, 1, .useProxy):
self.writeStaticString("HTTP/1.1 305 Use Proxy\r\n")
case (1, 1, .temporaryRedirect):
self.writeStaticString("HTTP/1.1 307 Tempory Redirect\r\n")
self.writeStaticString("HTTP/1.1 307 Temporary Redirect\r\n")
case (1, 1, .permanentRedirect):
self.writeStaticString("HTTP/1.1 308 Permanent Redirect\r\n")
case (1, 1, .badRequest):
@@ -547,7 +547,7 @@ extension ByteBuffer {
case (1, 1, .unsupportedMediaType):
self.writeStaticString("HTTP/1.1 415 Unsupported Media Type\r\n")
case (1, 1, .rangeNotSatisfiable):
self.writeStaticString("HTTP/1.1 416 Request Range Not Satisified\r\n")
self.writeStaticString("HTTP/1.1 416 Range Not Satisfiable\r\n")
case (1, 1, .expectationFailed):
self.writeStaticString("HTTP/1.1 417 Expectation Failed\r\n")
case (1, 1, .misdirectedRequest):
@@ -565,7 +565,7 @@ extension ByteBuffer {
case (1, 1, .tooManyRequests):
self.writeStaticString("HTTP/1.1 429 Too Many Requests\r\n")
case (1, 1, .requestHeaderFieldsTooLarge):
self.writeStaticString("HTTP/1.1 431 Range Not Satisfiable\r\n")
self.writeStaticString("HTTP/1.1 431 Request Header Fields Too Large\r\n")
case (1, 1, .unavailableForLegalReasons):
self.writeStaticString("HTTP/1.1 451 Unavailable For Legal Reasons\r\n")
case (1, 1, .internalServerError):