moonhttp

Figures · ten plates

The wire,
drawn to scale.

A protocol is a shape before it is a rule. Each plate below is one of the shapes this library has to get right: a head, a chunk, a frame, a stream, and the arithmetic a cache does over a clock.

1 cell = 8 bits/RFC 9112 · 9113 · 9114 · 6455 · 7692 · 9220 · 9111
FIG. 01

An HTTP/1.1 message

RFC 9112 §2
request, 66 octets of headCRLF ends every line
GET /notes/42 HTTP/1.1request-line: method SP target SP version
Host: example.comfield-line: name ":" value
If-None-Match: "v7"a field may be repeated, never folded
\r\n\r\nthe empty line that ends the head
{"seq":1}body, framed by Content-Length or Transfer-Encoding

What to notice

The head has no length field. It ends at the first empty line, which is why a reader scans for four octets rather than jumping to an offset — and why the octet count it returns matters: everything after it is body, or the next message on a kept-alive connection.

Fields are bytes. Deciding that "v7" is a quoted string is the next layer's problem, and a decision that can fail.

FIG. 02

Chunked body

RFC 9112 §7.1
Transfer-Encoding: chunkedsize in hex
1achunk size in hex — 26 octets
{"seq":1,"ok":true,"n":7}that many octets, then CRLF
0last chunk: a size of zero
trailer section, empty here

What to notice

The size is hex and counts the data, not the CRLF that follows it. A chunk extension may sit after the size behind a semicolon, and a reader that treats it as part of the number reads the wrong body.

The last chunk carries zero octets and is followed by the trailers — fields that belong to the message but arrive after its body.

FIG. 03

HTTP/2 frame header

RFC 9113 §4.1
nine octets, every framebig-endian
LENGTH24 bits
TYPE8
FLAGS8
R + STREAM ID1 + 31
012345678
Length counts the payload only, never the header.

What to notice

Every frame starts with the same nine octets, so a reader can skip a type it does not know by trusting the length — which is the point of putting the length first.

Flags mean different things per type: the same bit is END_STREAM on a DATA frame and END_HEADERS on a HEADERS one. The type and the flag are read together or not at all. The reserved bit is not spare: a receiver that sees it set treats the frame as a connection error.

FIG. 04

A variable-length integer

RFC 9000 §16
HTTP/3 and QUIC count in thisthe prefix is the width
PrefixWidthValueWhere it is used
001 octet6 bits · 0–63a small frame type or stream type
012 octets14 bits · 0–16 383most lengths
104 octets30 bits · 0–2³⁰−1a body that does not fit in 16 KiB
118 octets62 bits · 0–2⁶²−1the connection's stream count

What to notice

The first two bits say how many bits the value has, and the value is written in as few octets as its own size allows. The encoding is self-describing, so a reader never needs the field's type to parse it.

QUIC and HTTP/3 use the same integer, which is why it lives in moonvar rather than here.

FIG. 05

A WebSocket frame

RFC 6455 §5.2
client to server, two octets of payload1 cell = 1 bit
0x00
FIN1
RSV1
RSV2
RSV3
OPCODE4
0x01
MASK1
LENGTH7
MASKING KEY32 bits — the client's four octets
PAYLOAD2 octets, each XORed with the key
The same message on the wire: 81 82 01 02 03 04 69 6b — 81 is FIN and the text opcode, 82 is the mask bit and a length of two, and hi arrives as 69 6b.

What to notice

The mask is not encryption and is not meant to be: it exists so that a client cannot be tricked into sending octets a proxy would read as a request. That is also why a server must not mask, and why a client that does not mask is refused.

RSV1 belongs to the extension, which is the next plate.

FIG. 06

The stream permessage-deflate shares

RFC 7692 §7.2
one stream, three messagestail removed by the sender
message 1
f2 48 cd c9 c9 07 00
00 00 ff ff
message 2
2a 4e 4c 4a 2e 01 00
00 00 ff ff
message 3
a message that compressed to nothing
00 00 ff ff

One DEFLATE stream, stopped and resumed at each message boundary. The receiver puts the four octets back before decompressing, which is what makes the pieces one stream again.

What to notice

The window is not reset between messages unless the terms say server_no_context_takeover or client_no_context_takeover. A match in message 2 may reach into message 1, which is where most of the ratio comes from.

That is also why a message cannot be decompressed on its own: the stream, not the message, is the unit the compressor works in.

FIG. 07

How old is a stored response

RFC 9111 §4.2.3
four clock readings, one agedrag to change
0s
0s
1s
30s
60s
initial age
1s
current age
31s
left
29s
fresh — may be served

apparent = max(0, response_time − date)
corrected = age + (response_time − request_time)
current = max(apparent, corrected) + (now − response_time)

What to notice

The largest of the two ages wins: what the origin said, and what the trip added. That max is why a response dated in the future is not newborn — the delay still counts.

Freshness is age ≤ lifetime, not <. A response is stale once its age is greater than the lifetime, so the second it turns exactly is still fresh; move the sliders to the boundary and watch the verdict change.

FIG. 08

What a redirect does to the method

RFC 9110 §15.4
five statuses, two behavioursmethod and body together
StatusFromToBodyWhy
301POSTGETdroppedmoved permanently; the original said so
302POSTGETdroppedfound; what browsers have always done
303PUTGETdroppedsee other; the answer is elsewhere
307POSTPOSTkepttemporary; the method must not change
308PUTPUTkeptpermanent, and the same prohibition

What to notice

303 is the only one that rewrites for every method: it exists to turn a submission into a fetch. 307 and 308 exist for the opposite reason, and a client whose body cannot be replayed must refuse them rather than send an empty one.

Credentials are a separate decision from the method: Authorization and Cookie are dropped when the origin changes, compared by scheme, host and the effective port.

FIG. 09

An alternative service

RFC 7838 §3
Alt-Svc, as a server writes itone member of a list
h3=":443"; ma=3600protocol-id = alt-authority ; parameters
h3an ALPN name, percent-encoded when it has to be
":443"an empty host means the origin's own; the port is never implied
ma=3600seconds this stays fresh — 86400 when absent
persist=1optional: keep this across a change of network

What to notice

An alternative is authoritative for the origin, not for itself: a connection to it still presents the origin's name in TLS and its Host. That is what makes it a redirection rather than a different site.

clear as the whole value forgets every alternative the origin ever advertised. A member this reader cannot parse is dropped rather than failing the field: the others are still what the origin said.

FIG. 10

What opens a WebSocket over HTTP/3

RFC 9220 §3
one request, five pseudo-headersa QPACK field section
Pseudo-headerValueWhy
:methodCONNECTthe method an extended CONNECT borrows
:protocolwebsocketRFC 8441's one name for a WebSocket
:schemehttpsrequired, unlike a classic CONNECT
:authorityexample.compresent and not empty
:path/chatpresent and not empty
SETTINGS_ENABLE_CONNECT_PROTOCOL (0x08) must already be 1 before a client sends :protocol. A server answers 2xx to take the upgrade, and 501 for a protocol it does not implement.

What to notice

A classic CONNECT is the opposite shape: only :authority, and neither :scheme nor :path. The extended set without :protocol, or :protocol on anything but CONNECT, is a malformed request rather than an upgrade.

After the 2xx the stream stops being an HTTP message: the WebSocket frames of FIG. 05 ride its DATA frames, and a stream FIN is the orderly close a dropped TCP connection would have carried.

FIG. 11

What a connection error is called

RFC 9114 §8.1 · RFC 9204 §6
every fault is the connection'sthe number is what the peer is told
CodeNumberRaised when
H3_NO_ERROR0x100the connection ends and nothing went wrong
H3_GENERAL_PROTOCOL_ERROR0x101a fault no other code names
H3_INTERNAL_ERROR0x102this endpoint hit a fault of its own
H3_STREAM_CREATION_ERROR0x103a second control, encoder or decoder stream; a push stream at a server
H3_CLOSED_CRITICAL_STREAM0x104the control, encoder or decoder stream ended
H3_FRAME_UNEXPECTED0x105a frame on a stream that does not carry it, or in an order it may not take
H3_FRAME_ERROR0x106a frame that could not be read: a length that does not add up
H3_EXCESSIVE_LOAD0x107a frame over the size this endpoint advertised
H3_ID_ERROR0x108a push id never promised, or used twice
H3_SETTINGS_ERROR0x109a setting HTTP/2 defined, or the same identifier twice
H3_MISSING_SETTINGS0x10aa control stream that opened with something other than SETTINGS
H3_REQUEST_REJECTED0x10ba request this endpoint will not serve
H3_REQUEST_CANCELLED0x10ca request the client abandoned
H3_REQUEST_INCOMPLETE0x10da request stream that ended before the request did
H3_MESSAGE_ERROR0x10ea malformed request: a repeated, absent or misplaced pseudo-header, a connection-specific field
H3_CONNECT_ERROR0x10fthe CONNECT method or its extended form misused
H3_VERSION_FALLBACK0x110a version this endpoint does not serve
QPACK_DECOMPRESSION_FAILED0x200a field section the table cannot resolve: a static index past the table, a dynamic entry that is not there
QPACK_ENCODER_STREAM_ERROR0x201a table capacity over the one advertised, or an instruction that names no entry
QPACK_DECODER_STREAM_ERROR0x202an Insert Count Increment of 0
@http3.Code names all twenty and Code::code is the number. Refused::code is the code a refusal closes the connection with, and it is what Refused::to_string prints in front of the reason.

What to notice

None of these is a stream error. A peer that gets one of them wrong has a different idea of what the streams mean than this endpoint does, and resetting one stream leaves the two ends still disagreeing, so §4.1.2 and §7.2 close the connection instead.

The greeting a server sends on its control stream is inside all of this: SETTINGS first, once, and nothing a request stream carries. That is what Control::feed enforces before the connection is worth using.

Each plate is drawn from the section named in its header. The calculator runs RFC 9111 §4.2.3 with the same comparison the library uses.