Repository navigation
Don't write any bytes for H1 responses that shouldn't include a body - #3651
Merged
Merged
Conversation
gjcairo
force-pushed
the
http1-head-response
branch
from
July 9, 2026 14:37
d8caa34 to
a10f348
Compare
gjcairo
force-pushed
the
http1-head-response
branch
from
July 9, 2026 15:03
a10f348 to
93f3d27
Compare
aryan-25
reviewed
Jul 9, 2026
Comment on lines
+476
to
+477
| try self.encodeResponse(toRequestMethod: .CONNECT, status: .ok, body: ByteBuffer(string: "hello")), | ||
| "HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n" |
Contributor
There was a problem hiding this comment.
We should remove the transfer-encoding header if the request is a CONNECT request: https://datatracker.ietf.org/doc/html/rfc9112#section-6.1-10
A server MUST NOT send a Transfer-Encoding header field in any 2xx (Successful) response to a CONNECT request
Contributor
There was a problem hiding this comment.
correctlyFrameTransportHeaders will not add the header in the first place if hasBody == false.
But it is possible that that method won't be called if the automaticallySetFramingHeaders flag is overriden to false.
Lukasa
reviewed
Jul 13, 2026
| // `writeTrailers`). | ||
| let empty = context.channel.allocator.buffer(capacity: 0) | ||
| context.write(HTTPResponseEncoder.wrapOutboundOut(.byteBuffer(empty)), promise: promise) | ||
| break |
Contributor
There was a problem hiding this comment.
Rather than a trailing break, can we use an else?
| // mode) must not include (RFC 9110 Β§9.3.6, RFC 9112 Β§6.1). A HEAD response keeps | ||
| // status-based framing so it still advertises what an equivalent GET would return. | ||
| let hasBody: HTTPMethod.HasBody = | ||
| respondingToConnect ? .no : (response.status.mayHaveResponseBody ? .yes : .no) |
Contributor
There was a problem hiding this comment.
Let's split this ternary out, it's gotten a bit complex with the nesting.
Lukasa
approved these changes
Jul 15, 2026
gjcairo
added a commit
to swift-server/swift-http-server
that referenced
this pull request
Jul 15, 2026
This updates NIO to include an issue affecting H1 requests (see apple/swift-nio#3651)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
On the server side,
HTTPResponseEncoderchooses a response's body framing only from the response status (HTTPResponseStatus.mayHaveResponseBody): it has no knowledge of the request method as it's an outbound-only handler. But HTTP requires that a response to a HEAD request (and a 2xx response to CONNECT) carry no message body, regardless of status or framing headers.As a result, a response to a HEAD request with a status that allows a body (like 200) and no
Content-Lengthgets framed asTransfer-Encoding: chunked, and the encoder emits the chunked terminator (0\r\n\r\n). This is illegal.A conformant client won't read those bytes when receiving a HEAD response, so on a keep-alive connection they get misinterpreted as the start of the next response, corrupting the connection. This surfaces as intermittent, hard-to-diagnose failures (e.g. "I/O on closed channel") on the following request.
Modifications:
Made the server response framing method-aware without changing any public API:
HTTPResponseEncoderconsults the method and, for responses to HEAD/CONNECT requests, omits the body and the terminator.configureHTTPServerPipeline. When the encoder is constructed and used on its own, its behaviour is unchanged (framing is still derived solely from the status), so there is no behavioural or API change for users who may be creating this handler manually.Result:
Responses to
HEADandCONNECTrequests are no longer emitted with a message body, keeping keep-alive connections well-formed and RFC-compliant. No public API changes.