mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
### 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.