@amritk/nish-aarch64-linux 0.37.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/net/http2-cleartext` — HTTP/2 over plain TCP with prior knowledge
|
|
3
|
+
* (h2c, RFC 9113 §3.3): a pool of `Http2Connection`s, one per accepted
|
|
4
|
+
* socket, for a program that owns its readiness loop. The client starts with
|
|
5
|
+
* the 24-byte connection preface; there is no `Upgrade: h2c` (RFC 9113 §3.1
|
|
6
|
+
* deprecates it) and no fallback to HTTP/1.1 on the same port.
|
|
7
|
+
*
|
|
8
|
+
* **No confidentiality, no integrity.** Anyone on the path reads and edits
|
|
9
|
+
* the traffic. Serve h2c only upstream of a TLS-terminating proxy, on
|
|
10
|
+
* loopback or a private network (`docs/security/http2-cleartext.md`).
|
|
11
|
+
*
|
|
12
|
+
* import { pollAdd, pollModify, tcpListen } from "nish:net";
|
|
13
|
+
* import { H2_ERROR, H2_NEED_MORE, H2_REQUEST, Http2Config, Http2Connection } from "nish/net/http2";
|
|
14
|
+
* import { H2C_DONE, Http2CleartextServer } from "nish/net/http2-cleartext";
|
|
15
|
+
*
|
|
16
|
+
* // `loop` is the program's, from `pollCreate`.
|
|
17
|
+
* const LISTENER: i32 = 64; // past every slot's token
|
|
18
|
+
* const start = (loop: i32): Http2CleartextServer => {
|
|
19
|
+
* const listener: i32 = tcpListen("127.0.0.1", 8080, 128);
|
|
20
|
+
* pollAdd(loop, listener, 1, LISTENER);
|
|
21
|
+
* return new Http2CleartextServer(new Http2Config(), listener, 64, 10000);
|
|
22
|
+
* };
|
|
23
|
+
* // When the listener's token comes back:
|
|
24
|
+
* const admit = (loop: i32, server: Http2CleartextServer): void => {
|
|
25
|
+
* const slot: i32 = server.accept();
|
|
26
|
+
* if (slot >= 0) { pollAdd(loop, server.fd(slot), 1, slot); }
|
|
27
|
+
* };
|
|
28
|
+
* // When a slot's token comes back with `events`:
|
|
29
|
+
* const serve = (loop: i32, server: Http2CleartextServer, slot: i32, events: i32): void => {
|
|
30
|
+
* let wants: i32 = (events & 5) !== 0 ? server.readable(slot) : server.writable(slot);
|
|
31
|
+
* const none: u8[][] = [];
|
|
32
|
+
* let event: i32 = server.next(slot);
|
|
33
|
+
* while (event !== H2_NEED_MORE && event !== H2_ERROR) {
|
|
34
|
+
* const conn: Http2Connection = server.connection(slot);
|
|
35
|
+
* if (event === H2_REQUEST) { conn.respond(conn.stream, 200, none, none, true); }
|
|
36
|
+
* event = server.next(slot);
|
|
37
|
+
* }
|
|
38
|
+
* wants = server.flush(slot);
|
|
39
|
+
* if ((wants & H2C_DONE) !== 0) { server.close(slot); }
|
|
40
|
+
* else { pollModify(loop, server.fd(slot), wants & 3, slot); }
|
|
41
|
+
* };
|
|
42
|
+
* // Every so often, with `monotonicNanos()`:
|
|
43
|
+
* // server.expire(monotonicNanos());
|
|
44
|
+
*
|
|
45
|
+
* It is `nish/net/http2-tls`'s loop with a TCP socket in place of TLS, the
|
|
46
|
+
* same method names and the same interest bits (read 1, write 2, done 16),
|
|
47
|
+
* plus `expire(now)` for the preface deadline.
|
|
48
|
+
*
|
|
49
|
+
* **The preface is the carrier's.** `Http2Connection` checks the preface
|
|
50
|
+
* itself, but on a mismatch it queues SETTINGS and a GOAWAY, and a port
|
|
51
|
+
* scanner or a confused HTTP/1.1 client should get nothing back. So each slot
|
|
52
|
+
* reads its first 24 bytes into a 24-byte array of its own and compares each,
|
|
53
|
+
* as it comes, with `PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n`: at the first byte
|
|
54
|
+
* that differs the socket is closed, with no byte sent. A whole preface is
|
|
55
|
+
* fed to the connection, which checks it again, and from then on the socket
|
|
56
|
+
* is read straight into the connection's input, as `http2-tls.ts` reads TLS
|
|
57
|
+
* into it. Until the preface is whole the slot borrows no `Http2Pools`
|
|
58
|
+
* buffer and sends nothing, so a peer that stalls there holds 24 bytes and a
|
|
59
|
+
* slot until `expire` closes it, `prefaceTimeout` milliseconds after accept.
|
|
60
|
+
*
|
|
61
|
+
* **Relies on `@internal` members.** The carrier is the second server loop in
|
|
62
|
+
* `std/net` and uses `Http2Connection.inputTail`, `received` and `endInput`
|
|
63
|
+
* exactly as `http2-tls.ts` does. It keeps its own `ended` flag for the end of
|
|
64
|
+
* input it passed to `endInput`, rather than reading the `@internal`
|
|
65
|
+
* `inputEnded`.
|
|
66
|
+
*
|
|
67
|
+
* **Slots, a cap; buffers, borrowed.** As `Http2TlsServer`: slot 0's
|
|
68
|
+
* connection is made by the constructor, so a bad `Http2Config` panics there
|
|
69
|
+
* with its own words; every other slot's by the first `accept` into it, and
|
|
70
|
+
* kept. Each connection borrows its frame buffers from the shared `pools`
|
|
71
|
+
* and gives them back once empty and when the slot closes. Every final
|
|
72
|
+
* response carries the pool's `date`, formatted at most once a second.
|
|
73
|
+
*
|
|
74
|
+
* Written from RFC 9113 §3, not ported from another implementation.
|
|
75
|
+
*/
|
|
76
|
+
import { netClose, netRead, netWrite, tcpAccept } from "nish:net"
|
|
77
|
+
import { HttpDate } from "nish/net/http1-server"
|
|
78
|
+
import { H2_ERROR, H2_NEED_MORE, Http2Config, Http2Connection, Http2Pools } from "nish/net/http2"
|
|
79
|
+
|
|
80
|
+
/** A typed zero: a bare literal is an `f64` under `--number-mode f64`. */
|
|
81
|
+
const H2C_ZERO: i32 = 0
|
|
82
|
+
|
|
83
|
+
/** The interest bit for "read the socket": `pollModify`'s read event. */
|
|
84
|
+
export const H2C_WANT_READ: i32 = 1
|
|
85
|
+
/** The interest bit for "write the socket": `pollModify`'s write event. */
|
|
86
|
+
export const H2C_WANT_WRITE: i32 = 2
|
|
87
|
+
/** The interest bit for "the slot is over": the program calls `close(slot)`. */
|
|
88
|
+
export const H2C_DONE: i32 = 16
|
|
89
|
+
/** What `accept` answers when every slot is busy; the connection it took is closed. */
|
|
90
|
+
export const H2C_POOL_FULL: i32 = -105
|
|
91
|
+
|
|
92
|
+
/** The client connection preface's length (RFC 9113 §3.4). */
|
|
93
|
+
const H2C_PREFACE_SIZE: i32 = 24
|
|
94
|
+
/** The size of `nish:net`'s peer address form, which `tcpAccept` fills. */
|
|
95
|
+
const H2C_ADDRESS_SIZE: i32 = 18
|
|
96
|
+
/** What `netRead` and `netWrite` answer when the socket would block. */
|
|
97
|
+
const H2C_AGAIN: i32 = -11
|
|
98
|
+
/** A free slot's socket: no descriptor at all. */
|
|
99
|
+
const H2C_NO_FD: i32 = -1
|
|
100
|
+
/** Nanoseconds in a millisecond, for `prefaceTimeout` against `monotonicNanos`. */
|
|
101
|
+
const H2C_NANOS_PER_MS: i64 = 1000000
|
|
102
|
+
|
|
103
|
+
/** The client connection preface, `PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n` (RFC 9113 §3.4). */
|
|
104
|
+
const H2C_PREFACE: string = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A pool of HTTP/2 connections over plain TCP, with prior knowledge, on one
|
|
108
|
+
* listening socket. `connection(slot)` is the HTTP/2 side of each slot.
|
|
109
|
+
*/
|
|
110
|
+
export class Http2CleartextServer {
|
|
111
|
+
/** The caps every slot's connection is made under. */
|
|
112
|
+
private config: Http2Config
|
|
113
|
+
/**
|
|
114
|
+
* The connections made so far, slot `k`'s at `k`: slot 0's, made by the
|
|
115
|
+
* constructor, and one for each slot accepted into since.
|
|
116
|
+
*/
|
|
117
|
+
connections: Http2Connection[]
|
|
118
|
+
/**
|
|
119
|
+
* The `date` every slot's responses carry, formatted once a second for all
|
|
120
|
+
* of them: each slot's `Http2Connection.date` is its bytes, refreshed by
|
|
121
|
+
* `connection` and `next`. `date.pin` fixes its clock.
|
|
122
|
+
*/
|
|
123
|
+
date: HttpDate
|
|
124
|
+
/** The frame buffers every slot's connection borrows: one of each per slot at most, none made up front. */
|
|
125
|
+
readonly pools: Http2Pools
|
|
126
|
+
/** Each slot's socket, -1 while the slot is free; as long as the slots built so far. */
|
|
127
|
+
private fds: i32[]
|
|
128
|
+
/** The slots closed since they were used, a stack `accept` takes from before it builds a new one. */
|
|
129
|
+
private free: i32[]
|
|
130
|
+
/** Every slot's 24 preface bytes, slot `k`'s at `24 * k`; made whole by the constructor. */
|
|
131
|
+
private prefaces: u8[]
|
|
132
|
+
/** How many preface bytes each slot has read; 24 once the preface is whole and fed. */
|
|
133
|
+
private got: i32[]
|
|
134
|
+
/** When each slot was accepted, in `monotonicNanos`: its preface deadline's start. */
|
|
135
|
+
private since: i64[]
|
|
136
|
+
/** Whether each slot's socket has ended and `endInput` was called for it. */
|
|
137
|
+
private ended: boolean[]
|
|
138
|
+
/** The last accepted peer's address, in `nish:net`'s 18-byte form. */
|
|
139
|
+
private peer: u8[]
|
|
140
|
+
/** How many slots the pool may hold. */
|
|
141
|
+
private slots: i32 = 1
|
|
142
|
+
/** The listening socket, which the program made with `tcpListen` and still owns. */
|
|
143
|
+
private listener: i32 = -1
|
|
144
|
+
/** Nanoseconds a slot may wait for its whole preface before `expire` closes it. */
|
|
145
|
+
private prefaceNanos: i64 = 0
|
|
146
|
+
/** How many held slots have not read their whole preface: `expire` walks none while this is 0. */
|
|
147
|
+
private waiting: i32 = 0
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A pool of `size` slots serving HTTP/2 under `config` on `listener`. Only
|
|
151
|
+
* slot 0's connection is made here, so a config it refuses panics now, with
|
|
152
|
+
* its words; every other slot's is made by the first `accept` into it and
|
|
153
|
+
* kept. The slots' preface arrays, 24 bytes each, are made here too.
|
|
154
|
+
*
|
|
155
|
+
* @param config The caps every slot's connection is made under; kept, and shared by every slot.
|
|
156
|
+
* @param listener The listening socket from `tcpListen`; the program still owns it and closes it.
|
|
157
|
+
* @param size How many connections may be open at once, at least 1; anything less panics.
|
|
158
|
+
* @param prefaceTimeout Milliseconds a peer has, from `accept`, to send its whole 24-byte preface before
|
|
159
|
+
* `expire` closes the slot; at least 1.
|
|
160
|
+
*/
|
|
161
|
+
constructor(config: Http2Config, listener: i32, size: i32, prefaceTimeout: i32) {
|
|
162
|
+
if (size < 1) {
|
|
163
|
+
panic(`Http2CleartextServer: slots is ${size}; at least 1 is needed`)
|
|
164
|
+
}
|
|
165
|
+
if (prefaceTimeout < 1) {
|
|
166
|
+
panic(`Http2CleartextServer: prefaceTimeout is ${prefaceTimeout} ms; at least 1 is needed`)
|
|
167
|
+
}
|
|
168
|
+
this.config = config
|
|
169
|
+
this.listener = listener
|
|
170
|
+
this.slots = size
|
|
171
|
+
this.prefaceNanos = toI64(prefaceTimeout) * H2C_NANOS_PER_MS
|
|
172
|
+
this.connections = []
|
|
173
|
+
this.fds = []
|
|
174
|
+
this.free = []
|
|
175
|
+
this.got = []
|
|
176
|
+
this.since = []
|
|
177
|
+
this.ended = []
|
|
178
|
+
this.prefaces = new Array<u8>(size * H2C_PREFACE_SIZE)
|
|
179
|
+
this.peer = new Array<u8>(H2C_ADDRESS_SIZE)
|
|
180
|
+
this.date = new HttpDate()
|
|
181
|
+
// Formatted once now, so no slot ever sends the bytes before a first format.
|
|
182
|
+
const first: Http2Connection = new Http2Connection(config)
|
|
183
|
+
first.date = this.date.current()
|
|
184
|
+
this.connections.push(first)
|
|
185
|
+
// After slot 0's connection, so a config it refuses panics with its words.
|
|
186
|
+
this.pools = new Http2Pools("Http2CleartextServer", config, size)
|
|
187
|
+
first.pools = this.pools
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Makes the next slot's connection, on this pool's buffers and `date`. */
|
|
191
|
+
private build(): void {
|
|
192
|
+
const conn: Http2Connection = new Http2Connection(this.config)
|
|
193
|
+
conn.date = this.date.current()
|
|
194
|
+
conn.pools = this.pools
|
|
195
|
+
this.connections.push(conn)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Whether `slot` holds a socket and has read its whole preface. */
|
|
199
|
+
private live(slot: i32): boolean {
|
|
200
|
+
return this.holds(slot) && this.got[slot] === H2C_PREFACE_SIZE
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* How many slots the pool has, made or not.
|
|
205
|
+
*
|
|
206
|
+
* @returns The slot count the constructor was given, at least 1.
|
|
207
|
+
*/
|
|
208
|
+
size(): i32 {
|
|
209
|
+
return this.slots
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* How many slots hold a connection.
|
|
214
|
+
*
|
|
215
|
+
* @returns Slots with an open socket, whether their preface is whole or not; 0 to `size()`.
|
|
216
|
+
*/
|
|
217
|
+
busy(): i32 {
|
|
218
|
+
let n: i32 = 0
|
|
219
|
+
for (const fd of this.fds) {
|
|
220
|
+
if (fd >= 0) {
|
|
221
|
+
n = n + 1
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return n
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Whether `slot` holds a connection.
|
|
229
|
+
*
|
|
230
|
+
* @param slot A slot number, as `accept` answered it; any value is safe to ask about.
|
|
231
|
+
* @returns True when `slot` is in range and holds a socket.
|
|
232
|
+
*/
|
|
233
|
+
holds(slot: i32): boolean {
|
|
234
|
+
return slot >= 0 && slot < toI32(this.fds.length) && this.fds[slot] >= 0
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The descriptor of `slot`, for `pollAdd`.
|
|
239
|
+
*
|
|
240
|
+
* @param slot A slot number, as `accept` answered it.
|
|
241
|
+
* @returns The socket descriptor, which the pool still owns and closes in `close`; -1 when `slot` holds
|
|
242
|
+
* none.
|
|
243
|
+
*/
|
|
244
|
+
fd(slot: i32): i32 {
|
|
245
|
+
return this.holds(slot) ? this.fds[slot] : -1
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Whether `slot` has read its whole connection preface, so its connection is running.
|
|
250
|
+
*
|
|
251
|
+
* @param slot A slot number, as `accept` answered it; any value is safe to ask about.
|
|
252
|
+
* @returns True once the slot's 24 preface bytes have arrived and matched; false for a free slot.
|
|
253
|
+
*/
|
|
254
|
+
prefaced(slot: i32): boolean {
|
|
255
|
+
return this.live(slot)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The HTTP/2 connection of `slot`, its `date` brought up to now. A slot
|
|
260
|
+
* never accepted into, or out of range, names slot 0's, which may be live.
|
|
261
|
+
*
|
|
262
|
+
* @param slot A slot number, as `accept` answered it.
|
|
263
|
+
* @returns The slot's connection, to read events from and answer on; the server keeps it and restarts
|
|
264
|
+
* it for the slot's next peer.
|
|
265
|
+
*/
|
|
266
|
+
connection(slot: i32): Http2Connection {
|
|
267
|
+
this.date.current()
|
|
268
|
+
const at: i32 = slot >= 0 && slot < toI32(this.connections.length) ? slot : H2C_ZERO
|
|
269
|
+
return this.connections[at]
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Accepts the next waiting connection into a free slot and gives it a fresh
|
|
274
|
+
* HTTP/2 connection, which sends nothing until the peer's preface is whole:
|
|
275
|
+
* the slot's own, restarted, or one made now if this is the first accept
|
|
276
|
+
* into it. A slot closed before is taken first, then the next never used.
|
|
277
|
+
*
|
|
278
|
+
* @returns The slot, 0 or more, that now holds the connection; or, taking none, -11 when no connection is
|
|
279
|
+
* waiting, `tcpAccept`'s own failure, or `H2C_POOL_FULL` when every slot is busy (the connection it
|
|
280
|
+
* took is closed with nothing sent).
|
|
281
|
+
*/
|
|
282
|
+
accept(): i32 {
|
|
283
|
+
const fd: i32 = tcpAccept(this.listener, this.peer)
|
|
284
|
+
if (fd < 0) {
|
|
285
|
+
return fd
|
|
286
|
+
}
|
|
287
|
+
let k: i32 = H2C_POOL_FULL
|
|
288
|
+
if (this.free.length > 0) {
|
|
289
|
+
k = this.free[this.free.length - 1]
|
|
290
|
+
this.free.pop()
|
|
291
|
+
} else if (toI32(this.fds.length) < this.slots) {
|
|
292
|
+
k = toI32(this.fds.length)
|
|
293
|
+
this.fds.push(H2C_NO_FD)
|
|
294
|
+
this.got.push(H2C_ZERO)
|
|
295
|
+
this.since.push(toI64(H2C_ZERO))
|
|
296
|
+
this.ended.push(false)
|
|
297
|
+
}
|
|
298
|
+
if (k < 0) {
|
|
299
|
+
netClose(fd)
|
|
300
|
+
return H2C_POOL_FULL
|
|
301
|
+
}
|
|
302
|
+
// Slots are built in order, so this makes one connection at most.
|
|
303
|
+
while (toI32(this.connections.length) <= k) {
|
|
304
|
+
this.build()
|
|
305
|
+
}
|
|
306
|
+
// `restart` gives back every buffer; the preface is read before any is borrowed.
|
|
307
|
+
this.connections[k].restart()
|
|
308
|
+
this.fds[k] = fd
|
|
309
|
+
this.got[k] = H2C_ZERO
|
|
310
|
+
this.since[k] = monotonicNanos()
|
|
311
|
+
this.ended[k] = false
|
|
312
|
+
this.waiting = this.waiting + 1
|
|
313
|
+
return k
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Reads what is left of `slot`'s preface, byte for byte against the
|
|
318
|
+
* constant, and closes the slot at the first byte that differs, or when the
|
|
319
|
+
* peer hangs up or the socket fails first. Feeds a whole preface to the
|
|
320
|
+
* connection.
|
|
321
|
+
*
|
|
322
|
+
* @returns True once the preface is whole; false while it is not, or once the slot is closed.
|
|
323
|
+
*/
|
|
324
|
+
private admit(slot: i32): boolean {
|
|
325
|
+
if (!this.holds(slot)) {
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
328
|
+
if (this.got[slot] === H2C_PREFACE_SIZE) {
|
|
329
|
+
return true
|
|
330
|
+
}
|
|
331
|
+
const base: i32 = slot * H2C_PREFACE_SIZE
|
|
332
|
+
while (this.got[slot] < H2C_PREFACE_SIZE) {
|
|
333
|
+
const have: i32 = this.got[slot]
|
|
334
|
+
const n: i32 = netRead(this.fds[slot], this.prefaces, base + have, H2C_PREFACE_SIZE - have)
|
|
335
|
+
if (n === H2C_AGAIN) {
|
|
336
|
+
return false
|
|
337
|
+
}
|
|
338
|
+
if (n <= 0) {
|
|
339
|
+
this.close(slot)
|
|
340
|
+
return false
|
|
341
|
+
}
|
|
342
|
+
for (let k: i32 = have; k < have + n; k++) {
|
|
343
|
+
if (toI32(this.prefaces[base + k]) !== toI32(H2C_PREFACE.charCodeAt(k))) {
|
|
344
|
+
this.close(slot)
|
|
345
|
+
return false
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
this.got[slot] = have + n
|
|
349
|
+
}
|
|
350
|
+
this.waiting = this.waiting - 1
|
|
351
|
+
this.connections[slot].feed(this.prefaces, base, H2C_PREFACE_SIZE)
|
|
352
|
+
return true
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The socket of `slot` is readable: while the preface is not whole, reads
|
|
357
|
+
* it (and closes the slot at a byte that differs); then sends what HTTP/2
|
|
358
|
+
* has to send. Once the preface is whole, `next` reads the socket.
|
|
359
|
+
*
|
|
360
|
+
* @param slot The slot whose socket `pollWait` reported readable, or hung up.
|
|
361
|
+
* @returns The slot's interest, as `flush` answers it; `H2C_DONE` for a free slot or one this call closed.
|
|
362
|
+
*/
|
|
363
|
+
readable(slot: i32): i32 {
|
|
364
|
+
this.admit(slot)
|
|
365
|
+
const wants: i32 = this.flush(slot)
|
|
366
|
+
this.rest(slot)
|
|
367
|
+
return wants
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The socket of `slot` is writable: what HTTP/2 holds goes out.
|
|
372
|
+
*
|
|
373
|
+
* @param slot The slot whose socket `pollWait` reported writable.
|
|
374
|
+
* @returns The slot's interest, as `flush` answers it; `H2C_DONE` for a free slot.
|
|
375
|
+
*/
|
|
376
|
+
writable(slot: i32): i32 {
|
|
377
|
+
const wants: i32 = this.flush(slot)
|
|
378
|
+
this.rest(slot)
|
|
379
|
+
return wants
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Reads `slot`'s socket into its connection, as far as the connection has
|
|
384
|
+
* room, and answers the connection's next event. A slot whose preface is
|
|
385
|
+
* not whole reads that first and answers `H2_NEED_MORE` while it waits.
|
|
386
|
+
*
|
|
387
|
+
* @param slot The slot to read, as `accept` answered it.
|
|
388
|
+
* @returns The connection's next event, `H2_REQUEST` to `H2_GOAWAY` of `nish/net/http2`, read from
|
|
389
|
+
* `connection(slot)`; `H2_NEED_MORE` once it needs more input or its output to drain, after the
|
|
390
|
+
* output has gone to the socket, or while the preface is not whole; or `H2_ERROR` for a connection
|
|
391
|
+
* that failed, a free slot, or one closed for a bad preface.
|
|
392
|
+
*/
|
|
393
|
+
next(slot: i32): i32 {
|
|
394
|
+
if (!this.holds(slot)) {
|
|
395
|
+
return H2_ERROR
|
|
396
|
+
}
|
|
397
|
+
if (!this.admit(slot)) {
|
|
398
|
+
return this.holds(slot) ? H2_NEED_MORE : H2_ERROR
|
|
399
|
+
}
|
|
400
|
+
this.date.current()
|
|
401
|
+
const conn: Http2Connection = this.connections[slot]
|
|
402
|
+
// The socket is read straight into the connection's input, so a byte is
|
|
403
|
+
// copied once on its way in.
|
|
404
|
+
while (!this.ended[slot]) {
|
|
405
|
+
const room: i32 = conn.inputTail()
|
|
406
|
+
if (room <= 0) {
|
|
407
|
+
break
|
|
408
|
+
}
|
|
409
|
+
const n: i32 = netRead(this.fds[slot], conn.input, conn.inputEnd, room)
|
|
410
|
+
if (n === H2C_AGAIN) {
|
|
411
|
+
break
|
|
412
|
+
}
|
|
413
|
+
if (n <= 0) {
|
|
414
|
+
// The peer's end, or a socket that failed: either way nothing more comes.
|
|
415
|
+
this.ended[slot] = true
|
|
416
|
+
conn.endInput()
|
|
417
|
+
break
|
|
418
|
+
}
|
|
419
|
+
conn.received(n)
|
|
420
|
+
}
|
|
421
|
+
const event: i32 = conn.next()
|
|
422
|
+
if (event === H2_NEED_MORE || event === H2_ERROR) {
|
|
423
|
+
this.flush(slot)
|
|
424
|
+
this.rest(slot)
|
|
425
|
+
}
|
|
426
|
+
return event
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Writes what `slot`'s connection holds to the socket, as far as the socket
|
|
431
|
+
* takes it, and answers the interest. Nothing is written while the preface
|
|
432
|
+
* is not whole. A socket that fails ends the connection.
|
|
433
|
+
*
|
|
434
|
+
* @param slot The slot whose output to send, as `accept` answered it.
|
|
435
|
+
* @returns The slot's interest: `H2C_WANT_READ` while the preface is not whole; once it is,
|
|
436
|
+
* `H2C_WANT_WRITE` while the connection holds bytes and `H2C_WANT_READ` while its input has not ended;
|
|
437
|
+
* `H2C_DONE` once the connection is over (everything is sent, and it failed, or no stream is left after
|
|
438
|
+
* a GOAWAY or after the peer's byte stream ended), for a free slot, and for one closed for a bad
|
|
439
|
+
* preface.
|
|
440
|
+
*/
|
|
441
|
+
flush(slot: i32): i32 {
|
|
442
|
+
if (!this.holds(slot)) {
|
|
443
|
+
return H2C_DONE
|
|
444
|
+
}
|
|
445
|
+
if (!this.live(slot)) {
|
|
446
|
+
return H2C_WANT_READ
|
|
447
|
+
}
|
|
448
|
+
const conn: Http2Connection = this.connections[slot]
|
|
449
|
+
while (conn.wantsWrite()) {
|
|
450
|
+
const n: i32 = netWrite(
|
|
451
|
+
this.fds[slot],
|
|
452
|
+
conn.output,
|
|
453
|
+
conn.outputStart,
|
|
454
|
+
conn.outputEnd - conn.outputStart
|
|
455
|
+
)
|
|
456
|
+
if (n === H2C_AGAIN) {
|
|
457
|
+
break
|
|
458
|
+
}
|
|
459
|
+
if (n <= 0) {
|
|
460
|
+
// The peer is gone (-32) or the socket failed: what is left can never be sent.
|
|
461
|
+
conn.drop()
|
|
462
|
+
break
|
|
463
|
+
}
|
|
464
|
+
conn.consume(n)
|
|
465
|
+
}
|
|
466
|
+
if (conn.isDone()) {
|
|
467
|
+
return H2C_DONE
|
|
468
|
+
}
|
|
469
|
+
const read: i32 = this.ended[slot] ? H2C_ZERO : H2C_WANT_READ
|
|
470
|
+
return conn.wantsWrite() ? read | H2C_WANT_WRITE : read
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Closes every slot whose preface is not whole `prefaceTimeout`
|
|
475
|
+
* milliseconds after its accept, at `now`. A slot past its preface has no
|
|
476
|
+
* deadline here: a program times idle connections itself.
|
|
477
|
+
*
|
|
478
|
+
* @param now The current time, from `monotonicNanos()`.
|
|
479
|
+
* @returns How many slots this call closed.
|
|
480
|
+
*/
|
|
481
|
+
expire(now: i64): i32 {
|
|
482
|
+
if (this.waiting === 0) {
|
|
483
|
+
return 0
|
|
484
|
+
}
|
|
485
|
+
let n: i32 = 0
|
|
486
|
+
for (let k: i32 = 0; k < toI32(this.fds.length); k++) {
|
|
487
|
+
if (this.fds[k] >= 0 && this.got[k] < H2C_PREFACE_SIZE && now - this.since[k] >= this.prefaceNanos) {
|
|
488
|
+
this.close(k)
|
|
489
|
+
n = n + 1
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return n
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Ends `slot`'s connection and frees the slot at once: the socket is
|
|
497
|
+
* closed with nothing more sent, and the connection gives back every buffer
|
|
498
|
+
* it holds.
|
|
499
|
+
*
|
|
500
|
+
* @param slot The slot to free; for a free or unknown slot nothing is closed.
|
|
501
|
+
*/
|
|
502
|
+
close(slot: i32): void {
|
|
503
|
+
if (!this.holds(slot)) {
|
|
504
|
+
return
|
|
505
|
+
}
|
|
506
|
+
netClose(this.fds[slot])
|
|
507
|
+
this.fds[slot] = -1
|
|
508
|
+
if (this.got[slot] < H2C_PREFACE_SIZE) {
|
|
509
|
+
this.waiting = this.waiting - 1
|
|
510
|
+
}
|
|
511
|
+
this.got[slot] = H2C_ZERO
|
|
512
|
+
this.free.push(slot)
|
|
513
|
+
this.connections[slot].drop()
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Gives back the buffers `slot`'s connection holds and has emptied, so an idle slot holds none.
|
|
518
|
+
*
|
|
519
|
+
* @param slot A slot number; a slot never accepted into, or out of range, is left alone.
|
|
520
|
+
*/
|
|
521
|
+
rest(slot: i32): void {
|
|
522
|
+
if (slot >= 0 && slot < toI32(this.connections.length)) {
|
|
523
|
+
this.connections[slot].rest()
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
package/std/net/http2-tls.ts
CHANGED
|
@@ -84,6 +84,7 @@
|
|
|
84
84
|
*
|
|
85
85
|
* Written from RFC 9113 §3 and §9.2, not ported from another implementation.
|
|
86
86
|
*/
|
|
87
|
+
import { monotonicNanos } from "nish:process"
|
|
87
88
|
import { Secret } from "nish:secret"
|
|
88
89
|
import { HttpDate } from "nish/net/http1-server"
|
|
89
90
|
import { H2_ERROR, H2_NEED_MORE, Http2Config, Http2Connection, Http2Pools } from "nish/net/http2"
|
|
@@ -124,6 +125,11 @@ export class Http2TlsServer {
|
|
|
124
125
|
date: HttpDate
|
|
125
126
|
/** The frame buffers every slot's connection borrows: one of each per slot at most, none made up front. */
|
|
126
127
|
readonly pools: Http2Pools
|
|
128
|
+
/**
|
|
129
|
+
* No slot can expire before this (`monotonicNanos`), so `expire` called
|
|
130
|
+
* sooner answers 0 without walking the slots.
|
|
131
|
+
*/
|
|
132
|
+
private expireAfter: i64 = 0
|
|
127
133
|
|
|
128
134
|
/**
|
|
129
135
|
* A pool of `size` slots, at least one, serving HTTP/2 under `config` over
|
|
@@ -254,6 +260,7 @@ export class Http2TlsServer {
|
|
|
254
260
|
this.build()
|
|
255
261
|
}
|
|
256
262
|
this.connections[slot].restart()
|
|
263
|
+
this.connections[slot].begin(monotonicNanos())
|
|
257
264
|
return slot
|
|
258
265
|
}
|
|
259
266
|
|
|
@@ -334,6 +341,7 @@ export class Http2TlsServer {
|
|
|
334
341
|
break
|
|
335
342
|
}
|
|
336
343
|
conn.received(n)
|
|
344
|
+
conn.lastActive = monotonicNanos()
|
|
337
345
|
}
|
|
338
346
|
const event: i32 = conn.next()
|
|
339
347
|
// What the program answers to one event and the next shares a record:
|
|
@@ -373,6 +381,7 @@ export class Http2TlsServer {
|
|
|
373
381
|
break
|
|
374
382
|
}
|
|
375
383
|
conn.consume(n)
|
|
384
|
+
conn.lastActive = monotonicNanos()
|
|
376
385
|
}
|
|
377
386
|
if (conn.isDone()) {
|
|
378
387
|
return this.tls.shutdown(slot)
|
|
@@ -394,6 +403,45 @@ export class Http2TlsServer {
|
|
|
394
403
|
this.tls.close(slot)
|
|
395
404
|
}
|
|
396
405
|
|
|
406
|
+
/**
|
|
407
|
+
* Ends every slot whose connection has outlived a timeout at `now`: a
|
|
408
|
+
* client that has not acknowledged the server's SETTINGS within
|
|
409
|
+
* `settingsTimeout` of its accept, the TLS handshake included, is sent
|
|
410
|
+
* GOAWAY SETTINGS_TIMEOUT (RFC 9113 §6.5.3) once its handshake is done, and
|
|
411
|
+
* one with nothing moved for `idleTimeout`, mid-stream or lingering after
|
|
412
|
+
* its `close_notify`, GOAWAY NO_ERROR; each frame goes as far as the socket
|
|
413
|
+
* takes it now, and the slot is closed. A program calls it from its loop, with
|
|
414
|
+
* `pollWait`'s timeout no longer than the shorter of the two.
|
|
415
|
+
*
|
|
416
|
+
* @param now the current time in `monotonicNanos`, which both timeouts are measured against
|
|
417
|
+
* @returns how many slots this call closed
|
|
418
|
+
*/
|
|
419
|
+
expire(now: i64): i32 {
|
|
420
|
+
if (now < this.expireAfter) {
|
|
421
|
+
return 0
|
|
422
|
+
}
|
|
423
|
+
const config: Http2Config = this.config
|
|
424
|
+
const shorter: i32 =
|
|
425
|
+
config.settingsTimeout < config.idleTimeout ? config.settingsTimeout : config.idleTimeout
|
|
426
|
+
let soonest: i64 = now + toI64(shorter) * toI64(1000000)
|
|
427
|
+
let n: i32 = 0
|
|
428
|
+
for (let k: i32 = 0; k < toI32(this.connections.length); k++) {
|
|
429
|
+
if (this.tls.holds(k)) {
|
|
430
|
+
const conn: Http2Connection = this.connections[k]
|
|
431
|
+
if (conn.expired(now)) {
|
|
432
|
+
conn.timeOut(now)
|
|
433
|
+
this.flush(k)
|
|
434
|
+
this.close(k)
|
|
435
|
+
n = n + 1
|
|
436
|
+
} else if (conn.expiresNoSooner(now) < soonest) {
|
|
437
|
+
soonest = conn.expiresNoSooner(now)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
this.expireAfter = soonest
|
|
442
|
+
return n
|
|
443
|
+
}
|
|
444
|
+
|
|
397
445
|
/**
|
|
398
446
|
* Gives back the buffers `slot`'s connection holds and has emptied, so an idle slot holds none.
|
|
399
447
|
*
|