nish/net/http2-cleartext
HTTP/2 over plain TCP with prior knowledge (h2c, RFC 9113 §3.3): a pool of Http2Connections, one per accepted socket, for a program that owns its readiness loop. The client starts with the 24-byte connection preface; there is no Upgrade: h2c (RFC 9113 §3.1 deprecates it) and no fallback to HTTP/1.1 on the same port.
import {
H2C_DONE,
H2C_POOL_FULL,
H2C_WANT_READ,
H2C_WANT_WRITE,
Http2CleartextServer,
} from "nish/net/http2-cleartext";
import { pollAdd, pollModify, tcpListen } from "nish:net";
import { H2_ERROR, H2_NEED_MORE, H2_REQUEST, Http2Config, Http2Connection } from "nish/net/http2";
import { H2C_DONE, Http2CleartextServer } from "nish/net/http2-cleartext";
// `loop` is the program's, from `pollCreate`.
const LISTENER: i32 = 64; // past every slot's token
const start = (loop: i32): Http2CleartextServer => {
const listener: i32 = tcpListen("127.0.0.1", 8080, 128);
pollAdd(loop, listener, 1, LISTENER);
return new Http2CleartextServer(new Http2Config(), listener, 64, 10000);
};
// When the listener's token comes back:
const admit = (loop: i32, server: Http2CleartextServer): void => {
const slot: i32 = server.accept();
if (slot >= 0) { pollAdd(loop, server.fd(slot), 1, slot); }
};
// When a slot's token comes back with `events`:
const serve = (loop: i32, server: Http2CleartextServer, slot: i32, events: i32): void => {
let wants: i32 = (events & 5) !== 0 ? server.readable(slot) : server.writable(slot);
const none: u8[][] = [];
let event: i32 = server.next(slot);
while (event !== H2_NEED_MORE && event !== H2_ERROR) {
const conn: Http2Connection = server.connection(slot);
if (event === H2_REQUEST) { conn.respond(conn.stream, 200, none, none, true); }
event = server.next(slot);
}
wants = server.flush(slot);
if ((wants & H2C_DONE) !== 0) { server.close(slot); }
else { pollModify(loop, server.fd(slot), wants & 3, slot); }
};
// Every so often, with `monotonicNanos()`:
// server.expire(monotonicNanos());
No confidentiality, no integrity
Anyone on the path reads and edits
the traffic. Serve h2c only upstream of a TLS-terminating proxy, on
loopback or a private network (docs/security/http2-cleartext.md).
It is nish/net/http2-tls's loop with a TCP socket in place of TLS, the
same method names and the same interest bits (read 1, write 2, done 16),
plus expire(now) for the preface deadline.
The preface is the carrier's
Http2Connection checks the preface
itself, but on a mismatch it queues SETTINGS and a GOAWAY, and a port
scanner or a confused HTTP/1.1 client should get nothing back. So each slot
reads its first 24 bytes into a 24-byte array of its own and compares each,
as it comes, with PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n: at the first byte
that differs the socket is closed, with no byte sent. A whole preface is
fed to the connection, which checks it again, and from then on the socket
is read straight into the connection's input, as http2-tls.ts reads TLS
into it. Until the preface is whole the slot borrows no Http2Pools
buffer and sends nothing, so a peer that stalls there holds 24 bytes and a
slot until expire closes it, prefaceTimeout milliseconds after accept.
Relies on `@internal` members
The carrier is the second server loop in
std/net and uses Http2Connection.inputTail, received and endInput
exactly as http2-tls.ts does. It keeps its own ended flag for the end of
input it passed to endInput, rather than reading the @internal
inputEnded.
Slots, a cap; buffers, borrowed. As Http2TlsServer: slot 0's
connection is made by the constructor, so a bad Http2Config panics there
with its own words; every other slot's by the first accept into it, and
kept. Each connection borrows its frame buffers from the shared pools
and gives them back once empty and when the slot closes. Every final
response carries the pool's date, formatted at most once a second.
Written from RFC 9113 §3, not ported from another implementation.
Classes
Http2CleartextServer
A pool of HTTP/2 connections over plain TCP, with prior knowledge, on one
listening socket. connection(slot) is the HTTP/2 side of each slot.
class Http2CleartextServer
| Member | |
|---|---|
new Http2CleartextServer(config: Http2Config, listener: i32, size: i32, prefaceTimeout: i32) |
A pool of size slots serving HTTP/2 under config on listener. Only slot 0's connection is made here, so a config it refuses panics now, with its words; every other slot's is made by the first accept into it and kept. The slots' preface arrays, 24 bytes each, are made here too. |
connections: Http2Connection[] |
The connections made so far, slot k's at k: slot 0's, made by the constructor, and one for each slot accepted into since. |
date: HttpDate |
The date every slot's responses carry, formatted once a second for all of them: each slot's Http2Connection.date is its bytes, refreshed by connection and next. date.pin fixes its clock. |
readonly pools: Http2Pools |
The frame buffers every slot's connection borrows: one of each per slot at most, none made up front. |
accept(): i32 |
Accepts the next waiting connection into a free slot and gives it a fresh HTTP/2 connection, which sends nothing until the peer's preface is whole: the slot's own, restarted, or one made now if this is the first accept into it. A slot closed before is taken first, then the next never used. |
busy(): i32 |
How many slots hold a connection. |
close(slot: i32): void |
Ends slot's connection and frees the slot at once: the socket is closed with nothing more sent, and the connection gives back every buffer it holds. |
connection(slot: i32): Http2Connection |
The HTTP/2 connection of slot, its date brought up to now. A slot never accepted into, or out of range, names slot 0's, which may be live. |
expire(now: i64): i32 |
Closes every slot whose preface is not whole prefaceTimeout milliseconds after its accept, at now. A slot past its preface has no deadline here: a program times idle connections itself. |
fd(slot: i32): i32 |
The descriptor of slot, for pollAdd. |
flush(slot: i32): i32 |
Writes what slot's connection holds to the socket, as far as the socket takes it, and answers the interest. Nothing is written while the preface is not whole. A socket that fails ends the connection. |
holds(slot: i32): boolean |
Whether slot holds a connection. |
next(slot: i32): i32 |
Reads slot's socket into its connection, as far as the connection has room, and answers the connection's next event. A slot whose preface is not whole reads that first and answers H2_NEED_MORE while it waits. |
prefaced(slot: i32): boolean |
Whether slot has read its whole connection preface, so its connection is running. |
readable(slot: i32): i32 |
The socket of slot is readable: while the preface is not whole, reads it (and closes the slot at a byte that differs); then sends what HTTP/2 has to send. Once the preface is whole, next reads the socket. |
rest(slot: i32): void |
Gives back the buffers slot's connection holds and has emptied, so an idle slot holds none. |
size(): i32 |
How many slots the pool has, made or not. |
writable(slot: i32): i32 |
The socket of slot is writable: what HTTP/2 holds goes out. |
Constants
16
H2C_DONE: i32
The interest bit for "the slot is over": the program calls close(slot).
-105
H2C_POOL_FULL: i32
What accept answers when every slot is busy; the connection it took is closed.
1
H2C_WANT_READ: i32
The interest bit for "read the socket": pollModify's read event.
2
H2C_WANT_WRITE: i32
The interest bit for "write the socket": pollModify's write event.

