# HTTP/2 client, frame codec, and HPACK ```bend import bend-kit-http2@0.6.0.2/http2.bend as H2 import bend-kit-http2@0.6.0.2/hpack.bend as Hpack import bend-kit-bytes@0.3.3.3/bytes.bend as Bytes ``` `http2.bend`, `hpack.bend`, and `huffman.bend` import `bend-kit-bytes@0.3.3.3`. `hpack.bend` is a separate file in this package. Use the same package version for both imports. `http2.bend` encodes and parses the nine-octet frame header. It supports all ten frame types in RFC 9113 §6. A `Frame` carries the type, flags, stream ID, and packed payload bytes. The payload keeps padding and fixed fields in wire order. Callers can decode HPACK and other frame content without a `String` conversion. `parse(max, bytes)` returns `Need{input}` for an incomplete frame. It returns `Got{frame, rest}` for a complete frame, or `Bad{error}` for a malformed frame. Keep the `Need` input. Append new bytes before you call `parse` again. `encode(max, frame)` returns `Done{bytes}` or `Fail{error}`. Use `16384` for `max` until the peer advertises another `SETTINGS_MAX_FRAME_SIZE`. `Error` contains the RFC §7 code. It identifies a connection error or a stream error. The parser returns unknown frame types without changes. The client ignores them. The parser ignores reserved flag and stream-ID bits during validation. It clears the reserved stream-ID bit in the parsed frame. The encoder rejects unused flags on standard frame types. The connection checks stream state, header continuation order, flow-control windows, and direction rules. `hpack.bend` encodes and decodes RFC 7541 header blocks over packed `Bytes`. Start with `Hpack.new(4096)` or the current `SETTINGS_HEADER_TABLE_SIZE`. Pass the returned `State` to the next block in that direction. Keep separate encoder and decoder states. `Hpack.encode(fields, huffman, state)` returns `Some{(next, bytes)}`. `Hpack.decode(bytes, state)` returns `Some{(next, fields)}`. Decode returns `None{}` for a malformed block. Encode returns `None{}` for a non-octet string or an invalid field mode. A `Field` has a name, value, and mode. Mode `0` permits dynamic indexing. Mode `1` forbids indexing. Mode `2` keeps the never-indexed representation for sensitive fields. Names and values are byte strings, with one `Char` per octet. They are not decoded UTF-8 text. Call `Hpack.set_limit(max, state)` when the peer changes the encoder's table-size limit. Also call it when you advertise a new decoder limit. The encoder emits the required size updates at the start of its next block. The decoder requires them at the start of the peer's next nonempty block. It rejects updates above the advertised limit. It also rejects invalid indexes, integer overflow, invalid Huffman padding, and EOS. Multiple limit changes emit the smallest limit first. ## Client connection `client.start()` returns a `Client` and the client preface with a SETTINGS frame. That frame disables server push. Send those bytes first. `client.request(client, fields, body)` allocates an odd stream ID. It returns `Advanced{client, writes, events}` or `Failed{error}`. Supply `:method`, `:scheme`, `:authority`, and `:path` as HPACK fields. Send `writes` in order. Keep the returned `client` for the next call. Pass each packed byte chunk to `client.receive(client, chunk)`. It buffers incomplete frames and header blocks, but never accumulates a response body. It returns SETTINGS ACKs, PING replies, and resumed request DATA in `writes`. Send all writes in order, including those returned with response events. Events follow wire order and carry the response stream ID: - `Informational{id, headers}` carries each interim 1xx header block. - `Headers{id, headers}` carries the final response headers as soon as END_HEADERS arrives. It does not wait for END_STREAM. - `Chunk{id, bytes, capacity}` transfers one DATA frame's content, without padding. `capacity` counts the full flow-controlled payload, including the pad-length octet and padding. Empty DATA still emits an empty chunk. The client retains only the outstanding count, not the bytes. - `Trailers{id, headers}` carries the trailing header block, separate from final headers. - `Complete{id}` follows the final chunk or header event when END_STREAM arrives. HEADERS with END_STREAM emits `Headers` then `Complete`. Trailers emit `Trailers` then `Complete`. This separation follows [Rust h2's response head, DATA stream, and trailers](https://docs.rs/h2/latest/h2/struct.RecvStream.html). Each event has an ID because one receive can advance several streams. Buffered callers must collect their own headers and chunks until `Complete`. After you consume or discard a chunk, call `client.release_capacity(client, id, capacity)`. This follows [Rust h2's release_capacity model](https://docs.rs/h2/latest/h2/struct.FlowControl.html#method.release_capacity). You may release part of the capacity or combine consumed chunks from the same stream. Never release capacity for bytes still queued for a consumer. Include padding in the total release, even for a padding-only frame. Keep the returned client and send its writes before the next socket read. Receiving DATA does not emit WINDOW_UPDATE. Outstanding counts reduce both receive windows. Release emits connection credit and, while the stream is active, stream credit. After `Complete`, outstanding capacity remains releasable but emits connection credit only. A zero release emits nothing. Releasing more than the stream's outstanding capacity returns a stream-scoped PROTOCOL_ERROR, so duplicate releases cannot inflate a window. Bound socket reads and event queues as well. `client.cancel(client, id)` cancels an active stream. It emits RST_STREAM with CANCEL code `8`, removes that stream and its queued upload, and returns all outstanding connection credit. It does not return stream credit or revive the stream. Cancellation invalidates the capacity of its queued chunks: discard them without another release. Peer RST_STREAM and streams rejected by GOAWAY follow the same credit rule. Send all returned writes before you return the session to a pool. Cancellation emits no response event. Unknown or already-completed IDs return `Failed` with STREAM_CLOSED. In-flight DATA on a closed stream is discarded and returns connection credit immediately. In-flight header blocks still update HPACK, including a continuation that was pending at cancellation. `Reset`, `Shutdown`, and `Pong` report peer RST_STREAM, GOAWAY, and PING ACK. `client.ping.request(client, payload)` sends an eight-octet PING. Request DATA pauses when either flow-control window is empty. It resumes when the peer increases that window. GOAWAY prevents new requests but lets earlier streams finish. The laws prove the RFC 7541 Appendix C.2 to C.6 examples. They also exercise SETTINGS, fragmented headers, interleaved incremental events, cancellation, a flow-control stall, and GOAWAY. `scripts/check.sh http2` runs deterministic fixtures and a public `nghttp2.org` TLS/ALPN request. The public check needs network access. Frame, HPACK, and native client connection benchmarks are in `bench/`.