MoonBit · HTTP family · no I/O
HTTP logic,
no sockets.
Requests and responses, WebSocket frames, cache lifetime, redirect targets: the parts of HTTP that are decided rather than sent, written as functions over bytes. Nothing here opens a connection.
moon add moonbitstack/moonhttp
GET /notes/42 HTTP/1.1 Host: example.com If-None-Match: "v7"
HTTP/1.1 304 Not Modified
ETag: "v7"
What it holds
Six kinds of decision.
Each package is a piece of the specification with the octets taken seriously, and each one is usable on its own.
Framing
Where a frame starts, where it ends, and what the next one is. Chunked bodies, the nine-octet HTTP/2 header, HTTP/3 frames, WebSocket frames.
Messages
A request head, a status line, field lines, a body. Both ends of a connection, driven as octets in and events out.
Compression
HPACK and QPACK with both tables, the Huffman code they share, and the DEFLATE a WebSocket needs: a window kept across messages.
Caching
May this be stored, how long is it fresh, how old is it now, may it answer this request. Four questions, answered from fields and a clock.
Decisions
Which redirect to follow, with which method, which credentials stop at the origin; which alternative service to try next.
Payloads
Forms in both encodings, byte ranges, conditional requests, cookies, event streams. The fields and bodies that arrive with a message.
The wire, drawn
A frame is a byte layout.
Every length on this page is a number two ends had to agree about before the first message could exist.
Reading a request
Bytes in, values out.
No handler signature to satisfy and no runtime to start: three calls, then a decision.
// A request head, as it arrived. let (request, used) = @http1.request(bytes[:]) request.target // "/notes/42" request.field("if-none-match") // Some(b'"v7"'), and used = 66 // Does the copy the client holds still match the body we have? let tag = @conditional.hash(body[:], digest=@sha2.Hasher::new()) @conditional.Ask::read(request.fields) .evaluate(verb="GET", etag=tag) // Go | Fresh | Failed
Packages
Nineteen, each with its section.
Small enough to read, separate enough to depend on one without the other eighteen.