@amritk/nish-aarch64-linux 0.35.0 → 0.37.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 +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/net/http2.ts
CHANGED
|
@@ -9,78 +9,102 @@
|
|
|
9
9
|
* It is sans-IO: bytes in, events and bytes out, and no socket anywhere.
|
|
10
10
|
* `nish/net/http2-tls` runs it over TLS with ALPN `h2`.
|
|
11
11
|
*
|
|
12
|
-
* import {
|
|
12
|
+
* import { H2_ERROR, H2_NEED_MORE, H2_REQUEST, Http2Config, Http2Connection } from "nish/net/http2";
|
|
13
|
+
* import { httpFieldBytes } from "nish/net/http-fields";
|
|
13
14
|
*
|
|
15
|
+
* const names: u8[][] = [httpFieldBytes("content-type")];
|
|
16
|
+
* const values: u8[][] = [httpFieldBytes("text/plain")];
|
|
17
|
+
* const body: u8[] = httpFieldBytes("hello");
|
|
14
18
|
* const conn = new Http2Connection(new Http2Config());
|
|
15
|
-
* // send conn.output[conn.outputStart .. conn.outputEnd), then conn.consume(sent)
|
|
16
|
-
* conn.feed(received, 0, n);
|
|
17
|
-
* let event: i32 = conn.next();
|
|
18
|
-
* while (event !== H2_NEED_MORE && event !== H2_ERROR) {
|
|
19
|
-
* // H2_REQUEST: conn.stream, conn.fields (method, path, get("accept")), conn.endStream
|
|
20
|
-
* // H2_DATA: conn.data[conn.dataStart .. conn.dataStart + conn.dataLength), conn.endStream
|
|
21
|
-
* event = conn.next();
|
|
22
|
-
* }
|
|
23
|
-
* conn.respond(stream, 200, names, values, false);
|
|
24
|
-
* conn.writeData(stream, body, 0, n, true);
|
|
25
19
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* being read rather than growing a queue. Bodies move a frame at a time and
|
|
43
|
-
* nothing buffers a whole one.
|
|
20
|
+
* // Hands the connection `received[0 .. n)`, read from the socket, and answers every request.
|
|
21
|
+
* const serve = (received: u8[], n: i32): void => {
|
|
22
|
+
* conn.feed(received, 0, n); // answers how much it took; feed the rest after `next`
|
|
23
|
+
* let event: i32 = conn.next();
|
|
24
|
+
* while (event !== H2_NEED_MORE && event !== H2_ERROR) {
|
|
25
|
+
* // H2_DATA: conn.data[conn.dataStart .. conn.dataStart + conn.dataLength), conn.endStream
|
|
26
|
+
* if (event === H2_REQUEST) {
|
|
27
|
+
* // conn.stream, conn.fields (method, path, get("accept")), conn.endStream
|
|
28
|
+
* conn.respond(conn.stream, 200, names, values, false);
|
|
29
|
+
* conn.writeData(conn.stream, body, 0, toI32(body.length), true);
|
|
30
|
+
* }
|
|
31
|
+
* event = conn.next();
|
|
32
|
+
* }
|
|
33
|
+
* // while conn.wantsWrite(): send conn.output[conn.outputStart .. conn.outputEnd),
|
|
34
|
+
* // then conn.consume(sent)
|
|
35
|
+
* };
|
|
44
36
|
*
|
|
45
|
-
* **
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* (
|
|
54
|
-
*
|
|
55
|
-
* `
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
37
|
+
* **Events.**
|
|
38
|
+
*
|
|
39
|
+
* `next()` reads the frames `feed` handed it and answers the first that means
|
|
40
|
+
* something to the program:
|
|
41
|
+
* - `H2_REQUEST` (a stream opened with a request's header section; `fields`
|
|
42
|
+
* holds it until the next header block on the connection is read, for any
|
|
43
|
+
* stream, and a program copies what it keeps past that: the next block
|
|
44
|
+
* empties `fields` and reuses the arrays it pointed into),
|
|
45
|
+
* - `H2_DATA` (some of a request body, a window onto the input buffer that
|
|
46
|
+
* stays valid until the next `feed`),
|
|
47
|
+
* - `H2_TRAILERS`,
|
|
48
|
+
* - `H2_RESET` (the stream is gone: the peer reset it, or broke a rule that
|
|
49
|
+
* resets it — `errorCode` says which),
|
|
50
|
+
* - `H2_WINDOW` (a send window that held `writeData` back has opened: `stream`,
|
|
51
|
+
* or 0 for the connection's),
|
|
52
|
+
* - `H2_GOAWAY` (the peer is going away; `lastStreamId`),
|
|
53
|
+
* - and `H2_ERROR`, final: the connection broke a rule, a GOAWAY with
|
|
54
|
+
* `errorCode` is queued, and every later call answers it again.
|
|
55
|
+
*
|
|
56
|
+
* `H2_NEED_MORE` asks for more input, or for the output to drain: a frame is
|
|
57
|
+
* read only while the output has room for whatever it makes the connection
|
|
58
|
+
* answer, so a peer that sends PINGs or SETTINGS and never reads the
|
|
59
|
+
* acknowledgements stops being read rather than growing a queue. Bodies move a
|
|
60
|
+
* frame at a time and nothing buffers a whole one.
|
|
61
|
+
*
|
|
62
|
+
* **What the connection refuses**, as RFC 9113 classes it.
|
|
63
|
+
*
|
|
64
|
+
* A connection error sends GOAWAY and ends the connection:
|
|
65
|
+
* - a bad preface or a first frame that is not SETTINGS (PROTOCOL_ERROR, §3.4);
|
|
66
|
+
* - every refusal of `nish/net/http2-frame`;
|
|
67
|
+
* - a frame other than CONTINUATION inside a header block, or a CONTINUATION
|
|
68
|
+
* outside one (§6.10);
|
|
69
|
+
* - a stream identifier that is even or does not increase (§5.1.1);
|
|
70
|
+
* - DATA, WINDOW_UPDATE or RST_STREAM on an idle stream, and a PUSH_PROMISE
|
|
71
|
+
* from a client (PROTOCOL_ERROR);
|
|
72
|
+
* - HEADERS on a closed stream (STREAM_CLOSED);
|
|
73
|
+
* - a header block HPACK cannot decode (COMPRESSION_ERROR, RFC 7541 §2.3.2);
|
|
74
|
+
* - DATA past the connection window, or a window pushed past 2^31 − 1
|
|
75
|
+
* (FLOW_CONTROL_ERROR, §6.9);
|
|
76
|
+
* - a setting `http2SettingError` refuses;
|
|
77
|
+
* - a header block longer than `maxHeaderBlock` or cut into more than
|
|
78
|
+
* `H2_MAX_FRAGMENTS` frames, a header block that would make the HPACK table
|
|
79
|
+
* copy past its budget (`HPACK_ERR_COPY_BUDGET`), and more streams reset than
|
|
80
|
+
* `resetBudget` allows — by the peer, or by the server for a stream error the
|
|
81
|
+
* peer caused (ENHANCE_YOUR_CALM, §10.5).
|
|
82
|
+
*
|
|
83
|
+
* A stream error resets one stream and the connection lives: a malformed
|
|
84
|
+
* request — any refusal of `nish/net/http-fields`, trailers without END_STREAM,
|
|
85
|
+
* a body that disagrees with `content-length` (PROTOCOL_ERROR, §8.1.1); DATA or
|
|
86
|
+
* HEADERS after the peer ended the stream (STREAM_CLOSED, §5.1); DATA past the
|
|
87
|
+
* stream's window, or its window pushed past 2^31 − 1 (FLOW_CONTROL_ERROR); a
|
|
88
|
+
* stream past `maxStreams` (REFUSED_STREAM, §5.1.2); a stream that depends on
|
|
89
|
+
* itself and a PRIORITY of the wrong length. A request whose header list is
|
|
90
|
+
* longer than `maxHeaderListSize` is answered with a 431 and never reaches the
|
|
91
|
+
* program (§10.5.1).
|
|
69
92
|
*
|
|
70
93
|
* **Caps are start-up numbers**, all in `Http2Config`: the streams open at
|
|
71
94
|
* once, the windows, the largest frame and header block accepted, the header
|
|
72
|
-
* list, the output buffer and the reset budget. Every stream slot is
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
95
|
+
* list, the output buffer and the reset budget. Every stream slot is allocated
|
|
96
|
+
* by the constructor; the input, output and header-block buffers are borrowed
|
|
97
|
+
* from an `Http2Pools` when first needed and given back, zeroed as far as they
|
|
98
|
+
* were written, once each is empty or the connection ends, so an idle
|
|
99
|
+
* connection holds none.
|
|
100
|
+
*
|
|
101
|
+
* A server shares one `Http2Pools` among its slots; a connection on its own
|
|
102
|
+
* makes a private one that holds one buffer of each. Once the pools are warm,
|
|
103
|
+
* reading DATA, writing DATA and every control frame allocate nothing, and
|
|
104
|
+
* neither does a header block once the decoder's arrays have grown to the
|
|
105
|
+
* largest block and header list seen: it decodes into storage the connection
|
|
106
|
+
* owns, bounded by `maxHeaderBlock` and `maxHeaderListSize` whether the stream
|
|
107
|
+
* is opened, refused or reset (`docs/security/http2.md`, H2-1).
|
|
84
108
|
*
|
|
85
109
|
* The server sends no PUSH_PROMISE and no PRIORITY, and reads PRIORITY only to
|
|
86
110
|
* check it: RFC 9113 §5.3.2 deprecates the scheme. Every field it sends is a
|
|
@@ -253,7 +277,11 @@ export class Http2Config {
|
|
|
253
277
|
maxHeaderListSize: i32 = 16384
|
|
254
278
|
/** The largest header block, compressed, across HEADERS and CONTINUATION. */
|
|
255
279
|
maxHeaderBlock: i32 = 16384
|
|
256
|
-
/**
|
|
280
|
+
/**
|
|
281
|
+
* SETTINGS_HEADER_TABLE_SIZE, in bytes: the largest HPACK dynamic table the peer's header blocks may
|
|
282
|
+
* make the decoder keep. It is in force once the peer acknowledges the server's SETTINGS; until then
|
|
283
|
+
* the table is 4,096 bytes, RFC 7541's default. A size update past it is a COMPRESSION_ERROR.
|
|
284
|
+
*/
|
|
257
285
|
headerTableSize: i32 = 4096
|
|
258
286
|
/** SETTINGS_ENABLE_CONNECT_PROTOCOL (RFC 8441): whether `:protocol` is accepted. */
|
|
259
287
|
enableConnectProtocol: boolean = false
|
|
@@ -281,19 +309,34 @@ export class Http2Pools {
|
|
|
281
309
|
/** `maxHeaderBlock`. */
|
|
282
310
|
block: BufferPool
|
|
283
311
|
|
|
284
|
-
/**
|
|
312
|
+
/**
|
|
313
|
+
* Pools of at most `ceiling` buffers each, for connections under `config`. Allocates no buffer.
|
|
314
|
+
*
|
|
315
|
+
* @param name prefixes each pool's own name, as `name.input`, `name.output` and `name.block`
|
|
316
|
+
* @param config sizes the buffers: `maxFrameSize` plus a frame header, `outputSize` and `maxHeaderBlock`
|
|
317
|
+
* bytes; read here only
|
|
318
|
+
* @param ceiling how many buffers of each kind may be lent at once
|
|
319
|
+
*/
|
|
285
320
|
constructor(name: string, config: Http2Config, ceiling: i32) {
|
|
286
321
|
this.input = new BufferPool(`${name}.input`, config.maxFrameSize + H2_FRAME_HEADER_SIZE, ceiling)
|
|
287
322
|
this.output = new BufferPool(`${name}.output`, config.outputSize, ceiling)
|
|
288
323
|
this.block = new BufferPool(`${name}.block`, config.maxHeaderBlock, ceiling)
|
|
289
324
|
}
|
|
290
325
|
|
|
291
|
-
/**
|
|
326
|
+
/**
|
|
327
|
+
* How many buffers of the three kinds are lent now.
|
|
328
|
+
*
|
|
329
|
+
* @returns the buffers lent and not given back yet, of all three pools together
|
|
330
|
+
*/
|
|
292
331
|
lent(): i32 {
|
|
293
332
|
return this.input.lent() + this.output.lent() + this.block.lent()
|
|
294
333
|
}
|
|
295
334
|
|
|
296
|
-
/**
|
|
335
|
+
/**
|
|
336
|
+
* How many buffers of the three kinds have ever been made.
|
|
337
|
+
*
|
|
338
|
+
* @returns the buffers the three pools have allocated, together; at most three times the ceiling
|
|
339
|
+
*/
|
|
297
340
|
made(): i32 {
|
|
298
341
|
return this.input.made + this.output.made + this.block.made
|
|
299
342
|
}
|
|
@@ -309,8 +352,12 @@ export class Http2Stream {
|
|
|
309
352
|
contentLength: i64 = -1
|
|
310
353
|
/** The body bytes received. */
|
|
311
354
|
received: i64 = 0
|
|
355
|
+
/** The stream's identifier, odd since a client opened it, or 0 while the slot is free. */
|
|
312
356
|
id: i32 = 0
|
|
313
|
-
/**
|
|
357
|
+
/**
|
|
358
|
+
* What the program has been handed since the last WINDOW_UPDATE for it.
|
|
359
|
+
* @internal Read by `Http2Connection` to credit the stream's window; not for callers.
|
|
360
|
+
*/
|
|
314
361
|
recvUnacked: i32 = 0
|
|
315
362
|
/** The peer sent END_STREAM: half-closed (remote). */
|
|
316
363
|
remoteEnded: boolean = false
|
|
@@ -318,7 +365,10 @@ export class Http2Stream {
|
|
|
318
365
|
localEnded: boolean = false
|
|
319
366
|
/** A final (non-1xx) response head was sent. */
|
|
320
367
|
responded: boolean = false
|
|
321
|
-
/**
|
|
368
|
+
/**
|
|
369
|
+
* `writeData` was held back by this stream's window.
|
|
370
|
+
* @internal Read by `Http2Connection` to answer H2_WINDOW; not for callers.
|
|
371
|
+
*/
|
|
322
372
|
blocked: boolean = false
|
|
323
373
|
}
|
|
324
374
|
|
|
@@ -388,12 +438,13 @@ const http2CheckCap = (what: string, value: i32, low: i32, high: i32): void => {
|
|
|
388
438
|
export class Http2Connection {
|
|
389
439
|
// Pointers and 64-bit fields first, then 32-bit ones, then flags: the
|
|
390
440
|
// order the layout packs without padding.
|
|
391
|
-
config: Http2Config
|
|
392
|
-
frame: Http2Frame
|
|
441
|
+
private config: Http2Config
|
|
442
|
+
private frame: Http2Frame
|
|
393
443
|
/**
|
|
394
|
-
* Where
|
|
395
|
-
*
|
|
396
|
-
*
|
|
444
|
+
* Where the connection borrows its input, its output and the header block it gathers: the pools
|
|
445
|
+
* `Http2TlsServer` shares among its slots, or, while this is `null` when a buffer is first needed,
|
|
446
|
+
* pools of one of each the connection makes itself. While the connection holds no input or no
|
|
447
|
+
* output buffer, `input` or `output` is an empty array.
|
|
397
448
|
*/
|
|
398
449
|
pools: Http2Pools | null = null
|
|
399
450
|
/** Bytes fed and not read yet are `input[inputStart .. inputEnd)`. */
|
|
@@ -401,10 +452,12 @@ export class Http2Connection {
|
|
|
401
452
|
/** Bytes to send are `output[outputStart .. outputEnd)`. */
|
|
402
453
|
output: u8[]
|
|
403
454
|
/** The header block being gathered across CONTINUATION frames. */
|
|
404
|
-
block: u8[]
|
|
455
|
+
private block: u8[]
|
|
405
456
|
/** Where a response's header block is encoded, emptied and reused. */
|
|
406
|
-
encoded: u8[]
|
|
457
|
+
private encoded: u8[]
|
|
458
|
+
/** The HPACK decoder for the peer's header blocks; `restart` empties its table. */
|
|
407
459
|
decoder: HpackDecoder
|
|
460
|
+
/** The HPACK encoder for responses; every field is a literal without indexing, so its table stays empty. */
|
|
408
461
|
encoder: HpackEncoder
|
|
409
462
|
/**
|
|
410
463
|
* The last request's or trailers' fields, valid until the next header block
|
|
@@ -412,14 +465,14 @@ export class Http2Connection {
|
|
|
412
465
|
* their arrays, so a program copies what it keeps (`docs/security/http2.md`, H2-1).
|
|
413
466
|
*/
|
|
414
467
|
fields: HttpFields
|
|
415
|
-
streams: Http2Stream[]
|
|
468
|
+
private streams: Http2Stream[]
|
|
416
469
|
/** The identifiers the server reset most recently, a ring. */
|
|
417
|
-
resets: i32[]
|
|
470
|
+
private resets: i32[]
|
|
418
471
|
/** The identifiers of the streams closed most recently, however they closed, a ring. */
|
|
419
|
-
closed: i32[]
|
|
472
|
+
private closed: i32[]
|
|
420
473
|
/** The bytes of `:status` and of a three-digit status, reused for every response. */
|
|
421
|
-
statusName: u8[]
|
|
422
|
-
statusValue: u8[]
|
|
474
|
+
private statusName: u8[]
|
|
475
|
+
private statusValue: u8[]
|
|
423
476
|
/**
|
|
424
477
|
* The `date` every final response carries (RFC 9110 §6.6.1), as the 29
|
|
425
478
|
* bytes of an IMF-fixdate, or empty for none. A bare connection has no
|
|
@@ -428,43 +481,51 @@ export class Http2Connection {
|
|
|
428
481
|
*/
|
|
429
482
|
date: u8[]
|
|
430
483
|
/** The bytes of `date`, the field's name, made once. */
|
|
431
|
-
dateName: u8[]
|
|
484
|
+
private dateName: u8[]
|
|
432
485
|
/**
|
|
433
486
|
* One empty array, made once: GOAWAY's debug data, which the server never
|
|
434
487
|
* sends, and `input`, `output` and `block` while the connection holds none.
|
|
435
488
|
*/
|
|
436
|
-
empty: u8[]
|
|
489
|
+
private empty: u8[]
|
|
437
490
|
/** The server's SETTINGS, made once and sent by every `restart`. */
|
|
438
|
-
settingIds: i32[]
|
|
439
|
-
settingValues: i64[]
|
|
491
|
+
private settingIds: i32[]
|
|
492
|
+
private settingValues: i64[]
|
|
440
493
|
/** The peer's SETTINGS_INITIAL_WINDOW_SIZE. */
|
|
441
|
-
peerInitialWindow: i64 = 65535
|
|
442
|
-
/**
|
|
494
|
+
private peerInitialWindow: i64 = 65535
|
|
495
|
+
/**
|
|
496
|
+
* The peer's SETTINGS_MAX_HEADER_LIST_SIZE, advisory; -1 until it sends one.
|
|
497
|
+
* @internal Unused: written from the peer's SETTINGS, and nothing reads it; tsc's noUnusedLocals
|
|
498
|
+
* refuses it as private.
|
|
499
|
+
*/
|
|
443
500
|
peerMaxHeaderList: i64 = -1
|
|
444
501
|
/** The window a new stream starts with on the receiving side. */
|
|
445
|
-
localInitialWindow: i64 = 65535
|
|
502
|
+
private localInitialWindow: i64 = 65535
|
|
446
503
|
/** The connection's send window. */
|
|
447
|
-
sendWindow: i64 = 65535
|
|
504
|
+
private sendWindow: i64 = 65535
|
|
448
505
|
/** The connection's receive window, and what has been handed on since it was last credited. */
|
|
449
|
-
recvWindow: i64 = 65535
|
|
450
|
-
recvUnacked: i64 = 0
|
|
506
|
+
private recvWindow: i64 = 65535
|
|
507
|
+
private recvUnacked: i64 = 0
|
|
451
508
|
/** H2_DATA's bytes: the input buffer, valid until the next `feed`. */
|
|
452
509
|
data: u8[]
|
|
453
510
|
/** H2_RESET's, H2_GOAWAY's and H2_ERROR's code. */
|
|
454
511
|
errorCode: i64 = 0
|
|
512
|
+
/** Where the bytes fed and not read yet start in `input`. */
|
|
455
513
|
inputStart: i32 = 0
|
|
514
|
+
/** Where the bytes fed and not read yet end in `input`. */
|
|
456
515
|
inputEnd: i32 = 0
|
|
516
|
+
/** Where the bytes to send start in `output`; `consume` moves it. */
|
|
457
517
|
outputStart: i32 = 0
|
|
518
|
+
/** Where the bytes to send end in `output`. */
|
|
458
519
|
outputEnd: i32 = 0
|
|
459
|
-
blockLength: i32 = 0
|
|
520
|
+
private blockLength: i32 = 0
|
|
460
521
|
/** The stream the open header block is on, or 0 when none is open. */
|
|
461
|
-
blockStream: i32 = 0
|
|
522
|
+
private blockStream: i32 = 0
|
|
462
523
|
/** A stream error the HEADERS that opened the block carried. */
|
|
463
|
-
blockError: i32 = 0
|
|
464
|
-
blockFragments: i32 = 0
|
|
465
|
-
resetAt: i32 = 0
|
|
466
|
-
closedAt: i32 = 0
|
|
467
|
-
state: i32 = 0
|
|
524
|
+
private blockError: i32 = 0
|
|
525
|
+
private blockFragments: i32 = 0
|
|
526
|
+
private resetAt: i32 = 0
|
|
527
|
+
private closedAt: i32 = 0
|
|
528
|
+
private state: i32 = 0
|
|
468
529
|
/** The highest stream identifier the peer has opened. */
|
|
469
530
|
lastPeerStream: i32 = 0
|
|
470
531
|
/** Streams with a slot. */
|
|
@@ -472,35 +533,40 @@ export class Http2Connection {
|
|
|
472
533
|
/** The peer's SETTINGS_MAX_FRAME_SIZE. */
|
|
473
534
|
peerMaxFrameSize: i32 = 16384
|
|
474
535
|
/** What is left of `config.resetBudget`. */
|
|
475
|
-
resetCredit: i32 = 0
|
|
536
|
+
private resetCredit: i32 = 0
|
|
476
537
|
/** The last event's stream. */
|
|
477
538
|
stream: i32 = 0
|
|
478
539
|
/** H2_DATA's window onto `data`. */
|
|
479
540
|
dataStart: i32 = 0
|
|
541
|
+
/** How many bytes of `data`, from `dataStart`, H2_DATA hands the program; 0 for an empty DATA. */
|
|
480
542
|
dataLength: i32 = 0
|
|
481
543
|
/** H2_GOAWAY's last stream identifier. */
|
|
482
544
|
lastStreamId: i32 = 0
|
|
483
545
|
/** The last stream identifier the server's first GOAWAY named, which no later one may raise (§6.8). */
|
|
484
|
-
sentLast: i32 = 0
|
|
485
|
-
/** How far into `input
|
|
546
|
+
private sentLast: i32 = 0
|
|
547
|
+
/** How far into `input` the connection has written since it borrowed it: what giving it back zeroes. */
|
|
486
548
|
inputUsed: i32 = 0
|
|
487
|
-
outputUsed: i32 = 0
|
|
488
|
-
blockUsed: i32 = 0
|
|
489
|
-
blockEndStream: boolean = false
|
|
549
|
+
private outputUsed: i32 = 0
|
|
550
|
+
private blockUsed: i32 = 0
|
|
551
|
+
private blockEndStream: boolean = false
|
|
490
552
|
/** Whether the peer has acknowledged the server's SETTINGS. */
|
|
491
553
|
settingsAcked: boolean = false
|
|
492
554
|
/** `writeData` was held back by the connection's window. */
|
|
493
|
-
sendBlocked: boolean = false
|
|
555
|
+
private sendBlocked: boolean = false
|
|
556
|
+
/** Whether a GOAWAY from the server is in the output: by `goaway`, `sendGoaway` or a connection error. */
|
|
494
557
|
goawaySent: boolean = false
|
|
495
|
-
goawayReceived: boolean = false
|
|
496
|
-
/**
|
|
558
|
+
private goawayReceived: boolean = false
|
|
559
|
+
/**
|
|
560
|
+
* The peer's byte stream has ended (`endInput`).
|
|
561
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
562
|
+
*/
|
|
497
563
|
inputEnded: boolean = false
|
|
498
564
|
/** Whether the last H2_REQUEST, H2_DATA or H2_TRAILERS ended its stream. */
|
|
499
565
|
endStream: boolean = false
|
|
500
566
|
/** Whether H2_RESET came from the peer's RST_STREAM rather than a rule its stream broke. */
|
|
501
567
|
resetByPeer: boolean = false
|
|
502
568
|
/** The server's SETTINGS are not written yet: the first `holdOutput` writes them. */
|
|
503
|
-
prefaceOwed: boolean = false
|
|
569
|
+
private prefaceOwed: boolean = false
|
|
504
570
|
|
|
505
571
|
/**
|
|
506
572
|
* A connection under `config`, owing its SETTINGS — and the WINDOW_UPDATE
|
|
@@ -510,6 +576,9 @@ export class Http2Connection {
|
|
|
510
576
|
* buffer until then, so `output` is empty after the constructor and after
|
|
511
577
|
* `restart`: a caller that reads `output` itself calls `wantsWrite` first.
|
|
512
578
|
* A cap outside what RFC 9113 allows panics.
|
|
579
|
+
*
|
|
580
|
+
* @param config the caps, checked here and kept, not copied: the connection reads it for as long as
|
|
581
|
+
* it lives, so a program does not change it afterwards
|
|
513
582
|
*/
|
|
514
583
|
constructor(config: Http2Config) {
|
|
515
584
|
http2CheckCap("maxStreams", config.maxStreams, 1, 65536)
|
|
@@ -622,7 +691,7 @@ export class Http2Connection {
|
|
|
622
691
|
// ---- Borrowed buffers ---------------------------------------------------------
|
|
623
692
|
|
|
624
693
|
/** The pools the connection borrows from, made at ceiling 1 the first time a bare connection asks. */
|
|
625
|
-
lender(): Http2Pools {
|
|
694
|
+
private lender(): Http2Pools {
|
|
626
695
|
const pools: Http2Pools | null = this.pools
|
|
627
696
|
if (pools !== null) {
|
|
628
697
|
return pools
|
|
@@ -633,7 +702,7 @@ export class Http2Connection {
|
|
|
633
702
|
}
|
|
634
703
|
|
|
635
704
|
/** Borrows `input` unless the connection holds it. */
|
|
636
|
-
holdInput(): void {
|
|
705
|
+
private holdInput(): void {
|
|
637
706
|
if (this.input.length === 0) {
|
|
638
707
|
this.input = this.lender().input.take()
|
|
639
708
|
this.inputUsed = 0
|
|
@@ -641,7 +710,7 @@ export class Http2Connection {
|
|
|
641
710
|
}
|
|
642
711
|
|
|
643
712
|
/** Borrows `output` unless the connection holds it, and writes the SETTINGS it owes. */
|
|
644
|
-
holdOutput(): void {
|
|
713
|
+
private holdOutput(): void {
|
|
645
714
|
if (this.output.length === 0) {
|
|
646
715
|
this.output = this.lender().output.take()
|
|
647
716
|
this.outputUsed = 0
|
|
@@ -657,7 +726,7 @@ export class Http2Connection {
|
|
|
657
726
|
}
|
|
658
727
|
|
|
659
728
|
/** Borrows `block` unless the connection holds it. */
|
|
660
|
-
holdBlock(): void {
|
|
729
|
+
private holdBlock(): void {
|
|
661
730
|
if (this.block.length === 0) {
|
|
662
731
|
this.block = this.lender().block.take()
|
|
663
732
|
this.blockUsed = 0
|
|
@@ -665,7 +734,7 @@ export class Http2Connection {
|
|
|
665
734
|
}
|
|
666
735
|
|
|
667
736
|
/** Gives `input` back, zeroing what was written into it; ends the last H2_DATA's window. */
|
|
668
|
-
giveInput(): void {
|
|
737
|
+
private giveInput(): void {
|
|
669
738
|
if (this.input.length > 0) {
|
|
670
739
|
this.lender().input.give(this.input, this.inputUsed)
|
|
671
740
|
this.input = this.empty
|
|
@@ -679,7 +748,7 @@ export class Http2Connection {
|
|
|
679
748
|
}
|
|
680
749
|
|
|
681
750
|
/** Gives `output` back, zeroing what was written into it, sent or not. */
|
|
682
|
-
giveOutput(): void {
|
|
751
|
+
private giveOutput(): void {
|
|
683
752
|
if (this.output.length > 0) {
|
|
684
753
|
this.lender().output.give(this.output, this.outputUsed)
|
|
685
754
|
this.output = this.empty
|
|
@@ -690,7 +759,7 @@ export class Http2Connection {
|
|
|
690
759
|
}
|
|
691
760
|
|
|
692
761
|
/** Gives `block` back, zeroing what was gathered into it. */
|
|
693
|
-
giveBlock(): void {
|
|
762
|
+
private giveBlock(): void {
|
|
694
763
|
if (this.block.length > 0) {
|
|
695
764
|
this.lender().block.give(this.block, this.blockUsed)
|
|
696
765
|
this.block = this.empty
|
|
@@ -734,7 +803,11 @@ export class Http2Connection {
|
|
|
734
803
|
|
|
735
804
|
// ---- Bytes in and out ---------------------------------------------------------
|
|
736
805
|
|
|
737
|
-
/**
|
|
806
|
+
/**
|
|
807
|
+
* The room `feed` would find in the input buffer, counting the space its move to the front would free.
|
|
808
|
+
* @returns Bytes `feed` could take now, 0 to `maxFrameSize` plus the 9-byte frame header.
|
|
809
|
+
* @internal Unused: nothing calls it, and tsc's noUnusedLocals refuses a private member nothing reads.
|
|
810
|
+
*/
|
|
738
811
|
inputRoom(): i32 {
|
|
739
812
|
return this.config.maxFrameSize + H2_FRAME_HEADER_SIZE - (this.inputEnd - this.inputStart)
|
|
740
813
|
}
|
|
@@ -745,6 +818,12 @@ export class Http2Connection {
|
|
|
745
818
|
* has read what is there. Moves the unread bytes to the front of the
|
|
746
819
|
* buffer, which ends the last H2_DATA's window. Once the connection has
|
|
747
820
|
* failed it takes everything and keeps nothing.
|
|
821
|
+
*
|
|
822
|
+
* @param buf the bytes the peer sent; copied, so the caller may reuse it once `feed` answers
|
|
823
|
+
* @param off where they start in `buf`
|
|
824
|
+
* @param len how many there are; a window outside `buf` panics
|
|
825
|
+
* @returns how many bytes it took, from 0 to `len`: at most the room left in the input buffer, which
|
|
826
|
+
* holds one frame of `maxFrameSize` and its header; `len` once the connection has failed
|
|
748
827
|
*/
|
|
749
828
|
feed(buf: u8[], off: i32, len: i32): i32 {
|
|
750
829
|
http2CheckWindow("Http2Connection.feed", buf, off, len)
|
|
@@ -765,6 +844,7 @@ export class Http2Connection {
|
|
|
765
844
|
* Moves the unread input to the front of its buffer, which ends the last
|
|
766
845
|
* H2_DATA's window, and answers the room after it: where a carrier may read
|
|
767
846
|
* straight into `input` from `inputEnd`, then `received` what it read.
|
|
847
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
768
848
|
*/
|
|
769
849
|
inputTail(): i32 {
|
|
770
850
|
this.holdInput()
|
|
@@ -773,7 +853,10 @@ export class Http2Connection {
|
|
|
773
853
|
return toI32(this.input.length) - this.inputEnd
|
|
774
854
|
}
|
|
775
855
|
|
|
776
|
-
/**
|
|
856
|
+
/**
|
|
857
|
+
* Takes `n` bytes a carrier read into `input` at `inputEnd`, after `inputTail` gave it the room.
|
|
858
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
859
|
+
*/
|
|
777
860
|
received(n: i32): void {
|
|
778
861
|
const room: i32 = toI32(this.input.length) - this.inputEnd
|
|
779
862
|
if (n < 0 || n > room) {
|
|
@@ -786,7 +869,11 @@ export class Http2Connection {
|
|
|
786
869
|
}
|
|
787
870
|
}
|
|
788
871
|
|
|
789
|
-
/**
|
|
872
|
+
/**
|
|
873
|
+
* Marks the first `n` bytes of the output sent; once all of it is, the output goes back to its pool.
|
|
874
|
+
*
|
|
875
|
+
* @param n how many bytes of `output[outputStart .. outputEnd)` were sent; more than that counts as all
|
|
876
|
+
*/
|
|
790
877
|
consume(n: i32): void {
|
|
791
878
|
const pending: i32 = this.outputEnd - this.outputStart
|
|
792
879
|
this.outputStart = this.outputStart + (n < pending ? n : pending)
|
|
@@ -795,7 +882,11 @@ export class Http2Connection {
|
|
|
795
882
|
}
|
|
796
883
|
}
|
|
797
884
|
|
|
798
|
-
/**
|
|
885
|
+
/**
|
|
886
|
+
* Whether the output holds bytes to send; the SETTINGS a fresh connection owes are written first.
|
|
887
|
+
*
|
|
888
|
+
* @returns true when `output[outputStart .. outputEnd)` is not empty
|
|
889
|
+
*/
|
|
799
890
|
wantsWrite(): boolean {
|
|
800
891
|
if (this.prefaceOwed) {
|
|
801
892
|
this.holdOutput()
|
|
@@ -807,6 +898,8 @@ export class Http2Connection {
|
|
|
807
898
|
* Whether the connection is over: everything is sent, and it failed, or no
|
|
808
899
|
* stream is left after a GOAWAY went either way or after the peer's byte
|
|
809
900
|
* stream ended (`endInput`) with nothing of it left unread.
|
|
901
|
+
*
|
|
902
|
+
* @returns true when the carrier may close the connection; false while anything is left to send
|
|
810
903
|
*/
|
|
811
904
|
isDone(): boolean {
|
|
812
905
|
if (this.wantsWrite()) {
|
|
@@ -820,13 +913,14 @@ export class Http2Connection {
|
|
|
820
913
|
/**
|
|
821
914
|
* The peer's byte stream has ended, so nothing more will be fed: once the
|
|
822
915
|
* requests it already sent are answered, the connection is done.
|
|
916
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
823
917
|
*/
|
|
824
918
|
endInput(): void {
|
|
825
919
|
this.inputEnded = true
|
|
826
920
|
}
|
|
827
921
|
|
|
828
922
|
/** The room left at the end of the output, once what was sent is moved out of the way. */
|
|
829
|
-
room(): i32 {
|
|
923
|
+
private room(): i32 {
|
|
830
924
|
this.holdOutput()
|
|
831
925
|
this.outputEnd = http2Compact(this.output, this.outputStart, this.outputEnd)
|
|
832
926
|
this.outputStart = 0
|
|
@@ -834,13 +928,13 @@ export class Http2Connection {
|
|
|
834
928
|
}
|
|
835
929
|
|
|
836
930
|
/** The room DATA and HEADERS may use: all but the control reserve. */
|
|
837
|
-
bodyRoom(): i32 {
|
|
931
|
+
private bodyRoom(): i32 {
|
|
838
932
|
const r: i32 = this.room() - H2_CONTROL_RESERVE
|
|
839
933
|
return r > 0 ? r : 0
|
|
840
934
|
}
|
|
841
935
|
|
|
842
936
|
/** Appends a frame a writer put at the end of the output, when it fit (`end >= 0`), and answers whether it did. */
|
|
843
|
-
wrote(end: i32): boolean {
|
|
937
|
+
private wrote(end: i32): boolean {
|
|
844
938
|
if (end >= 0) {
|
|
845
939
|
this.outputEnd = end
|
|
846
940
|
this.outputUsed = end > this.outputUsed ? end : this.outputUsed
|
|
@@ -850,7 +944,13 @@ export class Http2Connection {
|
|
|
850
944
|
|
|
851
945
|
// ---- Streams ------------------------------------------------------------------
|
|
852
946
|
|
|
853
|
-
/**
|
|
947
|
+
/**
|
|
948
|
+
* The slot holding stream `id`, or `null`.
|
|
949
|
+
*
|
|
950
|
+
* @param id the stream identifier
|
|
951
|
+
* @returns the stream's slot, which stays the connection's and holds another stream once this one
|
|
952
|
+
* closes; `null` when no open stream has that identifier, and for an `id` of 0 or less
|
|
953
|
+
*/
|
|
854
954
|
find(id: i32): Http2Stream | null {
|
|
855
955
|
if (id <= 0) {
|
|
856
956
|
return null
|
|
@@ -864,7 +964,7 @@ export class Http2Connection {
|
|
|
864
964
|
}
|
|
865
965
|
|
|
866
966
|
/** Opens stream `id` in a free slot; the caller has checked there is one. */
|
|
867
|
-
open(id: i32): Http2Stream {
|
|
967
|
+
private open(id: i32): Http2Stream {
|
|
868
968
|
for (const s of this.streams) {
|
|
869
969
|
if (s.id === 0) {
|
|
870
970
|
s.id = id
|
|
@@ -885,7 +985,7 @@ export class Http2Connection {
|
|
|
885
985
|
}
|
|
886
986
|
|
|
887
987
|
/** Frees the slot of a stream that is closed. */
|
|
888
|
-
release(s: Http2Stream): void {
|
|
988
|
+
private release(s: Http2Stream): void {
|
|
889
989
|
if (s.id !== 0) {
|
|
890
990
|
this.closedAt = http2Remember(this.closed, this.closedAt, s.id)
|
|
891
991
|
s.id = 0
|
|
@@ -894,7 +994,7 @@ export class Http2Connection {
|
|
|
894
994
|
}
|
|
895
995
|
|
|
896
996
|
/** The peer ended stream `s`; closes it when the server had too. */
|
|
897
|
-
remoteEnd(s: Http2Stream): void {
|
|
997
|
+
private remoteEnd(s: Http2Stream): void {
|
|
898
998
|
s.remoteEnded = true
|
|
899
999
|
if (s.localEnded) {
|
|
900
1000
|
this.release(s)
|
|
@@ -902,7 +1002,7 @@ export class Http2Connection {
|
|
|
902
1002
|
}
|
|
903
1003
|
|
|
904
1004
|
/** The server ended stream `s`; closes it when the peer had too, and credits the reset budget. */
|
|
905
|
-
localEnd(s: Http2Stream): void {
|
|
1005
|
+
private localEnd(s: Http2Stream): void {
|
|
906
1006
|
s.localEnded = true
|
|
907
1007
|
if (this.resetCredit < this.config.resetBudget) {
|
|
908
1008
|
this.resetCredit = this.resetCredit + 1
|
|
@@ -913,7 +1013,7 @@ export class Http2Connection {
|
|
|
913
1013
|
}
|
|
914
1014
|
|
|
915
1015
|
/** Whether the server reset stream `id` recently, so frames still in flight for it are ignored (§5.4.2). */
|
|
916
|
-
recentlyReset(id: i32): boolean {
|
|
1016
|
+
private recentlyReset(id: i32): boolean {
|
|
917
1017
|
return id > 0 && this.resets.indexOf(id) >= 0
|
|
918
1018
|
}
|
|
919
1019
|
|
|
@@ -923,7 +1023,7 @@ export class Http2Connection {
|
|
|
923
1023
|
* opened, and is the out-of-order identifier §5.1.1 calls PROTOCOL_ERROR
|
|
924
1024
|
* rather than a frame on a closed stream (STREAM_CLOSED, §5.1).
|
|
925
1025
|
*/
|
|
926
|
-
recentlyClosed(id: i32): boolean {
|
|
1026
|
+
private recentlyClosed(id: i32): boolean {
|
|
927
1027
|
return id > 0 && this.closed.indexOf(id) >= 0
|
|
928
1028
|
}
|
|
929
1029
|
|
|
@@ -931,7 +1031,7 @@ export class Http2Connection {
|
|
|
931
1031
|
* Sends RST_STREAM for `id` with `code`, frees its slot if it has one, and
|
|
932
1032
|
* remembers it; answers false, changing nothing, when the frame did not fit.
|
|
933
1033
|
*/
|
|
934
|
-
resetStream(id: i32, code: i32): boolean {
|
|
1034
|
+
private resetStream(id: i32, code: i32): boolean {
|
|
935
1035
|
if (!this.wrote(http2WriteRstStream(this.output, this.outputEnd, id, toI64(code)))) {
|
|
936
1036
|
return false
|
|
937
1037
|
}
|
|
@@ -949,19 +1049,19 @@ export class Http2Connection {
|
|
|
949
1049
|
* server into it (MadeYouReset, CVE-2025-8671). Answers H2_NEED_MORE, or
|
|
950
1050
|
* once the budget is spent the connection error ENHANCE_YOUR_CALM (§10.5).
|
|
951
1051
|
*/
|
|
952
|
-
chargeReset(): i32 {
|
|
1052
|
+
private chargeReset(): i32 {
|
|
953
1053
|
this.resetCredit = this.resetCredit - 1
|
|
954
1054
|
return this.resetCredit < 0 ? this.fail(H2_ENHANCE_YOUR_CALM) : H2_NEED_MORE
|
|
955
1055
|
}
|
|
956
1056
|
|
|
957
1057
|
/** A stream error the peer caused on `id`, which the program never saw: resets it and charges the budget. */
|
|
958
|
-
refuseStream(id: i32, code: i32): i32 {
|
|
1058
|
+
private refuseStream(id: i32, code: i32): i32 {
|
|
959
1059
|
this.resetStream(id, code)
|
|
960
1060
|
return this.chargeReset()
|
|
961
1061
|
}
|
|
962
1062
|
|
|
963
1063
|
/** A stream error on `s`: resets it, charges the budget, and answers the H2_RESET that tells the program. */
|
|
964
|
-
streamError(s: Http2Stream, code: i32): i32 {
|
|
1064
|
+
private streamError(s: Http2Stream, code: i32): i32 {
|
|
965
1065
|
const id: i32 = s.id
|
|
966
1066
|
const charged: i32 = this.refuseStream(id, code)
|
|
967
1067
|
if (charged !== H2_NEED_MORE) {
|
|
@@ -977,6 +1077,9 @@ export class Http2Connection {
|
|
|
977
1077
|
* Writes GOAWAY with `code`, naming the last stream the peer had opened when
|
|
978
1078
|
* the server's first GOAWAY went: no later one may name a higher stream
|
|
979
1079
|
* (§6.8). Answers whether it fit.
|
|
1080
|
+
*
|
|
1081
|
+
* @param code the error code the GOAWAY carries (`H2_NO_ERROR` for a graceful close)
|
|
1082
|
+
* @returns true when the frame was written to the output; false, changing nothing, when it did not fit
|
|
980
1083
|
*/
|
|
981
1084
|
sendGoaway(code: i32): boolean {
|
|
982
1085
|
const last: i32 = this.goawaySent ? this.sentLast : this.lastPeerStream
|
|
@@ -993,7 +1096,7 @@ export class Http2Connection {
|
|
|
993
1096
|
}
|
|
994
1097
|
|
|
995
1098
|
/** A connection error: queues GOAWAY with `code` and answers H2_ERROR. */
|
|
996
|
-
fail(code: i32): i32 {
|
|
1099
|
+
private fail(code: i32): i32 {
|
|
997
1100
|
if (this.state !== H2_STATE_FAILED) {
|
|
998
1101
|
this.state = H2_STATE_FAILED
|
|
999
1102
|
this.errorCode = toI64(code)
|
|
@@ -1010,7 +1113,7 @@ export class Http2Connection {
|
|
|
1010
1113
|
* when it is still open for receiving, with a WINDOW_UPDATE once half a
|
|
1011
1114
|
* window is owed.
|
|
1012
1115
|
*/
|
|
1013
|
-
credit(s: Http2Stream | null, n: i32): void {
|
|
1116
|
+
private credit(s: Http2Stream | null, n: i32): void {
|
|
1014
1117
|
this.recvUnacked = this.recvUnacked + toI64(n)
|
|
1015
1118
|
const half: i64 = toI64(this.config.connectionWindowSize / 2)
|
|
1016
1119
|
if (this.recvUnacked > toI64(0) && this.recvUnacked >= half) {
|
|
@@ -1031,7 +1134,14 @@ export class Http2Connection {
|
|
|
1031
1134
|
|
|
1032
1135
|
// ---- Reading ------------------------------------------------------------------
|
|
1033
1136
|
|
|
1034
|
-
/**
|
|
1137
|
+
/**
|
|
1138
|
+
* Reads frames until one is an event for the program, and answers it.
|
|
1139
|
+
*
|
|
1140
|
+
* @returns `H2_REQUEST`, `H2_DATA`, `H2_TRAILERS`, `H2_RESET`, `H2_WINDOW` or `H2_GOAWAY`, with the
|
|
1141
|
+
* fields the module comment names for each; `H2_NEED_MORE` when the fed bytes hold no whole frame
|
|
1142
|
+
* (or preface) or the output lacks room for what reading one may make the connection send;
|
|
1143
|
+
* `H2_ERROR` once the connection has failed, now or before
|
|
1144
|
+
*/
|
|
1035
1145
|
next(): i32 {
|
|
1036
1146
|
while (true) {
|
|
1037
1147
|
if (this.state === H2_STATE_FAILED) {
|
|
@@ -1075,7 +1185,7 @@ export class Http2Connection {
|
|
|
1075
1185
|
}
|
|
1076
1186
|
|
|
1077
1187
|
/** One frame that parsed: the rules no single frame type owns, then the type's own. */
|
|
1078
|
-
handleFrame(f: Http2Frame): i32 {
|
|
1188
|
+
private handleFrame(f: Http2Frame): i32 {
|
|
1079
1189
|
if (this.blockStream !== 0 && (f.type !== H2_FRAME_CONTINUATION || f.streamId !== this.blockStream)) {
|
|
1080
1190
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1081
1191
|
}
|
|
@@ -1118,7 +1228,7 @@ export class Http2Connection {
|
|
|
1118
1228
|
}
|
|
1119
1229
|
|
|
1120
1230
|
/** Whether `id` names an idle stream: one the peer has not opened yet. */
|
|
1121
|
-
isIdle(id: i32): boolean {
|
|
1231
|
+
private isIdle(id: i32): boolean {
|
|
1122
1232
|
// An even identifier is the server's to open (§5.1.1), and this server
|
|
1123
1233
|
// opens none, so every even stream is idle for as long as it lives.
|
|
1124
1234
|
return id % 2 === 0 || id > this.lastPeerStream
|
|
@@ -1129,12 +1239,12 @@ export class Http2Connection {
|
|
|
1129
1239
|
* word: the server reset it and these were in flight (§5.4.2), or it was
|
|
1130
1240
|
* opened after the server's GOAWAY and so never opened at all (§6.8).
|
|
1131
1241
|
*/
|
|
1132
|
-
discarded(id: i32): boolean {
|
|
1242
|
+
private discarded(id: i32): boolean {
|
|
1133
1243
|
return this.recentlyReset(id) || (this.goawaySent && id > this.sentLast)
|
|
1134
1244
|
}
|
|
1135
1245
|
|
|
1136
1246
|
/** DATA (§6.1): flow control first, on the whole payload, then the stream's state. */
|
|
1137
|
-
handleData(f: Http2Frame): i32 {
|
|
1247
|
+
private handleData(f: Http2Frame): i32 {
|
|
1138
1248
|
const id: i32 = f.streamId
|
|
1139
1249
|
const size: i32 = f.length
|
|
1140
1250
|
const s: Http2Stream | null = this.find(id)
|
|
@@ -1181,7 +1291,7 @@ export class Http2Connection {
|
|
|
1181
1291
|
}
|
|
1182
1292
|
|
|
1183
1293
|
/** Appends a fragment of a header block; a block past `maxHeaderBlock` or `H2_MAX_FRAGMENTS` ends the connection. */
|
|
1184
|
-
gather(f: Http2Frame): boolean {
|
|
1294
|
+
private gather(f: Http2Frame): boolean {
|
|
1185
1295
|
this.holdBlock()
|
|
1186
1296
|
this.blockFragments = this.blockFragments + 1
|
|
1187
1297
|
if (
|
|
@@ -1199,7 +1309,7 @@ export class Http2Connection {
|
|
|
1199
1309
|
}
|
|
1200
1310
|
|
|
1201
1311
|
/** HEADERS (§6.2): starts a header block, and ends it when END_HEADERS says so. */
|
|
1202
|
-
handleHeaders(f: Http2Frame): i32 {
|
|
1312
|
+
private handleHeaders(f: Http2Frame): i32 {
|
|
1203
1313
|
this.blockStream = f.streamId
|
|
1204
1314
|
this.blockEndStream = f.has(H2_FLAG_END_STREAM)
|
|
1205
1315
|
this.blockError = f.streamError
|
|
@@ -1209,7 +1319,7 @@ export class Http2Connection {
|
|
|
1209
1319
|
}
|
|
1210
1320
|
|
|
1211
1321
|
/** CONTINUATION (§6.10): only inside a header block, which `handleFrame` has checked it continues. */
|
|
1212
|
-
handleContinuation(f: Http2Frame): i32 {
|
|
1322
|
+
private handleContinuation(f: Http2Frame): i32 {
|
|
1213
1323
|
if (this.blockStream === 0) {
|
|
1214
1324
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1215
1325
|
}
|
|
@@ -1224,7 +1334,7 @@ export class Http2Connection {
|
|
|
1224
1334
|
* because the decoder's table must stay in step with the peer's encoder
|
|
1225
1335
|
* (§4.3); then a new stream's request, or an open stream's trailers.
|
|
1226
1336
|
*/
|
|
1227
|
-
endBlock(): i32 {
|
|
1337
|
+
private endBlock(): i32 {
|
|
1228
1338
|
const id: i32 = this.blockStream
|
|
1229
1339
|
this.blockStream = 0
|
|
1230
1340
|
// The decoder takes back the arrays the last request's fields point into,
|
|
@@ -1283,7 +1393,7 @@ export class Http2Connection {
|
|
|
1283
1393
|
}
|
|
1284
1394
|
|
|
1285
1395
|
/** A header block on an open stream: trailers, which must end it (§8.1). */
|
|
1286
|
-
trailers(s: Http2Stream, decoded: i32): i32 {
|
|
1396
|
+
private trailers(s: Http2Stream, decoded: i32): i32 {
|
|
1287
1397
|
if (s.remoteEnded) {
|
|
1288
1398
|
return this.streamError(s, H2_STREAM_CLOSED)
|
|
1289
1399
|
}
|
|
@@ -1309,7 +1419,7 @@ export class Http2Connection {
|
|
|
1309
1419
|
* END_STREAM, then a RST_STREAM with NO_ERROR when the peer is still
|
|
1310
1420
|
* sending (§8.1), so the stream never reaches the program.
|
|
1311
1421
|
*/
|
|
1312
|
-
tooLarge(id: i32): void {
|
|
1422
|
+
private tooLarge(id: i32): void {
|
|
1313
1423
|
// The control reserve holds this with its date: under fifty bytes.
|
|
1314
1424
|
this.encodeStatus(H2_STATUS_TOO_LARGE, true)
|
|
1315
1425
|
const n: i32 = toI32(this.encoded.length)
|
|
@@ -1337,7 +1447,7 @@ export class Http2Connection {
|
|
|
1337
1447
|
* `date` is the static table's name 33 and a value of 29 bytes, in Huffman
|
|
1338
1448
|
* when that is shorter.
|
|
1339
1449
|
*/
|
|
1340
|
-
encodeStatus(status: i32, withDate: boolean): void {
|
|
1450
|
+
private encodeStatus(status: i32, withDate: boolean): void {
|
|
1341
1451
|
while (toI32(this.encoded.length) > 0) {
|
|
1342
1452
|
this.encoded.pop()
|
|
1343
1453
|
}
|
|
@@ -1356,7 +1466,7 @@ export class Http2Connection {
|
|
|
1356
1466
|
}
|
|
1357
1467
|
|
|
1358
1468
|
/** PRIORITY (§6.3): checked and otherwise ignored, and it opens nothing. */
|
|
1359
|
-
handlePriority(f: Http2Frame): i32 {
|
|
1469
|
+
private handlePriority(f: Http2Frame): i32 {
|
|
1360
1470
|
if (f.streamError === 0) {
|
|
1361
1471
|
return H2_NEED_MORE
|
|
1362
1472
|
}
|
|
@@ -1368,7 +1478,7 @@ export class Http2Connection {
|
|
|
1368
1478
|
}
|
|
1369
1479
|
|
|
1370
1480
|
/** RST_STREAM (§6.4): the stream is closed; on an idle one it is a connection error. */
|
|
1371
|
-
handleRstStream(f: Http2Frame): i32 {
|
|
1481
|
+
private handleRstStream(f: Http2Frame): i32 {
|
|
1372
1482
|
if (this.isIdle(f.streamId)) {
|
|
1373
1483
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1374
1484
|
}
|
|
@@ -1393,7 +1503,7 @@ export class Http2Connection {
|
|
|
1393
1503
|
* acknowledged. A new SETTINGS_INITIAL_WINDOW_SIZE moves every stream's send
|
|
1394
1504
|
* window by the difference (§6.9.2), and opening one answers H2_WINDOW.
|
|
1395
1505
|
*/
|
|
1396
|
-
handleSettings(f: Http2Frame): i32 {
|
|
1506
|
+
private handleSettings(f: Http2Frame): i32 {
|
|
1397
1507
|
if (f.has(H2_FLAG_ACK)) {
|
|
1398
1508
|
if (!this.settingsAcked) {
|
|
1399
1509
|
this.settingsAcked = true
|
|
@@ -1452,7 +1562,7 @@ export class Http2Connection {
|
|
|
1452
1562
|
}
|
|
1453
1563
|
|
|
1454
1564
|
/** WINDOW_UPDATE (§6.9): a send window grows, and a write it held back may go on. */
|
|
1455
|
-
handleWindowUpdate(f: Http2Frame): i32 {
|
|
1565
|
+
private handleWindowUpdate(f: Http2Frame): i32 {
|
|
1456
1566
|
const inc: i64 = toI64(f.increment)
|
|
1457
1567
|
if (f.streamId === 0) {
|
|
1458
1568
|
this.sendWindow = this.sendWindow + inc
|
|
@@ -1488,7 +1598,7 @@ export class Http2Connection {
|
|
|
1488
1598
|
// ---- Writing ------------------------------------------------------------------
|
|
1489
1599
|
|
|
1490
1600
|
/** The stream the program may still send on, or `null`. */
|
|
1491
|
-
writable(id: i32): Http2Stream | null {
|
|
1601
|
+
private writable(id: i32): Http2Stream | null {
|
|
1492
1602
|
if (this.state === H2_STATE_FAILED) {
|
|
1493
1603
|
return null
|
|
1494
1604
|
}
|
|
@@ -1499,13 +1609,14 @@ export class Http2Connection {
|
|
|
1499
1609
|
/**
|
|
1500
1610
|
* Encodes `:status` and `names`/`values` and sends the block as HEADERS and
|
|
1501
1611
|
* as many CONTINUATION frames as the peer's SETTINGS_MAX_FRAME_SIZE needs,
|
|
1502
|
-
* with END_STREAM when `endStream
|
|
1503
|
-
* `httpFieldsCheckOutgoing` refuses or
|
|
1504
|
-
*
|
|
1612
|
+
* with END_STREAM when `endStream`; a `status` of 0 sends no `:status`.
|
|
1613
|
+
* Answers 0, `H2_INVALID` for fields `httpFieldsCheckOutgoing` refuses or
|
|
1614
|
+
* a block the output could never hold, or `H2_AGAIN` when the output has
|
|
1615
|
+
* no room for it yet.
|
|
1505
1616
|
* Nothing is encoded until it is sure to be sent, so the HPACK state never
|
|
1506
1617
|
* moves for a block that was not.
|
|
1507
1618
|
*/
|
|
1508
|
-
sendHead(s: Http2Stream, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1619
|
+
private sendHead(s: Http2Stream, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1509
1620
|
if (httpFieldsCheckOutgoing(names, values) !== HTTP_FIELDS_OK) {
|
|
1510
1621
|
return H2_INVALID
|
|
1511
1622
|
}
|
|
@@ -1566,7 +1677,19 @@ export class Http2Connection {
|
|
|
1566
1677
|
* Answers stream `id` with `status` and the fields `names[k]`: `values[k]`,
|
|
1567
1678
|
* lowercase and none of them connection-specific; END_STREAM when
|
|
1568
1679
|
* `endStream`. A 1xx status but 101 may come before the final one and may
|
|
1569
|
-
* not end the stream.
|
|
1680
|
+
* not end the stream. The block is encoded into the output before it answers.
|
|
1681
|
+
*
|
|
1682
|
+
* @param id the stream to answer
|
|
1683
|
+
* @param status the status code, 100 to 599 but not 101
|
|
1684
|
+
* @param names the field names, lowercase; `:status` goes before them, and on a final response the
|
|
1685
|
+
* connection's `date` unless one of them is `date`
|
|
1686
|
+
* @param values the value for each name, at the same index
|
|
1687
|
+
* @param endStream whether the response has no body, which a 1xx may not ask
|
|
1688
|
+
* @returns 0 when the head is in the output; `H2_CLOSED` when the stream has no slot, the server has
|
|
1689
|
+
* ended it, or the connection has failed; `H2_INVALID` for a status out of range or 101, a second
|
|
1690
|
+
* final response, a 1xx with `endStream`, fields `httpFieldsCheckOutgoing` refuses (names and values
|
|
1691
|
+
* of different lengths among them), or a head too large for the output buffer even when empty;
|
|
1692
|
+
* `H2_AGAIN` when the output has no room for it yet, nothing sent
|
|
1570
1693
|
*/
|
|
1571
1694
|
respond(id: i32, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1572
1695
|
const s: Http2Stream | null = this.writable(id)
|
|
@@ -1585,7 +1708,19 @@ export class Http2Connection {
|
|
|
1585
1708
|
return result
|
|
1586
1709
|
}
|
|
1587
1710
|
|
|
1588
|
-
/**
|
|
1711
|
+
/**
|
|
1712
|
+
* Ends stream `id` with a trailer section, after its response head (§8.1). The block carries no
|
|
1713
|
+
* `:status` and no `date`, and is encoded into the output before it answers.
|
|
1714
|
+
*
|
|
1715
|
+
* @param id the stream to end
|
|
1716
|
+
* @param names the trailer field names, lowercase
|
|
1717
|
+
* @param values the value for each name, at the same index
|
|
1718
|
+
* @returns 0 when the trailers are in the output and the stream is ended; `H2_CLOSED` when the stream
|
|
1719
|
+
* has no slot, the server has ended it, or the connection has failed; `H2_INVALID` before a final
|
|
1720
|
+
* response head, for fields `httpFieldsCheckOutgoing` refuses (names and values of different
|
|
1721
|
+
* lengths among them), or a block too large for the output buffer even when empty; `H2_AGAIN` when
|
|
1722
|
+
* the output has no room for it yet, nothing sent
|
|
1723
|
+
*/
|
|
1589
1724
|
writeTrailers(id: i32, names: u8[][], values: u8[][]): i32 {
|
|
1590
1725
|
const s: Http2Stream | null = this.writable(id)
|
|
1591
1726
|
if (s === null) {
|
|
@@ -1607,6 +1742,16 @@ export class Http2Connection {
|
|
|
1607
1742
|
* stream or the connection, or once the output has drained, it may be
|
|
1608
1743
|
* tried again — and `H2_CLOSED` for a stream with no final response head
|
|
1609
1744
|
* or that has ended.
|
|
1745
|
+
*
|
|
1746
|
+
* @param id the stream to send on
|
|
1747
|
+
* @param buf the body bytes; copied into the output, so the caller may reuse it once this answers
|
|
1748
|
+
* @param off where they start in `buf`
|
|
1749
|
+
* @param len how many to send; a window outside `buf` panics
|
|
1750
|
+
* @param endStream whether these bytes end the body
|
|
1751
|
+
* @returns how many bytes were taken, from 0 to `len` (0 only when `len` is 0); `H2_AGAIN` when `len`
|
|
1752
|
+
* is above 0 and none were taken, or when `len` is 0 with `endStream` and the empty DATA does not fit
|
|
1753
|
+
* yet; `H2_CLOSED` for a stream with no slot, no final response head, or that the server has ended,
|
|
1754
|
+
* and once the connection has failed
|
|
1610
1755
|
*/
|
|
1611
1756
|
writeData(id: i32, buf: u8[], off: i32, len: i32, endStream: boolean): i32 {
|
|
1612
1757
|
http2CheckWindow("Http2Connection.writeData", buf, off, len)
|
|
@@ -1665,6 +1810,12 @@ export class Http2Connection {
|
|
|
1665
1810
|
* Resets stream `id` with `code` (§6.4), from the program's side: CANCEL,
|
|
1666
1811
|
* say. Answers 0, `H2_AGAIN` when the output has no room for the frame yet
|
|
1667
1812
|
* (the stream stays open), or `H2_CLOSED`.
|
|
1813
|
+
*
|
|
1814
|
+
* @param id the stream to reset
|
|
1815
|
+
* @param code the error code the RST_STREAM carries, such as CANCEL (`H2_CANCEL` in `nish/net/http2-frame`)
|
|
1816
|
+
* @returns 0 when the RST_STREAM is in the output and the stream's slot is free; `H2_AGAIN` when it
|
|
1817
|
+
* did not fit, the stream left open; `H2_CLOSED` when no open stream has that identifier or the
|
|
1818
|
+
* connection has failed
|
|
1668
1819
|
*/
|
|
1669
1820
|
reset(id: i32, code: i32): i32 {
|
|
1670
1821
|
if (this.state === H2_STATE_FAILED || this.find(id) === null) {
|
|
@@ -1680,6 +1831,9 @@ export class Http2Connection {
|
|
|
1680
1831
|
* ignored; `isDone` once the last has closed and everything is sent.
|
|
1681
1832
|
* Answers 0, also when a GOAWAY was already sent or the connection has
|
|
1682
1833
|
* failed, or `H2_AGAIN` when the output has no room for the frame yet.
|
|
1834
|
+
*
|
|
1835
|
+
* @returns 0 when the GOAWAY is in the output, was already sent, or the connection has failed;
|
|
1836
|
+
* `H2_AGAIN` when the output has no room for it yet
|
|
1683
1837
|
*/
|
|
1684
1838
|
goaway(): i32 {
|
|
1685
1839
|
if (this.state === H2_STATE_FAILED || this.goawaySent) {
|