# http An HTTP/1.1 and HTTP/2 client, and an HTTP/1.1 server. Bodies are packed `Bytes`. ```bend import bend-kit-http@0.45.0.1/http.bend as Http import bend-kit-http@0.45.0.1/pure.bend as Pure import bend-kit-http@0.45.0.1/completion.bend as Completion ``` `Pure.Body` uses `bend-kit-bytes@0.3.3.2`. JSON values for `post.json` use `bend-kit-json@0.7.3.1`. DNS lookup uses `bend-kit-dns@0.8.0.1`. TLS uses `bend-kit-wire@0.4.8.1`. Compression uses `bend-kit-zlib@0.7.0.3`: `zlib.bend` for pure decoding and `native.bend` for runtime decoding. HTTPS needs OpenSSL 3. On macOS, `brew install openssl@3`. Set `BEND_LIBSSL` if `libssl` is not on the default path. A body over about 30 KB overflows `bend file.bend`. Build with `bend file.bend -o app`. `pure.bend` owns the request and response types, codecs, framing, redirect policy, retries, and cookies. `http.bend` owns client and server IO. Import both for runtime use. Pure clock arguments use `bend-kit-time@0.3.0.4/pure.bend`. `Http.clock.now` and `Http.clock.mono` return those clock types. ## Fetch ```bend import Base import bend-kit-http@0.45.0.1/http.bend as Http import bend-kit-http@0.45.0.1/pure.bend as Pure def show(got: Result<&1, &1, Pure.Err, Pure.Res>) -> IO(Unit): match got: case Done{res}: Pure.Res{status, headers, body} = res IO.print(U32.show(status) ++ " " ++ Pure.header(headers, "content-type")) case Fail{err}: match err: case Pure.ErrUrl{}: IO.print("bad url") case Pure.ErrDns{}: IO.print("dns") case Pure.ErrConnect{code, why}: IO.print("connect " ++ U32.show(code) ++ " " ++ why) case Pure.ErrTls{code, why}: IO.print("tls " ++ U32.show(code) ++ " " ++ why) case Pure.ErrRead{code, why}: IO.print("read " ++ U32.show(code) ++ " " ++ why) case Pure.ErrWrite{code, why}: IO.print("write " ++ U32.show(code) ++ " " ++ why) case Pure.ErrTimeout{}: IO.print("timeout") case Pure.ErrRedirect{}: IO.print("redirect") case Pure.ErrBad{}: IO.print("bad response") def main() -> IO(Unit): do IO: got : Result<&1, &1, Pure.Err, Pure.Res> <- Http.get("https://example.com/") show(got) ``` `Http.get(url)` is `fetch("GET", url, Pure.empty(), Pure.from_string(""))`. `Http.fetch(method, url, headers, body)` is the full call. `fetch.with` adds a per-step timeout in milliseconds. The default is 30 seconds. A redirect chain stops after 20 hops (`ErrRedirect`). `fetch.how` takes `ModeFollow`, `ModeManual`, or `ModeError`. `ETIMEDOUT` is 60 on macOS and 110 on Linux. Both become `ErrTimeout`. HTTPS fetches and response streams offer `h2,http/1.1` through ALPN. An HTTP/2 response uses the same `Res` type. Upload streams negotiate HTTP/1.1 only. HTTP/2 uses `bend-kit-http2@0.6.0.1`. For several addresses, the client tries them in DNS order until TCP connects, within the step timeout. Attempts are sequential. `fetch`, `pool.fetch`, `open`, `open.how`, `upload`, and `upload.how` read `http_proxy`, `https_proxy`, and `no_proxy`, or the uppercase names when the lowercase ones are absent. An HTTP proxy gets an absolute-form target. An HTTPS proxy gets `CONNECT` before TLS to the origin. Certificate and host checks still apply. `fetch.cert` presents a PEM client chain and key. The identity follows same-origin redirects and is dropped after a cross-origin redirect. `post.json(url, v)` POSTs compact JSON with `content-type: application/json`. `resolve(base, ref)` applies RFC 3986 ยง5.2 and returns `None` when `base` is not an absolute `http` or `https` URL. `fetch.retry(n, method, url, headers, body)` allows `n` extra tries of an idempotent method after connect failure, timeout, or 408, 429, 500, 502, 503, or 504. ## Headers and bodies A header map is `Map>`. Names are lowercase after parse. `empty` is an empty map. `set` replaces one name. `add` appends. `header` is the first value, or `""`. `fields` is the list. Repeated `Set-Cookie` lines stay separate. A second `Host` or `Content-Length` is rejected. `from_string` and `to_string` convert a byte string, one `Char` per octet. `length` returns the body and its length. `text` decodes UTF-8 and replaces a bad byte with U+FFFD. `json` parses the body in place. `Url.form` builds an `application/x-www-form-urlencoded` body. `Http.fetch.ca(method, url, headers, body, ca_path)` trusts the PEM certificates in that file for the server handshake, instead of the default verify paths. Verification stays on. The file follows same-origin redirects and is dropped after a cross-origin redirect. `Http.pool.fetch.ca` keeps those sockets apart from default-trust sockets and from a different file. A missing file is `ErrTls` with EINVAL. This does not present a client certificate; use `fetch.cert` for that. `fetch` sends `accept-encoding: gzip, deflate`, plus `br` and `zstd` when those libraries load, unless you set `Accept-Encoding`. It decodes `gzip`, `x-gzip`, `deflate`, `br`, `zstd`, and `identity` in reverse order. An unknown coding stays as sent. A corrupt body, or one that decodes past 16 MiB, is `ErrBad`. `content-length` stays the compressed size. `exchange` does not decode. `decoded` is the pure gzip, deflate, and identity path, and the laws cover it. A response over 16 MiB of body or 64 KiB of head is `ErrBad`. `Content-Length` together with `Transfer-Encoding` is rejected. ## Streams, pools, and cookies `open` follows redirects and returns a `Stream` when the head is in. `stream.res` is the status and headers. `stream.read` is the next decoded piece, or `None` at the end. `stream.close` closes the socket. `open.raw` returns bytes as sent. `open.how(mode, cert, ca, jar, method, url, headers, body, ms)` opens a stream as `fetch` sends a request: `mode` decides what a redirect does, `cert` and the CA file `ca` follow only same-origin hops (`""` trusts the default store, as in `fetch.ca`), and the jar, when it is `Some`, sends and stores cookies on every hop. It returns the jar beside the stream. `open.raw.how` does the same with bytes as sent. `upload` sends a chunked body. `upload.how(cert, ca, method, url, headers, ms)` does the same with a client certificate and a CA file. `upload.write` sends one piece. `upload.finish` ends the body and returns the response as a `Stream`. Uploads do not follow redirects. `pool.fetch` is `fetch` on a pool of idle sockets. It returns the pool beside the result. Pass that pool on. `pool.close` closes the idle sockets. The default cap is 8 idle sockets per origin. `pool.new.with(cap)` sets another. Before it reuses an idle HTTP/1.1 socket, the pool peeks it with `Wire.idle`. A socket that the server closed, or that has bytes waiting, is closed, and the next idle socket or a new one is used, for every method. `pool.take.live` is that take. A server can still close a socket between the peek and the request. Then a reused socket that fails before any response byte is retried once for GET, HEAD, OPTIONS, TRACE, PUT, and DELETE. For an HTTP/2 origin, the pool keeps one session and closes it after GOAWAY. `pool.open(p, method, url, headers, body)` takes an idle connection to the origin or opens a new one with ALPN. It returns the pool beside the stream. `pool.open.with` sets the timeout. `pool.open.how` and `pool.open.raw.how` take the choices of `open.how` and `open.raw.how`. The stream owns its HTTP/2 session until `pool.stream.close(p, st)` returns it, so close one stream before you open the next on that session. Reads return DATA pieces without buffering the whole body. Informational headers and trailers do not replace final response headers. Socket reads are at most 16 KiB, and pending events come only from that read. Closing an unfinished HTTP/2 stream sends `RST_STREAM(CANCEL)` and preserves the session. An HTTP/1.1 stream returns its socket only after a clean body end; early close, decoder failure, `Connection: close`, a close-delimited body, or bytes past the body close that socket. A stale reused HTTP/1.1 socket is retried once for the same methods as `pool.fetch`. HTTP/2 receive credit includes DATA padding. Buffered fetches return credit after accepting each chunk. Streams keep the credit for the returned piece until the next `stream.read` call. Consume or discard that piece before calling `stream.read` again. Closing a pooled stream discards unread chunks and returns their connection credit once. A reset or GOAWAY can already have returned that credit. `fetch.all(reqs, n)` runs each `Fetch{method, url, headers, body}` on at most `n` workers. Each worker owns a pool. Results stay in request order. A failure is a `Fail` in its place. `Jar` is a pure cookie store. You pass the clock: `jar.new(now)` and `jar.at(jar, now)`. `pool.fetch.jar` sends matching cookies and stores each `Set-Cookie`, including on redirects. The public-suffix check rejects only a single-label `Domain` such as `com`. It does not ship the Public Suffix List, so `Domain=co.uk` is accepted. ## Serve A convenience handler returns `IO(Reply)`. `Reply` is the `Res` plus a completion receipt. `Completion.Unobserved{}` means you do not need a callback. `Completion.ignore` opts out of the owner observer. ```bend import Base import bend-kit-http@0.45.0.1/http.bend as Http import bend-kit-http@0.45.0.1/pure.bend as Pure import bend-kit-http@0.45.0.1/completion.bend as Completion def hello(req: Pure.Req) -> IO(Http.Reply): Pure.Req{method, path, headers, body} = req IO.pure(Http.Reply, Http.Reply{Pure.Res{200, Pure.empty(), Pure.from_string(path)}, Completion.Unobserved{}}) def main() -> IO(Unit): Http.serve.on(~hello, ~Completion.ignore, "127.0.0.1", 18080) ``` `serve.on` binds an address. `serve` binds `0.0.0.0`. Neither prints a start line. `serve.with` and `serve.on.with` set the body cap. The default is 16 MiB. Header cap on these entry points is 64 KiB. Phase deadlines are 5, 30, 30, and 30 seconds for header, body, idle, and write. A bad request is 400, an oversized body is 413, and a head over 64 KiB is 431. HTTP/1.1 connections stay open unless `Connection: close`. Pipelined requests run in order. HEAD, 1xx, 204, and 304 get no body. `serve.stream.on.with` takes `start`, `piece`, `finish`, and `abort`. `abort` runs on an incomplete upload and must close any handle it owns. It does not call `finish`. `serve.write.on.with` takes `start`, `write`, and `dispose`. `dispose` runs when the body is suppressed or the header write fails. `Some{n}` sends `Content-Length`. `None` sends chunked encoding. A length mismatch closes the socket and does not replay the body. `server.write.run(~C, ~S, ~start, ~write, ~dispose, ~observe, server)` uses a `Server`. `C` is reusable server context; `S` is affine response state. The start handler has type `C -> Pure.Req -> IO(WriteHead)`. The observer has type `C -> U32 -> Completion.Outcome -> IO(Unit)`. `serve.write.config` takes the same templates, then the context and configuration. The `on.with` and `with` conveniences use `Unit` context. ## Configured server `server.config(host, port)` is the same caps as above, plus 128 connections, 128 active requests, and 128 buffered-input units. Every size and deadline is a positive `U32`. Zero is an error, not an unlimited setting. A deadline must fit in signed host milliseconds, at most 2147483647. The largest header or body cap that fits is 4294901756. Port 0 asks the OS for a port. `server.start` returns your affine owner beside `Done{server}` or a `StartupError`. `InvalidConfig` and `BindError` both return the owner, so you can close it. `Done` means the listener is accepting. Do not treat a failure as ready. Use port `0` to let the OS select a free port. `(server, port) = Http.server.port(~Context, server)` returns the actual port beside the same server. The listener stays open from startup through serving. Both native and JS startup use `Wire.listen`. `0.31.0.0` adds `fetch.ca` and `pool.fetch.ca`. `Hops` and `Route` gain a `ca` field. A caller that builds those records must pass `""` when it wants the default trust store. `http` imports `bend-kit-wire@0.4.8.1`. `server.run` consumes the server and returns `ServerExit` after the listener is closed and the accepted work has returned. `server.close` does that close and returns the counts. It waits until handlers and cleanup callbacks return. It can wait forever. `server.stop` refuses new admission and returns `True` the first time. Stop is not drain. You still run or close the server. A callback that does not return is not cancelled. An external supervisor has to enforce a process deadline. `server.observer` returns a copyable `ServerStats` beside the server, so you can read counts while `run` owns the listener. `stats` returns `None` after the accounting actor has closed. That `None` is not a drain receipt. `HostAccepted` means the host accepted the writes. It does not mean the peer received them. `WriteFailed` keeps the host error. `IncompleteResponse` is a known-length writer that sent the wrong number of bytes. Failure closes the socket and does not send a replacement response. A buffered unit is one connection's retained input: incomplete headers, the decoded body, and at most one 64 KiB pipelined suffix. During framing the connection retains at most `8*T` logical octets, where `T` is header bytes + body bytes + 65536 + 1. That is not an RSS figure. Application stream state is extra. Idle sockets keep their reservation, so `buffered` below `connections` lowers how many sockets are admitted. Rejected connections are closed without a read buffer. A parsed request that exceeds active or buffered capacity gets `503` and `Connection: close`, and the handler is not called. The drain of a rejected request stops after 32 MiB or about 2.5 seconds. `0.31.1.0` keeps each count in a [`resources`](../resources) pool. A connection reserves one connection unit and one buffered unit together, and a refused buffer returns the connection unit. Every release splits one unit off the grant that its takes joined, so a release with no take stops the server with `Admission.give: no lease of this kind is held` instead of wrapping a counter. Deadlines do not cancel a handler. Affine erasure does not close an application file or socket. The `abort` and `dispose` callbacks do that, and they must return. `python3 -B http/drain_check.py`, `http/outcome_check.py`, `http/admission_check.py`, and `http/startup_check.py` are the socket checks for this contract. The laws do not cover the effects. ## Acceptance checks Run `scripts/check.sh http` for types, proofs, and local socket checks. `bend http/PROOF.bend --check-only` must report `ALL PROOFS CHECK`. This entry imports no foreign or unsafe code. The runtime check still reports its foreign effects. Run `scripts/native-check.sh http` to compile and run the native package acceptance program. CI also runs `python3 -B http/startup_check.py` for native and JS startup sockets with the pinned Bend and Bun. Run `bash http/local-smoke.sh` for native and JavaScript serving, keep-alive, rejection, streamed request, and response writer checks. The smoke demos bind port 0 and print `READY ` from the live listener. Clients use that port; ports 18080 through 18082 need not be free. CI runs both lanes sequentially on Linux and macOS arm64. Each build and streaming client run has a 20 GiB process-tree RSS ceiling. Run `sh http/mtls-check.sh` for native acceptance or append `js` for JavaScript. The TLS harness builds local, mTLS, and HTTP/2 pool checks as separate programs. It checks certificate isolation, redirects, uploads, idle reconnects, stream reuse, and cancellation. Run `sh http/ca-check.sh` for private CA acceptance or append `js` for JavaScript. CI runs both TLS harnesses in native and JS lanes on Linux and macOS arm64 when HTTP changes. Required TLS jobs set `WIRE_LOOPBACK_ONLY=1` and reuse Wire's client-only loopback runner and compiled public-IP denial probe in both runtimes. Only compiled clients enter the macOS sandbox or Linux IPv4/IPv6 firewall boundary. Builds, fixture servers, and RSS supervision stay outside it. The probe must receive a policy error; a timeout or connection refusal fails acceptance.