Skip to content

Design HTTP 2

Tuntii edited this page Dec 3, 2026 · 2 revisions

Design: HTTP/2

Status: h2c (1.8) and h2 over TLS (1.9) done · Date: 2026-10-24 · User docs: HTTP/2

Why

HTTP/2 is what browsers speak to edge servers and what many proxies and service meshes speak to the applications behind them. viltrum serves HTTP/1.1 only, so behind an h2 proxy every request is translated, and HttpArena's HTTP/2 board (four scored profiles) has no viltrum entry at all.

Scope

  • h2c (cleartext, RFC 9113 3.3): prior knowledge only. A cleartext listener with HTTP/2 enabled tells the connection preface from an HTTP/1.1 request line and serves both on the same port, as nginx and h2o do.
  • h2 over TLS, chosen by ALPN on both handshake paths (mbedtls, and our own TLS 1.3 handshake).
  • The same router, middleware chain and Ctx as HTTP/1.1: a handler does not know which protocol it answers.

Not planned: server push (browsers dropped it), RFC 7540 priorities (deprecated by RFC 9113; we serve streams in arrival order), the Upgrade: h2c dance from HTTP/1.1 (also deprecated), WebSocket over HTTP/2 (RFC 8441), trailers.

How it fits

Piece Where What
HPACK http2/hpack.v, http2/huffman.v header blocks to fields and back; the dynamic tables
Frames http2/ frame headers, SETTINGS, the error codes
Connection engine/ the preface, SETTINGS exchange, streams, flow control, GOAWAY
Bridge http1.Ctx requests and responses in and out of the handler chain

A request reaches the handler chain the way an HTTP/1.1 one does. Once its fields pass HTTP/2's own checks (pseudo-fields, lowercase token names, no connection-specific fields, clean values), they are written out as an HTTP/1.1 head in a worker buffer (:method :path HTTP/1.1, :authority as Host, cookies joined) and that head goes through http1.parse_head, as a request read off an HTTP/1.1 connection would. Every rule the HTTP/1.1 parser holds a request to holds here, with no second copy of them to keep in step. The body is the stream's DATA, collected until END_STREAM.

Ctx is not told the protocol. It writes its HTTP/1.1 response into a worker buffer, and the engine turns that into frames: the status and headers into an HPACK block (lowercase, the connection-specific ones dropped) in a HEADERS frame (CONTINUATION past 16 KiB), the body into DATA within the stream's and the connection's send windows. What a window holds back waits on the stream until WINDOW_UPDATE. Files stream from disk a frame at a time as the windows and the socket allow. Rewriting our own, known-well-formed head costs a pass over about a hundred bytes per response, and in exchange every Ctx feature (gzip, files, cookies, middleware that sets headers) works over HTTP/2 unchanged.

Streams are served in arrival order, each as soon as its request is complete; a stream only takes a slot while its body is coming, its response is held back by flow control, or its response is deferred.

A deferred response (Ctx.after) parks its stream: the slot keeps the callback, its argument and the method, and the worker's timer heap gets an entry naming the stream as well as the connection (fd and generation, as for HTTP/1.1). The stream's send window keeps moving with WINDOW_UPDATE and SETTINGS while it waits. When the timer is due the slot is released and the callback writes the response, which goes out like any other (and may be deferred again). A timer whose stream is gone (reset, or the connection closed or reused) finds no waiting slot and is dropped. Waiting slots count toward the stream limit, so a connection holds at most 128. The idle timeout pauses while a connection has nothing but waiting streams; the read and write timeouts do not.

WebSocket over HTTP/2 (RFC 8441) is not implemented: an upgrade answers 501.

HPACK

Decoder decodes a block into a caller-sized buffer: indexed and literal fields, Huffman-coded strings, dynamic table size updates (only at the start of a block, and never past our limit). A list past the size or field limit is refused, but the block is still decoded to the end, so the table stays in step with the peer's and the connection can answer the stream and go on. A malformed block is a COMPRESSION_ERROR.

Encoder writes indexed fields where either table holds the whole field, else a literal with the name indexed where it can be. The caller says whether a field enters the tables (.incremental), not (.without, for values that change on every response such as ETag and Last-Modified), or never (.never, for credentials). It writes strings raw: Huffman coding is optional for an encoder, and the fields we repeat are indexed after their first response anyway.

Both tables are a fixed ring of 4096 bytes (the default SETTINGS_HEADER_TABLE_SIZE; we never use more) with the entries' positions beside it, so neither side allocates per field. Huffman decoding goes four bits at a time through a 256-by-16 table built from the RFC's code when the module loads.

Tested against every example of RFC 7541 Appendix C, decoding (with and without Huffman, with evictions) and encoding (byte for byte), plus the error cases, a round trip of random lists with the two tables compared after each block, and mutated blocks (300k under ASAN and UBSAN during development, 500k in CI).

Limits

HTTP/2 lets one connection ask for a great deal of work. The engine holds each of these (values in HTTP/2):

  • the decoded header list (against Limits.max_header_bytes): 431, and the block is still decoded to keep the table in step. One byte can name a 4 KB table entry, so decoding to the end has a price: fields past the limit that the tables hold are counted and not copied, and a list four times past the limit (or 64 KiB past it) ends the connection;
  • concurrent streams (128, announced): REFUSED_STREAM;
  • a header block's size and its CONTINUATION frames (the 2024 CONTINUATION flood): the connection ends, since a block cannot be decoded in part;
  • request bodies: a handler gets its body whole, so bodies are held until their stream ends. The connection window is given back as DATA is read, and memory is bounded per stream instead: each stream's window starts at 64 KiB and reopens as its body is read, but while the bodies held on the connection pass max_body_bytes, only the oldest stream's window reopens. The oldest stream can always finish, so nothing deadlocks (giving the connection window back only as requests finished did: ten 900 KB uploads at once shared out 1 MiB and none could end), and a connection holds at most about twice max_body_bytes (the budget, and the oldest stream growing past it) plus 64 KiB a stream;
  • responses held back by the client's windows: past max_pending of bodies or 32 files, new streams are refused; the write timeout runs from when held output last moved, so frames that move nothing (PING, unknown types) do not keep a starved connection open. h2_pump sends a frame per stream in turn, so one large answer does not hold up the others;
  • streams we reset: the frames the client sent before it saw the reset (the rest of a body, trailers) are dropped rather than taken as errors;
  • control frames (PING, SETTINGS, PRIORITY, RST_STREAM, empty DATA, unknown types) between answered requests: past 1000 the connection ends with ENHANCE_YOUR_CALM. This also bounds the 2023 "rapid reset" pattern, though each request here is served as it arrives, so a reset never cancels work in progress;
  • output a peer never reads: a stream waiting on its window holds its body (or its file), and the connection times out like any other.

Over TLS

A TLS listener with http2 set offers h2 before http/1.1 in ALPN. mbedtls takes the first of the server's list the client also offers; our TLS 1.3 handshake (hs13) does the same when it parses the ClientHello, and writes the choice into EncryptedExtensions. When the handshake ends with h2, the connection is HTTP/2 from its first byte: our SETTINGS go out at once, and the client's preface must follow (anything else is a connection error). From there the frames go through the TLS record layer like any other bytes.

Conformance

h2spec 2.6.0 over TLS: 145 of 146 pass, one skipped, on both handshake paths (a gcc build with hs13, and a tcc build on mbedtls). Cleartext: 144 of 146 pass, one is skipped. The failure is 3.5/2, "invalid connection preface": the test sends INVALID CONNECTION PREFACE, which a listener that also serves HTTP/1.1 reads as an HTTP/1.1 request and answers 400 (RFC 9113 3.3 allows the dual listener). A preface that starts with its request line PRI * HTTP/2.0 and then goes wrong is an HTTP/2 client, and gets GOAWAY(PROTOCOL_ERROR). The engine's tests (engine/h2_test.v) cover the frames, flow control, limits and errors, the zero-allocation gate, and mutated frames.

Clone this wiki locally