The HTTP/2 wire protocol for Koja. A connection is a value. Its owner gives it the bytes that arrived, and it gives back events and the bytes to send.
HTTP2 opens no socket, starts no process, and reads no clock. A server or client package owns those and drives the connection.
ServerConnectionandClientConnectionspeak RFC 9113 over bytes that the owner moves- Requests and responses are the
HTTP.RequestandHTTP.Responseof the standard library - HPACK header compression (RFC 7541) with the static table, the dynamic table, and Huffman coding
HPACK.EncoderandHPACK.Decoderalso work with no connection- Flow control in both directions, so a body larger than a window waits and then goes out
- Stream multiplexing with the stream limit of each end
- Graceful shutdown with GOAWAY, PING, and RST_STREAM
Collectorbuilds whole messages from events and caps the size of each body- Limits on rapid resets, queued replies, and CONTINUATION growth close an abusive connection
Add the package to your koja.toml.
[dependencies]
http2 = { github = "koja-lang/http2", tag = "v0.1.0" }A server makes one ServerConnection for each socket. It gives each
chunk of bytes to read, acts on the events, and writes what
take_output returns.
This session collects each request and answers it.
alias HTTP.Request
alias HTTP.Response
alias HTTP.Status
alias HTTP2.Collector
alias HTTP2.ServerConnection
alias HTTP2.Settings
alias HTTP2.StreamID
struct Session
collector: Collector<Request>
conn: ServerConnection
fn new -> Session
Session{
collector: Collector.new(1048576),
conn: ServerConnection.new(Settings{}),
}
end
# Takes the bytes that arrived on the socket. Returns the bytes to
# write to it.
fn read(self, bytes: Binary) -> (Session, Binary)
(conn, events) = self.conn.read(bytes)
session = self
session.conn = conn
for event in events
session = session.handle(event)
end
(conn, output) = session.conn.take_output()
session.conn = conn
(session, output)
end
priv fn answer(self, stream: StreamID, response: Response) -> Session
self.collector = self.collector.discard(stream)
self.conn = self.conn.respond(stream, response) rescue _ -> self.conn
self
end
priv fn append(self, stream: StreamID, data: Binary) -> Session
match self.collector.append(stream, data)
Result.Ok(collector) ->
self.collector = collector
self
Result.Err(_) -> self.answer(stream, plain(413))
end
end
priv fn finish(self, stream: StreamID) -> Session
(collector, request) = self.collector.finish(stream)
self.collector = collector
match request
Option.Some(request) -> self.answer(stream, serve(request))
Option.None -> self
end
end
priv fn handle(self, event: ServerConnection.Event) -> Session
match event
ServerConnection.Event.Data{data: data, stream: stream} ->
self.append(stream, data)
ServerConnection.Event.End(stream) ->
self.finish(stream)
ServerConnection.Event.Request{head: head, stream: stream} ->
self.start(stream, head)
ServerConnection.Event.Reset{stream: stream} ->
self.collector = self.collector.discard(stream)
self
ServerConnection.Event.UnsupportedMethod{stream: stream} ->
self.answer(stream, plain(501))
_ ->
self
end
end
priv fn start(self, stream: StreamID, head: Request) -> Session
match self.collector.start(stream, head)
Result.Ok(collector) ->
self.collector = collector
self
Result.Err(_) -> self.answer(stream, plain(413))
end
end
end
fn plain(code: Int) -> Response
status = Status.new(code)
Response.new(status, status.reason())
end
fn serve(request: Request) -> Response
Response.new(Status.ok(), "hello from " <> request.uri.path)
end
A server that serves HTTP/1.1 and HTTP/2 on one port compares the
first bytes of a socket with HTTP2.PREFACE. It then gives those
bytes to read, which reads the preface itself.
A client makes one ClientConnection for each socket. request
queues a whole request and returns its stream. The stream names the
request in later events.
alias HTTP.Headers
alias HTTP.Method
alias HTTP.Request
alias HTTP2.ClientConnection
alias HTTP2.Settings
conn = ClientConnection.new(Settings{})
request = Request{
body: <<>>,
headers: Headers.new(),
method: Method.Get,
uri: URI.parse("https://example.com/hello").unwrap(),
}
(conn, stream) = try conn.request(request)
(conn, output) = conn.take_output()
# Write `output` to the socket, then read `bytes` from it.
(conn, events) = conn.read(bytes)
for event in events
match event
ClientConnection.Event.Response{head: head} ->
IO.puts("status #{head.status.code}")
ClientConnection.Event.Data{data: data} ->
IO.puts("#{data.byte_size()} bytes")
ClientConnection.Event.End(_) ->
IO.puts("done")
_ ->
()
end
end
A connection compresses headers with HPACK.Encoder and
HPACK.Decoder. A program that reads or writes header blocks itself
can use them with no connection.
alias HTTP2.HPACK
encoder = HPACK.Encoder{}
fields = [HPACK.Field{name: <<":method">>, value: <<"GET">>}]
(encoder, block) = encoder.encode(fields)
decoder = HPACK.Decoder{}
(decoder, decoded) = try decoder.decode(block, 16384)
decoded is Option.Some(fields). It is Option.None when the
header list is larger than the limit, which is 16384 bytes here.
read does not fail. It reports everything as events.
| Event | Carries | End | Meaning |
|---|---|---|---|
Closed |
an ErrorCode |
Both | The connection ended. Write the output, close the socket. |
Data |
data, stream |
Both | Body bytes of the message on a stream. |
End |
a StreamID |
Both | The message on a stream is complete. |
Pong |
Both | The peer answered a ping. |
|
Request |
head, stream |
Server | The head of a request. The body follows as Data events. |
Reset |
code, stream |
Both | A stream ended early. No more events come for it. |
Response |
head, stream |
Client | The head of a response. The body follows as Data events. |
UnsupportedMethod |
method, stream |
Server | A request whose method is not in HTTP.Method. |
An event that carries more than one value has named fields, as in
Data{data: data, stream: stream}. After UnsupportedMethod the
stream stays open for the answer, and no more events come for it.
- A connection is a value. Every function returns the next connection, so a test drives a connection with plain bytes and no socket.
- The owner moves the bytes.
readtakes what arrived andtake_outputreturns what to send. The owner decides when to write, which gives it back pressure. - The owner keeps time.
pingqueues a PING frame andPongreports the answer. The owner decides how long to wait. - Messages go out whole.
requestandrespondtake a complete message. The connection cuts the body into DATA frames and holds the bytes that the windows of the peer do not cover. - Messages arrive in parts. A head arrives first, then
Dataevents, thenEnd. An owner that wants the whole message uses aCollector, and an owner that streams reads the events itself. - The connection returns flow control credit itself. It sends WINDOW_UPDATE for the bytes of each
read, so the owner reads from its socket only as fast as it handles events.
Settings is what one end announces to its peer. Every field has a
default, so a call site names only what it changes.
| Field | Default | Meaning |
|---|---|---|
header_table_size |
4096 | The HPACK table that the peer compresses with. |
initial_window_size |
65535 | The flow control window of each new stream, in bytes. |
max_concurrent_streams |
100 | The streams that the peer has open at one time. |
max_frame_size |
16384 | The payload of one frame that the peer sends. |
max_header_list_size |
16384 | The headers of one message that the peer sends. |
- Trailers. The connection reads them and drops them.
- Responses with a 1xx status. A client connection drops them.
- A body that the owner sends in parts.
requestandrespondtake the whole body. - Server push. A connection turns it off in its settings.
- Stream priority. The connection reads the priority fields and ignores them.
- The upgrade from HTTP/1.1 (
h2c). A connection starts with the HTTP/2 preface.
koja testCopyright (c) 2026 Henry Popp
This project is MIT licensed. See the LICENSE for details.