nish/net/http1-server
The server side of HTTP/1.1 (RFC 9112): a sans-IO connection that drives nish/net/http1-codec's parser, and two pools of them for a program that owns its readiness loop, one on nish:net's plain TCP and one on nish/net/tls-tcp with ALPN http/1.1.
import {
H1_AGAIN,
H1_ALPN,
H1_BODY,
H1_CLOSED,
H1_DONE,
H1_END,
H1_ERROR,
H1_INVALID,
H1_NEED_MORE,
H1_POOL_FULL,
H1_REQUEST,
H1_WANT_READ,
H1_WANT_WRITE,
H1_WRITE,
H1_WS_CLOSE,
H1_WS_MESSAGE,
Http1Config,
Http1Connection,
Http1OutputPool,
Http1Server,
Http1TlsServer,
HttpDate,
} from "nish/net/http1-server";
const listener = tcpListen("0.0.0.0", 8080, 128);
const server = new Http1Server(new Http1Config(), listener, 64);
// when the listener is readable:
const slot: i32 = server.accept();
if (slot >= 0) { pollAdd(loop, server.fd(slot), 1, slot); }
// when a connection's token comes back:
let wants: i32 = (events & 5) !== 0 ? server.readable(slot) : server.writable(slot);
let event: i32 = server.next(slot);
while (event !== H1_NEED_MORE && event !== H1_ERROR) {
const conn: Http1Connection = server.connection(slot);
// H1_REQUEST: conn.parser.methodIs("GET"), conn.parser.targetIs("/"), ...
// conn.respond(200, names, values, HTTP1_CHUNKED)
// H1_BODY: conn.data[conn.dataStart .. conn.dataStart + conn.dataLength)
// H1_END: conn.write(body, 0, n); conn.end()
event = server.next(slot);
}
wants = server.flush(slot);
if ((wants & H1_DONE) !== 0) { server.close(slot); }
else if (server.rearm(slot, wants)) { pollModify(loop, server.fd(slot), wants & 3, slot); }
Http1Config
The caps of every connection made from it, fixed when the connection is made.
Http1Connection
One connection's HTTP/1.1 server, sans-IO: bytes in through feed, events out of next, and the response the program writes in output[outputStart .. outputEnd) for its carrier to send.
Http1OutputPool
Output buffers shared by the connections of one pool, each size bytes.
Http1Server
A pool of HTTP/1.1 connections over plain TCP on one listening socket.
Http1TlsServer
A pool of HTTP/1.1 connections over TLS 1.3 on one listening socket: one Http1Connection beside each slot of a TlsTcpServer, run the way nish/net/http2-tls runs HTTP/2.
HttpDate
The Date field an origin server with a clock sends on every final response (RFC 9110 §6.6.1): the IMF-fixdate of now, in 29 bytes the object owns and rewrites in place, at most once a second.
Events
next() answers H1_REQUEST (a request's head: the parser's
spans and ...Is methods read it), H1_BODY (some of its body, de-chunked,
a window that stays valid until the next next()), H1_END (the request
is complete), H1_WRITE (a write that was held back may be tried again),
H1_WS_MESSAGE and H1_WS_CLOSE once the slot has switched to WebSocket,
and H1_ERROR, final: the request broke a rule, the response that says so
is queued, and the connection closes once it is sent. H1_NEED_MORE asks
for more input, for the output to drain, or for the program to finish the
response it owes.
One exchange at a time
A request is answered before the next one on
the connection is read: respond, any number of writes, end. A
response with a length is over once its last byte is written, and one with
no body (Content-Length: 0, HEAD, 204, 304) once its head is, so end is
optional for both; a chunked body, and an unframed one to HTTP/1.0, has no
length to count and is over only at end, without which the connection
waits and the next request is never answered. The program may answer at
once (before the body has arrived) or after H1_END; either way the
request's body is read to its end before the next request starts, which is
what keeps pipelined requests in order (§9.3.2).
Pipelined bytes wait in the parser's buffer, and once it holds
inputLimit() bytes the connection stops asking for input, so the client's
TCP window closes: back-pressure on the way in.
Streaming both ways
A request body arrives a chunk at a time, at most
chunkSize bytes in each H1_BODY, whether it was sent with
Content-Length or chunked. A response is written with a length or chunked,
a write at a time into the output buffer; a write takes what fits and
answers how much, and when it could not take everything the connection
answers H1_NEED_MORE until the socket has drained half the output, then
H1_WRITE. Until then it reads nothing, so the window the program is writing
from stays valid: back-pressure on the way out.
Nothing buffers a whole body. On an Http1Server slot sendBody sends a
body with a length from the program's own array, the queued output with it,
and copies only what the socket did not take, under the same rules.
What the server decides
The framing field (Content-Length or
Transfer-Encoding: chunked) and the Connection field are the server's;
the program's fields may not hold either, nor Upgrade, Keep-Alive or
Content-Length. A request that did not ask to keep the connection open
gets Connection: close and the connection closes after it; an HTTP/1.0
request that asked for keep-alive gets Connection: keep-alive; a chunked
response to an HTTP/1.0 request is sent unframed and ends with the
connection, since chunked is HTTP/1.1's. A response to HEAD, and a 204 or
304, carries no body whatever the program writes (RFC 9110 §9.3.2, §15.3.5,
§15.4.5). A 1xx before the final response (100 Continue) is allowed; 101 is
acceptWebSocket's.
The Date
Every final response, an error included, carries a Date
field (RFC 9110 §6.6.1) from HttpDate, which formats the IMF-fixdate at
most once a second; a 1xx carries none. The pool's slots share one, so a
busy server formats a second once, not once a slot. A program that sets its
own date field sends that one instead, so a response has one Date.
Http1Server reads the clock once per serve call — from readable or
writable to the flush that ends it — and every response written in
between carries that reading; one written outside a serve call, an answer
given later, reads the clock then. date.pin(unixMs) fixes it, for a test
that compares bytes.
Refusals. Every refusal of nish/net/http1-codec's parser — the smuggling
shapes, a Content-Length with a Transfer-Encoding, two that disagree,
a malformed chunk size — is answered with its status (400, 413, 414, 431,
501, 505), Content-Length: 0 and Connection: close, and ends the
connection; so does a malformed WebSocket frame, with its close code.
docs/security/http1-codec.md is the record.
WebSocket
A request that asked to upgrade reaches the program like any
other; acceptWebSocket(protocol) answers it with the 101 of
nish/net/websocket when it is a valid opening handshake (RFC 6455 §4.2),
and once its exchange is over the slot reads frames with a WsDecoder
instead of requests, the bytes that followed the request included. A ping
is answered with a pong and a close with a close, by the connection.
Caps are start-up numbers, all in Http1Config: the request target, the
header section and its field count, a request body, the chunk handed over,
the output buffer, a WebSocket message, the idle timeout and the deadline for
a whole request head (headTimeout, against slowloris); the slots are the
pool's size.
Http1Server builds a slot's connection on the first accept into it and
keeps it, so a pool sized for its peak costs at start-up only what does not
depend on the size, and holds at most size connections once the peak has
come; Http1TlsServer does the same for a slot's connection and, through
TlsTcpServer, its TLS state. Every buffer is allocated with its slot or
grown to its cap on first use and kept, so a warmed slot serves request after
request with Arena.used() flat. Accepting a WebSocket computes the accept
key and writes the 101 into storage the slot keeps, built on its first
accept, so later handshakes leave the arena flat too.
Output is borrowed
The pools' slots share an Http1OutputPool: a
connection takes an outputSize buffer when it first needs room to write
and gives it back once its carrier has sent everything, so the output
memory a pool holds follows the connections the socket is holding back,
not its size. A pool that ever had n connections writing at once keeps
n buffers and allocates no more.
Private helpers share the importing program's flat symbol namespace
(docs/wp26-stdlib.md §3e), which is why each one carries the module's name.
Written from RFC 9112, RFC 9110 and RFC 6455, not ported from another implementation.
Classes
Http1Config
The caps of every connection made from it, fixed when the connection is made. Each is a number the program sets once, at start-up.
class Http1Config
| Member | |
|---|---|
new Http1Config() |
|
chunkSize: i32 |
The most body bytes one H1_BODY hands over, and how much is read from the socket at once. |
headTimeout: i32 |
Milliseconds a request head may take, from its first byte to its blank line, before expire closes the slot however steadily its bytes come. The idle timeout alone restarts on every byte, so a client that sends one byte of its head each idleTimeout would hold its slot for good (slowloris). |
idleInput: boolean |
Lend a slot's input and head buffers back to the server between exchanges: once a response is sent and no byte of the next request has come, the slot gives both to pools the server's slots share, and takes them again with the next byte. The memory they hold then follows the connections mid-request rather than every keep-alive connection open, for two pool operations a request. On by default; false keeps each slot's buffers across the rest, about 1.9 KB more an idle connection. |
idleTimeout: i32 |
Milliseconds without a byte read or written after which expire closes a slot. |
lingerBytes: i32 |
The most bytes a refused slot drains before it is closed whatever is left (RFC 9112 §9.6); 0 closes at once, without a lingering close. |
lingerTimeout: i32 |
Milliseconds Http1Server keeps reading, and throwing away, what a refused client still sends after its error response, before expire closes the slot (RFC 9112 §9.6). The server shuts its write side and drains rather than closing at once: a close with unread bytes resets the connection, and the reset can destroy the response before the client reads it. |
maxBody: i32 |
The longest request body, declared or de-chunked; past it, 413. |
maxHeaderBytes: i32 |
The longest header section, and trailer section; past it, 431. |
maxHeaders: i32 |
The most fields in a header section, trailers counted; past it, 431. |
maxMessage: i32 |
The longest WebSocket message, its fragments together; past it, close 1009. At most 2^30 − 14, so a whole frame fits the decoder's input. |
maxTarget: i32 |
The longest request target; past it, 414. With maxHeaderBytes, at most 2^30 − 44, the most a head buffer holds. |
outputSize: i32 |
The output buffer, in bytes: the most of a response waiting for the socket. |
Http1Connection
One connection's HTTP/1.1 server, sans-IO: bytes in through feed, events
out of next, and the response the program writes in
output[outputStart .. outputEnd) for its carrier to send.
class Http1Connection
| Member | |
|---|---|
new Http1Connection(config: Http1Config, date: HttpDate | null = null) |
A connection under config. A cap outside what the connection can work with panics. |
bufferLimit: i32 |
The most input the connection buffers, unread: the cap inputRoom holds it to. |
closeAfter: boolean |
The connection closes once this exchange is over. |
data: u8[] |
H1_BODY's and H1_WS_MESSAGE's bytes. |
dataLength: i32 |
How many bytes H1_BODY or H1_WS_MESSAGE handed over, from data[dataStart]. |
dataStart: i32 |
Where the bytes H1_BODY or H1_WS_MESSAGE handed over start in data. |
date: HttpDate |
The Date every final response carries: the constructor's, or one the connection makes when given none. Http1Server and Http1TlsServer give theirs, so that every slot of a pool shares one, formats at most once a second and makes no Date of its own. A program may put another, to share it or to pin its clock. |
fd: i32 |
The plain TCP socket the carrier sends output on, which sendBody writes to directly: Http1Server.accept sets it, and restart and abort put it back to -1. -1 for a connection with no such socket (a sans-IO carrier, Http1TlsServer's TLS records), where sendBody copies as write does. |
heads: Http1OutputPool | null |
Where the parser's head buffer goes while the slot rests, beside inputs; null otherwise. |
headSince: i64 |
When the head being read began to arrive, in monotonicNanos; meaningful while headTimed. |
headTimed: boolean |
A head has begun to arrive and is not read whole yet: headSince is its deadline's start. |
inputs: Http1OutputPool | null |
Where the parser's input and head buffers go while the slot rests, with Http1Config.idleInput; null otherwise. |
lastActive: i64 |
When the carrier last moved a byte for the connection, in monotonicNanos. |
lingerSince: i64 |
When the lingering close began, in monotonicNanos; meaningful while lingering. |
mss: i32 |
The TCP maximum segment size the program sets for this connection, for respondEncoded under HttpCompressor.segmentMode; 0, the default, leaves it to the compressor and the socket (segmentMss). Each accept puts it back to 0. |
noOutput: u8[] |
The empty array output is while the connection holds no buffer. |
opcode: i32 |
H1_WS_MESSAGE's opcode: text or binary. |
output: u8[] |
Bytes to send are output[outputStart .. outputEnd). Empty until the connection first needs room to write: then its own buffer, made once, or one borrowed from pool and given back by release once sent. |
outputEnd: i32 |
Where the unsent bytes end in output, one past the last; equal to outputStart when nothing waits. |
outputStart: i32 |
Where the unsent bytes start in output; a carrier sends from here and calls consume with what it sent. |
parser: Http1Parser |
The request parser: its head (methodIs, targetIs, header, spans) is the current request's from its H1_REQUEST until its response is done, H1_END included, however much of a pipelined request is fed behind it. |
pong: u8[] |
A ping's payload, kept until its pong fits: empty until the first ping. |
pool: Http1OutputPool | null |
Where output is borrowed from, for a connection of a pool; null for one that keeps its own. |
responseEmpty: boolean |
The response carries no body (HEAD, 204, 304): what is written is dropped. |
socketMss: i32 |
The MSS Http1Server.accept read from this connection's socket (netMss), or 0 when it reports none. |
status: i32 |
H1_ERROR's HTTP status or WebSocket close code, or H1_WS_CLOSE's close code. |
writeHeld: boolean |
A write was held back; next answers nothing but H1_WRITE until there is room. |
ws: WsDecoder | null |
The frame decoder: made when the slot first switches to WebSocket, so a slot that only ever speaks HTTP never holds its buffers, then kept for the slot's later peers. |
abort(): void |
The socket failed: nothing more is sent or read, and the connection is done. |
acceptEncoding(): i32 |
The content coding to answer the current request with, read from its Accept-Encoding with gzip offered (httpAcceptEncoding): HTTP_CODING_GZIP, HTTP_CODING_IDENTITY (also with no such field), or HTTP_CODING_NONE when the client refuses identity and does not take gzip. Allocates nothing. Read at H1_REQUEST, H1_BODY or H1_END alike: the head stays the request's until its response is done. |
acceptWebSocket(protocol: string): i32 |
Accepts the current request's WebSocket opening handshake (RFC 6455 §4.2.2) with protocol, empty for none: the 101 is queued, and once the request has ended the slot reads frames. Answers 0; H1_INVALID when the request is not a valid opening handshake (the program answers 400, or 426 for the wrong version) or protocol is not a token; H1_AGAIN when the output has no room yet; H1_CLOSED with no request to answer. |
answerable(): boolean |
Whether the program may answer the current request now. |
betweenRequests(): boolean |
Whether the connection is between requests, so that a server draining gracefully may close it without cutting a request short: it speaks HTTP, not upgraded or closing, no request is open, no next head has begun to arrive (its headTimeout clock has not started), and every byte of the last response is sent. |
closeWebSocket(code: i32): i32 |
Starts the closing handshake (§5.5.1, §7.1.2) with code: a close frame, then the connection is done once it is sent. Answers 0, H1_INVALID for a code that may not be sent, or H1_CLOSED when the slot is not an open WebSocket. |
consume(n: i32): void |
Marks the first n bytes of the output sent. |
end(): i32 |
Ends the response: the last chunk of a chunked body. Answers 0; H1_INVALID for a response with a length that still owes bytes; H1_AGAIN when the last chunk does not fit yet; H1_CLOSED with no response under way.Required for a chunked body, and for an unframed one (a chunked response to HTTP/1.0), which only end can say is over: without it the connection waits and the next request is never read. Optional for a response with no body or a Content-Length written in full, which the connection finishes itself when it moves to the next request; called before then it answers 0, and after then H1_CLOSED. |
ended(): boolean |
Whether the peer's byte stream has ended (endInput): a request still held on the connection is one its client hung up on, or half-closed. |
endInput(): void |
The peer's byte stream has ended: once what it sent is answered, the connection is done. |
feed(buf: u8[], off: i32, len: i32): i32 |
Takes as much of buf[off .. off + len) as inputRoom allows and answers how much; the rest is the caller's to feed again later. A window outside buf panics. |
headArrived(now: i64): void |
Starts the head deadline at now (monotonicNanos) when bytes of a request head wait to be read and its clock has not started. The carriers call it after each feed; a program that feeds a connection itself calls it the same way. Bytes that wait while the previous exchange is still open start no clock: they count from when it is over (nextRequest). |
identityRefused(): boolean |
Whether the current request's Accept-Encoding refuses the body as it is (httpIdentityRefused); respondEncoded then gzips any body it can. |
inputLimit(): i32 |
The most input the connection holds unread: what inputRoom counts against. |
inputRoom(): i32 |
How many bytes feed would take now: none while a write is held back or once the connection is closing. |
isDone(): boolean |
Whether the connection is over and everything is sent: the carrier closes it. |
isWebSocket(): boolean |
Whether the slot has switched to WebSocket and is open. |
lingering(): boolean |
Whether the connection is in its lingering close: its refusal is sent and its carrier drains what the peer still sends. |
next(): i32 |
The next event; see the module comment. |
park(): void |
Gives the parser's input and head buffers to inputs and heads while the connection rests between exchanges: speaking HTTP, no request open, nothing fed and unread. Does nothing without the pools or otherwise. |
release(): void |
Gives output back to the pool once nothing in it is waiting: no bytes to send, no write held back for room. The carrier calls it after each send; a connection that keeps its own buffer keeps it. |
respond(status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 |
Answers the current request with status and the fields names[k]: values[k], and the framing bodyLength asks for: a length, or HTTP1_CHUNKED, or HTTP1_NO_BODY for a 1xx, a 204 or a 304. A 1xx (but 101) is interim and the final status follows it. Answers 0; H1_INVALID for a status outside 100 to 599 or 101, a reserved field, a name or value http1WriteResponseHead refuses, or a head larger than the output; H1_AGAIN when the output has no room for it yet (H1_WRITE says when to try again); H1_CLOSED with no request to answer.A response with no body (a bodyLength of 0, a HEAD, a 204 or a 304) is finished once this returns 0, and one with a length once write or sendBody has taken all of it: end is optional for both. A chunked body is finished only by end. |
respondEncoded(status: i32, names: u8[][], values: u8[][], body: u8[], off: i32, len: i32, enc: HttpCompressor, coding: i32): i32 |
Answers the current request with all of body[off .. off + len) in one call: respond, write and end, the body compressed by enc when coding (from acceptEncoding) is gzip and enc.compress takes it, with Content-Encoding: gzip and the compressed Content-Length, and otherwise sent as it is; Vary: Accept-Encoding while enc.vary is set and the choice turned on the request (HttpCompressor.varies).Under enc.segmentMode the compressed response goes out only when, head included, it takes fewer segments of segmentMss(enc) bytes than the body as it is, and a response that fits one segment as it is goes so without compressing (enc.oneSegment); enc.decision says what was chosen. A request that refuses identity (identityRefused) is sent gzip whatever the length, the saving or the segments (enc.compressAnyway). The program's fields may not name Content-Encoding or, with enc.vary, Vary.The whole response is written or none of it: H1_AGAIN when the output has no room for it yet (H1_WRITE says when to try again, compressing again), H1_INVALID for a response larger than the output could ever hold or one respond refuses, H1_CLOSED with no request to answer; 0 once it is queued. A window outside body panics. |
restart(): void |
Puts the connection back as the constructor left it, for the next peer. It allocates nothing. |
segmentMss(enc: HttpCompressor): i32 |
The segment respondEncoded counts in under enc.segmentMode: this connection's mss when the program set it, else enc.mss when the program set it (above 0), else the socket's (socketMss), else HTTP_DEFAULT_MSS. |
sendBody(buf: readonly u8[], off: i32, len: i32): i32 |
write of buf[off .. off + len), sent from buf itself where it can be: under a Content-Length response on a connection whose carrier named its socket (fd, set by Http1Server.accept) and with no write held back, the queued output and the body leave in one netWrite2, and only what the socket did not take is copied into the output, the way write copies it (what does not fit is answered short, and H1_WRITE says when to send the rest).Once it returns the connection holds no reference to buf, so the caller may reuse or give back its bytes at once. A chunked or empty response, a write held back (H1_AGAIN or a short answer not yet followed by H1_WRITE), and a connection with no socket (Http1TlsServer, a sans-IO carrier) copy all of it: it is write, with the same bytes on the wire. Answers what write answers, H1_INVALID for more than the response still owes; H1_CLOSED also when the socket failed during the send, which aborts the connection. A window outside buf panics. As with write, a Content-Length body sent in full needs no end. |
sendFrame(fin: boolean, opcode: i32, buf: u8[], off: i32, len: i32): i32 |
Sends one WebSocket frame, unmasked as a server's are (§5.1): a whole text or binary message, a fragment of one (fin false, then continuations), a ping or a pong. Answers 0; H1_INVALID for a frame websocketWriteFrame refuses, a close (that is closeWebSocket), or one larger than the output; H1_AGAIN when it does not fit yet; H1_CLOSED when the slot is not an open WebSocket. A window outside buf panics. |
unpark(): void |
Takes back the buffers park gave away, before bytes are fed. A parked buffer is noOutput itself, which a pointer compare finds without loading the empty array's header from a line the request touches nowhere else; any other empty buffer is still swapped for a lent one. |
wantsWrite(): boolean |
Whether the output holds bytes to send. |
write(buf: readonly u8[], off: i32, len: i32): i32 |
Writes buf[off .. off + len) of the response body, as much as the output has room for, and answers how much: a chunk of a chunked body, or bytes of one with a length (more than it still owes is H1_INVALID). When not everything was taken, H1_WRITE says when to try the rest; H1_AGAIN when nothing was. A response with no body takes everything and sends nothing. H1_CLOSED with no response under way. A window outside buf panics. Once the bytes taken reach the Content-Length, the response is finished whether or not end follows; a chunked body still needs end. |
Http1OutputPool
Output buffers shared by the connections of one pool, each size bytes. A
connection borrows one when it has something to write and gives it back
once its carrier has sent everything, so a pool's output memory follows the
connections the socket is holding back, not the slots: Go's net/http
pools its writers the same way, and uWebSockets shares one per loop. A
buffer is made only when none is free, so the pool grows to the most
connections ever writing at once and then allocates nothing.
class Http1OutputPool
| Member | |
|---|---|
new Http1OutputPool(size: i32) |
An empty pool: no buffer is made until the first take. |
free: u8[][] |
The buffers given back and not yet taken again; made - free.length is how many are out. |
made: i32 |
How many buffers the pool has made. |
Http1Server
A pool of HTTP/1.1 connections over plain TCP on one listening socket.
slots are the indices accept answers; a slot is the program's from
accept until it calls close, and its descriptor is fd(slot).
class Http1Server
| Member | |
|---|---|
new Http1Server(config: Http1Config, listener: i32, size: i32) |
A pool of size slots, at least one, serving config on listener. No slot is built here: a slot's connection and its buffers are allocated by the first accept into it and kept, so a pool sized for a peak costs at start-up only the read scratch, the shared Date and the output pool, and at most size connections once the peak has come. |
connections: Http1Connection[] |
The connections built so far, slot k's at k: one per slot ever accepted into, and slot 0's once connection has asked for it. |
date: HttpDate |
The Date every slot's responses carry, formatted once a second for all of them and read from the clock once per serve call: readable and writable hold a reading and flush lets it go. date.pin fixes its clock. |
expireScans: i32 |
How many calls of expire walked every slot, for a program measuring its cost. |
fds: i32[] |
Each slot's descriptor, or -1 while it is free, for the slots accepted into so far: its length is the high-water mark, and a slot past it has never been used. |
free: i32[] |
The slots closed since they were used, a stack whose top accept takes before it builds a slot past the high-water mark: finding one costs the same with every slot free as with all but one busy, where a scan of fds walked the busy ones. |
heads: Http1OutputPool |
The head buffers resting slots lend, beside inputs; made counts how many the slots ever needed. |
inputs: Http1OutputPool |
The input and head buffers resting slots lend, with Http1Config.idleInput. |
outputs: Http1OutputPool |
The output buffers the slots borrow while they write. |
accept(): i32 |
Accepts the next waiting connection into a free slot and answers the slot, which the program adds to its loop with H1_WANT_READ; -11 when none is waiting, H1_POOL_FULL when every slot is busy (the connection is closed), or tcpAccept's own failure. |
busy(): i32 |
How many slots hold a connection. |
close(slot: i32): void |
Closes slot's socket, whatever is left unsent, and frees the slot. |
connection(slot: i32): Http1Connection |
The connection of slot. A slot never accepted into, or out of range, names slot 0's connection, which this builds when no slot has been yet (the first accept then takes it, restarted). |
expire(now: i64): i32 |
Closes every slot that has been idle past idleTimeout, or reading one head for longer than headTimeout, or lingering past lingerTimeout, at now (monotonicNanos); answers how many. |
fd(slot: i32): i32 |
The descriptor of slot, for pollAdd; -1 for a free or unknown one. |
flush(slot: i32): i32 |
Sends what slot's connection holds until the socket would block, and answers the interest: H1_DONE once the connection is over and sent, otherwise read while it has room and write while it holds bytes or a write is held back (its H1_WRITE comes from next once the socket is writable, even when this send emptied the output). It ends the serve call, so a response written after it reads the clock anew. Once a refusal (H1_ERROR) is sent the slot lingers: its write side is shut and what the client still sends is drained, answering H1_WANT_READ until the client's end of stream, lingerBytes, or expire past lingerTimeout; next answers H1_NEED_MORE meanwhile. |
holds(slot: i32): boolean |
Whether slot is a slot of this pool that holds a connection. |
next(slot: i32): i32 |
slot's connection's next event; H1_ERROR for a free slot. While a write is held back the output is sent first, so that the room it waits for is made here and H1_WRITE comes in the same pass; otherwise what the program writes in answer to one event and the next goes out together at flush, as one segment rather than several. |
readable(slot: i32): i32 |
The socket of slot is readable: reads it while the connection has room and the socket has more, then sends. Answers the interest. It begins a serve call, which the program's flush of the slot ends: the responses written in between carry one clock reading as their Date. |
rearm(slot: i32, wants: i32): boolean |
Whether the program must pollModify slot's descriptor to wants, the interest readable, writable or flush answered, and records it as armed when it must. A keep-alive connection answers the same interest request after request, and each pollModify is a system call (epoll_ctl) that changes nothing. |
size(): i32 |
How many slots the pool has, built or not. |
writable(slot: i32): i32 |
The socket of slot is writable: sends, and begins a serve call as readable does. Answers the interest. |
Http1TlsServer
A pool of HTTP/1.1 connections over TLS 1.3 on one listening socket: one
Http1Connection beside each slot of a TlsTcpServer, run the way
nish/net/http2-tls runs HTTP/2. A handshake that chose ALPN http/1.1,
or none (a client that does not send ALPN speaks HTTP/1.1), reaches its
connection; any other protocol is shut down with close_notify. A slot's
connection is built on the first accept into it and kept, as
Http1Server's is.
class Http1TlsServer
| Member | |
|---|---|
new Http1TlsServer(tlsConfig: TlsServerConfig, config: Http1Config, listener: i32, size: i32) |
A pool of size slots, at least one, serving config over TLS under tlsConfig on listener. No slot is built here: the first accept into a slot builds its connection and, in tls, its handshake state, and keeps them, so a pool sized for a peak costs at start-up only the read scratches, the shared Date and the empty pools. |
connections: Http1Connection[] |
The connections built so far, slot k's at k: one per slot ever accepted into. connection never adds to it; a slot never built reads vacant instead. |
date: HttpDate |
The Date every slot's responses carry, as Http1Server.date is. |
expireScans: i32 |
How many calls of expire walked every slot. |
outputs: Http1OutputPool |
The output buffers the slots borrow while they write. |
tls: TlsTcpServer |
The TLS 1.3 pool underneath, one slot beside each connection; read it for a slot's TLS connection. |
accept(serverRandom: u8[], ephemeralPrivate: u8[]): i32 |
Accepts the next waiting connection as TlsTcpServer.accept does, with the caller's 32-byte server random and x25519 key, and gives it a fresh connection, building the slot's on its first accept. Answers the slot, or what TlsTcpServer.accept answers. |
alpn(slot: i32): string |
The ALPN protocol slot's handshake chose, or empty. |
busy(): i32 |
How many slots hold a connection. |
close(slot: i32): void |
Ends slot's connection and frees the slot at once, as TlsTcpServer.close does. |
connection(slot: i32): Http1Connection |
The HTTP/1.1 connection of slot. A slot never accepted into answers vacant, as a free slot answers a connection that holds nothing, and builds no slot; a slot out of range names slot 0's. |
expire(now: i64): i32 |
Closes every slot that has been idle past idleTimeout, or reading one head for longer than headTimeout, at now (monotonicNanos); answers how many. |
fd(slot: i32): i32 |
The descriptor of slot, for pollAdd; -1 for a free one. |
flush(slot: i32): i32 |
Writes what slot's connection holds into TLS, as far as TLS takes it, and sends it; shuts TLS down once the connection is over or ALPN chose another protocol. Answers TLS's interest, with the write bit while the connection still holds bytes or a write is held back. |
holds(slot: i32): boolean |
Whether slot holds a connection. |
next(slot: i32): i32 |
Moves what TLS has decrypted for slot into its connection, as far as the connection has room, and answers the connection's next event. A free slot, or one whose ALPN was another protocol, answers H1_ERROR. |
readable(slot: i32): i32 |
The socket of slot is readable: TLS reads it, and what the connection has to send goes after. Answers the interest. |
refused(slot: i32): boolean |
Whether slot's handshake is done and chose a protocol other than http/1.1. |
signP256(slot: i32, key: Secret<u8[]>): i32 |
Signs slot's CertificateVerify with key, as TlsTcpServer.signP256 does. Answers the interest. |
size(): i32 |
How many slots the pool has, built or not. |
writable(slot: i32): i32 |
The socket of slot is writable. Answers the interest. |
HttpDate
The Date field an origin server with a clock sends on every final
response (RFC 9110 §6.6.1): the IMF-fixdate of now, in 29 bytes the object
owns and rewrites in place, at most once a second.
It reads the wall clock, Date.now(), which a wasm32 build has no host for,
so it lives here, in a module that is native already, while the formatting
it calls, httpDateWrite, stays in nish/net/http-fields, which compiles
for wasm32 (the split nish/crypto/x509 and x509-random make). pin
replaces the clock with one instant, which is how a test asserts a response
byte for byte. hold reads the clock once for a run of responses, a serve
call's, until unhold; pin wins over it.
class HttpDate
| Member | |
|---|---|
new HttpDate() |
A date on the wall clock, with nothing formatted yet. |
clockReads(): i64 |
How many times the wall clock has been read: once per hold, and once per current outside one. |
current(): u8[] |
The 29 bytes for now, which are rewritten only when the second differs from the one they hold — earlier as well as later, since a corrected clock steps back. It allocates nothing, and the array is the same one each time, so a caller copies it out rather than keeping it. A wall clock before 1970 or past 9999 answers the nearer of the two ends. |
formats(): i64 |
How many times the bytes have been rewritten: one per second that current was asked for, at most. |
hold(): void |
Reads the wall clock once and makes current answer that reading until unhold, so the responses of one serve call cost one read. A pinned date reads nothing. A second hold takes a fresh reading. |
pin(unixMs: i64): boolean |
Makes current answer unixMs, milliseconds since 1970, until unpin. False, and nothing changed, for an instant before 1970-01-01T00:00:00Z or after 9999-12-31T23:59:59.999Z, which an IMF-fixdate cannot name. |
unhold(): void |
Puts current back on a clock read per call, for a response written outside a serve call. |
unpin(): void |
Puts current back on the wall clock. |
Constants
-11
H1_AGAIN: i32
Nothing fits yet: Linux's EAGAIN, as nish:net spells it.
"http/1.1"
H1_ALPN: string
The ALPN protocol identifier of HTTP/1.1 (RFC 7301 §6).
2
H1_BODY: i32
Request body bytes: data[dataStart .. dataStart + dataLength), valid until the next next().
-32
H1_CLOSED: i32
No exchange the call belongs to: Linux's EPIPE.
16
H1_DONE: i32
The slot's connection is over and every byte is sent: close it.
3
H1_END: i32
The request is complete.
7
H1_ERROR: i32
The input broke a rule; status is the HTTP status or WebSocket close code sent. Final.
-22
H1_INVALID: i32
Something the server may not send: Linux's EINVAL.
0
H1_NEED_MORE: i32
next: nothing to hand over until more input arrives, the output drains, or the program answers.
-105
H1_POOL_FULL: i32
accept: every slot was busy, so the connection was accepted and closed. Linux's ENOBUFS.
1
H1_REQUEST: i32
A request's head was read: parser holds it.
1
H1_WANT_READ: i32
Interest bits, the same as nish/net/tls/record-server's: the low two are pollModify's events.
2
H1_WANT_WRITE: i32
The slot has output waiting for the socket: ask the readiness loop for writable.
4
H1_WRITE: i32
A write, respond, end or sendFrame that answered H1_AGAIN may be tried again.
6
H1_WS_CLOSE: i32
The WebSocket peer closed with status (1005 for none); the close is answered. Final.
5
H1_WS_MESSAGE: i32
A WebSocket message: opcode, data[dataStart .. dataStart + dataLength).

