mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +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.
50 lines
2.0 KiB
Swift
50 lines
2.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOCore
|
|
|
|
/// A FIFO of the methods of requests that have been decoded but whose responses have not yet been
|
|
/// written, used by ``HTTPResponseEncoder`` to omit the body from responses to `HEAD`/`CONNECT`
|
|
/// requests (which must not carry one — RFC 9110 §9.3.2, RFC 9112 §6.3).
|
|
///
|
|
/// HTTP/1 serializes responses, so in the common case only a single request is awaiting a response
|
|
/// and its method is held inline with no heap allocation. The `overflow` buffer is only allocated
|
|
/// if a peer pipelines requests that the decoder reads ahead within a single read (so more than one
|
|
/// is pending at once); it stays `nil` — and thus allocation-free — otherwise.
|
|
struct HTTPServerRequestMethodQueue {
|
|
/// The method of the oldest request still awaiting a response, or `nil` if none is pending.
|
|
private var first: HTTPMethod?
|
|
/// Methods of any further pending requests, oldest first. Allocated only under pipelining.
|
|
private var overflow: CircularBuffer<HTTPMethod>?
|
|
|
|
mutating func append(_ method: HTTPMethod) {
|
|
if self.first == nil {
|
|
self.first = method
|
|
} else {
|
|
if self.overflow == nil {
|
|
self.overflow = CircularBuffer(initialCapacity: 4)
|
|
}
|
|
self.overflow!.append(method)
|
|
}
|
|
}
|
|
|
|
mutating func popFirst() -> HTTPMethod? {
|
|
guard let method = self.first else {
|
|
return nil
|
|
}
|
|
self.first = self.overflow?.popFirst()
|
|
return method
|
|
}
|
|
}
|