/src/swift-nio/Sources/NIOHTTP1/HTTPServerRequestMethodQueue.swift
Line | Count | Source |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // This source file is part of the SwiftNIO open source project |
4 | | // |
5 | | // Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors |
6 | | // Licensed under Apache License v2.0 |
7 | | // |
8 | | // See LICENSE.txt for license information |
9 | | // See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
10 | | // |
11 | | // SPDX-License-Identifier: Apache-2.0 |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | import NIOCore |
16 | | |
17 | | /// A FIFO of the methods of requests that have been decoded but whose responses have not yet been |
18 | | /// written, used by ``HTTPResponseEncoder`` to omit the body from responses to `HEAD`/`CONNECT` |
19 | | /// requests (which must not carry one — RFC 9110 §9.3.2, RFC 9112 §6.3). |
20 | | /// |
21 | | /// HTTP/1 serializes responses, so in the common case only a single request is awaiting a response |
22 | | /// and its method is held inline with no heap allocation. The `overflow` buffer is only allocated |
23 | | /// if a peer pipelines requests that the decoder reads ahead within a single read (so more than one |
24 | | /// is pending at once); it stays `nil` — and thus allocation-free — otherwise. |
25 | | struct HTTPServerRequestMethodQueue { |
26 | | /// The method of the oldest request still awaiting a response, or `nil` if none is pending. |
27 | | private var first: HTTPMethod? |
28 | | /// Methods of any further pending requests, oldest first. Allocated only under pipelining. |
29 | | private var overflow: CircularBuffer<HTTPMethod>? |
30 | | |
31 | 0 | mutating func append(_ method: HTTPMethod) { |
32 | 0 | if self.first == nil { |
33 | 0 | self.first = method |
34 | 0 | } else { |
35 | 0 | if self.overflow == nil { |
36 | 0 | self.overflow = CircularBuffer(initialCapacity: 4) |
37 | 0 | } |
38 | 0 | self.overflow!.append(method) |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | mutating func popFirst() -> HTTPMethod? { |
43 | 0 | guard let method = self.first else { |
44 | 0 | return nil |
45 | 0 | } |
46 | 0 | self.first = self.overflow?.popFirst() |
47 | 0 | return method |
48 | 0 | } |
49 | | } |