@amritk/nish-aarch64-linux 0.34.1 → 0.36.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 +22 -13
- package/bin/nish +0 -0
- package/docs/AI.md +86 -16
- package/package.json +1 -1
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/net/http2.ts
CHANGED
|
@@ -9,19 +9,30 @@
|
|
|
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
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
19
|
+
*
|
|
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
|
+
* };
|
|
25
36
|
*
|
|
26
37
|
* **Events.** `next()` reads the frames `feed` handed it and answers the
|
|
27
38
|
* first that means something to the program: `H2_REQUEST` (a stream opened
|
|
@@ -253,7 +264,11 @@ export class Http2Config {
|
|
|
253
264
|
maxHeaderListSize: i32 = 16384
|
|
254
265
|
/** The largest header block, compressed, across HEADERS and CONTINUATION. */
|
|
255
266
|
maxHeaderBlock: i32 = 16384
|
|
256
|
-
/**
|
|
267
|
+
/**
|
|
268
|
+
* SETTINGS_HEADER_TABLE_SIZE, in bytes: the largest HPACK dynamic table the peer's header blocks may
|
|
269
|
+
* make the decoder keep. It is in force once the peer acknowledges the server's SETTINGS; until then
|
|
270
|
+
* the table is 4,096 bytes, RFC 7541's default. A size update past it is a COMPRESSION_ERROR.
|
|
271
|
+
*/
|
|
257
272
|
headerTableSize: i32 = 4096
|
|
258
273
|
/** SETTINGS_ENABLE_CONNECT_PROTOCOL (RFC 8441): whether `:protocol` is accepted. */
|
|
259
274
|
enableConnectProtocol: boolean = false
|
|
@@ -281,19 +296,34 @@ export class Http2Pools {
|
|
|
281
296
|
/** `maxHeaderBlock`. */
|
|
282
297
|
block: BufferPool
|
|
283
298
|
|
|
284
|
-
/**
|
|
299
|
+
/**
|
|
300
|
+
* Pools of at most `ceiling` buffers each, for connections under `config`. Allocates no buffer.
|
|
301
|
+
*
|
|
302
|
+
* @param name prefixes each pool's own name, as `name.input`, `name.output` and `name.block`
|
|
303
|
+
* @param config sizes the buffers: `maxFrameSize` plus a frame header, `outputSize` and `maxHeaderBlock`
|
|
304
|
+
* bytes; read here only
|
|
305
|
+
* @param ceiling how many buffers of each kind may be lent at once
|
|
306
|
+
*/
|
|
285
307
|
constructor(name: string, config: Http2Config, ceiling: i32) {
|
|
286
308
|
this.input = new BufferPool(`${name}.input`, config.maxFrameSize + H2_FRAME_HEADER_SIZE, ceiling)
|
|
287
309
|
this.output = new BufferPool(`${name}.output`, config.outputSize, ceiling)
|
|
288
310
|
this.block = new BufferPool(`${name}.block`, config.maxHeaderBlock, ceiling)
|
|
289
311
|
}
|
|
290
312
|
|
|
291
|
-
/**
|
|
313
|
+
/**
|
|
314
|
+
* How many buffers of the three kinds are lent now.
|
|
315
|
+
*
|
|
316
|
+
* @returns the buffers lent and not given back yet, of all three pools together
|
|
317
|
+
*/
|
|
292
318
|
lent(): i32 {
|
|
293
319
|
return this.input.lent() + this.output.lent() + this.block.lent()
|
|
294
320
|
}
|
|
295
321
|
|
|
296
|
-
/**
|
|
322
|
+
/**
|
|
323
|
+
* How many buffers of the three kinds have ever been made.
|
|
324
|
+
*
|
|
325
|
+
* @returns the buffers the three pools have allocated, together; at most three times the ceiling
|
|
326
|
+
*/
|
|
297
327
|
made(): i32 {
|
|
298
328
|
return this.input.made + this.output.made + this.block.made
|
|
299
329
|
}
|
|
@@ -309,8 +339,12 @@ export class Http2Stream {
|
|
|
309
339
|
contentLength: i64 = -1
|
|
310
340
|
/** The body bytes received. */
|
|
311
341
|
received: i64 = 0
|
|
342
|
+
/** The stream's identifier, odd since a client opened it, or 0 while the slot is free. */
|
|
312
343
|
id: i32 = 0
|
|
313
|
-
/**
|
|
344
|
+
/**
|
|
345
|
+
* What the program has been handed since the last WINDOW_UPDATE for it.
|
|
346
|
+
* @internal Read by `Http2Connection` to credit the stream's window; not for callers.
|
|
347
|
+
*/
|
|
314
348
|
recvUnacked: i32 = 0
|
|
315
349
|
/** The peer sent END_STREAM: half-closed (remote). */
|
|
316
350
|
remoteEnded: boolean = false
|
|
@@ -318,7 +352,10 @@ export class Http2Stream {
|
|
|
318
352
|
localEnded: boolean = false
|
|
319
353
|
/** A final (non-1xx) response head was sent. */
|
|
320
354
|
responded: boolean = false
|
|
321
|
-
/**
|
|
355
|
+
/**
|
|
356
|
+
* `writeData` was held back by this stream's window.
|
|
357
|
+
* @internal Read by `Http2Connection` to answer H2_WINDOW; not for callers.
|
|
358
|
+
*/
|
|
322
359
|
blocked: boolean = false
|
|
323
360
|
}
|
|
324
361
|
|
|
@@ -388,12 +425,13 @@ const http2CheckCap = (what: string, value: i32, low: i32, high: i32): void => {
|
|
|
388
425
|
export class Http2Connection {
|
|
389
426
|
// Pointers and 64-bit fields first, then 32-bit ones, then flags: the
|
|
390
427
|
// order the layout packs without padding.
|
|
391
|
-
config: Http2Config
|
|
392
|
-
frame: Http2Frame
|
|
428
|
+
private config: Http2Config
|
|
429
|
+
private frame: Http2Frame
|
|
393
430
|
/**
|
|
394
|
-
* Where
|
|
395
|
-
*
|
|
396
|
-
*
|
|
431
|
+
* Where the connection borrows its input, its output and the header block it gathers: the pools
|
|
432
|
+
* `Http2TlsServer` shares among its slots, or, while this is `null` when a buffer is first needed,
|
|
433
|
+
* pools of one of each the connection makes itself. While the connection holds no input or no
|
|
434
|
+
* output buffer, `input` or `output` is an empty array.
|
|
397
435
|
*/
|
|
398
436
|
pools: Http2Pools | null = null
|
|
399
437
|
/** Bytes fed and not read yet are `input[inputStart .. inputEnd)`. */
|
|
@@ -401,10 +439,12 @@ export class Http2Connection {
|
|
|
401
439
|
/** Bytes to send are `output[outputStart .. outputEnd)`. */
|
|
402
440
|
output: u8[]
|
|
403
441
|
/** The header block being gathered across CONTINUATION frames. */
|
|
404
|
-
block: u8[]
|
|
442
|
+
private block: u8[]
|
|
405
443
|
/** Where a response's header block is encoded, emptied and reused. */
|
|
406
|
-
encoded: u8[]
|
|
444
|
+
private encoded: u8[]
|
|
445
|
+
/** The HPACK decoder for the peer's header blocks; `restart` empties its table. */
|
|
407
446
|
decoder: HpackDecoder
|
|
447
|
+
/** The HPACK encoder for responses; every field is a literal without indexing, so its table stays empty. */
|
|
408
448
|
encoder: HpackEncoder
|
|
409
449
|
/**
|
|
410
450
|
* The last request's or trailers' fields, valid until the next header block
|
|
@@ -412,14 +452,14 @@ export class Http2Connection {
|
|
|
412
452
|
* their arrays, so a program copies what it keeps (`docs/security/http2.md`, H2-1).
|
|
413
453
|
*/
|
|
414
454
|
fields: HttpFields
|
|
415
|
-
streams: Http2Stream[]
|
|
455
|
+
private streams: Http2Stream[]
|
|
416
456
|
/** The identifiers the server reset most recently, a ring. */
|
|
417
|
-
resets: i32[]
|
|
457
|
+
private resets: i32[]
|
|
418
458
|
/** The identifiers of the streams closed most recently, however they closed, a ring. */
|
|
419
|
-
closed: i32[]
|
|
459
|
+
private closed: i32[]
|
|
420
460
|
/** The bytes of `:status` and of a three-digit status, reused for every response. */
|
|
421
|
-
statusName: u8[]
|
|
422
|
-
statusValue: u8[]
|
|
461
|
+
private statusName: u8[]
|
|
462
|
+
private statusValue: u8[]
|
|
423
463
|
/**
|
|
424
464
|
* The `date` every final response carries (RFC 9110 §6.6.1), as the 29
|
|
425
465
|
* bytes of an IMF-fixdate, or empty for none. A bare connection has no
|
|
@@ -428,43 +468,51 @@ export class Http2Connection {
|
|
|
428
468
|
*/
|
|
429
469
|
date: u8[]
|
|
430
470
|
/** The bytes of `date`, the field's name, made once. */
|
|
431
|
-
dateName: u8[]
|
|
471
|
+
private dateName: u8[]
|
|
432
472
|
/**
|
|
433
473
|
* One empty array, made once: GOAWAY's debug data, which the server never
|
|
434
474
|
* sends, and `input`, `output` and `block` while the connection holds none.
|
|
435
475
|
*/
|
|
436
|
-
empty: u8[]
|
|
476
|
+
private empty: u8[]
|
|
437
477
|
/** The server's SETTINGS, made once and sent by every `restart`. */
|
|
438
|
-
settingIds: i32[]
|
|
439
|
-
settingValues: i64[]
|
|
478
|
+
private settingIds: i32[]
|
|
479
|
+
private settingValues: i64[]
|
|
440
480
|
/** The peer's SETTINGS_INITIAL_WINDOW_SIZE. */
|
|
441
|
-
peerInitialWindow: i64 = 65535
|
|
442
|
-
/**
|
|
481
|
+
private peerInitialWindow: i64 = 65535
|
|
482
|
+
/**
|
|
483
|
+
* The peer's SETTINGS_MAX_HEADER_LIST_SIZE, advisory; -1 until it sends one.
|
|
484
|
+
* @internal Unused: written from the peer's SETTINGS, and nothing reads it; tsc's noUnusedLocals
|
|
485
|
+
* refuses it as private.
|
|
486
|
+
*/
|
|
443
487
|
peerMaxHeaderList: i64 = -1
|
|
444
488
|
/** The window a new stream starts with on the receiving side. */
|
|
445
|
-
localInitialWindow: i64 = 65535
|
|
489
|
+
private localInitialWindow: i64 = 65535
|
|
446
490
|
/** The connection's send window. */
|
|
447
|
-
sendWindow: i64 = 65535
|
|
491
|
+
private sendWindow: i64 = 65535
|
|
448
492
|
/** The connection's receive window, and what has been handed on since it was last credited. */
|
|
449
|
-
recvWindow: i64 = 65535
|
|
450
|
-
recvUnacked: i64 = 0
|
|
493
|
+
private recvWindow: i64 = 65535
|
|
494
|
+
private recvUnacked: i64 = 0
|
|
451
495
|
/** H2_DATA's bytes: the input buffer, valid until the next `feed`. */
|
|
452
496
|
data: u8[]
|
|
453
497
|
/** H2_RESET's, H2_GOAWAY's and H2_ERROR's code. */
|
|
454
498
|
errorCode: i64 = 0
|
|
499
|
+
/** Where the bytes fed and not read yet start in `input`. */
|
|
455
500
|
inputStart: i32 = 0
|
|
501
|
+
/** Where the bytes fed and not read yet end in `input`. */
|
|
456
502
|
inputEnd: i32 = 0
|
|
503
|
+
/** Where the bytes to send start in `output`; `consume` moves it. */
|
|
457
504
|
outputStart: i32 = 0
|
|
505
|
+
/** Where the bytes to send end in `output`. */
|
|
458
506
|
outputEnd: i32 = 0
|
|
459
|
-
blockLength: i32 = 0
|
|
507
|
+
private blockLength: i32 = 0
|
|
460
508
|
/** The stream the open header block is on, or 0 when none is open. */
|
|
461
|
-
blockStream: i32 = 0
|
|
509
|
+
private blockStream: i32 = 0
|
|
462
510
|
/** 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
|
|
511
|
+
private blockError: i32 = 0
|
|
512
|
+
private blockFragments: i32 = 0
|
|
513
|
+
private resetAt: i32 = 0
|
|
514
|
+
private closedAt: i32 = 0
|
|
515
|
+
private state: i32 = 0
|
|
468
516
|
/** The highest stream identifier the peer has opened. */
|
|
469
517
|
lastPeerStream: i32 = 0
|
|
470
518
|
/** Streams with a slot. */
|
|
@@ -472,35 +520,40 @@ export class Http2Connection {
|
|
|
472
520
|
/** The peer's SETTINGS_MAX_FRAME_SIZE. */
|
|
473
521
|
peerMaxFrameSize: i32 = 16384
|
|
474
522
|
/** What is left of `config.resetBudget`. */
|
|
475
|
-
resetCredit: i32 = 0
|
|
523
|
+
private resetCredit: i32 = 0
|
|
476
524
|
/** The last event's stream. */
|
|
477
525
|
stream: i32 = 0
|
|
478
526
|
/** H2_DATA's window onto `data`. */
|
|
479
527
|
dataStart: i32 = 0
|
|
528
|
+
/** How many bytes of `data`, from `dataStart`, H2_DATA hands the program; 0 for an empty DATA. */
|
|
480
529
|
dataLength: i32 = 0
|
|
481
530
|
/** H2_GOAWAY's last stream identifier. */
|
|
482
531
|
lastStreamId: i32 = 0
|
|
483
532
|
/** 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
|
|
533
|
+
private sentLast: i32 = 0
|
|
534
|
+
/** How far into `input` the connection has written since it borrowed it: what giving it back zeroes. */
|
|
486
535
|
inputUsed: i32 = 0
|
|
487
|
-
outputUsed: i32 = 0
|
|
488
|
-
blockUsed: i32 = 0
|
|
489
|
-
blockEndStream: boolean = false
|
|
536
|
+
private outputUsed: i32 = 0
|
|
537
|
+
private blockUsed: i32 = 0
|
|
538
|
+
private blockEndStream: boolean = false
|
|
490
539
|
/** Whether the peer has acknowledged the server's SETTINGS. */
|
|
491
540
|
settingsAcked: boolean = false
|
|
492
541
|
/** `writeData` was held back by the connection's window. */
|
|
493
|
-
sendBlocked: boolean = false
|
|
542
|
+
private sendBlocked: boolean = false
|
|
543
|
+
/** Whether a GOAWAY from the server is in the output: by `goaway`, `sendGoaway` or a connection error. */
|
|
494
544
|
goawaySent: boolean = false
|
|
495
|
-
goawayReceived: boolean = false
|
|
496
|
-
/**
|
|
545
|
+
private goawayReceived: boolean = false
|
|
546
|
+
/**
|
|
547
|
+
* The peer's byte stream has ended (`endInput`).
|
|
548
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
549
|
+
*/
|
|
497
550
|
inputEnded: boolean = false
|
|
498
551
|
/** Whether the last H2_REQUEST, H2_DATA or H2_TRAILERS ended its stream. */
|
|
499
552
|
endStream: boolean = false
|
|
500
553
|
/** Whether H2_RESET came from the peer's RST_STREAM rather than a rule its stream broke. */
|
|
501
554
|
resetByPeer: boolean = false
|
|
502
555
|
/** The server's SETTINGS are not written yet: the first `holdOutput` writes them. */
|
|
503
|
-
prefaceOwed: boolean = false
|
|
556
|
+
private prefaceOwed: boolean = false
|
|
504
557
|
|
|
505
558
|
/**
|
|
506
559
|
* A connection under `config`, owing its SETTINGS — and the WINDOW_UPDATE
|
|
@@ -510,6 +563,9 @@ export class Http2Connection {
|
|
|
510
563
|
* buffer until then, so `output` is empty after the constructor and after
|
|
511
564
|
* `restart`: a caller that reads `output` itself calls `wantsWrite` first.
|
|
512
565
|
* A cap outside what RFC 9113 allows panics.
|
|
566
|
+
*
|
|
567
|
+
* @param config the caps, checked here and kept, not copied: the connection reads it for as long as
|
|
568
|
+
* it lives, so a program does not change it afterwards
|
|
513
569
|
*/
|
|
514
570
|
constructor(config: Http2Config) {
|
|
515
571
|
http2CheckCap("maxStreams", config.maxStreams, 1, 65536)
|
|
@@ -622,7 +678,7 @@ export class Http2Connection {
|
|
|
622
678
|
// ---- Borrowed buffers ---------------------------------------------------------
|
|
623
679
|
|
|
624
680
|
/** The pools the connection borrows from, made at ceiling 1 the first time a bare connection asks. */
|
|
625
|
-
lender(): Http2Pools {
|
|
681
|
+
private lender(): Http2Pools {
|
|
626
682
|
const pools: Http2Pools | null = this.pools
|
|
627
683
|
if (pools !== null) {
|
|
628
684
|
return pools
|
|
@@ -633,7 +689,7 @@ export class Http2Connection {
|
|
|
633
689
|
}
|
|
634
690
|
|
|
635
691
|
/** Borrows `input` unless the connection holds it. */
|
|
636
|
-
holdInput(): void {
|
|
692
|
+
private holdInput(): void {
|
|
637
693
|
if (this.input.length === 0) {
|
|
638
694
|
this.input = this.lender().input.take()
|
|
639
695
|
this.inputUsed = 0
|
|
@@ -641,7 +697,7 @@ export class Http2Connection {
|
|
|
641
697
|
}
|
|
642
698
|
|
|
643
699
|
/** Borrows `output` unless the connection holds it, and writes the SETTINGS it owes. */
|
|
644
|
-
holdOutput(): void {
|
|
700
|
+
private holdOutput(): void {
|
|
645
701
|
if (this.output.length === 0) {
|
|
646
702
|
this.output = this.lender().output.take()
|
|
647
703
|
this.outputUsed = 0
|
|
@@ -657,7 +713,7 @@ export class Http2Connection {
|
|
|
657
713
|
}
|
|
658
714
|
|
|
659
715
|
/** Borrows `block` unless the connection holds it. */
|
|
660
|
-
holdBlock(): void {
|
|
716
|
+
private holdBlock(): void {
|
|
661
717
|
if (this.block.length === 0) {
|
|
662
718
|
this.block = this.lender().block.take()
|
|
663
719
|
this.blockUsed = 0
|
|
@@ -665,7 +721,7 @@ export class Http2Connection {
|
|
|
665
721
|
}
|
|
666
722
|
|
|
667
723
|
/** Gives `input` back, zeroing what was written into it; ends the last H2_DATA's window. */
|
|
668
|
-
giveInput(): void {
|
|
724
|
+
private giveInput(): void {
|
|
669
725
|
if (this.input.length > 0) {
|
|
670
726
|
this.lender().input.give(this.input, this.inputUsed)
|
|
671
727
|
this.input = this.empty
|
|
@@ -679,7 +735,7 @@ export class Http2Connection {
|
|
|
679
735
|
}
|
|
680
736
|
|
|
681
737
|
/** Gives `output` back, zeroing what was written into it, sent or not. */
|
|
682
|
-
giveOutput(): void {
|
|
738
|
+
private giveOutput(): void {
|
|
683
739
|
if (this.output.length > 0) {
|
|
684
740
|
this.lender().output.give(this.output, this.outputUsed)
|
|
685
741
|
this.output = this.empty
|
|
@@ -690,7 +746,7 @@ export class Http2Connection {
|
|
|
690
746
|
}
|
|
691
747
|
|
|
692
748
|
/** Gives `block` back, zeroing what was gathered into it. */
|
|
693
|
-
giveBlock(): void {
|
|
749
|
+
private giveBlock(): void {
|
|
694
750
|
if (this.block.length > 0) {
|
|
695
751
|
this.lender().block.give(this.block, this.blockUsed)
|
|
696
752
|
this.block = this.empty
|
|
@@ -734,7 +790,11 @@ export class Http2Connection {
|
|
|
734
790
|
|
|
735
791
|
// ---- Bytes in and out ---------------------------------------------------------
|
|
736
792
|
|
|
737
|
-
/**
|
|
793
|
+
/**
|
|
794
|
+
* The room `feed` would find in the input buffer, counting the space its move to the front would free.
|
|
795
|
+
* @returns Bytes `feed` could take now, 0 to `maxFrameSize` plus the 9-byte frame header.
|
|
796
|
+
* @internal Unused: nothing calls it, and tsc's noUnusedLocals refuses a private member nothing reads.
|
|
797
|
+
*/
|
|
738
798
|
inputRoom(): i32 {
|
|
739
799
|
return this.config.maxFrameSize + H2_FRAME_HEADER_SIZE - (this.inputEnd - this.inputStart)
|
|
740
800
|
}
|
|
@@ -745,6 +805,12 @@ export class Http2Connection {
|
|
|
745
805
|
* has read what is there. Moves the unread bytes to the front of the
|
|
746
806
|
* buffer, which ends the last H2_DATA's window. Once the connection has
|
|
747
807
|
* failed it takes everything and keeps nothing.
|
|
808
|
+
*
|
|
809
|
+
* @param buf the bytes the peer sent; copied, so the caller may reuse it once `feed` answers
|
|
810
|
+
* @param off where they start in `buf`
|
|
811
|
+
* @param len how many there are; a window outside `buf` panics
|
|
812
|
+
* @returns how many bytes it took, from 0 to `len`: at most the room left in the input buffer, which
|
|
813
|
+
* holds one frame of `maxFrameSize` and its header; `len` once the connection has failed
|
|
748
814
|
*/
|
|
749
815
|
feed(buf: u8[], off: i32, len: i32): i32 {
|
|
750
816
|
http2CheckWindow("Http2Connection.feed", buf, off, len)
|
|
@@ -765,6 +831,7 @@ export class Http2Connection {
|
|
|
765
831
|
* Moves the unread input to the front of its buffer, which ends the last
|
|
766
832
|
* H2_DATA's window, and answers the room after it: where a carrier may read
|
|
767
833
|
* straight into `input` from `inputEnd`, then `received` what it read.
|
|
834
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
768
835
|
*/
|
|
769
836
|
inputTail(): i32 {
|
|
770
837
|
this.holdInput()
|
|
@@ -773,7 +840,10 @@ export class Http2Connection {
|
|
|
773
840
|
return toI32(this.input.length) - this.inputEnd
|
|
774
841
|
}
|
|
775
842
|
|
|
776
|
-
/**
|
|
843
|
+
/**
|
|
844
|
+
* Takes `n` bytes a carrier read into `input` at `inputEnd`, after `inputTail` gave it the room.
|
|
845
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
846
|
+
*/
|
|
777
847
|
received(n: i32): void {
|
|
778
848
|
const room: i32 = toI32(this.input.length) - this.inputEnd
|
|
779
849
|
if (n < 0 || n > room) {
|
|
@@ -786,7 +856,11 @@ export class Http2Connection {
|
|
|
786
856
|
}
|
|
787
857
|
}
|
|
788
858
|
|
|
789
|
-
/**
|
|
859
|
+
/**
|
|
860
|
+
* Marks the first `n` bytes of the output sent; once all of it is, the output goes back to its pool.
|
|
861
|
+
*
|
|
862
|
+
* @param n how many bytes of `output[outputStart .. outputEnd)` were sent; more than that counts as all
|
|
863
|
+
*/
|
|
790
864
|
consume(n: i32): void {
|
|
791
865
|
const pending: i32 = this.outputEnd - this.outputStart
|
|
792
866
|
this.outputStart = this.outputStart + (n < pending ? n : pending)
|
|
@@ -795,7 +869,11 @@ export class Http2Connection {
|
|
|
795
869
|
}
|
|
796
870
|
}
|
|
797
871
|
|
|
798
|
-
/**
|
|
872
|
+
/**
|
|
873
|
+
* Whether the output holds bytes to send; the SETTINGS a fresh connection owes are written first.
|
|
874
|
+
*
|
|
875
|
+
* @returns true when `output[outputStart .. outputEnd)` is not empty
|
|
876
|
+
*/
|
|
799
877
|
wantsWrite(): boolean {
|
|
800
878
|
if (this.prefaceOwed) {
|
|
801
879
|
this.holdOutput()
|
|
@@ -807,6 +885,8 @@ export class Http2Connection {
|
|
|
807
885
|
* Whether the connection is over: everything is sent, and it failed, or no
|
|
808
886
|
* stream is left after a GOAWAY went either way or after the peer's byte
|
|
809
887
|
* stream ended (`endInput`) with nothing of it left unread.
|
|
888
|
+
*
|
|
889
|
+
* @returns true when the carrier may close the connection; false while anything is left to send
|
|
810
890
|
*/
|
|
811
891
|
isDone(): boolean {
|
|
812
892
|
if (this.wantsWrite()) {
|
|
@@ -820,13 +900,14 @@ export class Http2Connection {
|
|
|
820
900
|
/**
|
|
821
901
|
* The peer's byte stream has ended, so nothing more will be fed: once the
|
|
822
902
|
* requests it already sent are answered, the connection is done.
|
|
903
|
+
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
823
904
|
*/
|
|
824
905
|
endInput(): void {
|
|
825
906
|
this.inputEnded = true
|
|
826
907
|
}
|
|
827
908
|
|
|
828
909
|
/** The room left at the end of the output, once what was sent is moved out of the way. */
|
|
829
|
-
room(): i32 {
|
|
910
|
+
private room(): i32 {
|
|
830
911
|
this.holdOutput()
|
|
831
912
|
this.outputEnd = http2Compact(this.output, this.outputStart, this.outputEnd)
|
|
832
913
|
this.outputStart = 0
|
|
@@ -834,13 +915,13 @@ export class Http2Connection {
|
|
|
834
915
|
}
|
|
835
916
|
|
|
836
917
|
/** The room DATA and HEADERS may use: all but the control reserve. */
|
|
837
|
-
bodyRoom(): i32 {
|
|
918
|
+
private bodyRoom(): i32 {
|
|
838
919
|
const r: i32 = this.room() - H2_CONTROL_RESERVE
|
|
839
920
|
return r > 0 ? r : 0
|
|
840
921
|
}
|
|
841
922
|
|
|
842
923
|
/** 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 {
|
|
924
|
+
private wrote(end: i32): boolean {
|
|
844
925
|
if (end >= 0) {
|
|
845
926
|
this.outputEnd = end
|
|
846
927
|
this.outputUsed = end > this.outputUsed ? end : this.outputUsed
|
|
@@ -850,7 +931,13 @@ export class Http2Connection {
|
|
|
850
931
|
|
|
851
932
|
// ---- Streams ------------------------------------------------------------------
|
|
852
933
|
|
|
853
|
-
/**
|
|
934
|
+
/**
|
|
935
|
+
* The slot holding stream `id`, or `null`.
|
|
936
|
+
*
|
|
937
|
+
* @param id the stream identifier
|
|
938
|
+
* @returns the stream's slot, which stays the connection's and holds another stream once this one
|
|
939
|
+
* closes; `null` when no open stream has that identifier, and for an `id` of 0 or less
|
|
940
|
+
*/
|
|
854
941
|
find(id: i32): Http2Stream | null {
|
|
855
942
|
if (id <= 0) {
|
|
856
943
|
return null
|
|
@@ -864,7 +951,7 @@ export class Http2Connection {
|
|
|
864
951
|
}
|
|
865
952
|
|
|
866
953
|
/** Opens stream `id` in a free slot; the caller has checked there is one. */
|
|
867
|
-
open(id: i32): Http2Stream {
|
|
954
|
+
private open(id: i32): Http2Stream {
|
|
868
955
|
for (const s of this.streams) {
|
|
869
956
|
if (s.id === 0) {
|
|
870
957
|
s.id = id
|
|
@@ -885,7 +972,7 @@ export class Http2Connection {
|
|
|
885
972
|
}
|
|
886
973
|
|
|
887
974
|
/** Frees the slot of a stream that is closed. */
|
|
888
|
-
release(s: Http2Stream): void {
|
|
975
|
+
private release(s: Http2Stream): void {
|
|
889
976
|
if (s.id !== 0) {
|
|
890
977
|
this.closedAt = http2Remember(this.closed, this.closedAt, s.id)
|
|
891
978
|
s.id = 0
|
|
@@ -894,7 +981,7 @@ export class Http2Connection {
|
|
|
894
981
|
}
|
|
895
982
|
|
|
896
983
|
/** The peer ended stream `s`; closes it when the server had too. */
|
|
897
|
-
remoteEnd(s: Http2Stream): void {
|
|
984
|
+
private remoteEnd(s: Http2Stream): void {
|
|
898
985
|
s.remoteEnded = true
|
|
899
986
|
if (s.localEnded) {
|
|
900
987
|
this.release(s)
|
|
@@ -902,7 +989,7 @@ export class Http2Connection {
|
|
|
902
989
|
}
|
|
903
990
|
|
|
904
991
|
/** The server ended stream `s`; closes it when the peer had too, and credits the reset budget. */
|
|
905
|
-
localEnd(s: Http2Stream): void {
|
|
992
|
+
private localEnd(s: Http2Stream): void {
|
|
906
993
|
s.localEnded = true
|
|
907
994
|
if (this.resetCredit < this.config.resetBudget) {
|
|
908
995
|
this.resetCredit = this.resetCredit + 1
|
|
@@ -913,7 +1000,7 @@ export class Http2Connection {
|
|
|
913
1000
|
}
|
|
914
1001
|
|
|
915
1002
|
/** Whether the server reset stream `id` recently, so frames still in flight for it are ignored (§5.4.2). */
|
|
916
|
-
recentlyReset(id: i32): boolean {
|
|
1003
|
+
private recentlyReset(id: i32): boolean {
|
|
917
1004
|
return id > 0 && this.resets.indexOf(id) >= 0
|
|
918
1005
|
}
|
|
919
1006
|
|
|
@@ -923,7 +1010,7 @@ export class Http2Connection {
|
|
|
923
1010
|
* opened, and is the out-of-order identifier §5.1.1 calls PROTOCOL_ERROR
|
|
924
1011
|
* rather than a frame on a closed stream (STREAM_CLOSED, §5.1).
|
|
925
1012
|
*/
|
|
926
|
-
recentlyClosed(id: i32): boolean {
|
|
1013
|
+
private recentlyClosed(id: i32): boolean {
|
|
927
1014
|
return id > 0 && this.closed.indexOf(id) >= 0
|
|
928
1015
|
}
|
|
929
1016
|
|
|
@@ -931,7 +1018,7 @@ export class Http2Connection {
|
|
|
931
1018
|
* Sends RST_STREAM for `id` with `code`, frees its slot if it has one, and
|
|
932
1019
|
* remembers it; answers false, changing nothing, when the frame did not fit.
|
|
933
1020
|
*/
|
|
934
|
-
resetStream(id: i32, code: i32): boolean {
|
|
1021
|
+
private resetStream(id: i32, code: i32): boolean {
|
|
935
1022
|
if (!this.wrote(http2WriteRstStream(this.output, this.outputEnd, id, toI64(code)))) {
|
|
936
1023
|
return false
|
|
937
1024
|
}
|
|
@@ -949,19 +1036,19 @@ export class Http2Connection {
|
|
|
949
1036
|
* server into it (MadeYouReset, CVE-2025-8671). Answers H2_NEED_MORE, or
|
|
950
1037
|
* once the budget is spent the connection error ENHANCE_YOUR_CALM (§10.5).
|
|
951
1038
|
*/
|
|
952
|
-
chargeReset(): i32 {
|
|
1039
|
+
private chargeReset(): i32 {
|
|
953
1040
|
this.resetCredit = this.resetCredit - 1
|
|
954
1041
|
return this.resetCredit < 0 ? this.fail(H2_ENHANCE_YOUR_CALM) : H2_NEED_MORE
|
|
955
1042
|
}
|
|
956
1043
|
|
|
957
1044
|
/** 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 {
|
|
1045
|
+
private refuseStream(id: i32, code: i32): i32 {
|
|
959
1046
|
this.resetStream(id, code)
|
|
960
1047
|
return this.chargeReset()
|
|
961
1048
|
}
|
|
962
1049
|
|
|
963
1050
|
/** 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 {
|
|
1051
|
+
private streamError(s: Http2Stream, code: i32): i32 {
|
|
965
1052
|
const id: i32 = s.id
|
|
966
1053
|
const charged: i32 = this.refuseStream(id, code)
|
|
967
1054
|
if (charged !== H2_NEED_MORE) {
|
|
@@ -977,6 +1064,9 @@ export class Http2Connection {
|
|
|
977
1064
|
* Writes GOAWAY with `code`, naming the last stream the peer had opened when
|
|
978
1065
|
* the server's first GOAWAY went: no later one may name a higher stream
|
|
979
1066
|
* (§6.8). Answers whether it fit.
|
|
1067
|
+
*
|
|
1068
|
+
* @param code the error code the GOAWAY carries (`H2_NO_ERROR` for a graceful close)
|
|
1069
|
+
* @returns true when the frame was written to the output; false, changing nothing, when it did not fit
|
|
980
1070
|
*/
|
|
981
1071
|
sendGoaway(code: i32): boolean {
|
|
982
1072
|
const last: i32 = this.goawaySent ? this.sentLast : this.lastPeerStream
|
|
@@ -993,7 +1083,7 @@ export class Http2Connection {
|
|
|
993
1083
|
}
|
|
994
1084
|
|
|
995
1085
|
/** A connection error: queues GOAWAY with `code` and answers H2_ERROR. */
|
|
996
|
-
fail(code: i32): i32 {
|
|
1086
|
+
private fail(code: i32): i32 {
|
|
997
1087
|
if (this.state !== H2_STATE_FAILED) {
|
|
998
1088
|
this.state = H2_STATE_FAILED
|
|
999
1089
|
this.errorCode = toI64(code)
|
|
@@ -1010,7 +1100,7 @@ export class Http2Connection {
|
|
|
1010
1100
|
* when it is still open for receiving, with a WINDOW_UPDATE once half a
|
|
1011
1101
|
* window is owed.
|
|
1012
1102
|
*/
|
|
1013
|
-
credit(s: Http2Stream | null, n: i32): void {
|
|
1103
|
+
private credit(s: Http2Stream | null, n: i32): void {
|
|
1014
1104
|
this.recvUnacked = this.recvUnacked + toI64(n)
|
|
1015
1105
|
const half: i64 = toI64(this.config.connectionWindowSize / 2)
|
|
1016
1106
|
if (this.recvUnacked > toI64(0) && this.recvUnacked >= half) {
|
|
@@ -1031,7 +1121,14 @@ export class Http2Connection {
|
|
|
1031
1121
|
|
|
1032
1122
|
// ---- Reading ------------------------------------------------------------------
|
|
1033
1123
|
|
|
1034
|
-
/**
|
|
1124
|
+
/**
|
|
1125
|
+
* Reads frames until one is an event for the program, and answers it.
|
|
1126
|
+
*
|
|
1127
|
+
* @returns `H2_REQUEST`, `H2_DATA`, `H2_TRAILERS`, `H2_RESET`, `H2_WINDOW` or `H2_GOAWAY`, with the
|
|
1128
|
+
* fields the module comment names for each; `H2_NEED_MORE` when the fed bytes hold no whole frame
|
|
1129
|
+
* (or preface) or the output lacks room for what reading one may make the connection send;
|
|
1130
|
+
* `H2_ERROR` once the connection has failed, now or before
|
|
1131
|
+
*/
|
|
1035
1132
|
next(): i32 {
|
|
1036
1133
|
while (true) {
|
|
1037
1134
|
if (this.state === H2_STATE_FAILED) {
|
|
@@ -1075,7 +1172,7 @@ export class Http2Connection {
|
|
|
1075
1172
|
}
|
|
1076
1173
|
|
|
1077
1174
|
/** One frame that parsed: the rules no single frame type owns, then the type's own. */
|
|
1078
|
-
handleFrame(f: Http2Frame): i32 {
|
|
1175
|
+
private handleFrame(f: Http2Frame): i32 {
|
|
1079
1176
|
if (this.blockStream !== 0 && (f.type !== H2_FRAME_CONTINUATION || f.streamId !== this.blockStream)) {
|
|
1080
1177
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1081
1178
|
}
|
|
@@ -1118,7 +1215,7 @@ export class Http2Connection {
|
|
|
1118
1215
|
}
|
|
1119
1216
|
|
|
1120
1217
|
/** Whether `id` names an idle stream: one the peer has not opened yet. */
|
|
1121
|
-
isIdle(id: i32): boolean {
|
|
1218
|
+
private isIdle(id: i32): boolean {
|
|
1122
1219
|
// An even identifier is the server's to open (§5.1.1), and this server
|
|
1123
1220
|
// opens none, so every even stream is idle for as long as it lives.
|
|
1124
1221
|
return id % 2 === 0 || id > this.lastPeerStream
|
|
@@ -1129,12 +1226,12 @@ export class Http2Connection {
|
|
|
1129
1226
|
* word: the server reset it and these were in flight (§5.4.2), or it was
|
|
1130
1227
|
* opened after the server's GOAWAY and so never opened at all (§6.8).
|
|
1131
1228
|
*/
|
|
1132
|
-
discarded(id: i32): boolean {
|
|
1229
|
+
private discarded(id: i32): boolean {
|
|
1133
1230
|
return this.recentlyReset(id) || (this.goawaySent && id > this.sentLast)
|
|
1134
1231
|
}
|
|
1135
1232
|
|
|
1136
1233
|
/** DATA (§6.1): flow control first, on the whole payload, then the stream's state. */
|
|
1137
|
-
handleData(f: Http2Frame): i32 {
|
|
1234
|
+
private handleData(f: Http2Frame): i32 {
|
|
1138
1235
|
const id: i32 = f.streamId
|
|
1139
1236
|
const size: i32 = f.length
|
|
1140
1237
|
const s: Http2Stream | null = this.find(id)
|
|
@@ -1181,7 +1278,7 @@ export class Http2Connection {
|
|
|
1181
1278
|
}
|
|
1182
1279
|
|
|
1183
1280
|
/** Appends a fragment of a header block; a block past `maxHeaderBlock` or `H2_MAX_FRAGMENTS` ends the connection. */
|
|
1184
|
-
gather(f: Http2Frame): boolean {
|
|
1281
|
+
private gather(f: Http2Frame): boolean {
|
|
1185
1282
|
this.holdBlock()
|
|
1186
1283
|
this.blockFragments = this.blockFragments + 1
|
|
1187
1284
|
if (
|
|
@@ -1199,7 +1296,7 @@ export class Http2Connection {
|
|
|
1199
1296
|
}
|
|
1200
1297
|
|
|
1201
1298
|
/** HEADERS (§6.2): starts a header block, and ends it when END_HEADERS says so. */
|
|
1202
|
-
handleHeaders(f: Http2Frame): i32 {
|
|
1299
|
+
private handleHeaders(f: Http2Frame): i32 {
|
|
1203
1300
|
this.blockStream = f.streamId
|
|
1204
1301
|
this.blockEndStream = f.has(H2_FLAG_END_STREAM)
|
|
1205
1302
|
this.blockError = f.streamError
|
|
@@ -1209,7 +1306,7 @@ export class Http2Connection {
|
|
|
1209
1306
|
}
|
|
1210
1307
|
|
|
1211
1308
|
/** CONTINUATION (§6.10): only inside a header block, which `handleFrame` has checked it continues. */
|
|
1212
|
-
handleContinuation(f: Http2Frame): i32 {
|
|
1309
|
+
private handleContinuation(f: Http2Frame): i32 {
|
|
1213
1310
|
if (this.blockStream === 0) {
|
|
1214
1311
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1215
1312
|
}
|
|
@@ -1224,7 +1321,7 @@ export class Http2Connection {
|
|
|
1224
1321
|
* because the decoder's table must stay in step with the peer's encoder
|
|
1225
1322
|
* (§4.3); then a new stream's request, or an open stream's trailers.
|
|
1226
1323
|
*/
|
|
1227
|
-
endBlock(): i32 {
|
|
1324
|
+
private endBlock(): i32 {
|
|
1228
1325
|
const id: i32 = this.blockStream
|
|
1229
1326
|
this.blockStream = 0
|
|
1230
1327
|
// The decoder takes back the arrays the last request's fields point into,
|
|
@@ -1283,7 +1380,7 @@ export class Http2Connection {
|
|
|
1283
1380
|
}
|
|
1284
1381
|
|
|
1285
1382
|
/** A header block on an open stream: trailers, which must end it (§8.1). */
|
|
1286
|
-
trailers(s: Http2Stream, decoded: i32): i32 {
|
|
1383
|
+
private trailers(s: Http2Stream, decoded: i32): i32 {
|
|
1287
1384
|
if (s.remoteEnded) {
|
|
1288
1385
|
return this.streamError(s, H2_STREAM_CLOSED)
|
|
1289
1386
|
}
|
|
@@ -1309,7 +1406,7 @@ export class Http2Connection {
|
|
|
1309
1406
|
* END_STREAM, then a RST_STREAM with NO_ERROR when the peer is still
|
|
1310
1407
|
* sending (§8.1), so the stream never reaches the program.
|
|
1311
1408
|
*/
|
|
1312
|
-
tooLarge(id: i32): void {
|
|
1409
|
+
private tooLarge(id: i32): void {
|
|
1313
1410
|
// The control reserve holds this with its date: under fifty bytes.
|
|
1314
1411
|
this.encodeStatus(H2_STATUS_TOO_LARGE, true)
|
|
1315
1412
|
const n: i32 = toI32(this.encoded.length)
|
|
@@ -1337,7 +1434,7 @@ export class Http2Connection {
|
|
|
1337
1434
|
* `date` is the static table's name 33 and a value of 29 bytes, in Huffman
|
|
1338
1435
|
* when that is shorter.
|
|
1339
1436
|
*/
|
|
1340
|
-
encodeStatus(status: i32, withDate: boolean): void {
|
|
1437
|
+
private encodeStatus(status: i32, withDate: boolean): void {
|
|
1341
1438
|
while (toI32(this.encoded.length) > 0) {
|
|
1342
1439
|
this.encoded.pop()
|
|
1343
1440
|
}
|
|
@@ -1356,7 +1453,7 @@ export class Http2Connection {
|
|
|
1356
1453
|
}
|
|
1357
1454
|
|
|
1358
1455
|
/** PRIORITY (§6.3): checked and otherwise ignored, and it opens nothing. */
|
|
1359
|
-
handlePriority(f: Http2Frame): i32 {
|
|
1456
|
+
private handlePriority(f: Http2Frame): i32 {
|
|
1360
1457
|
if (f.streamError === 0) {
|
|
1361
1458
|
return H2_NEED_MORE
|
|
1362
1459
|
}
|
|
@@ -1368,7 +1465,7 @@ export class Http2Connection {
|
|
|
1368
1465
|
}
|
|
1369
1466
|
|
|
1370
1467
|
/** RST_STREAM (§6.4): the stream is closed; on an idle one it is a connection error. */
|
|
1371
|
-
handleRstStream(f: Http2Frame): i32 {
|
|
1468
|
+
private handleRstStream(f: Http2Frame): i32 {
|
|
1372
1469
|
if (this.isIdle(f.streamId)) {
|
|
1373
1470
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1374
1471
|
}
|
|
@@ -1393,7 +1490,7 @@ export class Http2Connection {
|
|
|
1393
1490
|
* acknowledged. A new SETTINGS_INITIAL_WINDOW_SIZE moves every stream's send
|
|
1394
1491
|
* window by the difference (§6.9.2), and opening one answers H2_WINDOW.
|
|
1395
1492
|
*/
|
|
1396
|
-
handleSettings(f: Http2Frame): i32 {
|
|
1493
|
+
private handleSettings(f: Http2Frame): i32 {
|
|
1397
1494
|
if (f.has(H2_FLAG_ACK)) {
|
|
1398
1495
|
if (!this.settingsAcked) {
|
|
1399
1496
|
this.settingsAcked = true
|
|
@@ -1452,7 +1549,7 @@ export class Http2Connection {
|
|
|
1452
1549
|
}
|
|
1453
1550
|
|
|
1454
1551
|
/** WINDOW_UPDATE (§6.9): a send window grows, and a write it held back may go on. */
|
|
1455
|
-
handleWindowUpdate(f: Http2Frame): i32 {
|
|
1552
|
+
private handleWindowUpdate(f: Http2Frame): i32 {
|
|
1456
1553
|
const inc: i64 = toI64(f.increment)
|
|
1457
1554
|
if (f.streamId === 0) {
|
|
1458
1555
|
this.sendWindow = this.sendWindow + inc
|
|
@@ -1488,7 +1585,7 @@ export class Http2Connection {
|
|
|
1488
1585
|
// ---- Writing ------------------------------------------------------------------
|
|
1489
1586
|
|
|
1490
1587
|
/** The stream the program may still send on, or `null`. */
|
|
1491
|
-
writable(id: i32): Http2Stream | null {
|
|
1588
|
+
private writable(id: i32): Http2Stream | null {
|
|
1492
1589
|
if (this.state === H2_STATE_FAILED) {
|
|
1493
1590
|
return null
|
|
1494
1591
|
}
|
|
@@ -1499,13 +1596,14 @@ export class Http2Connection {
|
|
|
1499
1596
|
/**
|
|
1500
1597
|
* Encodes `:status` and `names`/`values` and sends the block as HEADERS and
|
|
1501
1598
|
* as many CONTINUATION frames as the peer's SETTINGS_MAX_FRAME_SIZE needs,
|
|
1502
|
-
* with END_STREAM when `endStream
|
|
1503
|
-
* `httpFieldsCheckOutgoing` refuses or
|
|
1504
|
-
*
|
|
1599
|
+
* with END_STREAM when `endStream`; a `status` of 0 sends no `:status`.
|
|
1600
|
+
* Answers 0, `H2_INVALID` for fields `httpFieldsCheckOutgoing` refuses or
|
|
1601
|
+
* a block the output could never hold, or `H2_AGAIN` when the output has
|
|
1602
|
+
* no room for it yet.
|
|
1505
1603
|
* Nothing is encoded until it is sure to be sent, so the HPACK state never
|
|
1506
1604
|
* moves for a block that was not.
|
|
1507
1605
|
*/
|
|
1508
|
-
sendHead(s: Http2Stream, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1606
|
+
private sendHead(s: Http2Stream, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1509
1607
|
if (httpFieldsCheckOutgoing(names, values) !== HTTP_FIELDS_OK) {
|
|
1510
1608
|
return H2_INVALID
|
|
1511
1609
|
}
|
|
@@ -1566,7 +1664,19 @@ export class Http2Connection {
|
|
|
1566
1664
|
* Answers stream `id` with `status` and the fields `names[k]`: `values[k]`,
|
|
1567
1665
|
* lowercase and none of them connection-specific; END_STREAM when
|
|
1568
1666
|
* `endStream`. A 1xx status but 101 may come before the final one and may
|
|
1569
|
-
* not end the stream.
|
|
1667
|
+
* not end the stream. The block is encoded into the output before it answers.
|
|
1668
|
+
*
|
|
1669
|
+
* @param id the stream to answer
|
|
1670
|
+
* @param status the status code, 100 to 599 but not 101
|
|
1671
|
+
* @param names the field names, lowercase; `:status` goes before them, and on a final response the
|
|
1672
|
+
* connection's `date` unless one of them is `date`
|
|
1673
|
+
* @param values the value for each name, at the same index
|
|
1674
|
+
* @param endStream whether the response has no body, which a 1xx may not ask
|
|
1675
|
+
* @returns 0 when the head is in the output; `H2_CLOSED` when the stream has no slot, the server has
|
|
1676
|
+
* ended it, or the connection has failed; `H2_INVALID` for a status out of range or 101, a second
|
|
1677
|
+
* final response, a 1xx with `endStream`, fields `httpFieldsCheckOutgoing` refuses (names and values
|
|
1678
|
+
* of different lengths among them), or a head too large for the output buffer even when empty;
|
|
1679
|
+
* `H2_AGAIN` when the output has no room for it yet, nothing sent
|
|
1570
1680
|
*/
|
|
1571
1681
|
respond(id: i32, status: i32, names: u8[][], values: u8[][], endStream: boolean): i32 {
|
|
1572
1682
|
const s: Http2Stream | null = this.writable(id)
|
|
@@ -1585,7 +1695,19 @@ export class Http2Connection {
|
|
|
1585
1695
|
return result
|
|
1586
1696
|
}
|
|
1587
1697
|
|
|
1588
|
-
/**
|
|
1698
|
+
/**
|
|
1699
|
+
* Ends stream `id` with a trailer section, after its response head (§8.1). The block carries no
|
|
1700
|
+
* `:status` and no `date`, and is encoded into the output before it answers.
|
|
1701
|
+
*
|
|
1702
|
+
* @param id the stream to end
|
|
1703
|
+
* @param names the trailer field names, lowercase
|
|
1704
|
+
* @param values the value for each name, at the same index
|
|
1705
|
+
* @returns 0 when the trailers are in the output and the stream is ended; `H2_CLOSED` when the stream
|
|
1706
|
+
* has no slot, the server has ended it, or the connection has failed; `H2_INVALID` before a final
|
|
1707
|
+
* response head, for fields `httpFieldsCheckOutgoing` refuses (names and values of different
|
|
1708
|
+
* lengths among them), or a block too large for the output buffer even when empty; `H2_AGAIN` when
|
|
1709
|
+
* the output has no room for it yet, nothing sent
|
|
1710
|
+
*/
|
|
1589
1711
|
writeTrailers(id: i32, names: u8[][], values: u8[][]): i32 {
|
|
1590
1712
|
const s: Http2Stream | null = this.writable(id)
|
|
1591
1713
|
if (s === null) {
|
|
@@ -1607,6 +1729,16 @@ export class Http2Connection {
|
|
|
1607
1729
|
* stream or the connection, or once the output has drained, it may be
|
|
1608
1730
|
* tried again — and `H2_CLOSED` for a stream with no final response head
|
|
1609
1731
|
* or that has ended.
|
|
1732
|
+
*
|
|
1733
|
+
* @param id the stream to send on
|
|
1734
|
+
* @param buf the body bytes; copied into the output, so the caller may reuse it once this answers
|
|
1735
|
+
* @param off where they start in `buf`
|
|
1736
|
+
* @param len how many to send; a window outside `buf` panics
|
|
1737
|
+
* @param endStream whether these bytes end the body
|
|
1738
|
+
* @returns how many bytes were taken, from 0 to `len` (0 only when `len` is 0); `H2_AGAIN` when `len`
|
|
1739
|
+
* is above 0 and none were taken, or when `len` is 0 with `endStream` and the empty DATA does not fit
|
|
1740
|
+
* yet; `H2_CLOSED` for a stream with no slot, no final response head, or that the server has ended,
|
|
1741
|
+
* and once the connection has failed
|
|
1610
1742
|
*/
|
|
1611
1743
|
writeData(id: i32, buf: u8[], off: i32, len: i32, endStream: boolean): i32 {
|
|
1612
1744
|
http2CheckWindow("Http2Connection.writeData", buf, off, len)
|
|
@@ -1665,6 +1797,12 @@ export class Http2Connection {
|
|
|
1665
1797
|
* Resets stream `id` with `code` (§6.4), from the program's side: CANCEL,
|
|
1666
1798
|
* say. Answers 0, `H2_AGAIN` when the output has no room for the frame yet
|
|
1667
1799
|
* (the stream stays open), or `H2_CLOSED`.
|
|
1800
|
+
*
|
|
1801
|
+
* @param id the stream to reset
|
|
1802
|
+
* @param code the error code the RST_STREAM carries, such as CANCEL (`H2_CANCEL` in `nish/net/http2-frame`)
|
|
1803
|
+
* @returns 0 when the RST_STREAM is in the output and the stream's slot is free; `H2_AGAIN` when it
|
|
1804
|
+
* did not fit, the stream left open; `H2_CLOSED` when no open stream has that identifier or the
|
|
1805
|
+
* connection has failed
|
|
1668
1806
|
*/
|
|
1669
1807
|
reset(id: i32, code: i32): i32 {
|
|
1670
1808
|
if (this.state === H2_STATE_FAILED || this.find(id) === null) {
|
|
@@ -1680,6 +1818,9 @@ export class Http2Connection {
|
|
|
1680
1818
|
* ignored; `isDone` once the last has closed and everything is sent.
|
|
1681
1819
|
* Answers 0, also when a GOAWAY was already sent or the connection has
|
|
1682
1820
|
* failed, or `H2_AGAIN` when the output has no room for the frame yet.
|
|
1821
|
+
*
|
|
1822
|
+
* @returns 0 when the GOAWAY is in the output, was already sent, or the connection has failed;
|
|
1823
|
+
* `H2_AGAIN` when the output has no room for it yet
|
|
1683
1824
|
*/
|
|
1684
1825
|
goaway(): i32 {
|
|
1685
1826
|
if (this.state === H2_STATE_FAILED || this.goawaySent) {
|