Overview and architecture

On this page

PhotonHTTP is an HTTP/1.1, HTTP/2 and HTTP/3 server for macOS 15 or later and Linux, written in Swift 6 with strict concurrency and no package dependencies. It uses Darwin (POSIX sockets, kqueue, sendfile(2), zlib, libcompression) and Network.framework (TLS, QUIC). On Linux it builds with epoll in place of kqueue, OpenSSL for TLS and ngtcp2 for QUIC, and serves HTTP/1.1, HTTP/2 and HTTP/3 (Running on Linux).

It comes in two forms:

Core and optional components

The design rule (ROADMAP §1.1) is a minimal core plus optional components, each in its own target. A component that is not registered is not in the compiled request path at all.

LayerTargetsRole
CoreHTTPCoreBytes (ByteBuffer), header fields, HTTPRequest/HTTPResponse, the HTTPHandler and HTTPMiddleware protocols, the middleware builder, transport protocols, RequestContext
CoreHTTP1HTTP/1.1 parser, serializer, chunked codec, connection state machine and runner
CoreHTTPServerServer: listeners, protocol registry, connection limit, lifecycle and graceful drain
CoreNetworkTransportListeners and connections: BSD sockets + kqueue, Network.framework TCP, TLS and QUIC
ProtocolsHPACK + HTTP2, QPACK + HTTP3 (both on HeaderCompression), WebSocketsOptional protocols
ComponentsStaticFiles (includes ErrorPages), Compression, VirtualHosts, Auth, AccessLog, Router, ProxyOptional request-path features
GlueConfigFilehttp-config.json → settings → listeners, protocols and middleware chain (uses Foundation, startup only)
Toolsphoton, sws-bench, HTTPTestSupportThe shipped server, the benchmark harness, in-memory test transports

The component table lists dependencies and when to include each one.

Protocols

ProtocolListenerHow a connection gets it
HTTP/1.1cleartext TCP and TLSAlways available. Keep-alive, pipelining, chunked and Content-Length bodies, Expect: 100-continue
HTTP/2TLS (ALPN h2)The TLS listener offers h2 and http/1.1 through ALPN; the client picks. Optional cleartext h2c with prior knowledge ("http2": {"cleartext": true}), which makes every cleartext connection sniff its first bytes. h2c Upgrade and server push are not supported (removed from RFC 9113 / dropped by browsers)
HTTP/3QUIC (UDP)A separate QUIC listener, by default on the TLS port number over UDP. HTTPS responses carry Alt-Svc: h3=":PORT"; ma=SECONDS so browsers discover it; cleartext and HTTP/3 responses do not
WebSocketsanyRFC 6455 Upgrade on HTTP/1.1, extended CONNECT on HTTP/2 (RFC 8441) and HTTP/3 (RFC 9220)

Protocol choice happens once per connection, by ALPN on TLS and QUIC or by the first bytes on cleartext. The handler chain is the same for every protocol, so a component written once serves h1, h2 and h3.

HTTP/2 passes the h2spec conformance suite (146/146) and has limits against the known HTTP/2 attacks (rapid reset, CONTINUATION, control-frame floods, slow reads). HTTP/3 has QPACK (static table by default, dynamic table opt-in), control streams, RFC 9218 priorities and a GOAWAY drain. One known Network.framework limitation: an HTTP/3 application close code never reaches the wire (see notes/nw-transport.md). There is no 0-RTT.

Transports

TransportUsed forType
BSD sockets + kqueuecleartext TCP (default, "transport": "bsd")BSDSocketListener
Network.framework NWListenercleartext TCP when "transport": "nw"TCPListener
Network.framework TLSHTTPS (TLS 1.2+, ALPN)TCPListener(configuration:tls:)
Network.framework QUICHTTP/3QUICListener

Cleartext defaults to BSD sockets because Network.framework connection setup costs about 665 µs and 409 allocations per connection, serialized on one queue; the BSD listener accepts about 22k new connections per second, on par with nginx (decision #0113, M3-accept). TLS and QUIC stay on Network.framework, which supplies TLS 1.3, QUIC, loss recovery and congestion control. Large static files go out with sendfile(2) on the BSD transport.

TLS identities are PKCS#12 files loaded in memory only (SecPKCS12Import with kSecImportToMemoryOnly); the server never reads or writes a keychain.

Concurrency model

Requests, handlers and the chain

public protocol HTTPHandler: Sendable {
    func handle(_ request: HTTPRequest, context: RequestContext) async throws -> HTTPResponse
}
public protocol HTTPMiddleware: Sendable {
    func handle<Next: HTTPHandler>(_ request: HTTPRequest, context: RequestContext,
                                   next: Next) async throws -> HTTPResponse
}

A response body is .empty, .bytes(ByteBuffer), .stream(length:writer:) (written under backpressure), .file(FileRegion) (sent with sendfile) or .tunnel (take over the byte stream: WebSockets and proxied upgrades). An HTTPRequest is valid only while handle runs; snapshot() copies it.

Server takes a @MiddlewareBuilder block. The builder turns the list into one nested generic type at compile time, so the chain is statically dispatched and an if that leaves a component out costs one predictable branch. Order is onion order: the first element sees the request first and the response last; the last element is the handler. Details: MIDDLEWARE.md.

The chain the executable builds

ServerSettings.makeHandler() (target ConfigFile) registers only the enabled components, in this order:

#ComponentConfig keyWhy here
1AltSvchttp3Adds Alt-Svc to HTTPS responses
2AccessLogaccessLogOutermost, so it records final statuses: error pages, auth rejections, proxied responses
3MetricsEndpointaccessLog.metricsAnswers GET /metrics before anything else runs
4CompressioncompressionOutside error pages, so generated pages are compressed too
5ErrorPageserrorPagesFills bodiless 4xx/5xx from everything inside it (auth 401, proxy 502/504, files 404)
6AuthenticatorauthBefore the proxy, so protected paths are checked before anything is forwarded
7ReverseProxyproxyMatches mounts; everything else falls through
8StaticFiles or VirtualHostsroot / hostsThe terminal handler. With hosts, each site has its own StaticFiles and its own hosts[].proxy mounts

A request for /api/items with proxy and auth configured walks: access log → compression → error pages → auth check → proxy (match: forwarded upstream). A request for /style.css walks the same way and falls through the proxy to the static files.

Lifecycle

Server.run() binds every listener in order (cleartext, then TLS, then QUIC), runs until requestShutdown() and returns after a graceful drain: listeners stop accepting, every connection's shutdown signal fires (HTTP/2 and HTTP/3 send GOAWAY, WebSockets close with 1001), and open connections get up to drainTimeout to finish before they are cancelled. The executable maps SIGINT and SIGTERM to that drain. See Running on a Mac.