Skip to content

Don't write any bytes for H1 responses that shouldn't include a body - #3651

Merged
gjcairo merged 6 commits into
apple:mainfrom
gjcairo:http1-head-response
Jul 15, 2026
Merged

gjcairo merged 6 commits into
apple:mainfrom
gjcairo:http1-head-response

Conversation

@gjcairo

@gjcairo gjcairo commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation:

On the server side, HTTPResponseEncoder chooses 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-Length gets framed as Transfer-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:

  • The request decoder now hands each decoded request's method to the response encoder.
  • HTTPResponseEncoder consults the method and, for responses to HEAD/CONNECT requests, omits the body and the terminator.
  • This is wired up in 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 HEAD and CONNECT requests are no longer emitted with a message body, keeping keep-alive connections well-formed and RFC-compliant. No public API changes.

@gjcairo gjcairo added the πŸ”¨ semver/patch No public API change. label Jul 9, 2026
@gjcairo
gjcairo force-pushed the http1-head-response branch from d8caa34 to a10f348 Compare July 9, 2026 14:37
@gjcairo
gjcairo force-pushed the http1-head-response branch from a10f348 to 93f3d27 Compare July 9, 2026 15:03
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"

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.

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

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.

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.

@gjcairo
gjcairo requested a review from aryan-25 July 13, 2026 10:42
Comment thread Sources/NIOHTTP1/HTTPEncoder.swift Outdated
// `writeTrailers`).
let empty = context.channel.allocator.buffer(capacity: 0)
context.write(HTTPResponseEncoder.wrapOutboundOut(.byteBuffer(empty)), promise: promise)
break

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.

Rather than a trailing break, can we use an else?

Comment thread Sources/NIOHTTP1/HTTPEncoder.swift Outdated
// 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)

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.

Let's split this ternary out, it's gotten a bit complex with the nesting.

Comment thread Sources/NIOHTTP1/HTTPEncoder.swift Resolved
Comment thread Sources/NIOHTTP1/HTTPEncoder.swift Resolved
@gjcairo
gjcairo requested a review from Lukasa July 14, 2026 15:42
@gjcairo
gjcairo merged commit 0b18836 into apple:main Jul 15, 2026
53 of 57 checks passed
@gjcairo
gjcairo deleted the http1-head-response branch July 15, 2026 13:55
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)
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.

3 participants