HTTP/1.1

On this page

How the HTTP1 target serves HTTP/1.1: when a connection uses it, what happens to the bytes on a connection, which limits and timeouts apply, which status codes malformed requests get, and how to configure it from Swift or http-config.json. For the server as a whole, start with the overview.

Where it sits

listener ─▶ transport ─▶ protocol registry ─▶ HTTP1.run ─▶ handler chain ─▶ response
 (BSD / NW TCP / TLS)     (ALPN or sniff)      (one per connection)  (middleware…, handler)

A listener accepts a connection and wraps it in a ByteStreamTransport (BSD sockets + kqueue or Network.framework TCP for cleartext, Network.framework for TLS). The Server's protocol registry picks a protocol for it once. If that is HTTP/1.1, HTTP1.run owns the connection until it closes: it parses each request head, calls the handler chain (the same chain HTTP/2 and HTTP/3 call), and writes the response. Middleware never sees HTTP/1.1 framing; it sees an HTTPRequest and returns an HTTPResponse.

The target holds these pieces, each tested alone over in-memory transports:

TypeFileRole
HTTP1HTTP1.swiftThe ConnectionProtocol you register, and the per-connection loop
HTTP1ConfigurationHTTP1.swiftBuffer sizes, body limits, parser and chunked limits, timeouts
HTTP1Parser, HTTP1ParserLimits, HTTP1ParseErrorHTTP1Parser.swift, HTTP1RequestHead.swiftIncremental request-head parser and its limits and errors
HTTP1ConnectionHTTP1Connection.swiftThe connection state machine: keep-alive, body and error decisions. A value type with no I/O
ChunkedDecoder, HTTP1ChunkedEncoderChunkedDecoder.swift, HTTP1Serializer.swiftChunked transfer coding in and out
HTTP1RequestBodyReaderHTTP1RequestBodyReader.swiftReads request bodies on demand while the handler runs
HTTP1Serializer, HTTP1ResponseBodyWriterHTTP1Serializer.swift, HTTP1ResponseBodyWriter.swiftResponse heads and framing; .stream bodies under backpressure
HTTP1TimeoutsHTTP1Timeouts.swiftHeader, idle, body, send and linger limits

HTTP1 depends only on HTTPCore. HTTPServer depends on it, so every server includes it.

When a connection speaks HTTP/1.1

The registry picks a protocol once per connection (ProtocolRegistry.swift):

ConnectionRegistered protocolsResult
Cleartext[HTTP1()] or [TLSNegotiated(HTTP2(), fallback: HTTP1())]HTTP/1.1, with nothing read ahead
Cleartext[HTTP2(), HTTP1()] (h2c on)The first bytes are sniffed: the HTTP/2 preface goes to HTTP/2, anything that starts like a request line (optional empty lines, then a method token and a space) to HTTP/1.1. The sniffed bytes are replayed to the chosen protocol
TLS, ALPN http/1.1anyHTTP/1.1
TLS, ALPN h2TLSNegotiated(HTTP2(), fallback: HTTP1()) or [HTTP2(), HTTP1()]HTTP/2
TLS, no ALPN, or a token no protocol listsanyThe protocol that lists http/1.1, the TLS default

TLSNegotiated(HTTP2(), fallback: HTTP1()) is one protocol that offers h2 and http/1.1 by ALPN and runs HTTP/2 only when TLS negotiated h2; every other connection runs the fallback. With a single protocol registered, cleartext connections are neither sniffed nor wrapped in a replay transport, so the cleartext path costs what HTTP1 alone costs. The shipped executable registers exactly this unless "http2": {"cleartext": true}; with "http2": false it registers [HTTP1] (ServerProtocols.swift). Choosing components shows the Swift side.

When sniffing is on, a client that does not send enough bytes to decide within ServerConfiguration.sniffTimeout (10 s by default) is closed. Sniffing reads at most 64 bytes.

A connection's lifecycle

One connection is one call to HTTP1.run. It owns the receive buffer, the parser, the serializer, the output buffer and the state machine as locals: no actor, no task per request, no lock. Two sibling tasks supervise it for its lifetime: a timer and a waiter on the server's shutdown signal.

          ┌──────── needMoreBytes ───────┐
          ▼                              │
  idle ──bytes──▶ readingHead ──head──▶ handling ──response──▶ writing ──sent──▶ idle
    │                 │                    │                      │
    │            parse error          handler throws          close / tunnel
    ▼                 ▼                    ▼                      ▼
  closing ◀───────────┴────────────────────┘            closing / tunnelled

Parsing the head

The parser scans bytes once, wherever the head is split across receives, and allocates nothing once its field index has grown. It follows RFC 9112 strictly:

The request the handler gets borrows the connection's receive buffer: no copy of the path, query or fields. It is valid only while handle runs; request.snapshot() copies it.

Keep-alive and pipelining

RequestConnection after the response
HTTP/1.1Stays open unless the request's Connection lists close
HTTP/1.0Stays open only if Connection lists keep-alive

Every response says connection: keep-alive or connection: close. The serializer writes that field itself from the state machine's decision; a Connection field set by the handler is not written as is (except on 1xx and .tunnel responses, so 101 keeps the handler's connection: upgrade). A handler that wants the connection closed after its response (after an auth failure, under overload, to rebalance clients) sets connection: close: when that field lists close (any case, any position in the list), the response says connection: close and the connection closes after it, as RFC 9112 §9.6 describes; pipelined requests already read are not answered. Any other Connection value is ignored: a handler cannot keep open a connection the client or the server decided to close. HTTP/2 and HTTP/3 drop the field, which is connection-specific there, and change nothing else.

There is no limit on the number of requests per connection; the idle timeout and maxConnections bound what idle clients can hold.

Pipelining is supported. Per batch the loop does one receive, parses every complete head the buffer holds, calls the handler for each in order, serializes the responses into one output buffer, and sends it with one send. The output is sent early once it holds 64 KiB (flushThreshold), so a long pipeline of large responses does not grow it without bound. Before a request that has a body, earlier responses are sent first. After each batch consumed bytes are discarded, so the receive buffer does not grow across requests.

Request bodies

The handler reads the body through request.body, an AsyncSequence of ByteBuffer chunks. Bytes are received only when the handler asks for them, so a handler that reads slowly applies TCP backpressure to the client, and memory stays at about one receive (16 KiB by default) whatever the body size.

for try await chunk in request.body { /* … */ }        // streamed
let whole = try await request.body.collect(upTo: 1 << 20)   // or collected, with a cap
let length = request.body.knownLength                   // Content-Length, nil for chunked
let trailers = request.body.trailers                    // chunked trailers, after the last chunk
FramingHow it is read
No Content-Length, no Transfer-EncodingNo body
Content-Length: NExactly N bytes; reads never go past the body into the next request. Above maxBodySize: 413 and close before the handler runs
Transfer-Encoding: chunkedDecoded chunk by chunk. Chunk extensions are checked and ignored. Trailers are checked and exposed as request.body.trailers. Over maxBodySize decoded bytes: the read throws, the response is 413 and the connection closes

A body is readable only while the handler runs; a body kept after it returns throws HTTPRequestBodyError.expired. If the handler lets a body error propagate, the connection answers for it: 413 for too large (including collect(upTo:)'s own cap), 400 for a malformed or truncated body. Either way the connection closes. A handler that catches the error and returns its own response gets that response sent with connection: close.

When the handler returns without reading the whole body, the rest is read and discarded so the next request starts at the right byte, up to 64 KiB (maxDrainSize). A longer rest, or a body that failed, makes the response say connection: close instead.

Expect: 100-continue

HTTP/1.1 100 Continue is sent when the handler first reads a body that has not arrived yet. A handler that rejects the request without reading (a 401, say) sends no 100; since the client was never asked to send the body, the response says close. If body bytes already arrived with the head, no 100 is sent. An expectation other than 100-continue gets 417 Expectation Failed and close. HTTP/1.0 requests cannot receive 100, so their 100-continue is ignored.

Responses and framing

The serializer writes the status line, the handler's fields in order (names in lowercase), a date field unless the response has one, the framing, and connection. It owns framing: a handler's transfer-encoding and connection fields are not written, nor is its content-length where the serializer writes its own.

Response bodyFraming
.emptycontent-length: 0
.bytes(b)content-length and the bytes, in the same send as the head
.file(region)content-length; the head is sent, then the file with sendFile (sendfile(2) on the BSD transport) in slices of at most 1 MiB
.stream(length: n, writer:)content-length: n; each write is one send. Writing more or fewer than n bytes aborts the connection
.stream(length: nil, writer:)transfer-encoding: chunked, one chunk per write, then the last chunk. To an HTTP/1.0 request: no framing field and connection: close, and the body ends when the connection closes
.tunnelNo framing field and no connection field; see Upgrades and tunnels

Each .stream write returns only once the transport accepted the bytes, so a writer faster than the client slows down instead of buffering. A writer that throws after the head was sent aborts the connection (the client sees a truncated body, not a second response).

HEAD, 204 and 304

ResponseWhat is sent
To HEADThe headers a GET would get, with the content-length of the content when it is known (.bytes, .file, .stream with a length), and no body. A .stream writer or file is never run
304 Not ModifiedSame as HEAD: headers only
204 No ContentNo content-length, no body
1xxThe handler's fields only: no date, framing or connection

For an .empty or chunked body to HEAD or 304, the handler's own content-length passes through, so it can state the length a GET would have had.

When the connection closes

CauseWhat the client gets
Request asked for close, or HTTP/1.0 without keep-aliveThe response with connection: close, then close
Handler's response lists close in its Connection fieldThat response with connection: close, then close; pipelined requests already read are not answered
Malformed headThe error status (below) with an empty body and connection: close
Handler throws, or returns a field that fails validation (see Request smuggling defences)500 with connection: close. Responses already serialized for earlier pipelined requests go out first
Client closes between requests or mid-headNothing; quiet close
Receive or send failsNothing; the connection ends
Server drainingSee Running on a Mac. A connection between requests (idle, or mid-head) closes at once. A request in flight finishes and its response says close; pipelined requests not yet started get no response

Lingering close. When the connection closes with request bytes possibly unread (an error response, an unread body, dropped pipelined requests, a 408), the server half-closes its output and reads and discards input for up to timeouts.linger (2 s) or 1 MiB (maxLingerSize). Without this, the client's unread bytes would make the kernel reset the connection, and the client could lose the error response.

Error responses for malformed heads, 413 for a Content-Length over the limit, 417, the timeout 408, and the 500 (or 413/400 for a failed body) for a handler that throws are written by HTTP1 itself, without calling the handler chain. They still go through the chain's ProtocolResponseObservers (#0139), which the server finds at startup like connection observers:

A response replaced with 500 because a field would split it (#0136) is the exception: the access log keeps the line it wrote when the handler returned (with the handler's status), and the replacement is reported in the os log.

Limits and timeouts

LimitDefaultSwifthttp-config.jsonWhen exceeded
Request line8 KiB (excluding CRLF)HTTP1ParserLimits.maxRequestLineLength–414 + close
Head size (request line through final CRLF)16 KiBHTTP1ParserLimits.maxHeadBytes–431 + close
Header fields per head100HTTP1ParserLimits.maxHeaderCount–431 + close
Method length24 bytesfixed (HTTPMethod.Token.maximumLength)–501 + close
Request body10 MiBHTTP1Configuration.maxBodySizemaxBodySize (alias maxRequestSize)413 + close
Unread body drained before the next request64 KiBHTTP1Configuration.maxDrainSize–Response says close
Chunk size1 TiBChunkedDecoderLimits.maxChunkSize–400 + close (maxBodySize applies first in practice)
Chunk extensions per chunk1 KiBChunkedDecoderLimits.maxExtensionLength–400 + close
Trailer section8 KiB, 32 fieldsChunkedDecoderLimits.maxTrailerBytes, .maxTrailerFields–400 + close
Bytes per receive16 KiBHTTP1Configuration.receiveSize––
Output sent early during a pipelined batch64 KiBHTTP1Configuration.flushThreshold––
Bytes discarded while lingering1 MiBHTTP1Configuration.maxLingerSize–Close
Header timeout: a head must complete this long after its first byte10 sHTTP1Timeouts.headertimeouts.header408 + close
Idle timeout: no byte of the next head (also before the first request)5 sHTTP1Timeouts.idletimeouts.idle (legacy keepAliveTimeout)Silent close
Body timeout: wait for request body bytes, restarted on progress30 sHTTP1Timeouts.bodytimeouts.body408 + close
Send timeout: one send waiting for the client to accept bytes, restarted per send30 sHTTP1Timeouts.sendtimeouts.sendConnection reset (abort)
Linger after an error response2 sHTTP1Timeouts.lingertimeouts.lingerClose
Connections at once (all protocols)1024ServerConfiguration.maxConnectionsmaxConnectionsNew connections wait in the listen backlog

Notes:

Errors

A malformed head gets one of these statuses, with an empty body and connection: close (HTTP1RequestHead.swift, HTTP1ParseError):

StatusCause
400 Bad RequestInvalid method token; invalid request target (bad byte, wrong form for the method, userinfo in absolute form, CONNECT without host:port); malformed version; bare LF or CR; invalid field name; whitespace before :; obsolete line folding; invalid byte in a field value (NUL, DEL, other controls); no Host on HTTP/1.1; more than one Host; both Transfer-Encoding and Content-Length; Transfer-Encoding whose last coding is not chunked, that repeats chunked, or on HTTP/1.0; invalid Content-Length; Content-Length values that differ
414 URI Too LongRequest line over maxRequestLineLength
431 Request Header Fields Too LargeHead over maxHeadBytes, or more than maxHeaderCount fields
501 Not ImplementedMethod longer than 24 bytes; a transfer coding other than chunked before the final chunked (gzip, chunked)
505 HTTP Version Not SupportedA well-formed HTTP/d.d other than 1.0 and 1.1

After a valid head:

StatusCause
413 Content Too LargeContent-Length over maxBodySize (before the handler runs); a chunked body that grows past it; a handler's collect(upTo:) cap exceeded
400 Bad RequestA malformed chunked body (bad chunk size, missing CRLF, invalid extension or trailer), or a body cut short by the client
408 Request TimeoutHeader or body timeout
417 Expectation FailedExpect with anything other than 100-continue
500 Internal Server ErrorThe handler threw, or returned a .tunnel body with a 1xx status other than 101

Request smuggling defences

Request smuggling relies on two parsers disagreeing about where a body ends. The parser takes the strict reading of RFC 9112 everywhere:

The test-support ReferenceHTTP1Parser implements the same rules independently, and HTTP1DifferentialTests checks that both parsers agree over a corpus of valid and invalid requests split at every byte offset (Tests/HTTP1Tests).

On the response side, every handler response is checked before it is written, in release builds too (HTTPFieldValidation, #0136): each field name must be a token, and no field value or custom reason phrase may hold CR, LF, NUL, another control byte or DEL (a tab is fine). A response that fails is never written as is: it is replaced with a bare 500 with connection: close, like a handler that throws, and an error is logged (os log, subsystem PhotonHTTP, category HTTP). So a handler that copies decoded request bytes into a location cannot split the response, but it gets a 500; validate or encode such values to answer properly. HTTP/2 and HTTP/3 run the same check and answer the stream with a bare 500. Code that serializes responses itself uses HTTP1Serializer.writeValidated, which throws instead of writing; chunked trailers that fail the check are dropped.

Upgrades and tunnels

A handler takes over the connection by returning a .tunnel body. HTTP1 sends the head, then hands the byte stream to the closure, replaying any bytes the client sent after the request. No HTTP/1.1 limit or timeout applies to a tunnel, and a server drain does not interrupt it: the closure gets the shutdown signal in its TunnelContext and should end itself when it fires; a tunnel still running at the drain deadline is cancelled.

HTTPResponse(status: .switchingProtocols, headers: upgradeHeaders, body: .tunnel { transport, context in
    // read and write `transport` until done; watch `context.shutdown`
})

Using HTTP1 in your own server

Register HTTP1 (or TLSNegotiated(HTTP2(), fallback: HTTP1()) when you also serve HTTPS with HTTP/2) with a Server. Pass an HTTP1Configuration to change limits:

import HTTP1
import HTTPCore
import HTTPServer
import NetworkTransport
import Router

let http1 = HTTP1(configuration: HTTP1Configuration(
    parserLimits: HTTP1ParserLimits(maxRequestLineLength: 4 * 1024, maxHeaderCount: 64,
                                    maxHeadBytes: 8 * 1024),
    maxBodySize: 50 * 1024 * 1024,
    timeouts: HTTP1Timeouts(header: .seconds(5), idle: .seconds(15))))   // others keep defaults

let router = try Router {
    POST("/upload") { request, _ in
        var received = 0
        for try await chunk in request.body { received += chunk.readableBytes }   // read on demand
        return HTTPResponse(status: .ok, body: .bytes(ByteBuffer(bytes: Array("\(received)\n".utf8))))
    }
    GET("/count") { _, _ in
        // No length: chunked on HTTP/1.1, close-delimited on HTTP/1.0.
        HTTPResponse(status: .ok, body: .stream(length: nil, writer: { writer in
            for i in 1...3 { try await writer.write(ByteBuffer(bytes: Array("\(i)\n".utf8))) }
        }))
    }
}
let listener = try BSDSocketListener(configuration: .init(host: "127.0.0.1", port: 8080))
let server = Server(configuration: ServerConfiguration(maxConnections: 512),
                    listeners: [listener], protocols: [http1]) {
    router
}
try await server.run()

This type-checks against this checkout. A complete package with signal handling, static files and WebSockets is Examples/custom-server, walked through in Choosing components.

If you load http-config.json with ConfigFile, settings.http1Configuration gives the HTTP1Configuration the executable uses (maxBodySize and timeouts from the file, every other field at its default), so you can start from it and change the rest:

var configuration = settings.http1Configuration
configuration.parserLimits.maxHeadBytes = 32 * 1024
let http1 = HTTP1(configuration: configuration)

Configuring the executable

HTTP/1.1 is always on. Its keys in http-config.json, with their defaults:

{
  "maxBodySize": 10485760,
  "timeouts": {
    "header": 10,
    "idle": 5,
    "body": 30,
    "send": 30,
    "linger": 2
  },
  "maxConnections": 1024
}

For long uploads over slow links raise timeouts.body; for long-polling or server-sent events raise or turn off timeouts.send only if clients stall reading, since the send timeout restarts with every send. Handler time itself is never limited.

Performance

All numbers are from the owner's Apple M4 Pro over loopback, on a shared machine; each record gives its load average and method.

WhatResultRecord
Head parse, 10-header browser requestAbout 130 ns, 0 allocations; minimal request about 27 nsM1.3-parser, M-final-micro
Serialize a 200 with 5 headersAbout 75 ns, 0 allocations; a chunk header about 23 nsM-final-micro
Connection loop per request (parse, handler call, serialize, send bookkeeping)About 250 ns keep-alive, 177 ns with 16 pipelined requests per receive, 0 allocations; the timeout supervision added no measurable costM3.3-timeouts, M3.1-h1conn
Request bodiesEchoing a 1 KiB POST body costs a few percent against a GET; a 4 MiB body keeps memory at one receiveM3.2-bodies
Static files over keep-aliveAbout 93k req/s at 16 connections, 97k at 64compare-servers-final
New connectionsAbout 22k/s on the BSD listenerM3-accept

The performance notes summarize the rest. What matters for HTTP/1.1:

Standards