@amritk/nish-aarch64-linux 0.44.0 → 0.46.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.
Files changed (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -28,7 +28,8 @@
28
28
  * request's own `head`, a buffer of `limits.maxHead` bytes made on the
29
29
  * request's first use and never grown: each listen call checks that no head
30
30
  * its carrier accepts can overrun it. The body is held whole, up to
31
- * `limits.maxBody`; past it the request is answered 413 on every version.
31
+ * `limits.maxBody`, past which the request is answered 413 on every version,
32
+ * except on an HTTP/2 route marked by `streamBody`, whose body comes in pieces.
32
33
  *
33
34
  * **A fixed pool.** `limits.requests` requests are shared by every listener.
34
35
  * With none free, an HTTP/1.1 connection stops being read until one is, an
@@ -48,10 +49,24 @@
48
49
  * in the `h2Holds` family.
49
50
  */
50
51
 
51
- import { copyBytes } from "nish/bytes"
52
+ import { copyBytes, tryCopyBytes } from "nish/bytes"
52
53
  import { JsonWriter } from "nish/json"
53
- import { HANDLER_ABSENT, HANDLER_DROPPED, HANDLER_MAX_WATCHED, HANDLER_SENT } from "nish/net/handler"
54
- import { HttpFields, httpFieldBytes, httpFieldIs } from "nish/net/http-fields"
54
+ import {
55
+ HANDLER_ABSENT,
56
+ HANDLER_ANSWERED,
57
+ HANDLER_DROPPED,
58
+ HANDLER_MAX_ADDED,
59
+ HANDLER_MAX_WATCHED,
60
+ HANDLER_NEXT,
61
+ HANDLER_SENT,
62
+ } from "nish/net/handler"
63
+ import {
64
+ HTTP_FIELDS_OK,
65
+ HttpFields,
66
+ httpFieldBytes,
67
+ httpFieldIs,
68
+ httpFieldsCheckOutgoing,
69
+ } from "nish/net/http-fields"
55
70
  import { HTTP1_CHUNKED, HTTP1_NO_BODY, http1HexValue } from "nish/net/http1-codec"
56
71
  import {
57
72
  H1_AGAIN,
@@ -125,6 +140,8 @@ const HTTP_SERVER_ZERO_DIGIT: i32 = 48
125
140
 
126
141
  /** The status a body past `maxBody` is answered with. */
127
142
  const HTTP_SERVER_TOO_LARGE: i32 = 413
143
+ /** The status a request gets when no request record is free. */
144
+ const HTTP_SERVER_UNAVAILABLE: i32 = 503
128
145
 
129
146
  // A request's life: free, reading its body, whole but parked behind a full
130
147
  // output, queued for `take`, held by the program, streaming its response,
@@ -136,6 +153,14 @@ const HTTP_SERVER_QUEUED: i32 = 3
136
153
  const HTTP_SERVER_HELD: i32 = 4
137
154
  const HTTP_SERVER_STREAMING: i32 = 5
138
155
  const HTTP_SERVER_FLUSHING: i32 = 6
156
+ /** `checkParam`'s `end` for a parameter's start offset (an i32 under `--number-mode f64`). */
157
+ const HTTP_SERVER_PARAM_START: i32 = 0
158
+ /** `checkParam`'s `end` for a parameter's end offset. */
159
+ const HTTP_SERVER_PARAM_END: i32 = 1
160
+ /** `spanAt`'s `part` for a field value's start offset. */
161
+ const HTTP_SERVER_VALUE_START: i32 = 2
162
+ /** `spanAt`'s `part` for a field value's end offset. */
163
+ const HTTP_SERVER_VALUE_END: i32 = 3
139
164
 
140
165
  // Which carrier a request came in on.
141
166
  const HTTP_SERVER_H1: i32 = 1
@@ -240,6 +265,26 @@ export class HttpServerRequest {
240
265
  * @internal Read by `HttpServer` in this module to answer on the stream.
241
266
  */
242
267
  stream: i64 = 0
268
+ /**
269
+ * The trailer field names `trailers` was given: the program's own array, held until the stream ends.
270
+ * @internal Read by `HttpServer` in this module once the body's last pending byte is with the carrier.
271
+ */
272
+ trailerNames: u8[][]
273
+ /**
274
+ * The trailer field values `trailers` was given, `trailerValues[k]` for `trailerNames[k]`.
275
+ * @internal Read by `HttpServer` in this module beside `trailerNames`.
276
+ */
277
+ trailerValues: u8[][]
278
+ /**
279
+ * The names `field` added, kept by reference until the answer starts.
280
+ * @internal Grown by `HttpServer.field` in this module, to at most `HANDLER_MAX_ADDED`, and kept for the requests after.
281
+ */
282
+ addedNames: u8[][]
283
+ /**
284
+ * The values `field` added, `addedValues[k]` for `addedNames[k]`.
285
+ * @internal Grown beside `addedNames` by `HttpServer.field` in this module.
286
+ */
287
+ addedValues: u8[][]
243
288
  /** The index `Router.add` answered for the route, or a negative `ROUTER_*` code. */
244
289
  route: i32 = 0
245
290
  /** The HTTP version it came in on: 1, 2 or 3. */
@@ -269,6 +314,18 @@ export class HttpServerRequest {
269
314
  headerCount: i32 = 0
270
315
  /** How many bytes of `body` are the request's body, 0 for none; read the body as `body[0 .. bodyLength)`. */
271
316
  bodyLength: i32 = 0
317
+ /**
318
+ * Whether the body comes in pieces: an HTTP/2 request on a route `streamBody` marked, handed over at its
319
+ * head. `body[0 .. bodyLength)` is then what arrived and `consume` has not taken yet.
320
+ */
321
+ streaming: boolean = false
322
+ /** Whether the whole body has arrived: always true unless `streaming`, where END_STREAM or trailers set it. */
323
+ bodyEnded: boolean = true
324
+ /**
325
+ * Whether the request is in the queue again for the program that holds it, to say a piece, the end or a reset came.
326
+ * @internal Set by `HttpServer` in this module for a streaming request, and cleared by `take` or when it goes back.
327
+ */
328
+ again: boolean = false
272
329
  /** The connection's slot in its carrier. */
273
330
  slot: i32 = 0
274
331
  /**
@@ -312,6 +369,21 @@ export class HttpServerRequest {
312
369
  queryValueStart: i32 = 0
313
370
  /** Where the raw value `queryIndex` found ends in `head`, one past its last byte; it starts at `queryValueStart`. */
314
371
  queryValueEnd: i32 = 0
372
+ /**
373
+ * The steps `before` has still to hand out, one bit each; loaded from the router by the first `before`.
374
+ * @internal Moved only by `HttpServer.before` in this module.
375
+ */
376
+ stepsLeft: i32 = 0
377
+ /**
378
+ * The steps `before` handed out that `after` has still to hand out again.
379
+ * @internal Moved by `HttpServer.before` and `after` in this module.
380
+ */
381
+ stepsRan: i32 = 0
382
+ /**
383
+ * How many fields `field` added: `addedNames[0 .. added)` beside `addedValues`.
384
+ * @internal Counted by `HttpServer.field` in this module, read by `start`, and cleared when a request is read into it.
385
+ */
386
+ added: i32 = 0
315
387
  /** Whether it came over TLS or QUIC: true on HTTP/2 over TLS and on HTTP/3. */
316
388
  secure: boolean = false
317
389
  /**
@@ -329,6 +401,16 @@ export class HttpServerRequest {
329
401
  * @internal Set and read by `HttpServer` in this module on HTTP/2 and 3.
330
402
  */
331
403
  ended: boolean = false
404
+ /**
405
+ * Whether `trailers` ended the response, so its stream ends with `trailerNames` and `trailerValues`, not an empty DATA frame.
406
+ * @internal Set by `HttpServer.trailers` in this module on HTTP/2, and cleared when a request is read into it.
407
+ */
408
+ trailing: boolean = false
409
+ /**
410
+ * Whether `before` has loaded the route's steps into `stepsLeft`.
411
+ * @internal Set by `HttpServer.before` and cleared when a request is read into it, in this module.
412
+ */
413
+ stepped: boolean = false
332
414
 
333
415
  /**
334
416
  * An empty request for the server's pool; its buffers are made on its first use.
@@ -341,6 +423,10 @@ export class HttpServerRequest {
341
423
  this.params = []
342
424
  this.body = []
343
425
  this.pending = []
426
+ this.trailerNames = []
427
+ this.trailerValues = []
428
+ this.addedNames = []
429
+ this.addedValues = []
344
430
  }
345
431
 
346
432
  /**
@@ -367,8 +453,7 @@ export class HttpServerRequest {
367
453
  * @returns the byte offset in `head` of the parameter's first raw byte
368
454
  */
369
455
  paramStart(k: i32): i32 {
370
- this.checkParam(k)
371
- return this.params[2 * k]
456
+ return this.checkParam(k, HTTP_SERVER_PARAM_START)
372
457
  }
373
458
 
374
459
  /**
@@ -377,8 +462,7 @@ export class HttpServerRequest {
377
462
  * @returns the byte offset in `head` just past the parameter's last raw byte
378
463
  */
379
464
  paramEnd(k: i32): i32 {
380
- this.checkParam(k)
381
- return this.params[2 * k + 1]
465
+ return this.checkParam(k, HTTP_SERVER_PARAM_END)
382
466
  }
383
467
 
384
468
  /**
@@ -388,8 +472,12 @@ export class HttpServerRequest {
388
472
  * @returns true when the raw parameter bytes are exactly `text`
389
473
  */
390
474
  paramIs(k: i32, text: string): boolean {
391
- this.checkParam(k)
392
- return httpServerSpanIs(this.head, this.params[2 * k], this.params[2 * k + 1], text)
475
+ return httpServerSpanIs(
476
+ this.head,
477
+ this.checkParam(k, HTTP_SERVER_PARAM_START),
478
+ this.checkParam(k, HTTP_SERVER_PARAM_END),
479
+ text
480
+ )
393
481
  }
394
482
 
395
483
  /**
@@ -401,8 +489,12 @@ export class HttpServerRequest {
401
489
  * @returns how many decoded bytes were written, or a negative `ROUTER_*` code
402
490
  */
403
491
  param(k: i32, out: u8[]): i32 {
404
- this.checkParam(k)
405
- return routerDecode(this.head, this.params[2 * k], this.params[2 * k + 1], out)
492
+ return routerDecode(
493
+ this.head,
494
+ this.checkParam(k, HTTP_SERVER_PARAM_START),
495
+ this.checkParam(k, HTTP_SERVER_PARAM_END),
496
+ out
497
+ )
406
498
  }
407
499
 
408
500
  /**
@@ -411,14 +503,27 @@ export class HttpServerRequest {
411
503
  * @returns the number the parameter's digits spell, or -1 when it is empty, holds a non-digit or overflows
412
504
  */
413
505
  paramDecimal(k: i32): i32 {
414
- this.checkParam(k)
415
- return httpServerDecimal(this.head, this.params[2 * k], this.params[2 * k + 1])
506
+ return httpServerDecimal(
507
+ this.head,
508
+ this.checkParam(k, HTTP_SERVER_PARAM_START),
509
+ this.checkParam(k, HTTP_SERVER_PARAM_END)
510
+ )
416
511
  }
417
512
 
418
- private checkParam(k: i32): void {
419
- if (k < 0 || k >= this.paramCount) {
513
+ /**
514
+ * Parameter `k`'s start (`end` 0) or end (`end` 1) offset in `head`.
515
+ * Panics on a parameter the route does not have. `params` holds two
516
+ * entries for each of the router's parameters, so the bound on its length
517
+ * never refuses a `k` below `paramCount`.
518
+ */
519
+ private checkParam(k: i32, end: i32): i32 {
520
+ const params: i32[] = this.params
521
+ const wide: i64 = toI64(k) * toI64(2) + toI64(end)
522
+ const at: i32 = wide < toI64(0) || wide >= toI64(params.length) ? -1 : toI32(wide)
523
+ if (k < 0 || k >= this.paramCount || at < 0 || at >= toI32(params.length)) {
420
524
  panic(`HttpServerRequest.param: the route has ${this.paramCount} parameter(s), not one at ${k}`)
421
525
  }
526
+ return params[at]
422
527
  }
423
528
 
424
529
  /**
@@ -455,22 +560,31 @@ export class HttpServerRequest {
455
560
  * @returns the byte offset in `head` where the matching `name=value` pair starts, or -1 when none matches
456
561
  */
457
562
  queryIndex(name: string): i32 {
458
- let at: i32 = this.pathEnd + 1
563
+ const head: u8[] = this.head
564
+ // Each scan below also stops at the end of `head`, so `end` needs no clamp.
459
565
  const end: i32 = this.targetEnd
460
- while (at < end) {
566
+ const from: i32 = this.pathEnd
567
+ if (from >= end) {
568
+ return -1
569
+ }
570
+ let at: i32 = from < 0 ? 0 : from + 1
571
+ while (at >= 0 && at < end) {
461
572
  let pairEnd: i32 = at
462
- while (pairEnd < end && toI32(this.head[pairEnd]) !== 38) {
573
+ while (pairEnd < end && pairEnd < toI32(head.length) && toI32(head[pairEnd]) !== 38) {
463
574
  pairEnd += 1
464
575
  }
465
576
  let keyEnd: i32 = at
466
- while (keyEnd < pairEnd && toI32(this.head[keyEnd]) !== 61) {
577
+ while (keyEnd < pairEnd && keyEnd < toI32(head.length) && toI32(head[keyEnd]) !== 61) {
467
578
  keyEnd += 1
468
579
  }
469
- if (httpServerFormIs(this.head, at, keyEnd, name)) {
580
+ if (httpServerFormIs(head, at, keyEnd, name)) {
470
581
  this.queryValueStart = keyEnd < pairEnd ? keyEnd + 1 : pairEnd
471
582
  this.queryValueEnd = pairEnd
472
583
  return at
473
584
  }
585
+ if (pairEnd >= end) {
586
+ break
587
+ }
474
588
  at = pairEnd + 1
475
589
  }
476
590
  return -1
@@ -482,30 +596,44 @@ export class HttpServerRequest {
482
596
  * @returns the field's index for `valueStart` and `valueEnd`, or `HANDLER_ABSENT` when no field has the name
483
597
  */
484
598
  header(name: string): i32 {
485
- for (let i: i32 = 0; i < this.headerCount; i++) {
486
- if (httpServerSpanIs(this.head, this.spans[4 * i], this.spans[4 * i + 1], name)) {
599
+ const count: i32 = this.headerCount
600
+ // `spans` holds four entries per field, so `k` never stops the loop before `count` does.
601
+ let k: i32 = 0
602
+ for (let i: i32 = 0; i < count && k >= 0 && k < toI32(this.spans.length) - 3; i++) {
603
+ if (httpServerSpanIs(this.head, this.spans[k], this.spans[k + 1], name)) {
487
604
  return i
488
605
  }
606
+ k += 4
489
607
  }
490
608
  return HANDLER_ABSENT
491
609
  }
492
610
 
493
611
  /**
494
- * Where field `i`'s value starts in `head`.
612
+ * Where field `i`'s value starts in `head`, or -1 when the request holds no field `i`.
495
613
  * @param i a field index, as `header` answered it
496
- * @returns the byte offset in `head` of the value's first byte
614
+ * @returns the byte offset in `head` of the value's first byte, or -1 for an index `header` cannot answer
497
615
  */
498
616
  valueStart(i: i32): i32 {
499
- return this.spans[4 * i + 2]
617
+ return this.spanAt(i, HTTP_SERVER_VALUE_START)
500
618
  }
501
619
 
502
620
  /**
503
- * Where field `i`'s value ends in `head`.
621
+ * Where field `i`'s value ends in `head`, or -1 when the request holds no field `i`.
504
622
  * @param i a field index, as `header` answered it
505
- * @returns the byte offset in `head` just past the value's last byte
623
+ * @returns the byte offset in `head` just past the value's last byte, or -1 for an index `header` cannot answer
506
624
  */
507
625
  valueEnd(i: i32): i32 {
508
- return this.spans[4 * i + 3]
626
+ return this.spanAt(i, HTTP_SERVER_VALUE_END)
627
+ }
628
+
629
+ /** Entry `part` of field `i`'s four in `spans`, or -1 past the fields the request holds. */
630
+ private spanAt(i: i32, part: i32): i32 {
631
+ const wide: i64 = toI64(i) * toI64(4) + toI64(part)
632
+ const at: i32 = wide < toI64(0) || wide >= toI64(this.spans.length) ? -1 : toI32(wide)
633
+ if (i < 0 || i >= this.headerCount || at < 0 || at >= toI32(this.spans.length)) {
634
+ return -1
635
+ }
636
+ return this.spans[at]
509
637
  }
510
638
 
511
639
  /**
@@ -516,7 +644,15 @@ export class HttpServerRequest {
516
644
  */
517
645
  headerIs(name: string, value: string): boolean {
518
646
  const i: i32 = this.header(name)
519
- return i >= 0 && httpServerSpanIs(this.head, this.spans[4 * i + 2], this.spans[4 * i + 3], value)
647
+ return (
648
+ i >= 0 &&
649
+ httpServerSpanIs(
650
+ this.head,
651
+ this.spanAt(i, HTTP_SERVER_VALUE_START),
652
+ this.spanAt(i, HTTP_SERVER_VALUE_END),
653
+ value
654
+ )
655
+ )
520
656
  }
521
657
  }
522
658
  /** Appends `bytes[start .. end)` to an `Allow` value, after a `, ` if it is not the first. */
@@ -543,34 +679,127 @@ const httpServerPendingSize = (have: i32, need: i64): i32 => {
543
679
  return size > toI64(HTTP_SERVER_MAX_BODY) ? HTTP_SERVER_MAX_BODY : toI32(size)
544
680
  }
545
681
 
682
+ /**
683
+ * A zeroed array of `n` elements, `zero` each. The first 65535 come from
684
+ * one `new Array` whose `u16` length needs no runtime check; only a length
685
+ * past that, which a large `jsonBytes` or `maxHead` gives, pushes the rest.
686
+ * An `n` at or below 0 gives an empty array; one past `i32` stops there.
687
+ *
688
+ * @param n - the element count
689
+ * @param zero - the value of each element
690
+ * @returns the array
691
+ */
692
+ const httpServerZeros = <T>(n: i64, zero: T): T[] => {
693
+ let want: i32 = 2147483647
694
+ if (n <= toI64(0)) {
695
+ want = 0
696
+ } else if (n < toI64(2147483647)) {
697
+ want = toI32(n)
698
+ }
699
+ const head: i32 = want >= 65535 ? 65535 : want
700
+ const out: T[] = new Array<T>(toU16(head))
701
+ for (let k = head; k < want; k++) {
702
+ out.push(zero)
703
+ }
704
+ return out
705
+ }
706
+
707
+ /**
708
+ * `n` zeroed cells, clamped to 0 through 65535, in an array no one else holds.
709
+ *
710
+ * `spans` takes its array from here rather than from `httpServerZeros`: an
711
+ * array a plain function returns from `new` is one `HttpServerRequest` can keep
712
+ * boxed inside the object (`src/inline-arrays.ts`), which saves a header and a
713
+ * load per field lookup.
714
+ *
715
+ * @param n - how many cells
716
+ * @returns `n` zeros, or 65535 of them past that, or none below 0
717
+ */
718
+ const httpServerCells = (n: i64): i32[] => new Array<i32>(httpServerU16(n))
719
+
720
+ /**
721
+ * `n` clamped to 0 through 65535.
722
+ *
723
+ * @param n - the count to clamp
724
+ * @returns `n` as a `u16`, 0 below 0 and 65535 above it
725
+ */
726
+ const httpServerU16 = (n: i64): u16 => {
727
+ if (n <= toI64(0)) {
728
+ return toU16(0)
729
+ }
730
+ if (n >= toI64(65535)) {
731
+ return toU16(65535)
732
+ }
733
+ return toU16(toI32(n))
734
+ }
735
+
736
+ /**
737
+ * Cell `col` of row `row` in a table of rows `width` wide, as an index into
738
+ * its `length` elements, or -1 when that cell lies outside them.
739
+ *
740
+ * @param row - the row
741
+ * @param width - the elements per row
742
+ * @param col - the column within the row
743
+ * @param length - the table's element count
744
+ * @returns the index, or -1
745
+ */
746
+ const httpServerCell = (row: i32, width: i32, col: i32, length: i32): i32 => {
747
+ const wide: i64 = toI64(row) * toI64(width) + toI64(col)
748
+ return wide < toI64(0) || wide >= toI64(length) ? -1 : toI32(wide)
749
+ }
750
+
751
+ /**
752
+ * The `monotonicNanos` instant `ns` nanoseconds after `at`. Worked in `u64`,
753
+ * whose wrapping sum has the bits of the signed one: an instant is centuries
754
+ * short of 2^63 and every delay a constant or a checked limit, so neither wraps.
755
+ */
756
+ const httpServerAfter = (at: i64, ns: i64): i64 => toI64(toU64(at) + toU64(ns))
757
+
758
+ /** `until - now` in nanoseconds, worked in `u64` for the same reason as `httpServerAfter`. */
759
+ const httpServerLeft = (until: i64, now: i64): i64 => toI64(toU64(until) - toU64(now))
760
+
761
+ /**
762
+ * Whether more than `ns` nanoseconds lie between the `monotonicNanos`
763
+ * instants `since` and `now`; a `now` at or before `since` is no time at all.
764
+ */
765
+ const httpServerPast = (now: i64, since: i64, ns: i64): boolean =>
766
+ now > since && toU64(now) - toU64(since) > toU64(ns)
767
+
546
768
  /** Whether `bytes[start .. end)` is exactly `text`. */
547
769
  const httpServerSpanIs = (bytes: u8[], start: i32, end: i32, text: string): boolean => {
548
770
  const n: i32 = toI32(text.length)
549
- if (end - start !== n || start < 0 || end > toI32(bytes.length)) {
771
+ const size: i32 = toI32(bytes.length)
772
+ if (start < 0 || end > size || toI64(end) - toI64(start) !== toI64(n)) {
550
773
  return false
551
774
  }
552
- for (let k: i32 = 0; k < n; k++) {
553
- if (toI32(bytes[start + k]) !== toI32(text.charCodeAt(k))) {
775
+ let j: i32 = 0
776
+ for (let k: i32 = start; k < size && j < n; k++) {
777
+ if (toI32(bytes[k]) !== toI32(text.charCodeAt(j))) {
554
778
  return false
555
779
  }
780
+ j += 1
556
781
  }
557
- return true
782
+ return j === n
558
783
  }
559
784
 
560
785
  /** `bytes[start .. end)` as a decimal number that is not negative and fits an `i32`, or -1. */
561
786
  const httpServerDecimal = (bytes: u8[], start: i32, end: i32): i32 => {
562
- if (start < 0 || end <= start || end - start > 10 || end > toI32(bytes.length)) {
787
+ if (start < 0 || end <= start || toI64(end) - toI64(start) > toI64(10) || end > toI32(bytes.length)) {
563
788
  return -1
564
789
  }
565
- let n: i64 = 0
790
+ let n: i32 = 0
566
791
  for (let k: i32 = start; k < end && k < toI32(bytes.length); k++) {
567
792
  const digit: i32 = toI32(bytes[k]) - 48
568
- if (digit < 0 || digit > 9) {
793
+ if (digit < 0 || digit > 9 || n < 0 || n > 214748364) {
569
794
  return -1
570
795
  }
571
- n = n * toI64(10) + toI64(digit)
796
+ const next: i64 = toI64(n) * toI64(10) + toI64(digit)
797
+ if (next > toI64(2147483647)) {
798
+ return -1
799
+ }
800
+ n = toI32(next)
572
801
  }
573
- return n > toI64(2147483647) ? -1 : toI32(n)
802
+ return n
574
803
  }
575
804
 
576
805
  /**
@@ -578,6 +807,9 @@ const httpServerDecimal = (bytes: u8[], start: i32, end: i32): i32 => {
578
807
  * many bytes it took, packed as `byte | taken << 8`; -1 for a malformed escape.
579
808
  */
580
809
  const httpServerFormByte = (bytes: u8[], at: i32, end: i32): i32 => {
810
+ if (at < 0 || at >= toI32(bytes.length)) {
811
+ return -1 // outside the bytes: nothing there to decode
812
+ }
581
813
  const c: i32 = toI32(bytes[at])
582
814
  if (c === 43) {
583
815
  return 32 | HTTP_SERVER_TOOK_ONE
@@ -585,12 +817,12 @@ const httpServerFormByte = (bytes: u8[], at: i32, end: i32): i32 => {
585
817
  if (c !== 37) {
586
818
  return c | HTTP_SERVER_TOOK_ONE
587
819
  }
588
- if (at + 2 >= end) {
820
+ if (at >= end - 2 || at >= toI32(bytes.length) - 2) {
589
821
  return -1
590
822
  }
591
823
  const hi: i32 = http1HexValue(toI32(bytes[at + 1]))
592
824
  const lo: i32 = http1HexValue(toI32(bytes[at + 2]))
593
- if (hi < 0 || lo < 0) {
825
+ if (hi < 0 || lo < 0 || hi > 15 || lo > 15) {
594
826
  return -1
595
827
  }
596
828
  return (hi * 16 + lo) | HTTP_SERVER_TOOK_THREE
@@ -603,10 +835,16 @@ const httpServerFormIs = (bytes: u8[], start: i32, end: i32, name: string): bool
603
835
  let k: i32 = 0
604
836
  while (at < end) {
605
837
  const packed: i32 = httpServerFormByte(bytes, at, end)
606
- if (packed < 0 || k >= n || (packed & 255) !== toI32(name.charCodeAt(k))) {
838
+ const took: i32 = packed >> 8
839
+ if (
840
+ packed < 0 ||
841
+ k >= n ||
842
+ (packed & 255) !== toI32(name.charCodeAt(k)) ||
843
+ toI64(took) > toI64(end) - toI64(at)
844
+ ) {
607
845
  return false
608
846
  }
609
- at += packed >> 8
847
+ at = toI32(toI64(at) + toI64(took))
610
848
  k += 1
611
849
  }
612
850
  return k === n
@@ -618,10 +856,14 @@ const httpServerFormIs = (bytes: u8[], start: i32, end: i32, name: string): bool
618
856
  * route parameter's does.
619
857
  */
620
858
  const httpServerFormDecode = (bytes: u8[], start: i32, end: i32, out: u8[]): i32 => {
859
+ // `queryIndex` found the span in the head, so this does not fire.
860
+ if (start < 0 || end < start) {
861
+ return ROUTER_BAD_ESCAPE
862
+ }
621
863
  const length: i32 = percentDecode(
622
864
  bytes,
623
865
  start,
624
- end - start,
866
+ toI32(toI64(end) - toI64(start)),
625
867
  out,
626
868
  HTTP_SERVER_ZERO,
627
869
  URL_DECODE_FORM | URL_DECODE_ALLOW_NUL
@@ -632,6 +874,25 @@ const httpServerFormDecode = (bytes: u8[], start: i32, end: i32, out: u8[]): i32
632
874
  return length < 0 ? ROUTER_BAD_ESCAPE : length
633
875
  }
634
876
 
877
+ /** The generation after `gen`: it only has to differ from the last one, so it wraps to 0 past i32. */
878
+ const httpServerNextGen = (gen: i32): i32 => (gen < 2147483647 ? gen + 1 : 0)
879
+
880
+ /** Entry `at` of the per-slot table `xs`, or -1 outside it. */
881
+ const httpServerAt = (xs: i32[], at: i32): i32 => (at >= 0 && at < toI32(xs.length) ? xs[at] : -1)
882
+
883
+ /** Adds `by` to entry `at` of the per-slot count `xs` and gives the new count, or -1 outside the table or past i32. */
884
+ const httpServerAdd = (xs: i32[], at: i32, by: i32): i32 => {
885
+ if (at < 0 || at >= toI32(xs.length)) {
886
+ return -1
887
+ }
888
+ const next: i64 = toI64(xs[at]) + toI64(by)
889
+ if (next < -2147483648 || next > 2147483647) {
890
+ return -1
891
+ }
892
+ xs[at] = toI32(next)
893
+ return toI32(next)
894
+ }
895
+
635
896
  /** Panics unless `limits.<name>`, `value`, is within `low` to `high`. */
636
897
  const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void => {
637
898
  if (value < low || value > high) {
@@ -639,6 +900,35 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
639
900
  }
640
901
  }
641
902
 
903
+ /**
904
+ * One HTTP/1.1 slot's bookkeeping beside the carrier's connection in it: the
905
+ * slot number, its generation, its request, and whether it is open, waits for
906
+ * a free request or is being driven. `listenHttp1` makes one per carrier slot.
907
+ */
908
+ class HttpServerH1Slot {
909
+ /** The carrier's slot number. */
910
+ slot: i32
911
+ /** Bumped each time the slot closes, so an answer still owed to the connection before is dropped; it wraps. */
912
+ gen: i32 = 0
913
+ /** The request the slot is reading or answering, null for none. */
914
+ current: HttpServerRequest | null = null
915
+ /** Whether the carrier's connection in the slot is open. */
916
+ open: boolean = false
917
+ /** Whether it stopped reading for want of a free request and waits on `starved`. */
918
+ starved: boolean = false
919
+ /**
920
+ * 0 while no `drive` of it runs, 1 while one does, 2 once a call inside it
921
+ * (an answer screened while it reads) asked for another pass. The nested
922
+ * call only marks it, so the depth stays one however many screened
923
+ * requests one write pipelines.
924
+ */
925
+ driving: i32 = 0
926
+
927
+ constructor(slot: i32) {
928
+ this.slot = slot
929
+ }
930
+ }
931
+
642
932
  /**
643
933
  * One handler for HTTP/1.1, HTTP/2 over TLS and HTTP/3: configure up to one
644
934
  * listener of each kind, then loop on `waitSigned` (or `wait`), `ready` and
@@ -647,9 +937,9 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
647
937
  */
648
938
  export class HttpServer {
649
939
  private router: Router
650
- /** Every request of the pool, made up front, and the indices of the free ones. */
940
+ /** Every request of the pool, made up front, and the free ones. */
651
941
  private requests: HttpServerRequest[]
652
- private free: i32[]
942
+ private free: HttpServerRequest[]
653
943
  /** Whole requests waiting for `take`, a ring of `queue.length` with `queueCount` from `queueStart`. */
654
944
  private queue: i32[]
655
945
  /** `pollWait`'s output. */
@@ -695,23 +985,13 @@ export class HttpServer {
695
985
  /** When `wait` next looks for timeouts. */
696
986
  private sweepAt: i64 = 0
697
987
 
698
- // HTTP/1.1: the carrier, its listener, and per slot its generation, its request (-1 for none) and whether it is open.
988
+ // HTTP/1.1: the carrier, its listener, and a record per slot.
699
989
  private h1: Http1Server | null = null
700
990
  /** Per listener (`-100 - token`): when its rest for want of descriptors ends, 0 while it is not resting. */
701
991
  private restUntil: i64[]
702
- private h1Gen: i32[]
703
- private h1Current: i32[]
704
- private h1Open: boolean[]
705
- /** Slots that stopped reading for want of a free request, `starved`, and whether each is in it. */
706
- private starved: i32[]
707
- private h1Starved: boolean[]
708
- /**
709
- * Per slot: 0 while no `drive` of it runs, 1 while one does, 2 once a call
710
- * inside it (an answer screened while it reads) asked for another pass.
711
- * The nested call only marks it, so the depth stays one however many
712
- * screened requests one write pipelines.
713
- */
714
- private h1Driving: i32[]
992
+ private h1Slots: HttpServerH1Slot[]
993
+ /** Slots that stopped reading for want of a free request, in the order they did. */
994
+ private starved: HttpServerH1Slot[]
715
995
 
716
996
  /** The response fields `start` hands the carrier, set for each response. */
717
997
  private outNames: u8[][]
@@ -748,6 +1028,9 @@ export class HttpServer {
748
1028
  private h3Streams: i64[]
749
1029
  private h3Reqs: i32[]
750
1030
  private h3Count: i32[]
1031
+ /** Which routes take their body in pieces, by route index, and how many do. */
1032
+ private streamRoutes: boolean[]
1033
+ private streamCount: i32 = 0
751
1034
  private queueStart: i32 = 0
752
1035
  private queueCount: i32 = 0
753
1036
  private maxBody: i32 = 0
@@ -811,15 +1094,17 @@ export class HttpServer {
811
1094
  this.requests = []
812
1095
  this.free = []
813
1096
  for (let k: i32 = 0; k < limits.requests; k++) {
814
- this.requests.push(new HttpServerRequest(k))
815
- this.free.push(limits.requests - 1 - k)
1097
+ const r: HttpServerRequest = new HttpServerRequest(k)
1098
+ this.requests.push(r)
1099
+ this.free.push(r)
816
1100
  }
817
- this.queue = new Array<i32>(limits.requests)
818
- this.readyFds = new Array<i32>(2 * limits.maxEvents)
1101
+ this.queue = httpServerZeros(toI64(limits.requests), toI32(0))
1102
+ this.streamRoutes = []
1103
+ this.readyFds = httpServerZeros(toI64(2) * toI64(limits.maxEvents), toI32(0))
819
1104
  this.watchedFired = new Array<boolean>(HANDLER_MAX_WATCHED)
820
- this.scratch = new Array<u8>(limits.jsonBytes)
1105
+ this.scratch = httpServerZeros(toI64(limits.jsonBytes), toU8(0))
821
1106
  this.writer = new JsonWriter(this.scratch, HTTP_SERVER_ZERO)
822
- this.probe = new Array<i32>(2 * router.maxParams + 2)
1107
+ this.probe = httpServerZeros(toI64(2) * toI64(router.maxParams) + toI64(2), toI32(0))
823
1108
  this.jsonType = httpFieldBytes("application/json")
824
1109
  this.problemType = httpFieldBytes("application/problem+json")
825
1110
  const contentType: u8[] = httpFieldBytes("content-type")
@@ -838,25 +1123,25 @@ export class HttpServer {
838
1123
  this.outValuesBy = []
839
1124
  this.digitBufs = []
840
1125
  for (let d: i32 = 0; d <= 10; d++) {
841
- this.digitBufs.push(new Array<u8>(d))
1126
+ this.digitBufs.push(httpServerZeros(toI64(d), toU8(0)))
842
1127
  }
843
1128
  this.altSvcName = httpFieldBytes("alt-svc")
844
1129
  this.altNames = [this.altSvcName]
845
- this.altValues = [this.altSvcName]
1130
+ const altValues: u8[][] = [this.altSvcName]
1131
+ this.altValues = altValues
846
1132
  if (this.altSvcMaxAge === 0) {
847
1133
  this.altSvc = httpFieldBytes("clear")
848
- this.altValues[0] = httpFieldBytes("clear")
1134
+ const clear: u8[] = httpFieldBytes("clear")
1135
+ if (toI32(altValues.length) > 0) {
1136
+ altValues[0] = clear
1137
+ }
849
1138
  }
850
1139
  this.contentLengthName = httpFieldBytes("content-length")
851
1140
  this.hostName = httpFieldBytes("host")
852
1141
  this.outNames = []
853
1142
  this.outValues = []
854
- this.h1Gen = []
855
- this.h1Current = []
856
- this.h1Open = []
1143
+ this.h1Slots = []
857
1144
  this.starved = []
858
- this.h1Starved = []
859
- this.h1Driving = []
860
1145
  this.restUntil = new Array<i64>(4)
861
1146
  this.h2Clear = []
862
1147
  this.h2Gen = []
@@ -911,14 +1196,11 @@ export class HttpServer {
911
1196
  this.h1 = new Http1Server(config, fd, slots)
912
1197
  this.h1Fd = fd
913
1198
  this.h1OutputSize = config.outputSize
914
- this.h1Gen = new Array<i32>(slots)
915
- this.h1Current = new Array<i32>(slots)
916
- this.h1Open = new Array<boolean>(slots)
917
- this.h1Starved = new Array<boolean>(slots)
918
- this.h1Driving = new Array<i32>(slots)
1199
+ const table: HttpServerH1Slot[] = []
919
1200
  for (let s: i32 = 0; s < slots; s++) {
920
- this.h1Current[s] = -1
1201
+ table.push(new HttpServerH1Slot(s))
921
1202
  }
1203
+ this.h1Slots = table
922
1204
  pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
923
1205
  }
924
1206
 
@@ -943,9 +1225,11 @@ export class HttpServer {
943
1225
  panic("HttpServer.listenH2c: an h2c listener is already configured")
944
1226
  }
945
1227
  this.checkHttp2("listenH2c", config, slots)
946
- this.h2cBase = this.h2Grow(slots, this.perOf(config))
947
- for (let v: i32 = this.h2cBase; v < this.h2cBase + slots; v++) {
948
- this.h2Clear[v] = true
1228
+ const base: i32 = this.h2Grow(slots, this.perOf(config))
1229
+ this.h2cBase = base
1230
+ const clear: boolean[] = this.h2Clear
1231
+ for (let v: i32 = base; v >= 0 && v < toI32(clear.length) && toI64(v) < toI64(base) + toI64(slots); v++) {
1232
+ clear[v] = true
949
1233
  }
950
1234
  this.h2c = new Http2CleartextServer(config, fd, slots, this.prefaceTimeout)
951
1235
  this.h2cFd = fd
@@ -1009,22 +1293,33 @@ export class HttpServer {
1009
1293
  const base: i32 = toI32(this.h2Open.length)
1010
1294
  const was: i32 = this.perConnection2
1011
1295
  const width: i32 = per > was ? per : was
1012
- const streams: i32[] = new Array<i32>((base + slots) * width)
1013
- const reqs: i32[] = new Array<i32>((base + slots) * width)
1014
- for (let e: i32 = 0; e < toI32(reqs.length); e++) {
1015
- reqs[e] = -1
1016
- }
1296
+ const size: i64 = toI64(base) * toI64(width) + toI64(slots) * toI64(width)
1297
+ const streams: i32[] = httpServerZeros(size, toI32(0))
1298
+ const reqs: i32[] = httpServerZeros(size, toI32(-1))
1299
+ const oldStreams: i32[] = this.h2Streams
1300
+ const oldReqs: i32[] = this.h2Reqs
1017
1301
  for (let v: i32 = 0; v < base; v++) {
1018
1302
  for (let i: i32 = 0; i < was; i++) {
1019
- streams[v * width + i] = this.h2Streams[v * was + i]
1020
- reqs[v * width + i] = this.h2Reqs[v * was + i]
1303
+ const to: i32 = httpServerCell(v, width, i, toI32(streams.length))
1304
+ const from: i32 = httpServerCell(v, was, i, toI32(oldStreams.length))
1305
+ if (
1306
+ to >= 0 &&
1307
+ to < toI32(streams.length) &&
1308
+ to < toI32(reqs.length) &&
1309
+ from >= 0 &&
1310
+ from < toI32(oldStreams.length) &&
1311
+ from < toI32(oldReqs.length)
1312
+ ) {
1313
+ streams[to] = oldStreams[from]
1314
+ reqs[to] = oldReqs[from]
1315
+ }
1021
1316
  }
1022
1317
  }
1023
- if (width !== was) {
1318
+ if (width !== was && was > 0) {
1024
1319
  for (let k: i32 = 0; k < toI32(this.requests.length); k++) {
1025
1320
  const r: HttpServerRequest = this.requests[k]
1026
1321
  if (r.kind === HTTP_SERVER_H2 && r.entry >= 0) {
1027
- r.entry = (r.entry / was) * width + (r.entry % was)
1322
+ r.entry = httpServerCell(r.entry / was, width, r.entry % was, toI32(streams.length))
1028
1323
  }
1029
1324
  }
1030
1325
  }
@@ -1077,10 +1372,10 @@ export class HttpServer {
1077
1372
  this.perConnection3 = per < toI64(1) ? 1 : toI32(per)
1078
1373
  this.h3 = new Http3Server(quic, config, fd, slots, entropy)
1079
1374
  this.h3Fd = fd
1080
- this.h3Gen = new Array<i32>(slots)
1081
- this.h3Count = new Array<i32>(slots)
1082
- this.h3Streams = new Array<i64>(slots * this.perConnection3)
1083
- this.h3Reqs = new Array<i32>(slots * this.perConnection3)
1375
+ this.h3Gen = httpServerZeros(toI64(slots), toI32(0))
1376
+ this.h3Count = httpServerZeros(toI64(slots), toI32(0))
1377
+ this.h3Streams = httpServerZeros(toI64(slots) * toI64(this.perConnection3), toI64(0))
1378
+ this.h3Reqs = httpServerZeros(toI64(slots) * toI64(this.perConnection3), toI32(0))
1084
1379
  for (let k: i32 = 0; k < toI32(this.h3Reqs.length); k++) {
1085
1380
  this.h3Reqs[k] = -1
1086
1381
  }
@@ -1094,7 +1389,10 @@ export class HttpServer {
1094
1389
  /** Sets the `alt-svc` value every HTTP/1.1 and HTTP/2 over TLS answer carries. */
1095
1390
  private setAltSvc(value: u8[]): void {
1096
1391
  this.altSvc = value
1097
- this.altValues[0] = value
1392
+ const alt: u8[][] = this.altValues
1393
+ if (toI32(alt.length) > 0) {
1394
+ alt[0] = value
1395
+ }
1098
1396
  }
1099
1397
 
1100
1398
  // ---- The loop -------------------------------------------------------------------
@@ -1120,7 +1418,8 @@ export class HttpServer {
1120
1418
  * @returns true when it was readable in the last turn; false for an id outside the range
1121
1419
  */
1122
1420
  fired(id: i32): boolean {
1123
- return id >= 0 && id < HANDLER_MAX_WATCHED && this.watchedFired[id]
1421
+ const fired: boolean[] = this.watchedFired
1422
+ return id >= 0 && id < toI32(fired.length) && fired[id]
1124
1423
  }
1125
1424
 
1126
1425
  /**
@@ -1153,8 +1452,10 @@ export class HttpServer {
1153
1452
 
1154
1453
  /** `waitSigned`'s turn, with no key under `wait`, which serves no listener that reads one. */
1155
1454
  private turnWith(timeoutMs: i32, key: Secret<u8[]> | null): i32 {
1156
- for (let id: i32 = 0; id < HANDLER_MAX_WATCHED; id++) {
1157
- this.watchedFired[id] = false
1455
+ const fired: boolean[] = this.watchedFired
1456
+ const watched: i32 = toI32(fired.length)
1457
+ for (let id: i32 = 0; id < watched; id++) {
1458
+ fired[id] = false
1158
1459
  }
1159
1460
  // A starved slot driven here may read a head, so it reads the clock of now.
1160
1461
  this.turnAt = monotonicNanos()
@@ -1177,9 +1478,16 @@ export class HttpServer {
1177
1478
  now = monotonicNanos()
1178
1479
  this.turnAt = now
1179
1480
  this.h3Readable = false
1481
+ const ready: i32[] = this.readyFds
1180
1482
  for (let k: i32 = 0; k < n; k++) {
1181
- const token: i32 = this.readyFds[2 * k]
1182
- const events: i32 = this.readyFds[2 * k + 1]
1483
+ // `pollWait` fills at most `slots / 2` pairs.
1484
+ const at: i32 = toI32(toI64(2) * toI64(k))
1485
+ const after: i32 = toI32(toI64(2) * toI64(k) + toI64(1))
1486
+ if (at < 0 || at >= toI32(ready.length) || after < 0 || after >= toI32(ready.length)) {
1487
+ break
1488
+ }
1489
+ const token: i32 = ready[at]
1490
+ const events: i32 = ready[after]
1183
1491
  if (token === HTTP_SERVER_H1_LISTENER) {
1184
1492
  this.acceptHttp1()
1185
1493
  } else if (token === HTTP_SERVER_H2_LISTENER) {
@@ -1189,12 +1497,12 @@ export class HttpServer {
1189
1497
  } else if (token === HTTP_SERVER_H3_SOCKET) {
1190
1498
  this.h3Readable = true
1191
1499
  } else if (token <= -2 && token > HTTP_SERVER_H1_LISTENER) {
1192
- const id: i32 = -2 - token
1193
- if (id < HANDLER_MAX_WATCHED) {
1194
- this.watchedFired[id] = true
1500
+ const id: i32 = toI32(toI64(-2) - toI64(token))
1501
+ if (id >= 0 && id < toI32(fired.length)) {
1502
+ fired[id] = true
1195
1503
  }
1196
1504
  } else if (token >= HTTP_SERVER_H2_BASE) {
1197
- this.h2Serve(token - HTTP_SERVER_H2_BASE, events, key)
1505
+ this.h2Serve(toI32(toI64(token) - toI64(HTTP_SERVER_H2_BASE)), events, key)
1198
1506
  } else if (token >= 0) {
1199
1507
  this.h1Serve(token, events)
1200
1508
  }
@@ -1208,7 +1516,7 @@ export class HttpServer {
1208
1516
  }
1209
1517
  this.feedStarved()
1210
1518
  if (now >= this.sweepAt) {
1211
- this.sweepAt = now + HTTP_SERVER_SWEEP
1519
+ this.sweepAt = httpServerAfter(now, HTTP_SERVER_SWEEP)
1212
1520
  this.expireAll(now)
1213
1521
  }
1214
1522
  return this.queueCount
@@ -1224,8 +1532,13 @@ export class HttpServer {
1224
1532
  if (this.connections() === 0) {
1225
1533
  return timeout
1226
1534
  }
1227
- const left: i64 = this.sweepAt - now
1228
- const cap: i32 = left <= 0 ? HTTP_SERVER_ZERO : toI32(left / HTTP_SERVER_NANOS_PER_MS) + 1
1535
+ const left: i64 = httpServerLeft(this.sweepAt, now)
1536
+ const ms: i64 = left / HTTP_SERVER_NANOS_PER_MS
1537
+ const whole: i32 = ms >= 0 && ms < 2147483647 ? toI32(ms) : 2147483646
1538
+ let cap: i32 = HTTP_SERVER_ZERO
1539
+ if (left > 0 && whole < 2147483647) {
1540
+ cap = whole + 1
1541
+ }
1229
1542
  return timeout < 0 || cap < timeout ? cap : timeout
1230
1543
  }
1231
1544
 
@@ -1234,12 +1547,17 @@ export class HttpServer {
1234
1547
  * over, and answers `timeout` shortened to wake for that.
1235
1548
  */
1236
1549
  private restListener(fd: i32, token: i32, now: i64, timeout: i32): i32 {
1237
- const until: i64 = this.restUntil[HTTP_SERVER_H1_LISTENER - token]
1550
+ const rests: i64[] = this.restUntil
1551
+ const at: i32 = this.restAt(token)
1552
+ if (at < 0 || at >= toI32(rests.length)) {
1553
+ return timeout
1554
+ }
1555
+ const until: i64 = rests[at]
1238
1556
  if (until === 0 || fd < 0) {
1239
1557
  return timeout
1240
1558
  }
1241
1559
  if (now >= until) {
1242
- this.restUntil[HTTP_SERVER_H1_LISTENER - token] = 0
1560
+ rests[at] = 0
1243
1561
  if (!this.draining) {
1244
1562
  pollAdd(this.loop, fd, H1_WANT_READ, token)
1245
1563
  }
@@ -1248,10 +1566,18 @@ export class HttpServer {
1248
1566
  if (this.draining) {
1249
1567
  return timeout
1250
1568
  }
1251
- const rest: i32 = toI32((until - now) / HTTP_SERVER_NANOS_PER_MS) + 1
1569
+ const wait: i64 = httpServerLeft(until, now) / HTTP_SERVER_NANOS_PER_MS
1570
+ const ms: i32 = wait >= 0 && wait < 2147483647 ? toI32(wait) : 2147483646
1571
+ const rest: i32 = ms < 2147483647 ? ms + 1 : ms
1252
1572
  return timeout < 0 || rest < timeout ? rest : timeout
1253
1573
  }
1254
1574
 
1575
+ /** Where listener `token`'s deadline sits in `restUntil`, or -1 for a token that names no listener. */
1576
+ private restAt(token: i32): i32 {
1577
+ const at: i64 = toI64(HTTP_SERVER_H1_LISTENER) - toI64(token)
1578
+ return at >= 0 && at < toI64(this.restUntil.length) ? toI32(at) : -1
1579
+ }
1580
+
1255
1581
  /**
1256
1582
  * Begins a graceful stop: the server accepts no more connections, closes
1257
1583
  * each idle HTTP/1.1 connection now and each other one once its request is
@@ -1269,23 +1595,24 @@ export class HttpServer {
1269
1595
  this.unlisten(this.h2cFd, HTTP_SERVER_H2C_LISTENER)
1270
1596
  const h1: Http1Server | null = this.h1
1271
1597
  if (h1 !== null) {
1272
- for (let s: i32 = 0; s < toI32(this.h1Open.length); s++) {
1273
- if (this.h1Open[s] && h1.holds(s)) {
1274
- if (this.h1Current[s] === -1 && (h1.resting(s) || !h1.connection(s).wantsWrite())) {
1275
- this.h1Close(h1, s)
1598
+ for (const s of this.h1Slots) {
1599
+ if (s.open && h1.holds(s.slot)) {
1600
+ if (s.current === null && (h1.resting(s.slot) || !h1.connection(s.slot).wantsWrite())) {
1601
+ this.h1Close(h1, s.slot)
1276
1602
  } else {
1277
- h1.connection(s).closeAfter = true
1603
+ h1.connection(s.slot).closeAfter = true
1278
1604
  }
1279
1605
  }
1280
1606
  }
1281
1607
  }
1282
1608
  for (let v: i32 = 0; v < toI32(this.h2Open.length); v++) {
1283
1609
  if (this.h2Open[v] && this.h2Holds(v)) {
1284
- if (this.h2Clear[v] && !this.h2cOf().prefaced(v - this.h2cBase)) {
1610
+ const conn: Http2Connection | null = this.h2Prefaced(v)
1611
+ if (conn === null) {
1285
1612
  // No preface yet, so no connection to say GOAWAY on.
1286
1613
  this.h2Close(v)
1287
1614
  } else {
1288
- this.h2Conn(v).goaway()
1615
+ conn.goaway()
1289
1616
  this.h2Settle(v)
1290
1617
  }
1291
1618
  }
@@ -1303,7 +1630,9 @@ export class HttpServer {
1303
1630
 
1304
1631
  /** Takes listener `fd` out of the loop, unless it is not there: never configured, or resting. */
1305
1632
  private unlisten(fd: i32, token: i32): void {
1306
- if (fd >= 0 && this.restUntil[HTTP_SERVER_H1_LISTENER - token] === 0) {
1633
+ const rests: i64[] = this.restUntil
1634
+ const at: i32 = this.restAt(token)
1635
+ if (fd >= 0 && at >= 0 && at < toI32(rests.length) && rests[at] === 0) {
1307
1636
  pollRemove(this.loop, fd)
1308
1637
  }
1309
1638
  }
@@ -1313,24 +1642,17 @@ export class HttpServer {
1313
1642
  * @returns the connections the carriers hold, which falls to 0 after `drain` as they finish
1314
1643
  */
1315
1644
  connections(): i32 {
1316
- let n: i32 = 0
1317
1645
  const h1: Http1Server | null = this.h1
1318
- if (h1 !== null) {
1319
- n += h1.busy()
1320
- }
1321
1646
  const h2: Http2TlsServer | null = this.h2
1322
- if (h2 !== null) {
1323
- n += h2.busy()
1324
- }
1325
1647
  const h2c: Http2CleartextServer | null = this.h2c
1326
- if (h2c !== null) {
1327
- n += h2c.busy()
1328
- }
1329
1648
  const h3: Http3Server | null = this.h3
1330
- if (h3 !== null) {
1331
- n += h3.busy()
1332
- }
1333
- return n
1649
+ const one: i32 = h1 !== null ? h1.busy() : 0
1650
+ const two: i32 = h2 !== null ? h2.busy() : 0
1651
+ const clear: i32 = h2c !== null ? h2c.busy() : 0
1652
+ const three: i32 = h3 !== null ? h3.busy() : 0
1653
+ // Each carrier's count is an `i32`; their sum saturates rather than wraps.
1654
+ const n: i64 = toI64(one) + toI64(two) + (toI64(clear) + toI64(three))
1655
+ return n > toI64(2147483647) ? 2147483647 : toI32(n)
1334
1656
  }
1335
1657
 
1336
1658
  // ---- HTTP/1.1 -------------------------------------------------------------------
@@ -1344,9 +1666,17 @@ export class HttpServer {
1344
1666
  let slot: i32 = h1.accept()
1345
1667
  while (slot >= 0 || slot === H1_POOL_FULL || slot === HTTP_SERVER_ACCEPT_ABORTED) {
1346
1668
  if (slot >= 0) {
1347
- this.h1Open[slot] = true
1348
- this.h1Current[slot] = -1
1349
- pollAdd(this.loop, h1.fd(slot), H1_WANT_READ, slot)
1669
+ const s: HttpServerH1Slot | null = this.h1Slot(slot)
1670
+ if (s === null) {
1671
+ // `listenHttp1` makes a record for every carrier slot, so a slot
1672
+ // without one is no connection the server can serve: it is
1673
+ // closed, never left open and unwatched.
1674
+ h1.close(slot)
1675
+ } else {
1676
+ s.open = true
1677
+ s.current = null
1678
+ pollAdd(this.loop, h1.fd(slot), H1_WANT_READ, slot)
1679
+ }
1350
1680
  }
1351
1681
  slot = h1.accept()
1352
1682
  }
@@ -1363,12 +1693,18 @@ export class HttpServer {
1363
1693
  if (h1 === null || !h1.holds(slot)) {
1364
1694
  return
1365
1695
  }
1696
+ const s: HttpServerH1Slot | null = this.h1Slot(slot)
1697
+ if (s === null) {
1698
+ // As in `acceptHttp1`: a carrier slot without a record is closed.
1699
+ h1.close(slot)
1700
+ return
1701
+ }
1366
1702
  if ((events & HTTP_SERVER_READ_BITS) !== 0) {
1367
1703
  h1.readable(slot)
1368
1704
  } else {
1369
1705
  h1.writable(slot)
1370
1706
  }
1371
- this.drive(h1, slot)
1707
+ this.drive(h1, s)
1372
1708
  }
1373
1709
 
1374
1710
  /**
@@ -1376,37 +1712,46 @@ export class HttpServer {
1376
1712
  * settles it. A head with no free request left waits: the slot stops
1377
1713
  * reading and goes on `starved` until a request is handed back.
1378
1714
  */
1379
- private drive(h1: Http1Server, slot: i32): void {
1380
- if (this.h1Driving[slot] !== 0) {
1381
- this.h1Driving[slot] = 2
1715
+ private drive(h1: Http1Server, s: HttpServerH1Slot): void {
1716
+ if (s.driving !== 0) {
1717
+ s.driving = 2
1382
1718
  return
1383
1719
  }
1384
- this.h1Driving[slot] = 1
1385
- this.drivePass(h1, slot)
1386
- while (this.h1Driving[slot] === 2 && this.h1Open[slot]) {
1387
- this.h1Driving[slot] = 1
1388
- this.drivePass(h1, slot)
1720
+ s.driving = 1
1721
+ this.drivePass(h1, s)
1722
+ while (s.driving === 2 && s.open) {
1723
+ s.driving = 1
1724
+ this.drivePass(h1, s)
1725
+ }
1726
+ s.driving = 0
1727
+ }
1728
+
1729
+ /** `drive` for the slot request `r` was read on; nothing once that slot has no record, as `live` then says too. */
1730
+ private driveOf(h1: Http1Server, r: HttpServerRequest): void {
1731
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
1732
+ if (s !== null) {
1733
+ this.drive(h1, s)
1389
1734
  }
1390
- this.h1Driving[slot] = 0
1391
1735
  }
1392
1736
 
1393
1737
  /** One pass of `drive`: the slot's events until it needs more input, then `settle`. */
1394
- private drivePass(h1: Http1Server, slot: i32): void {
1738
+ private drivePass(h1: Http1Server, s: HttpServerH1Slot): void {
1739
+ const slot: i32 = s.slot
1395
1740
  const conn: Http1Connection = h1.connection(slot)
1396
1741
  let event: i32 = H1_NEED_MORE
1397
- if (!this.starve(slot) && !this.unended(slot)) {
1742
+ if (!this.starve(s) && !this.unended(s)) {
1398
1743
  event = h1.next(slot)
1399
1744
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
1400
1745
  if (event === H1_REQUEST) {
1401
- this.begin(slot, conn)
1746
+ this.begin(s, conn)
1402
1747
  } else if (event === H1_BODY) {
1403
- this.gather(slot, conn)
1748
+ this.gather(s, conn)
1404
1749
  } else if (event === H1_END) {
1405
- this.whole(slot, conn)
1750
+ this.whole(s, conn)
1406
1751
  } else if (event === H1_WRITE) {
1407
- this.resume(slot, conn)
1752
+ this.resume(s, conn)
1408
1753
  }
1409
- if (this.starve(slot) || this.unended(slot)) {
1754
+ if (this.starve(s) || this.unended(s)) {
1410
1755
  break
1411
1756
  }
1412
1757
  event = h1.next(slot)
@@ -1415,9 +1760,9 @@ export class HttpServer {
1415
1760
  if (event === H1_ERROR) {
1416
1761
  // The carrier has written its refusal and ends the connection; a
1417
1762
  // request it was reading is nobody's.
1418
- this.releaseReading(slot)
1763
+ this.releaseReading(s)
1419
1764
  }
1420
- this.settle(h1, slot, conn)
1765
+ this.settle(h1, s, conn)
1421
1766
  }
1422
1767
 
1423
1768
  /**
@@ -1427,23 +1772,22 @@ export class HttpServer {
1427
1772
  * so the slot is not asked until the program's `end`: until then the
1428
1773
  * response is the program's, and a draining connection stays open for it.
1429
1774
  */
1430
- private unended(slot: i32): boolean {
1431
- const at: i32 = this.h1Current[slot]
1432
- if (at < 0) {
1775
+ private unended(s: HttpServerH1Slot): boolean {
1776
+ const r: HttpServerRequest | null = s.current
1777
+ if (r === null) {
1433
1778
  return false
1434
1779
  }
1435
- const r: HttpServerRequest = this.requests[at]
1436
1780
  return r.state === HTTP_SERVER_STREAMING && r.pendingEnd === r.pendingStart
1437
1781
  }
1438
1782
 
1439
- /** Whether `slot` must wait for a free request before it reads a head: it then joins `starved`. */
1440
- private starve(slot: i32): boolean {
1441
- if (this.h1Current[slot] !== -1 || toI32(this.free.length) > 0) {
1783
+ /** Whether `s` must wait for a free request before it reads a head: it then joins `starved`. */
1784
+ private starve(s: HttpServerH1Slot): boolean {
1785
+ if (s.current !== null || toI32(this.free.length) > 0) {
1442
1786
  return false
1443
1787
  }
1444
- if (!this.h1Starved[slot]) {
1445
- this.h1Starved[slot] = true
1446
- this.starved.push(slot)
1788
+ if (!s.starved) {
1789
+ s.starved = true
1790
+ this.starved.push(s)
1447
1791
  }
1448
1792
  return true
1449
1793
  }
@@ -1454,68 +1798,99 @@ export class HttpServer {
1454
1798
  if (h1 === null) {
1455
1799
  return
1456
1800
  }
1457
- while (this.starvedHead < toI32(this.starved.length) && toI32(this.free.length) > 0) {
1458
- const slot: i32 = this.starved[this.starvedHead]
1459
- this.starvedHead += 1
1460
- this.h1Starved[slot] = false
1461
- if (this.h1Open[slot] && h1.holds(slot)) {
1462
- this.drive(h1, slot)
1801
+ const starved: HttpServerH1Slot[] = this.starved
1802
+ while (toI32(this.free.length) > 0) {
1803
+ const head: i32 = this.starvedHead
1804
+ if (head < 0 || head >= toI32(starved.length)) {
1805
+ break
1806
+ }
1807
+ const s: HttpServerH1Slot = starved[head]
1808
+ this.starvedHead = head + 1
1809
+ s.starved = false
1810
+ if (s.open && h1.holds(s.slot)) {
1811
+ this.drive(h1, s)
1463
1812
  }
1464
1813
  }
1465
1814
  // Drop the spent slots once they are half the list, so each is moved O(1) times.
1466
1815
  const spent: i32 = this.starvedHead
1467
- const length: i32 = toI32(this.starved.length)
1468
- if (spent > 0 && spent * 2 >= length) {
1469
- for (let k: i32 = spent; k < length; k++) {
1470
- this.starved[k - spent] = this.starved[k]
1816
+ const length: i32 = toI32(starved.length)
1817
+ if (spent > 0 && spent <= length && spent >= length - spent) {
1818
+ let j: i32 = 0
1819
+ for (let k: i32 = spent; k < toI32(starved.length) && j < toI32(starved.length); k++) {
1820
+ starved[j] = starved[k]
1821
+ j += 1
1471
1822
  }
1472
- for (let k: i32 = 0; k < spent; k++) {
1473
- this.starved.pop()
1823
+ for (let k: i32 = 0; k < spent && toI32(starved.length) > 0; k++) {
1824
+ starved.pop()
1474
1825
  }
1475
1826
  this.starvedHead = 0
1476
1827
  }
1477
1828
  }
1478
1829
 
1479
1830
  /** Sends what `slot` holds and re-arms it, or closes it once it is over; enqueues a parked request once there is room. */
1480
- private settle(h1: Http1Server, slot: i32, conn: Http1Connection): void {
1831
+ private settle(h1: Http1Server, s: HttpServerH1Slot, conn: Http1Connection): void {
1832
+ const slot: i32 = s.slot
1481
1833
  let wants: i32 = h1.flush(slot)
1482
1834
  if ((wants & H1_DONE) !== 0) {
1483
1835
  this.h1Close(h1, slot)
1484
1836
  return
1485
1837
  }
1486
- if (this.draining && this.h1Current[slot] === -1 && !conn.wantsWrite()) {
1838
+ if (this.draining && s.current === null && !conn.wantsWrite()) {
1487
1839
  this.h1Close(h1, slot)
1488
1840
  return
1489
1841
  }
1490
- if (this.h1Starved[slot]) {
1842
+ if (s.starved) {
1491
1843
  wants = wants & ~H1_WANT_READ
1492
1844
  }
1493
1845
  if (h1.rearm(slot, wants)) {
1494
1846
  pollModify(this.loop, h1.fd(slot), wants & (H1_WANT_READ | H1_WANT_WRITE), slot)
1495
1847
  }
1496
- const at: i32 = this.h1Current[slot]
1497
- if (at >= 0 && this.requests[at].state === HTTP_SERVER_PARKED && this.roomy(conn)) {
1498
- this.h1Ready(this.requests[at])
1848
+ const r: HttpServerRequest | null = s.current
1849
+ if (r !== null && r.state === HTTP_SERVER_PARKED && this.roomy(conn)) {
1850
+ this.h1Ready(r)
1499
1851
  }
1500
1852
  }
1501
1853
 
1502
1854
  /** Whether `conn`'s output holds at most half its size unsent, so a response head fits behind it. */
1503
1855
  private roomy(conn: Http1Connection): boolean {
1504
- return (conn.outputEnd - conn.outputStart) * 2 <= this.h1OutputSize
1856
+ return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(this.h1OutputSize) / toI64(2)
1505
1857
  }
1506
1858
 
1507
1859
  /** A head: a free request, the head copied into it, and its route. */
1508
- private begin(slot: i32, conn: Http1Connection): void {
1860
+ private begin(s: HttpServerH1Slot, conn: Http1Connection): void {
1509
1861
  if (this.draining) {
1510
1862
  // A head read during the drain is the connection's last.
1511
1863
  conn.closeAfter = true
1512
1864
  }
1513
- const r: HttpServerRequest = this.requests[this.obtain()]
1865
+ const r: HttpServerRequest | null = this.obtain()
1866
+ if (r === null) {
1867
+ // `drivePass` asked `starve` first, so a request was free; were none,
1868
+ // the head is answered 503 and the connection ends, and the body
1869
+ // events after it find no request and are passed over.
1870
+ conn.closeAfter = true
1871
+ conn.respond(HTTP_SERVER_UNAVAILABLE, this.noFields, this.noFields, toI32(0))
1872
+ return
1873
+ }
1514
1874
  const p = conn.parser
1515
1875
  copyBytes(r.head, HTTP_SERVER_ZERO, p.head, HTTP_SERVER_ZERO, p.headLength)
1516
- const spanCount: i32 = 4 * p.headerCount
1517
- for (let k: i32 = 0; k < spanCount; k++) {
1518
- r.spans[k] = p.spans[k]
1876
+ const from: i32[] = p.spans
1877
+ const into: i32[] = r.spans
1878
+ // Four spans per field. `listenHttp1` refuses a `maxHeaders` past
1879
+ // `maxFields`, the parser stops at `maxHeaders`, and `obtain` made
1880
+ // `into` 4 * `maxFields` long, so every span fits; a head that did not
1881
+ // is refused 431 whole rather than copied in part.
1882
+ const used: i64 = toI64(p.headerCount) * toI64(4)
1883
+ if (p.headerCount < 0 || used > toI64(toI32(from.length)) || used > toI64(toI32(into.length))) {
1884
+ this.free.push(r)
1885
+ conn.closeAfter = true
1886
+ conn.respond(toI32(431), this.noFields, this.noFields, toI32(0))
1887
+ return
1888
+ }
1889
+ // The length tests below only restate the refusal above for the prover,
1890
+ // which keeps no fact from it: they never end the copy early.
1891
+ const n: i32 = toI32(used)
1892
+ for (let k: i32 = 0; k < n && k < toI32(from.length) && k < toI32(into.length); k++) {
1893
+ into[k] = from[k]
1519
1894
  }
1520
1895
  r.headerCount = p.headerCount
1521
1896
  r.methodStart = p.methodStart
@@ -1525,32 +1900,31 @@ export class HttpServer {
1525
1900
  r.kind = HTTP_SERVER_H1
1526
1901
  r.version = 1
1527
1902
  r.secure = false
1528
- r.slot = slot
1529
- r.gen = this.h1Gen[slot]
1903
+ r.slot = s.slot
1904
+ r.gen = s.gen
1530
1905
  if (r.methodIs("CONNECT")) {
1531
1906
  // Its 501 ends the connection: the client may already be sending what
1532
1907
  // it meant for a tunnel.
1533
1908
  conn.closeAfter = true
1534
1909
  }
1535
- this.admit(r)
1536
- this.h1Current[slot] = r.index
1910
+ this.admit(r, false)
1911
+ s.current = r
1537
1912
  }
1538
1913
 
1539
1914
  /** Body bytes: appended to the request's body, which the carrier's cap bounds. */
1540
- private gather(slot: i32, conn: Http1Connection): void {
1541
- const at: i32 = this.h1Current[slot]
1542
- if (at >= 0) {
1543
- this.append(this.requests[at], conn.data, conn.dataStart, conn.dataLength)
1915
+ private gather(s: HttpServerH1Slot, conn: Http1Connection): void {
1916
+ const r: HttpServerRequest | null = s.current
1917
+ if (r !== null) {
1918
+ this.append(r, conn.data, conn.dataStart, conn.dataLength)
1544
1919
  }
1545
1920
  }
1546
1921
 
1547
1922
  /** The request is whole: queued for `take`, or parked while the response before it is still mostly unsent. */
1548
- private whole(slot: i32, conn: Http1Connection): void {
1549
- const at: i32 = this.h1Current[slot]
1550
- if (at < 0) {
1923
+ private whole(s: HttpServerH1Slot, conn: Http1Connection): void {
1924
+ const r: HttpServerRequest | null = s.current
1925
+ if (r === null) {
1551
1926
  return
1552
1927
  }
1553
- const r: HttpServerRequest = this.requests[at]
1554
1928
  if (this.roomy(conn)) {
1555
1929
  this.h1Ready(r)
1556
1930
  } else {
@@ -1574,19 +1948,27 @@ export class HttpServer {
1574
1948
  if (r.methodIs("CONNECT")) {
1575
1949
  return 501
1576
1950
  }
1577
- if (r.targetEnd - r.targetStart === 1 && toI32(r.head[r.targetStart]) === 42 && r.methodIs("OPTIONS")) {
1951
+ const head: u8[] = r.head
1952
+ const target: i32 = r.targetStart
1953
+ const one: boolean = toI64(r.targetEnd) - toI64(target) === toI64(1)
1954
+ if (
1955
+ one &&
1956
+ target >= 0 &&
1957
+ target < toI32(head.length) &&
1958
+ toI32(head[target]) === 42 &&
1959
+ r.methodIs("OPTIONS")
1960
+ ) {
1578
1961
  return 204
1579
1962
  }
1580
1963
  return 0
1581
1964
  }
1582
1965
 
1583
1966
  /** `H1_WRITE`: the carrier has room again for the response under way. */
1584
- private resume(slot: i32, conn: Http1Connection): void {
1585
- const at: i32 = this.h1Current[slot]
1586
- if (at < 0) {
1967
+ private resume(s: HttpServerH1Slot, conn: Http1Connection): void {
1968
+ const r: HttpServerRequest | null = s.current
1969
+ if (r === null) {
1587
1970
  return
1588
1971
  }
1589
- const r: HttpServerRequest = this.requests[at]
1590
1972
  if (r.state !== HTTP_SERVER_STREAMING && r.state !== HTTP_SERVER_FLUSHING) {
1591
1973
  return
1592
1974
  }
@@ -1598,7 +1980,7 @@ export class HttpServer {
1598
1980
 
1599
1981
  /** Hands the carrier as much of `r`'s pending bytes as it takes. */
1600
1982
  private pushH1(r: HttpServerRequest, conn: Http1Connection): void {
1601
- const left: i32 = r.pendingEnd - r.pendingStart
1983
+ const left: i32 = toI32(toI64(r.pendingEnd) - toI64(r.pendingStart))
1602
1984
  if (left === 0) {
1603
1985
  return
1604
1986
  }
@@ -1618,16 +2000,22 @@ export class HttpServer {
1618
2000
  if (conn.end() === H1_AGAIN) {
1619
2001
  return
1620
2002
  }
1621
- this.h1Current[r.slot] = -1
2003
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
2004
+ if (s !== null) {
2005
+ const held: HttpServerRequest | null = s.current
2006
+ if (held !== null && held.index === r.index) {
2007
+ s.current = null
2008
+ }
2009
+ }
1622
2010
  this.release(r)
1623
2011
  }
1624
2012
 
1625
- /** Hands back the request `slot` was reading when the carrier refused it. */
1626
- private releaseReading(slot: i32): void {
1627
- const at: i32 = this.h1Current[slot]
1628
- if (at >= 0 && this.requests[at].state === HTTP_SERVER_READING) {
1629
- this.h1Current[slot] = -1
1630
- this.release(this.requests[at])
2013
+ /** Hands back the request `s` was reading when the carrier refused it. */
2014
+ private releaseReading(s: HttpServerH1Slot): void {
2015
+ const r: HttpServerRequest | null = s.current
2016
+ if (r !== null && r.state === HTTP_SERVER_READING) {
2017
+ s.current = null
2018
+ this.release(r)
1631
2019
  }
1632
2020
  }
1633
2021
 
@@ -1638,24 +2026,27 @@ export class HttpServer {
1638
2026
  */
1639
2027
  private h1Close(h1: Http1Server, slot: i32): void {
1640
2028
  h1.close(slot)
1641
- this.h1Forget(slot)
2029
+ const s: HttpServerH1Slot | null = this.h1Slot(slot)
2030
+ if (s !== null) {
2031
+ this.h1Forget(s)
2032
+ }
1642
2033
  }
1643
2034
 
1644
2035
  /** The bookkeeping of a slot the carrier has closed. */
1645
- private h1Forget(slot: i32): void {
1646
- this.h1Open[slot] = false
1647
- this.h1Gen[slot] = this.h1Gen[slot] + 1
1648
- const at: i32 = this.h1Current[slot]
1649
- this.h1Current[slot] = -1
1650
- if (at >= 0) {
1651
- this.drop(this.requests[at])
2036
+ private h1Forget(s: HttpServerH1Slot): void {
2037
+ s.open = false
2038
+ s.gen = httpServerNextGen(s.gen)
2039
+ const r: HttpServerRequest | null = s.current
2040
+ s.current = null
2041
+ if (r !== null) {
2042
+ this.drop(r)
1652
2043
  }
1653
2044
  }
1654
2045
 
1655
2046
  /** Every slot the carrier's `expire` closed. */
1656
2047
  private h1Sweep(h1: Http1Server): void {
1657
- for (let s: i32 = 0; s < toI32(this.h1Open.length); s++) {
1658
- if (this.h1Open[s] && !h1.holds(s)) {
2048
+ for (const s of this.h1Slots) {
2049
+ if (s.open && !h1.holds(s.slot)) {
1659
2050
  this.h1Forget(s)
1660
2051
  }
1661
2052
  }
@@ -1663,27 +2054,42 @@ export class HttpServer {
1663
2054
 
1664
2055
  // ---- The pool -------------------------------------------------------------------
1665
2056
 
1666
- /** The index of a free request, its buffers made at their caps on first use; -1 when none is free. */
1667
- private obtain(): i32 {
1668
- const count: i32 = toI32(this.free.length)
1669
- if (count === 0) {
1670
- return -1
2057
+ /** A free request, its buffers made at their caps on first use; null when none is free. */
2058
+ private obtain(): HttpServerRequest | null {
2059
+ const free: HttpServerRequest[] = this.free
2060
+ const last: i32 = toI32(free.length) - 1
2061
+ if (last < 0) {
2062
+ return null
2063
+ }
2064
+ const r: HttpServerRequest = free[last]
2065
+ // `last` proved the index; the prover keeps no length fact past that
2066
+ // read, so the pop is guarded again rather than left a site.
2067
+ if (free.length > 0) {
2068
+ free.pop()
1671
2069
  }
1672
- const at: i32 = this.free[count - 1]
1673
- this.free.pop()
1674
- const r: HttpServerRequest = this.requests[at]
1675
2070
  if (toI32(r.head.length) === 0) {
1676
- r.head = new Array<u8>(this.maxHead)
1677
- r.spans = new Array<i32>(4 * this.maxFields)
1678
- r.params = new Array<i32>(2 * this.router.maxParams + 2)
2071
+ r.head = httpServerZeros(toI64(this.maxHead), toU8(0))
2072
+ r.spans = httpServerCells(toI64(4) * toI64(this.maxFields))
2073
+ r.params = httpServerZeros(toI64(2) * toI64(this.router.maxParams) + toI64(2), toI32(0))
1679
2074
  }
1680
- return at
2075
+ return r
1681
2076
  }
1682
2077
 
1683
- /** A head is in `r`: its path's end found, its reading state set and its route matched. */
1684
- private admit(r: HttpServerRequest): void {
2078
+ /**
2079
+ * A head is in `r`: its path's end found, its reading state set and its
2080
+ * route matched. Its body buffer grows to the declared length unless
2081
+ * `mayStream` (an HTTP/2 request whose body is still to come) and its route
2082
+ * takes the body in pieces, which never hold more than a stream window.
2083
+ */
2084
+ private admit(r: HttpServerRequest, mayStream: boolean): void {
2085
+ const head: u8[] = r.head
1685
2086
  let pathEnd: i32 = r.targetStart
1686
- while (pathEnd < r.targetEnd && toI32(r.head[pathEnd]) !== 63) {
2087
+ while (
2088
+ pathEnd >= 0 &&
2089
+ pathEnd < r.targetEnd &&
2090
+ pathEnd < toI32(head.length) &&
2091
+ toI32(head[pathEnd]) !== 63
2092
+ ) {
1687
2093
  pathEnd += 1
1688
2094
  }
1689
2095
  r.pathEnd = pathEnd
@@ -1696,9 +2102,14 @@ export class HttpServer {
1696
2102
  r.quiet = r.methodIs("HEAD")
1697
2103
  r.left = -1
1698
2104
  r.ended = false
2105
+ r.trailing = false
2106
+ r.streaming = false
2107
+ r.bodyEnded = true
2108
+ r.stepped = false
2109
+ r.added = 0
1699
2110
  this.route(r)
1700
2111
  const declared: i32 = this.declared(r)
1701
- if (declared > toI32(r.body.length)) {
2112
+ if (declared > toI32(r.body.length) && !(mayStream && this.streamsBody(r.route))) {
1702
2113
  this.growBody(r, declared)
1703
2114
  }
1704
2115
  }
@@ -1720,12 +2131,15 @@ export class HttpServer {
1720
2131
  params
1721
2132
  )
1722
2133
  if (route === ROUTER_METHOD_NOT_ALLOWED && r.quiet) {
1723
- const get: i32 = this.router.methodIndex("GET")
1724
- if (get >= 0) {
1725
- const viaGet: i32 = this.router.match(
1726
- this.router.methodBytes,
1727
- this.router.methodStart[get],
1728
- this.router.methodEnd[get],
2134
+ const router: Router = this.router
2135
+ const get: i32 = router.methodIndex("GET")
2136
+ const starts: i32[] = router.methodStart
2137
+ const ends: i32[] = router.methodEnd
2138
+ if (get >= 0 && get < toI32(starts.length) && get < toI32(ends.length)) {
2139
+ const viaGet: i32 = router.match(
2140
+ router.methodBytes,
2141
+ starts[get],
2142
+ ends[get],
1729
2143
  r.head,
1730
2144
  r.targetStart,
1731
2145
  r.targetEnd,
@@ -1742,7 +2156,9 @@ export class HttpServer {
1742
2156
 
1743
2157
  /** Appends `buf[start .. start + n)` to `r`'s body; the caller has kept it within `maxBody`. */
1744
2158
  private append(r: HttpServerRequest, buf: u8[], start: i32, n: i32): void {
1745
- const need: i32 = r.bodyLength + n
2159
+ // The caller's `maxBody` check keeps the sum inside an `i32`.
2160
+ const wide: i64 = toI64(r.bodyLength) + toI64(n)
2161
+ const need: i32 = wide > toI64(2147483647) ? 2147483647 : toI32(wide)
1746
2162
  if (toI32(r.body.length) < need) {
1747
2163
  this.growBody(r, need)
1748
2164
  }
@@ -1763,7 +2179,12 @@ export class HttpServer {
1763
2179
  const tier: i32 = this.maxBody < HTTP_SERVER_BODY_TIER ? this.maxBody : HTTP_SERVER_BODY_TIER
1764
2180
  let size: i32 = this.maxBody
1765
2181
  if (need <= tier) {
1766
- size = have < HTTP_SERVER_BODY_FIRST ? HTTP_SERVER_BODY_FIRST : have * 2
2182
+ // Doubling past i32 is past `tier` too, which the clamp below gives.
2183
+ let doubled: i32 = tier
2184
+ if (have < 1073741824) {
2185
+ doubled = have * 2
2186
+ }
2187
+ size = have < HTTP_SERVER_BODY_FIRST ? HTTP_SERVER_BODY_FIRST : doubled
1767
2188
  if (size < need) {
1768
2189
  size = need
1769
2190
  }
@@ -1787,13 +2208,16 @@ export class HttpServer {
1787
2208
  if (i < 0) {
1788
2209
  return -1
1789
2210
  }
2211
+ const head: u8[] = r.head
2212
+ const end: i32 = r.valueEnd(i)
2213
+ const ten: i64 = toI64(HTTP_SERVER_TEN)
1790
2214
  let n: i32 = 0
1791
- for (let k: i32 = r.valueStart(i); k < r.valueEnd(i); k++) {
1792
- const d: i32 = toI32(r.head[k]) - HTTP_SERVER_ZERO_DIGIT
1793
- if (d < 0 || d > 9 || n > (this.maxBody - d) / HTTP_SERVER_TEN) {
2215
+ for (let k: i32 = r.valueStart(i); k >= 0 && k < end && k < toI32(head.length); k++) {
2216
+ const d: i32 = toI32(head[k]) - HTTP_SERVER_ZERO_DIGIT
2217
+ if (d < 0 || d > 9 || toI64(n) > (toI64(this.maxBody) - toI64(d)) / ten) {
1794
2218
  return -1
1795
2219
  }
1796
- n = n * HTTP_SERVER_TEN + d
2220
+ n = toI32(toI64(n) * ten + toI64(d))
1797
2221
  }
1798
2222
  return n
1799
2223
  }
@@ -1801,9 +2225,47 @@ export class HttpServer {
1801
2225
  /** Queues `r` for `take`. The ring holds every request, so it never fills. */
1802
2226
  private enqueue(r: HttpServerRequest): void {
1803
2227
  r.state = HTTP_SERVER_QUEUED
1804
- const size: i32 = toI32(this.queue.length)
1805
- this.queue[(this.queueStart + this.queueCount) % size] = r.index
1806
- this.queueCount += 1
2228
+ this.queuePush(r.index)
2229
+ }
2230
+
2231
+ /** Puts request `index` at the back of the ring; nothing when the ring is full, which it never is. */
2232
+ private queuePush(index: i32): void {
2233
+ const queue: i32[] = this.queue
2234
+ const size: i32 = toI32(queue.length)
2235
+ const count: i32 = this.queueCount
2236
+ const start: i32 = this.queueStart
2237
+ if (count < 0 || count >= size || start < 0 || start >= size) {
2238
+ return
2239
+ }
2240
+ // Both are below `size`, so one wrap brings the sum back into the ring.
2241
+ const sum: i64 = toI64(start) + toI64(count)
2242
+ const at: i32 = toI32(sum >= toI64(size) ? sum - toI64(size) : sum)
2243
+ if (at >= 0 && at < toI32(queue.length)) {
2244
+ queue[at] = index
2245
+ this.queueCount = count + 1
2246
+ }
2247
+ }
2248
+
2249
+ /** The request at the front of the ring, or null when it is empty. */
2250
+ private queueFront(): HttpServerRequest | null {
2251
+ const queue: i32[] = this.queue
2252
+ const start: i32 = this.queueStart
2253
+ if (this.queueCount <= 0 || start < 0 || start >= toI32(queue.length)) {
2254
+ return null
2255
+ }
2256
+ return this.requestAt(queue[start])
2257
+ }
2258
+
2259
+ /** Drops the front of the ring. */
2260
+ private queueShift(): void {
2261
+ const start: i32 = this.queueStart
2262
+ const count: i32 = this.queueCount
2263
+ if (count <= 0 || start < 0 || start >= toI32(this.queue.length)) {
2264
+ return
2265
+ }
2266
+ const after: i64 = toI64(start) + toI64(1)
2267
+ this.queueStart = after < toI64(toI32(this.queue.length)) ? toI32(after) : 0
2268
+ this.queueCount = count - 1
1807
2269
  }
1808
2270
 
1809
2271
  /** Hands `r` back to the pool, and frees its HTTP/2 or HTTP/3 stream entry. */
@@ -1811,9 +2273,57 @@ export class HttpServer {
1811
2273
  if (r.state === HTTP_SERVER_FREE) {
1812
2274
  return
1813
2275
  }
2276
+ if (r.again) {
2277
+ this.unqueue(r)
2278
+ }
2279
+ if (r.streaming && !r.bodyEnded && this.live(r)) {
2280
+ // Answered before the body ended: the client may stop sending (RFC 9113 §8.1). With no room for
2281
+ // the reset the stream stays held, so the client's window, not this server, bounds what it sends.
2282
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
2283
+ if (conn !== null) {
2284
+ conn.reset(toI32(r.stream), H2_NO_ERROR)
2285
+ }
2286
+ }
1814
2287
  this.unlink(r)
1815
2288
  r.state = HTTP_SERVER_FREE
1816
- this.free.push(r.index)
2289
+ this.free.push(r)
2290
+ }
2291
+
2292
+ /**
2293
+ * Queues held streaming request `r` again for the program, once until `take`
2294
+ * hands it over, to say a piece, the end of its body or a reset came. One
2295
+ * queued for the first time is already there, and one whose answer is
2296
+ * flushing is past caring.
2297
+ */
2298
+ private requeue(r: HttpServerRequest): void {
2299
+ if (r.again || (r.state !== HTTP_SERVER_HELD && r.state !== HTTP_SERVER_STREAMING)) {
2300
+ return
2301
+ }
2302
+ r.again = true
2303
+ this.queuePush(r.index)
2304
+ }
2305
+
2306
+ /** Takes `r`'s second place out of the queue, closing the gap so the ring still holds each request at most once. */
2307
+ private unqueue(r: HttpServerRequest): void {
2308
+ r.again = false
2309
+ const queue: i32[] = this.queue
2310
+ let found: boolean = false
2311
+ let before: i32 = -1
2312
+ let at: i32 = this.queueStart
2313
+ for (let k: i32 = 0; k < this.queueCount && at >= 0 && at < toI32(queue.length); k++) {
2314
+ if (found && before >= 0 && before < toI32(queue.length)) {
2315
+ queue[before] = queue[at]
2316
+ } else if (queue[at] === r.index) {
2317
+ found = true
2318
+ }
2319
+ before = at
2320
+ const after: i64 = toI64(at) + toI64(1)
2321
+ at = after < toI64(toI32(queue.length)) ? toI32(after) : 0
2322
+ }
2323
+ const count: i32 = this.queueCount
2324
+ if (found && count > 0) {
2325
+ this.queueCount = count - 1
2326
+ }
1817
2327
  }
1818
2328
 
1819
2329
  /**
@@ -1833,21 +2343,25 @@ export class HttpServer {
1833
2343
 
1834
2344
  /** `n` of `r`'s pending bytes were taken. */
1835
2345
  private taken(r: HttpServerRequest, n: i32): void {
1836
- r.pendingStart += n
1837
- if (r.pendingStart === r.pendingEnd) {
2346
+ // A carrier takes at most what it was offered, so this never passes `pendingEnd`.
2347
+ const next: i64 = toI64(r.pendingStart) + toI64(n)
2348
+ if (next >= toI64(r.pendingEnd)) {
1838
2349
  r.pendingStart = 0
1839
2350
  r.pendingEnd = 0
2351
+ } else {
2352
+ r.pendingStart = toI32(next)
1840
2353
  }
1841
2354
  }
1842
2355
 
1843
2356
  /**
1844
2357
  * Appends bytes to `r`'s pending buffer, grown to
1845
2358
  * `min(2^30, max(needed, 2 × current))`; `write` has refused a remainder
1846
- * past 2^30.
2359
+ * past 2^30. False, with nothing kept, when the bytes still do not fit
2360
+ * or the window lies outside `buf`: `write` then cuts the response.
1847
2361
  */
1848
- private keep(r: HttpServerRequest, buf: u8[], off: i32, len: i32): void {
1849
- const live: i32 = r.pendingEnd - r.pendingStart
1850
- if (toI32(r.pending.length) - r.pendingEnd < len) {
2362
+ private keep(r: HttpServerRequest, buf: u8[], off: i32, len: i32): boolean {
2363
+ const live: i32 = toI32(toI64(r.pendingEnd) - toI64(r.pendingStart))
2364
+ if (toI64(r.pending.length) - toI64(r.pendingEnd) < toI64(len)) {
1851
2365
  const need: i64 = toI64(live) + toI64(len)
1852
2366
  const target: u8[] =
1853
2367
  toI64(r.pending.length) < need
@@ -1858,46 +2372,239 @@ export class HttpServer {
1858
2372
  r.pendingStart = 0
1859
2373
  r.pendingEnd = live
1860
2374
  }
1861
- copyBytes(r.pending, r.pendingEnd, buf, off, len)
1862
- r.pendingEnd += len
2375
+ const end: i64 = toI64(r.pendingEnd) + toI64(len)
2376
+ if (end > toI64(r.pending.length) || !tryCopyBytes(r.pending, r.pendingEnd, buf, off, len)) {
2377
+ return false
2378
+ }
2379
+ r.pendingEnd = toI32(end)
2380
+ return true
1863
2381
  }
1864
2382
 
1865
2383
  // ---- Requests -------------------------------------------------------------------
1866
2384
 
1867
2385
  /**
1868
- * Whether a whole request is waiting for `take`. Requests whose connection
1869
- * or stream closed while queued are handed back here.
1870
- * @returns true when `take` will give out a request whose connection is still open
2386
+ * Whether a request is waiting for `take`: a whole one, the head of a
2387
+ * `streaming` one, or a streaming one the program holds coming back with
2388
+ * a piece, its end or a reset. Requests whose connection or stream closed
2389
+ * while first queued are handed back here, unseen.
2390
+ * @returns true when `take` will give out a request
1871
2391
  */
1872
2392
  ready(): boolean {
2393
+ return this.next() !== null
2394
+ }
2395
+
2396
+ /** The request `take` gives out next, after handing back the ones queued unseen, or null for none. */
2397
+ private next(): HttpServerRequest | null {
1873
2398
  while (this.queueCount > 0) {
1874
- const r: HttpServerRequest = this.requests[this.queue[this.queueStart]]
1875
- if (this.live(r)) {
1876
- return true
2399
+ const r: HttpServerRequest | null = this.queueFront()
2400
+ if (r === null) {
2401
+ // Not a request: skip it.
2402
+ this.queueShift()
2403
+ continue
2404
+ }
2405
+ // A streaming request queued again is the program's, live or not: it learns of a reset by taking it.
2406
+ // One whose answer ended since (`end`, `trailers`) is flushing on its own, and stays out of `take`.
2407
+ const held: boolean = r.state === HTTP_SERVER_HELD || r.state === HTTP_SERVER_STREAMING
2408
+ if ((r.again && held) || (!r.again && this.live(r))) {
2409
+ return r
2410
+ }
2411
+ this.queueShift()
2412
+ if (r.again) {
2413
+ r.again = false
2414
+ } else {
2415
+ this.release(r)
1877
2416
  }
1878
- this.queueStart = (this.queueStart + 1) % toI32(this.queue.length)
1879
- this.queueCount -= 1
1880
- this.release(r)
1881
2417
  }
1882
- return false
2418
+ return null
1883
2419
  }
1884
2420
 
1885
2421
  /**
1886
- * The next whole request, of any version, which the program now owes an
1887
- * answer. Panics when none is `ready`.
2422
+ * The next request, of any version, which the program now owes an answer;
2423
+ * a `streaming` request it already holds comes out again each time more
2424
+ * of its body, its end or a reset arrives, at most once per wait. Panics
2425
+ * when none is `ready`.
1888
2426
  * @returns the oldest queued request, the program's until it answers it
1889
2427
  */
1890
2428
  take(): HttpServerRequest {
1891
- if (!this.ready()) {
2429
+ const r: HttpServerRequest | null = this.next()
2430
+ if (r === null) {
1892
2431
  panic("HttpServer.take: no request is ready")
1893
2432
  }
1894
- const r: HttpServerRequest = this.requests[this.queue[this.queueStart]]
1895
- this.queueStart = (this.queueStart + 1) % toI32(this.queue.length)
1896
- this.queueCount -= 1
1897
- r.state = HTTP_SERVER_HELD
2433
+ this.queueShift()
2434
+ if (r.again) {
2435
+ r.again = false
2436
+ } else {
2437
+ r.state = HTTP_SERVER_HELD
2438
+ }
1898
2439
  return r
1899
2440
  }
1900
2441
 
2442
+ /**
2443
+ * Marks `route` as taking its request body in pieces on the HTTP/2
2444
+ * listeners: such a request comes out of `take` at its head, with
2445
+ * `streaming` set, and again whenever a piece, the end of the body or a
2446
+ * reset arrives. The program reads `body` up to `bodyLength`, hands bytes
2447
+ * back with `consume`, and only then does the peer get window to send
2448
+ * more. HTTP/1.1 and HTTP/3 requests to `route` keep the whole body, up to
2449
+ * `maxBody`. Panics when `route` is not one of the router's.
2450
+ * @param route a route index the server's router gave out
2451
+ */
2452
+ streamBody(route: i32): void {
2453
+ this.router.paramCount(route)
2454
+ while (toI32(this.streamRoutes.length) <= route) {
2455
+ this.streamRoutes.push(false)
2456
+ }
2457
+ const routes: boolean[] = this.streamRoutes
2458
+ const count: i32 = this.streamCount
2459
+ if (route >= 0 && route < toI32(routes.length) && !routes[route]) {
2460
+ routes[route] = true
2461
+ // One per route, and a router has far fewer than two billion.
2462
+ if (count < 2147483647) {
2463
+ this.streamCount = count + 1
2464
+ }
2465
+ }
2466
+ }
2467
+
2468
+ /** Whether `route` takes its body in pieces. */
2469
+ private streamsBody(route: i32): boolean {
2470
+ return route >= 0 && route < toI32(this.streamRoutes.length) && this.streamRoutes[route]
2471
+ }
2472
+
2473
+ /**
2474
+ * Hands the first `n` bytes of `r`'s body back: what follows moves to the
2475
+ * front of `body` and `bodyLength` drops by `n`. For a `streaming` request
2476
+ * the peer is credited `n` bytes of window, so it may send that much more.
2477
+ * Panics when `r` is another server's, is not the program's, or `n` is
2478
+ * negative or past `bodyLength`.
2479
+ * @param r a request the program holds
2480
+ * @param n how many bytes from the front of `body` the program is done with
2481
+ */
2482
+ consume(r: HttpServerRequest, n: i32): void {
2483
+ this.own(r, "consume")
2484
+ if (r.state !== HTTP_SERVER_HELD && r.state !== HTTP_SERVER_STREAMING) {
2485
+ panic("HttpServer.consume: the request is not the program's")
2486
+ }
2487
+ const length: i32 = r.bodyLength
2488
+ if (n < 0 || n > length) {
2489
+ panic(`HttpServer.consume: ${n} bytes is outside the body's ${length}`)
2490
+ }
2491
+ const left: i32 = toI32(toI64(length) - toI64(n))
2492
+ copyBytes(r.body, HTTP_SERVER_ZERO, r.body, n, left)
2493
+ r.bodyLength = left
2494
+ if (r.streaming && n > 0 && this.live(r)) {
2495
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
2496
+ if (conn !== null) {
2497
+ conn.consumeBody(toI32(r.stream), n)
2498
+ }
2499
+ this.h2Settle(r.slot)
2500
+ }
2501
+ }
2502
+
2503
+ /**
2504
+ * How many bytes of `r`'s response are written but not yet sent, so a
2505
+ * program streaming an answer can wait for them to drain.
2506
+ * @param r one of this server's requests
2507
+ * @returns the response bytes the server still holds for `r`
2508
+ */
2509
+ pending(r: HttpServerRequest): i32 {
2510
+ this.own(r, "pending")
2511
+ return toI32(toI64(r.pendingEnd) - toI64(r.pendingStart))
2512
+ }
2513
+
2514
+ /**
2515
+ * The next step to run before `r`'s route body, lowest id first, or
2516
+ * `HANDLER_NEXT` once every step `Router.use` bound to `r.route` has run,
2517
+ * or `HANDLER_ANSWERED` once a step has answered `r`; the same on every
2518
+ * version. A step reads `r`, and may answer it — `refuse`, `problem`,
2519
+ * `empty` or any other answer — or add answer fields with `field`; call
2520
+ * `before` again straight after each step, before the next `take`, until it
2521
+ * answers a negative code. Only a request `take` handed over runs steps: the
2522
+ * answers the server sends itself run none. Panics on another server's request.
2523
+ *
2524
+ * let s = server.before(req)
2525
+ * while (s >= 0) { switch (s) { case AUTH: ... } s = server.before(req) }
2526
+ * if (s === HANDLER_NEXT) { switch (req.route) { ... } }
2527
+ *
2528
+ * @param r a request `take` handed over
2529
+ * @returns the next step's id, `HANDLER_NEXT`, or `HANDLER_ANSWERED`
2530
+ */
2531
+ before(r: HttpServerRequest): i32 {
2532
+ this.own(r, "before")
2533
+ if (r.state !== HTTP_SERVER_HELD) {
2534
+ return HANDLER_ANSWERED
2535
+ }
2536
+ if (!r.stepped) {
2537
+ r.stepped = true
2538
+ r.stepsLeft = this.router.steps(r.route)
2539
+ r.stepsRan = 0
2540
+ }
2541
+ const left: i32 = r.stepsLeft
2542
+ if (left === 0) {
2543
+ return HANDLER_NEXT
2544
+ }
2545
+ const low: i32 = toI32(toI64(left) & (toI64(0) - toI64(left)))
2546
+ r.stepsLeft = left ^ low
2547
+ r.stepsRan = r.stepsRan | low
2548
+ return toI32(31) - toI32(Math.clz32(toU32(low)))
2549
+ }
2550
+
2551
+ /**
2552
+ * The next step to run after `r`'s route body has chosen its answer and
2553
+ * before it sends it: the steps `before` handed out, highest id first, then
2554
+ * `HANDLER_NEXT`; or `HANDLER_ANSWERED` once a step has answered `r`. A
2555
+ * step here may still `field` or answer in the body's place. A request
2556
+ * `before` was never called for runs no step here. Panics on another
2557
+ * server's request.
2558
+ * @param r a request `take` handed over, not yet answered
2559
+ * @returns the next step's id, `HANDLER_NEXT`, or `HANDLER_ANSWERED`
2560
+ */
2561
+ after(r: HttpServerRequest): i32 {
2562
+ this.own(r, "after")
2563
+ if (r.state !== HTTP_SERVER_HELD) {
2564
+ return HANDLER_ANSWERED
2565
+ }
2566
+ const ran: i32 = r.stepped ? r.stepsRan : 0
2567
+ if (ran === 0) {
2568
+ return HANDLER_NEXT
2569
+ }
2570
+ const step: i32 = toI32(31) - toI32(Math.clz32(toU32(ran)))
2571
+ r.stepsRan = ran ^ (1 << step)
2572
+ return step
2573
+ }
2574
+
2575
+ /**
2576
+ * Adds the field `name: value` to `r`'s answer, after the fields the answer
2577
+ * names itself, whichever answer it is and on every version; the added
2578
+ * fields count against `maxResponseFields`. Both are kept by reference, so
2579
+ * they stay the caller's bytes, unchanged, until the answer starts. Panics
2580
+ * past `HANDLER_MAX_ADDED` fields, on a request not held, or on another
2581
+ * server's request; a name or value a carrier refuses panics as the
2582
+ * answer's own would.
2583
+ * @param r a request `take` handed over, not yet answered
2584
+ * @param name the field name, a lowercase token
2585
+ * @param value the field value, field text
2586
+ */
2587
+ field(r: HttpServerRequest, name: u8[], value: u8[]): void {
2588
+ this.own(r, "field")
2589
+ if (r.state !== HTTP_SERVER_HELD) {
2590
+ panic(`HttpServer.field: the request is not one taken and unanswered (state ${r.state})`)
2591
+ }
2592
+ const at: i32 = r.added
2593
+ if (at >= HANDLER_MAX_ADDED) {
2594
+ panic(`HttpServer.field: more than ${HANDLER_MAX_ADDED} fields added to one answer`)
2595
+ }
2596
+ const names: u8[][] = r.addedNames
2597
+ const values: u8[][] = r.addedValues
2598
+ if (at >= 0 && at < toI32(names.length) && at < toI32(values.length)) {
2599
+ names[at] = name
2600
+ values[at] = value
2601
+ } else {
2602
+ r.addedNames.push(name)
2603
+ r.addedValues.push(value)
2604
+ }
2605
+ r.added = at + 1
2606
+ }
2607
+
1901
2608
  /**
1902
2609
  * Whether the client of `r` has hung up: its connection is closed or
1903
2610
  * reused, its HTTP/2 or HTTP/3 stream was reset, or, on HTTP/1.1 and
@@ -1915,7 +2622,8 @@ export class HttpServer {
1915
2622
  return true
1916
2623
  }
1917
2624
  if (r.kind === HTTP_SERVER_H2) {
1918
- return this.h2Conn(r.slot).inputEnded
2625
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
2626
+ return conn === null || conn.inputEnded
1919
2627
  }
1920
2628
  const h1: Http1Server | null = this.h1
1921
2629
  return r.kind === HTTP_SERVER_H1 && h1 !== null && h1.connection(r.slot).ended()
@@ -1929,17 +2637,30 @@ export class HttpServer {
1929
2637
  }
1930
2638
  }
1931
2639
 
2640
+ /** HTTP/1.1 slot `slot`'s record, or null for a number outside the table `listenHttp1` made one per carrier slot. */
2641
+ private h1Slot(slot: i32): HttpServerH1Slot | null {
2642
+ const slots: HttpServerH1Slot[] = this.h1Slots
2643
+ return slot >= 0 && slot < toI32(slots.length) ? slots[slot] : null
2644
+ }
2645
+
2646
+ /** Request `at` of the pool, or null for -1 or any other number outside it. */
2647
+ private requestAt(at: i32): HttpServerRequest | null {
2648
+ const requests: HttpServerRequest[] = this.requests
2649
+ return at >= 0 && at < toI32(requests.length) ? requests[at] : null
2650
+ }
2651
+
1932
2652
  /** Whether `r`'s connection is the one it was read on, still open, and its stream not reset. */
1933
2653
  private live(r: HttpServerRequest): boolean {
1934
2654
  if (r.kind === HTTP_SERVER_H1) {
1935
2655
  const h1: Http1Server | null = this.h1
1936
- return h1 !== null && h1.holds(r.slot) && this.h1Gen[r.slot] === r.gen
2656
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
2657
+ return h1 !== null && s !== null && h1.holds(r.slot) && s.gen === r.gen
1937
2658
  }
1938
2659
  if (r.hung) {
1939
2660
  return false
1940
2661
  }
1941
2662
  if (r.kind === HTTP_SERVER_H2) {
1942
- return this.h2Holds(r.slot) && this.h2Gen[r.slot] === r.gen
2663
+ return this.h2Holds(r.slot) && httpServerAt(this.h2Gen, r.slot) === r.gen
1943
2664
  }
1944
2665
  const h3: Http3Server | null = this.h3
1945
2666
  return h3 !== null && h3.holds(r.slot) && h3.connection(r.slot).generation === r.gen
@@ -1953,17 +2674,18 @@ export class HttpServer {
1953
2674
  return
1954
2675
  }
1955
2676
  if (r.kind === HTTP_SERVER_H2) {
1956
- if (this.h2Reqs[e] === r.index && this.h2Gen[r.slot] === r.gen) {
1957
- this.h2Reqs[e] = -1
1958
- this.h2Count[r.slot] = this.h2Count[r.slot] - 1
1959
- if (this.h2Count[r.slot] === 0) {
1960
- this.h2IdleSince[r.slot] = this.turnAt
2677
+ const reqs: i32[] = this.h2Reqs
2678
+ if (e < toI32(reqs.length) && reqs[e] === r.index && httpServerAt(this.h2Gen, r.slot) === r.gen) {
2679
+ reqs[e] = -1
2680
+ if (httpServerAdd(this.h2Count, r.slot, -1) === 0) {
2681
+ this.h2Idle(r.slot, this.turnAt)
1961
2682
  }
1962
2683
  }
1963
2684
  } else if (r.kind === HTTP_SERVER_H3) {
1964
- if (this.h3Reqs[e] === r.index && this.h3Gen[r.slot] === r.gen) {
1965
- this.h3Reqs[e] = -1
1966
- this.h3Count[r.slot] = this.h3Count[r.slot] - 1
2685
+ const reqs: i32[] = this.h3Reqs
2686
+ if (e < toI32(reqs.length) && reqs[e] === r.index && httpServerAt(this.h3Gen, r.slot) === r.gen) {
2687
+ reqs[e] = -1
2688
+ httpServerAdd(this.h3Count, r.slot, -1)
1967
2689
  }
1968
2690
  }
1969
2691
  }
@@ -2015,7 +2737,11 @@ export class HttpServer {
2015
2737
  return HANDLER_DROPPED
2016
2738
  }
2017
2739
  if (r.kind === HTTP_SERVER_H1) {
2018
- const conn: Http1Connection = this.h1Of().connection(r.slot)
2740
+ const h1: Http1Server | null = this.h1
2741
+ if (h1 === null) {
2742
+ return HANDLER_DROPPED // `live` holds no HTTP/1.1 request without the listener
2743
+ }
2744
+ const conn: Http1Connection = h1.connection(r.slot)
2019
2745
  const sent: i32 = conn.respond(
2020
2746
  status,
2021
2747
  this.outNames,
@@ -2036,16 +2762,22 @@ export class HttpServer {
2036
2762
  return HANDLER_SENT
2037
2763
  }
2038
2764
  const endNow: boolean = r.quiet || bodyLength === 0
2039
- let sent: i32 = 0
2765
+ let sent: i32 = -1
2040
2766
  if (r.kind === HTTP_SERVER_H2) {
2041
- sent = this.h2Conn(r.slot).respond(toI32(r.stream), status, this.outNames, this.outValues, endNow)
2767
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
2768
+ if (conn !== null) {
2769
+ sent = conn.respond(toI32(r.stream), status, this.outNames, this.outValues, endNow)
2770
+ }
2042
2771
  if (sent === H2_INVALID) {
2043
2772
  panic(
2044
2773
  "HttpServer.start: the HTTP/2 carrier refused the head: a name that is not a lowercase token, a reserved field, or a value that is not field text"
2045
2774
  )
2046
2775
  }
2047
2776
  } else {
2048
- sent = this.h3Of().connection(r.slot).respond(r.stream, status, this.outNames, this.outValues, endNow)
2777
+ const h3: Http3Server | null = this.h3
2778
+ if (h3 !== null) {
2779
+ sent = h3.connection(r.slot).respond(r.stream, status, this.outNames, this.outValues, endNow)
2780
+ }
2049
2781
  if (sent === H3_INVALID) {
2050
2782
  panic(
2051
2783
  "HttpServer.start: the HTTP/3 carrier refused the head: a name that is not a lowercase token, a reserved field, or a value that is not field text"
@@ -2053,7 +2785,7 @@ export class HttpServer {
2053
2785
  }
2054
2786
  }
2055
2787
  if (sent !== 0) {
2056
- // Closed under it, out of room, or a head past the peer's limit.
2788
+ // Closed under it, out of room, a head past the peer's limit, or no carrier.
2057
2789
  this.abort(r)
2058
2790
  return HANDLER_DROPPED
2059
2791
  }
@@ -2064,8 +2796,9 @@ export class HttpServer {
2064
2796
 
2065
2797
  /**
2066
2798
  * Gathers the response's fields into `outNames` and `outValues`: the
2067
- * program's, then Alt-Svc and content-length where they are owed and the
2068
- * program gave none. False when that is more than `maxResponseFields`.
2799
+ * program's, then those `field` added, then Alt-Svc and content-length
2800
+ * where they are owed and neither the program nor a step gave one. False
2801
+ * when that is more than `maxResponseFields`.
2069
2802
  */
2070
2803
  private outFields(r: HttpServerRequest, names: u8[][], values: u8[][], bodyLength: i32): boolean {
2071
2804
  const given: i32 = toI32(names.length)
@@ -2076,16 +2809,20 @@ export class HttpServer {
2076
2809
  altSvc !== null &&
2077
2810
  r.kind !== HTTP_SERVER_H3 &&
2078
2811
  (r.kind !== HTTP_SERVER_H2 || r.secure) &&
2079
- !this.hasField(names, this.altSvcName)
2812
+ !this.hasField(names, given, this.altSvcName) &&
2813
+ !this.hasField(r.addedNames, r.added, this.altSvcName)
2080
2814
  const addLength: boolean =
2081
2815
  r.kind !== HTTP_SERVER_H1 &&
2082
2816
  !r.quiet &&
2083
2817
  bodyLength >= 0 &&
2084
- !this.hasField(names, this.contentLengthName)
2085
- const count: i32 = given + (addAlt ? 1 : 0) + (addLength ? 1 : 0)
2086
- if (count > this.maxResponseFields) {
2818
+ !this.hasField(names, given, this.contentLengthName) &&
2819
+ !this.hasField(r.addedNames, r.added, this.contentLengthName)
2820
+ const added: i32 = r.added
2821
+ const wide: i64 = toI64(given) + toI64(added) + toI64(addAlt ? 1 : 0) + toI64(addLength ? 1 : 0)
2822
+ if (wide > toI64(this.maxResponseFields)) {
2087
2823
  return false
2088
2824
  }
2825
+ const count: i32 = toI32(wide)
2089
2826
  if (count === given) {
2090
2827
  this.outNames = names
2091
2828
  this.outValues = values
@@ -2102,8 +2839,14 @@ export class HttpServer {
2102
2839
  this.outNamesBy.push(n)
2103
2840
  this.outValuesBy.push(v)
2104
2841
  }
2105
- const outN: u8[][] = this.outNamesBy[count]
2106
- const outV: u8[][] = this.outValuesBy[count]
2842
+ const namesBy: u8[][][] = this.outNamesBy
2843
+ const valuesBy: u8[][][] = this.outValuesBy
2844
+ // The loop above made both tables longer than `count`.
2845
+ if (count < 0 || count >= toI32(namesBy.length) || count >= toI32(valuesBy.length)) {
2846
+ return false
2847
+ }
2848
+ const outN: u8[][] = namesBy[count]
2849
+ const outV: u8[][] = valuesBy[count]
2107
2850
  for (let k: i32 = 0; k < given; k++) {
2108
2851
  if (
2109
2852
  k < toI32(outN.length) &&
@@ -2115,24 +2858,39 @@ export class HttpServer {
2115
2858
  outV[k] = values[k]
2116
2859
  }
2117
2860
  }
2118
- let at: i32 = given
2861
+ // Every slot below is under `count`, which fits: it was compared with `maxResponseFields`.
2862
+ const addedNames: u8[][] = r.addedNames
2863
+ const addedValues: u8[][] = r.addedValues
2864
+ for (let k: i32 = 0; k < added && k < toI32(addedNames.length) && k < toI32(addedValues.length); k++) {
2865
+ const slot: i32 = toI32(toI64(given) + toI64(k))
2866
+ if (slot >= 0 && slot < toI32(outN.length) && slot < toI32(outV.length)) {
2867
+ outN[slot] = addedNames[k]
2868
+ outV[slot] = addedValues[k]
2869
+ }
2870
+ }
2871
+ let at: i32 = toI32(toI64(given) + toI64(added))
2119
2872
  if (altSvc !== null && addAlt) {
2120
- outN[at] = this.altSvcName
2121
- outV[at] = altSvc
2122
- at += 1
2873
+ if (at >= 0 && at < toI32(outN.length) && at < toI32(outV.length)) {
2874
+ outN[at] = this.altSvcName
2875
+ outV[at] = altSvc
2876
+ }
2877
+ at = toI32(toI64(at) + toI64(1))
2123
2878
  }
2124
2879
  if (addLength) {
2125
- outN[at] = this.contentLengthName
2126
- outV[at] = this.digits(bodyLength)
2880
+ const length: u8[] = this.digits(bodyLength)
2881
+ if (at >= 0 && at < toI32(outN.length) && at < toI32(outV.length)) {
2882
+ outN[at] = this.contentLengthName
2883
+ outV[at] = length
2884
+ }
2127
2885
  }
2128
2886
  this.outNames = outN
2129
2887
  this.outValues = outV
2130
2888
  return true
2131
2889
  }
2132
2890
 
2133
- /** Whether `names` holds `name`, in any case. */
2134
- private hasField(names: u8[][], name: u8[]): boolean {
2135
- for (let k: i32 = 0; k < toI32(names.length); k++) {
2891
+ /** Whether `names[0 .. count)` holds `name`, in any case. */
2892
+ private hasField(names: u8[][], count: i32, name: u8[]): boolean {
2893
+ for (let k: i32 = 0; k < count && k < toI32(names.length); k++) {
2136
2894
  const have: u8[] = names[k]
2137
2895
  if (toI32(have.length) === toI32(name.length)) {
2138
2896
  let same: boolean = true
@@ -2155,16 +2913,18 @@ export class HttpServer {
2155
2913
  private digits(n: i32): u8[] {
2156
2914
  let count: i32 = 1
2157
2915
  let rest: i32 = n
2158
- while (rest >= HTTP_SERVER_TEN) {
2159
- rest = (rest - (rest % HTTP_SERVER_TEN)) / HTTP_SERVER_TEN
2916
+ // An i32 has at most ten digits.
2917
+ while (rest >= 10 && count < 10) {
2918
+ rest = rest / 10
2160
2919
  count += 1
2161
2920
  }
2162
- const out: u8[] = this.digitBufs[count]
2921
+ // The constructor gives every count from 0 to 10 its buffer.
2922
+ const bufs: u8[][] = this.digitBufs
2923
+ const out: u8[] = count < toI32(bufs.length) ? bufs[count] : new Array<u8>(toU16(count))
2163
2924
  rest = n
2164
2925
  for (let k: i32 = count - 1; k >= 0 && k < toI32(out.length); k--) {
2165
- const digit: i32 = rest % HTTP_SERVER_TEN
2166
- out[k] = toU8(HTTP_SERVER_ZERO_DIGIT + digit)
2167
- rest = (rest - digit) / HTTP_SERVER_TEN
2926
+ out[k] = toU8(toI64(HTTP_SERVER_ZERO_DIGIT) + toI64(rest % 10))
2927
+ rest = rest / 10
2168
2928
  }
2169
2929
  return out
2170
2930
  }
@@ -2176,15 +2936,16 @@ export class HttpServer {
2176
2936
  private abort(r: HttpServerRequest): void {
2177
2937
  r.hung = true
2178
2938
  if (r.kind === HTTP_SERVER_H1) {
2179
- const h1: Http1Server = this.h1Of()
2180
- if (this.live(r)) {
2939
+ const h1: Http1Server | null = this.h1
2940
+ if (h1 !== null && this.live(r)) {
2181
2941
  this.h1Close(h1, r.slot)
2182
2942
  }
2183
2943
  return
2184
2944
  }
2185
2945
  if (r.kind === HTTP_SERVER_H2) {
2186
- if (this.h2Holds(r.slot) && this.h2Gen[r.slot] === r.gen) {
2187
- if (this.h2Conn(r.slot).reset(toI32(r.stream), H2_INTERNAL_ERROR) === H2_AGAIN) {
2946
+ if (this.h2Holds(r.slot) && httpServerAt(this.h2Gen, r.slot) === r.gen) {
2947
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
2948
+ if (conn === null || conn.reset(toI32(r.stream), H2_INTERNAL_ERROR) === H2_AGAIN) {
2188
2949
  // No room even for the reset: the connection is past saving.
2189
2950
  this.h2Close(r.slot)
2190
2951
  return
@@ -2193,8 +2954,8 @@ export class HttpServer {
2193
2954
  }
2194
2955
  return
2195
2956
  }
2196
- const h3: Http3Server = this.h3Of()
2197
- if (h3.holds(r.slot) && h3.connection(r.slot).generation === r.gen) {
2957
+ const h3: Http3Server | null = this.h3
2958
+ if (h3 !== null && h3.holds(r.slot) && h3.connection(r.slot).generation === r.gen) {
2198
2959
  h3.connection(r.slot).reset(r.stream, H3_INTERNAL_ERROR)
2199
2960
  h3.touch(r.slot)
2200
2961
  }
@@ -2231,14 +2992,15 @@ export class HttpServer {
2231
2992
  if (r.quiet || len === 0) {
2232
2993
  return HANDLER_SENT
2233
2994
  }
2234
- if (toI64(r.pendingEnd - r.pendingStart) + toI64(len) > toI64(HTTP_SERVER_MAX_BODY)) {
2995
+ if (toI64(r.pendingEnd) - toI64(r.pendingStart) + toI64(len) > toI64(HTTP_SERVER_MAX_BODY)) {
2235
2996
  // A remainder past 2^30, the longest array the runtime grows to:
2236
2997
  // nothing of it is written, and the response is cut.
2237
2998
  this.abort(r)
2238
2999
  return HANDLER_DROPPED
2239
3000
  }
3001
+ // Both are at least 0 here, so the difference fits.
2240
3002
  if (r.left >= 0) {
2241
- r.left -= len
3003
+ r.left = toI32(toI64(r.left) - toI64(len))
2242
3004
  }
2243
3005
  let taken: i32 = 0
2244
3006
  if (r.pendingEnd === r.pendingStart) {
@@ -2248,8 +3010,13 @@ export class HttpServer {
2248
3010
  return HANDLER_DROPPED
2249
3011
  }
2250
3012
  }
2251
- if (taken < len) {
2252
- this.keep(r, buf, off + taken, len - taken)
3013
+ // `carry` took at most `len`, and `off + len` is within `buf`.
3014
+ if (
3015
+ taken < len &&
3016
+ !this.keep(r, buf, toI32(toI64(off) + toI64(taken)), toI32(toI64(len) - toI64(taken)))
3017
+ ) {
3018
+ this.abort(r)
3019
+ return HANDLER_DROPPED
2253
3020
  }
2254
3021
  this.nudge(r)
2255
3022
  return HANDLER_SENT
@@ -2261,26 +3028,30 @@ export class HttpServer {
2261
3028
  * when the connection or stream is gone.
2262
3029
  */
2263
3030
  private carry(r: HttpServerRequest, buf: u8[], off: i32, len: i32, last: boolean): i32 {
3031
+ let taken: i32 = -1
2264
3032
  if (r.kind === HTTP_SERVER_H1) {
2265
- const taken: i32 = this.h1Of().connection(r.slot).write(buf, off, len)
3033
+ // `write` keeps `len` within what the head declared, so the carrier
3034
+ // refuses only a closed connection here.
3035
+ const h1: Http1Server | null = this.h1
3036
+ if (h1 !== null) {
3037
+ taken = h1.connection(r.slot).write(buf, off, len)
3038
+ }
2266
3039
  if (taken === H1_AGAIN) {
2267
3040
  return 0
2268
3041
  }
2269
- if (taken < 0) {
2270
- panic(
2271
- `HttpServer.write: the carrier refused the body (${taken}): more than the length the response declared`
2272
- )
3042
+ } else if (r.kind === HTTP_SERVER_H2) {
3043
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
3044
+ if (conn !== null) {
3045
+ taken = conn.writeData(toI32(r.stream), buf, off, len, last)
2273
3046
  }
2274
- return taken
2275
- }
2276
- let taken: i32 = 0
2277
- if (r.kind === HTTP_SERVER_H2) {
2278
- taken = this.h2Conn(r.slot).writeData(toI32(r.stream), buf, off, len, last)
2279
3047
  if (taken === H2_AGAIN) {
2280
3048
  return 0
2281
3049
  }
2282
3050
  } else {
2283
- taken = this.h3Of().connection(r.slot).writeData(r.stream, buf, off, len, last)
3051
+ const h3: Http3Server | null = this.h3
3052
+ if (h3 !== null) {
3053
+ taken = h3.connection(r.slot).writeData(r.stream, buf, off, len, last)
3054
+ }
2284
3055
  if (taken === H3_AGAIN) {
2285
3056
  return 0
2286
3057
  }
@@ -2290,17 +3061,18 @@ export class HttpServer {
2290
3061
 
2291
3062
  /** After the program wrote to `r`: what the carrier holds is sent now on HTTP/1.1 and HTTP/2, and by the turn's flush on HTTP/3. */
2292
3063
  private nudge(r: HttpServerRequest): void {
3064
+ const h1: Http1Server | null = this.h1
3065
+ const h3: Http3Server | null = this.h3
2293
3066
  if (r.kind === HTTP_SERVER_H1) {
2294
- this.drive(this.h1Of(), r.slot)
3067
+ if (h1 !== null) {
3068
+ this.driveOf(h1, r)
3069
+ }
2295
3070
  } else if (r.kind === HTTP_SERVER_H2) {
2296
3071
  if (this.h2Holds(r.slot)) {
2297
3072
  this.h2Settle(r.slot)
2298
3073
  }
2299
- } else {
2300
- const h3: Http3Server = this.h3Of()
2301
- if (h3.holds(r.slot)) {
2302
- h3.touch(r.slot)
2303
- }
3074
+ } else if (h3 !== null && h3.holds(r.slot)) {
3075
+ h3.touch(r.slot)
2304
3076
  }
2305
3077
  }
2306
3078
 
@@ -2316,16 +3088,17 @@ export class HttpServer {
2316
3088
  if (r.state !== HTTP_SERVER_STREAMING) {
2317
3089
  panic(`HttpServer.end: the response is not started (state ${r.state})`)
2318
3090
  }
2319
- if (!this.live(r)) {
3091
+ const h1: Http1Server | null = this.h1
3092
+ if (!this.live(r) || (r.kind === HTTP_SERVER_H1 && h1 === null)) {
2320
3093
  this.release(r)
2321
3094
  return HANDLER_DROPPED
2322
3095
  }
2323
3096
  if (r.left > 0 && !r.quiet) {
2324
3097
  panic(`HttpServer.end: the body is ${r.left} byte(s) short of the length the response declared`)
2325
3098
  }
2326
- if (r.kind === HTTP_SERVER_H1) {
2327
- this.finishH1(r, this.h1Of().connection(r.slot))
2328
- this.drive(this.h1Of(), r.slot)
3099
+ if (r.kind === HTTP_SERVER_H1 && h1 !== null) {
3100
+ this.finishH1(r, h1.connection(r.slot))
3101
+ this.driveOf(h1, r)
2329
3102
  return HANDLER_SENT
2330
3103
  }
2331
3104
  r.state = HTTP_SERVER_FLUSHING
@@ -2334,6 +3107,72 @@ export class HttpServer {
2334
3107
  return HANDLER_SENT
2335
3108
  }
2336
3109
 
3110
+ /**
3111
+ * Ends `r`'s HTTP/2 response with a trailer section, the fields
3112
+ * `names[k]`: `values[k]`, in place of `end`: one HEADERS frame with
3113
+ * END_STREAM after the body (RFC 9113 §8.1), sent once the body's last
3114
+ * byte is with the carrier. `r` is the server's again once they are sent.
3115
+ * The arrays are read until then, not copied: arrays that stay as they
3116
+ * are, or ones kept per request (by `r.index`), which `take` hands out
3117
+ * again only after the trailers went. An answer that ends with trailers
3118
+ * starts with a body length above 0 or `HTTP1_CHUNKED`, since a head
3119
+ * declaring 0 has ended the stream; on a HEAD request, a 204 or a 304 the
3120
+ * head ended it too, and the trailers are dropped as body bytes are.
3121
+ * Panics on another server's request, when the response is not started,
3122
+ * on HTTP/1.1 or HTTP/3 (neither carrier sends trailers through the
3123
+ * façade), on a head that declared a body length of 0, on a body short of
3124
+ * the declared length, or on fields the carrier refuses (a name that is
3125
+ * not a lowercase token, a value that is not field text, a
3126
+ * connection-specific field, or names and values of different lengths).
3127
+ * @param r a request whose response `start` began on HTTP/2; the server's again once the trailers are sent
3128
+ * @param names the trailer field names, lowercase, as bytes beside `values`; read until the trailers are sent
3129
+ * @param values each trailer field's value, `values[k]` for `names[k]`; read until the trailers are sent
3130
+ * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection or
3131
+ * stream closed first, or there are more fields than
3132
+ * `limits.maxResponseFields` and the stream was reset for it
3133
+ */
3134
+ trailers(r: HttpServerRequest, names: u8[][], values: u8[][]): i32 {
3135
+ this.own(r, "trailers")
3136
+ if (r.state !== HTTP_SERVER_STREAMING) {
3137
+ panic(`HttpServer.trailers: the response is not started (state ${r.state})`)
3138
+ }
3139
+ if (r.kind === HTTP_SERVER_H1) {
3140
+ panic("HttpServer.trailers: trailers are not sent on HTTP/1.1; end the response with end")
3141
+ }
3142
+ if (r.kind === HTTP_SERVER_H3) {
3143
+ panic("HttpServer.trailers: trailers are not sent on HTTP/3; end the response with end")
3144
+ }
3145
+ if (httpFieldsCheckOutgoing(names, values) !== HTTP_FIELDS_OK) {
3146
+ panic(
3147
+ "HttpServer.trailers: the HTTP/2 carrier refuses the fields: a name that is not a lowercase token, a connection-specific field, a value that is not field text, or names and values of different lengths"
3148
+ )
3149
+ }
3150
+ if (r.ended && !r.quiet) {
3151
+ panic(
3152
+ "HttpServer.trailers: the head declared a body length of 0, which ended the stream; start with HTTP1_CHUNKED"
3153
+ )
3154
+ }
3155
+ if (!this.live(r)) {
3156
+ this.release(r)
3157
+ return HANDLER_DROPPED
3158
+ }
3159
+ if (r.left > 0 && !r.quiet) {
3160
+ panic(`HttpServer.trailers: the body is ${r.left} byte(s) short of the length the response declared`)
3161
+ }
3162
+ if (toI32(names.length) > this.maxResponseFields) {
3163
+ this.abort(r)
3164
+ this.release(r)
3165
+ return HANDLER_DROPPED
3166
+ }
3167
+ r.trailing = !r.quiet
3168
+ r.trailerNames = names
3169
+ r.trailerValues = values
3170
+ r.state = HTTP_SERVER_FLUSHING
3171
+ this.push(r)
3172
+ this.nudge(r)
3173
+ return HANDLER_SENT
3174
+ }
3175
+
2337
3176
  /**
2338
3177
  * Hands an HTTP/2 or HTTP/3 stream's pending bytes to the carrier and, once
2339
3178
  * a flushing response has none left, ends the stream and hands `r` back.
@@ -2346,9 +3185,10 @@ export class HttpServer {
2346
3185
  this.drop(r)
2347
3186
  return
2348
3187
  }
2349
- const left: i32 = r.pendingEnd - r.pendingStart
3188
+ const left: i32 = toI32(toI64(r.pendingEnd) - toI64(r.pendingStart))
2350
3189
  if (left > 0) {
2351
- const last: boolean = r.state === HTTP_SERVER_FLUSHING
3190
+ // Trailers end the stream themselves, so the body's last DATA frame does not.
3191
+ const last: boolean = r.state === HTTP_SERVER_FLUSHING && !r.trailing
2352
3192
  const taken: i32 = this.carry(r, r.pending, r.pendingStart, left, last)
2353
3193
  if (taken < 0) {
2354
3194
  this.drop(r)
@@ -2363,7 +3203,7 @@ export class HttpServer {
2363
3203
  return
2364
3204
  }
2365
3205
  if (!r.ended) {
2366
- const done: i32 = this.carryEnd(r)
3206
+ const done: i32 = r.trailing ? this.carryTrailers(r) : this.carryEnd(r)
2367
3207
  if (done === 0) {
2368
3208
  return // no room for the end yet: the next push sends it
2369
3209
  }
@@ -2381,22 +3221,20 @@ export class HttpServer {
2381
3221
  * `carry` cannot say this, since the frame is taken as 0 bytes either way.
2382
3222
  */
2383
3223
  private carryEnd(r: HttpServerRequest): i32 {
2384
- let done: i32 = 0
3224
+ let done: i32 = -1
2385
3225
  if (r.kind === HTTP_SERVER_H2) {
2386
- done = this.h2Conn(r.slot).writeData(
2387
- toI32(r.stream),
2388
- r.pending,
2389
- HTTP_SERVER_ZERO,
2390
- HTTP_SERVER_ZERO,
2391
- true
2392
- )
3226
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
3227
+ if (conn !== null) {
3228
+ done = conn.writeData(toI32(r.stream), r.pending, HTTP_SERVER_ZERO, HTTP_SERVER_ZERO, true)
3229
+ }
2393
3230
  if (done === H2_AGAIN) {
2394
3231
  return 0
2395
3232
  }
2396
3233
  } else {
2397
- done = this.h3Of()
2398
- .connection(r.slot)
2399
- .writeData(r.stream, r.pending, HTTP_SERVER_ZERO, HTTP_SERVER_ZERO, true)
3234
+ const h3: Http3Server | null = this.h3
3235
+ if (h3 !== null) {
3236
+ done = h3.connection(r.slot).writeData(r.stream, r.pending, HTTP_SERVER_ZERO, HTTP_SERVER_ZERO, true)
3237
+ }
2400
3238
  if (done === H3_AGAIN) {
2401
3239
  return 0
2402
3240
  }
@@ -2404,6 +3242,26 @@ export class HttpServer {
2404
3242
  return done < 0 ? -1 : 1
2405
3243
  }
2406
3244
 
3245
+ /**
3246
+ * Ends `r`'s HTTP/2 stream with the trailer section `trailers` was given:
3247
+ * 1 when it is sent, 0 when the output has no room yet, -1 when the stream
3248
+ * is gone or the section is too large for the output, which resets it.
3249
+ */
3250
+ private carryTrailers(r: HttpServerRequest): i32 {
3251
+ const conn: Http2Connection | null = this.h2Conn(r.slot)
3252
+ if (conn === null) {
3253
+ return -1
3254
+ }
3255
+ const done: i32 = conn.writeTrailers(toI32(r.stream), r.trailerNames, r.trailerValues)
3256
+ if (done === H2_AGAIN) {
3257
+ return 0
3258
+ }
3259
+ if (done === H2_INVALID) {
3260
+ this.abort(r)
3261
+ }
3262
+ return done < 0 ? -1 : 1
3263
+ }
3264
+
2407
3265
  /**
2408
3266
  * Answers `r` with `status`, a `Content-Type` of `contentType` and the body `body[off .. off + len)`, in one call.
2409
3267
  * @param r a request taken and not yet answered; the server's again after this call
@@ -2415,7 +3273,10 @@ export class HttpServer {
2415
3273
  * @returns `HANDLER_SENT`, or `HANDLER_DROPPED` when the connection or stream closed first, or the head could not be sent
2416
3274
  */
2417
3275
  send(r: HttpServerRequest, status: i32, contentType: u8[], body: u8[], off: i32, len: i32): i32 {
2418
- this.typeValue[0] = contentType
3276
+ const type: u8[][] = this.typeValue
3277
+ if (toI32(type.length) > 0) {
3278
+ type[0] = contentType
3279
+ }
2419
3280
  return this.sendFields(r, status, this.typeName, this.typeValue, body, off, len)
2420
3281
  }
2421
3282
 
@@ -2556,7 +3417,11 @@ export class HttpServer {
2556
3417
  refuse(r: HttpServerRequest): i32 {
2557
3418
  if (r.route === ROUTER_METHOD_NOT_ALLOWED) {
2558
3419
  const length: i32 = this.writeProblem(toI32(405), "Method Not Allowed", "")
2559
- this.allowValues[1] = this.allowFor(r)
3420
+ const allowed: u8[] = this.allowFor(r)
3421
+ const allow: u8[][] = this.allowValues
3422
+ if (toI32(allow.length) > 1) {
3423
+ allow[1] = allowed
3424
+ }
2560
3425
  return this.sendFields(
2561
3426
  r,
2562
3427
  toI32(405),
@@ -2573,13 +3438,15 @@ export class HttpServer {
2573
3438
  /** The `Allow` value for `r`'s path: each method a route of it takes, and HEAD wherever GET is. Made once for each set of methods. */
2574
3439
  private allowFor(r: HttpServerRequest): u8[] {
2575
3440
  const router: Router = this.router
2576
- const methods: i32 = toI32(router.methodStart.length)
3441
+ const starts: i32[] = router.methodStart
3442
+ const ends: i32[] = router.methodEnd
3443
+ const methods: i32 = toI32(starts.length)
2577
3444
  let mask: i64 = 0
2578
- for (let m: i32 = 0; m < methods; m++) {
3445
+ for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
2579
3446
  const route: i32 = router.match(
2580
3447
  router.methodBytes,
2581
- router.methodStart[m],
2582
- router.methodEnd[m],
3448
+ starts[m],
3449
+ ends[m],
2583
3450
  r.head,
2584
3451
  r.targetStart,
2585
3452
  r.targetEnd,
@@ -2589,18 +3456,20 @@ export class HttpServer {
2589
3456
  mask = mask | (toI64(1) << toI64(m))
2590
3457
  }
2591
3458
  }
2592
- for (let k: i32 = 0; k < toI32(this.allowMasks.length); k++) {
2593
- if (this.allowMasks[k] === mask) {
2594
- return this.allowBytes[k]
3459
+ const masks: i64[] = this.allowMasks
3460
+ const bytes: u8[][] = this.allowBytes
3461
+ for (let k: i32 = 0; k < toI32(masks.length) && k < toI32(bytes.length); k++) {
3462
+ if (masks[k] === mask) {
3463
+ return bytes[k]
2595
3464
  }
2596
3465
  }
2597
3466
  let get: boolean = false
2598
3467
  let head: boolean = false
2599
3468
  const value: u8[] = []
2600
- for (let m: i32 = 0; m < methods; m++) {
3469
+ for (let m: i32 = 0; m < methods && m < toI32(starts.length) && m < toI32(ends.length); m++) {
2601
3470
  if ((mask & (toI64(1) << toI64(m))) !== toI64(0)) {
2602
- const start: i32 = router.methodStart[m]
2603
- const end: i32 = router.methodEnd[m]
3471
+ const start: i32 = starts[m]
3472
+ const end: i32 = ends[m]
2604
3473
  get = get || httpServerSpanIs(router.methodBytes, start, end, "GET")
2605
3474
  head = head || httpServerSpanIs(router.methodBytes, start, end, "HEAD")
2606
3475
  httpServerAllowAppend(value, router.methodBytes, start, end)
@@ -2615,42 +3484,6 @@ export class HttpServer {
2615
3484
  return value
2616
3485
  }
2617
3486
 
2618
- /** The HTTP/1.1 carrier, for a request read on it. */
2619
- private h1Of(): Http1Server {
2620
- const h1: Http1Server | null = this.h1
2621
- if (h1 === null) {
2622
- panic("HttpServer: no HTTP/1.1 listener")
2623
- }
2624
- return h1
2625
- }
2626
-
2627
- /** The HTTP/2 over TLS carrier, for a slot on it. */
2628
- private h2Of(): Http2TlsServer {
2629
- const h2: Http2TlsServer | null = this.h2
2630
- if (h2 === null) {
2631
- panic("HttpServer: no HTTP/2 over TLS listener")
2632
- }
2633
- return h2
2634
- }
2635
-
2636
- /** The h2c carrier, for a slot on it. */
2637
- private h2cOf(): Http2CleartextServer {
2638
- const h2c: Http2CleartextServer | null = this.h2c
2639
- if (h2c === null) {
2640
- panic("HttpServer: no h2c listener")
2641
- }
2642
- return h2c
2643
- }
2644
-
2645
- /** The HTTP/3 carrier, for a request read on it. */
2646
- private h3Of(): Http3Server {
2647
- const h3: Http3Server | null = this.h3
2648
- if (h3 === null) {
2649
- panic("HttpServer: no HTTP/3 listener")
2650
- }
2651
- return h3
2652
- }
2653
-
2654
3487
  // ---- HTTP/2 and HTTP/3 heads ----------------------------------------------------
2655
3488
 
2656
3489
  /**
@@ -2662,22 +3495,28 @@ export class HttpServer {
2662
3495
  if (f.isConnect()) {
2663
3496
  return 501
2664
3497
  }
2665
- const pathLength: i32 = toI32(f.path.length)
2666
- if (pathLength === 1 && toI32(f.path[0]) === 42 && httpFieldIs(f.method, "OPTIONS")) {
3498
+ const path: u8[] = f.path
3499
+ const pathLength: i32 = toI32(path.length)
3500
+ if (toI32(path.length) === 1 && toI32(path[0]) === 42 && httpFieldIs(f.method, "OPTIONS")) {
2667
3501
  return 204
2668
3502
  }
2669
3503
  // Counts what `fromFields` lays out: `:authority`, when present, as a
2670
3504
  // `host` field in place of every `host` field the head carries.
2671
3505
  const authority: boolean = toI32(f.authority.length) > 0
3506
+ const names: u8[][] = f.names
3507
+ const values: u8[][] = f.values
2672
3508
  let fields: i32 = authority ? 1 : 0
2673
- let bytes: i32 = toI32(f.method.length) + pathLength + 4 + toI32(f.authority.length)
2674
- for (let k: i32 = 0; k < toI32(f.names.length); k++) {
2675
- if (!authority || !httpFieldIs(f.names[k], "host")) {
3509
+ let bytes: i64 = toI64(f.method.length) + toI64(pathLength) + toI64(4) + toI64(f.authority.length)
3510
+ // Stops counting once either passes i32, which is past every cap.
3511
+ for (let k: i32 = 0; k < toI32(names.length) && k < toI32(values.length) && bytes < 2147483647; k++) {
3512
+ const counted: boolean = !authority || !httpFieldIs(names[k], "host")
3513
+ if (counted && fields < 2147483647 && bytes < 2147483647) {
2676
3514
  fields += 1
2677
- bytes += toI32(f.names[k].length) + toI32(f.values[k].length)
3515
+ // `bytes` is under 2^31 here, so it narrows exactly.
3516
+ bytes = toI64(toI32(bytes)) + toI64(names[k].length) + toI64(values[k].length)
2678
3517
  }
2679
3518
  }
2680
- if (fields > this.maxFields || bytes > this.maxHead) {
3519
+ if (fields > this.maxFields || bytes > toI64(this.maxHead)) {
2681
3520
  return 431
2682
3521
  }
2683
3522
  if (f.contentLength > toI64(this.maxBody)) {
@@ -2689,47 +3528,80 @@ export class HttpServer {
2689
3528
  /**
2690
3529
  * Lays a screened head out in `r` as HTTP/1.1's reads: the method, the
2691
3530
  * path as the target, and the fields, with `:authority` given as `host`
2692
- * in place of any `host` field.
3531
+ * in place of any `host` field. False when the head or its spans have no
3532
+ * room for it, which `screen`'s 431 past `maxHead` and `maxFields` leaves
3533
+ * for none; the caller then refuses the stream.
2693
3534
  */
2694
- private fromFields(r: HttpServerRequest, f: HttpFields): void {
3535
+ private fromFields(r: HttpServerRequest, f: HttpFields): boolean {
2695
3536
  this.headAt = 0
2696
3537
  r.methodStart = 0
2697
- this.put(r, f.method)
3538
+ if (!this.put(r, f.method)) {
3539
+ return false
3540
+ }
2698
3541
  r.methodEnd = this.headAt
2699
3542
  r.targetStart = this.headAt
2700
- this.put(r, f.path)
3543
+ if (!this.put(r, f.path)) {
3544
+ return false
3545
+ }
2701
3546
  r.targetEnd = this.headAt
2702
3547
  let n: i32 = 0
2703
3548
  const authority: boolean = toI32(f.authority.length) > 0
2704
3549
  if (authority) {
2705
- this.putField(r, n, this.hostName, f.authority)
3550
+ if (!this.putField(r, n, this.hostName, f.authority)) {
3551
+ return false
3552
+ }
2706
3553
  n += 1
2707
3554
  }
2708
- for (let k: i32 = 0; k < toI32(f.names.length); k++) {
2709
- const name: u8[] = f.names[k]
3555
+ const names: u8[][] = f.names
3556
+ const values: u8[][] = f.values
3557
+ for (let k: i32 = 0; k < toI32(names.length) && k < toI32(values.length) && n < 2147483647; k++) {
3558
+ const name: u8[] = names[k]
2710
3559
  if (!authority || !httpFieldIs(name, "host")) {
2711
- this.putField(r, n, name, f.values[k])
3560
+ if (!this.putField(r, n, name, values[k])) {
3561
+ return false
3562
+ }
2712
3563
  n += 1
2713
3564
  }
2714
3565
  }
2715
3566
  r.headerCount = n
3567
+ return true
2716
3568
  }
2717
3569
 
2718
- /** Copies `bytes` into `r`'s head at `headAt`. */
2719
- private put(r: HttpServerRequest, bytes: u8[]): void {
3570
+ /** Copies `bytes` into `r`'s head at `headAt`; false, with nothing copied, when they do not fit. */
3571
+ private put(r: HttpServerRequest, bytes: u8[]): boolean {
2720
3572
  const n: i32 = toI32(bytes.length)
2721
- copyBytes(r.head, this.headAt, bytes, HTTP_SERVER_ZERO, n)
2722
- this.headAt += n
3573
+ const end: i64 = toI64(this.headAt) + toI64(n)
3574
+ if (end > toI64(r.head.length) || !tryCopyBytes(r.head, this.headAt, bytes, HTTP_SERVER_ZERO, n)) {
3575
+ return false
3576
+ }
3577
+ this.headAt = toI32(end)
3578
+ return true
2723
3579
  }
2724
3580
 
2725
- /** Copies field `n`, `name`: `value`, into `r`'s head and spans. */
2726
- private putField(r: HttpServerRequest, n: i32, name: u8[], value: u8[]): void {
2727
- r.spans[4 * n] = this.headAt
2728
- this.put(r, name)
2729
- r.spans[4 * n + 1] = this.headAt
2730
- r.spans[4 * n + 2] = this.headAt
2731
- this.put(r, value)
2732
- r.spans[4 * n + 3] = this.headAt
3581
+ /** Copies field `n`, `name`: `value`, into `r`'s head and spans; false when either has no room for it. */
3582
+ private putField(r: HttpServerRequest, n: i32, name: u8[], value: u8[]): boolean {
3583
+ // Four spans per field, and `spans` is far shorter than 2^31, so `4 * n` fits.
3584
+ if (n < 0 || n > 536870911) {
3585
+ return false
3586
+ }
3587
+ const k: i32 = n * 4
3588
+ const spans: i32[] = r.spans
3589
+ if (k > toI32(spans.length) - 4) {
3590
+ return false
3591
+ }
3592
+ const nameStart: i32 = this.headAt
3593
+ if (!this.put(r, name)) {
3594
+ return false
3595
+ }
3596
+ const nameEnd: i32 = this.headAt
3597
+ if (!this.put(r, value)) {
3598
+ return false
3599
+ }
3600
+ spans[k] = nameStart
3601
+ spans[k + 1] = nameEnd
3602
+ spans[k + 2] = nameEnd
3603
+ spans[k + 3] = this.headAt
3604
+ return true
2733
3605
  }
2734
3606
 
2735
3607
  // ---- HTTP/2 ---------------------------------------------------------------------
@@ -2745,7 +3617,11 @@ export class HttpServer {
2745
3617
  crypto.getRandomValues(this.keyBuf)
2746
3618
  const slot: i32 = h2.accept(this.rnd, this.keyBuf)
2747
3619
  if (slot >= 0) {
2748
- this.h2Opened(this.h2TlsBase + slot, h2.fd(slot))
3620
+ // `h2Grow` gave the listener `[h2TlsBase, h2TlsBase + slots)`; a slot past it is closed.
3621
+ const v: i64 = toI64(this.h2TlsBase) + toI64(slot)
3622
+ if (!this.h2Opened(v <= 2147483647 ? toI32(v) : toI32(-1), h2.fd(slot))) {
3623
+ h2.close(slot)
3624
+ }
2749
3625
  } else if (slot === HTTP_SERVER_ACCEPT_NONE) {
2750
3626
  return
2751
3627
  } else if (slot !== TLS_TCP_POOL_FULL && slot !== HTTP_SERVER_ACCEPT_ABORTED) {
@@ -2765,7 +3641,11 @@ export class HttpServer {
2765
3641
  let slot: i32 = h2c.accept()
2766
3642
  while (slot >= 0 || slot === H2C_POOL_FULL || slot === HTTP_SERVER_ACCEPT_ABORTED) {
2767
3643
  if (slot >= 0) {
2768
- this.h2Opened(this.h2cBase + slot, h2c.fd(slot))
3644
+ // `h2Grow` gave the listener `[h2cBase, h2cBase + slots)`; a slot past it is closed.
3645
+ const v: i64 = toI64(this.h2cBase) + toI64(slot)
3646
+ if (!this.h2Opened(v <= 2147483647 ? toI32(v) : toI32(-1), h2c.fd(slot))) {
3647
+ h2c.close(slot)
3648
+ }
2769
3649
  }
2770
3650
  slot = h2c.accept()
2771
3651
  }
@@ -2781,61 +3661,157 @@ export class HttpServer {
2781
3661
  */
2782
3662
  private rest(fd: i32, token: i32): void {
2783
3663
  pollRemove(this.loop, fd)
2784
- this.restUntil[HTTP_SERVER_H1_LISTENER - token] = monotonicNanos() + HTTP_SERVER_ACCEPT_REST
3664
+ const until: i64 = httpServerAfter(monotonicNanos(), HTTP_SERVER_ACCEPT_REST)
3665
+ const rests: i64[] = this.restUntil
3666
+ const at: i32 = this.restAt(token)
3667
+ if (at >= 0 && at < toI32(rests.length)) {
3668
+ rests[at] = until
3669
+ }
3670
+ }
3671
+
3672
+ /** The first entry of HTTP/2 slot `v`'s stream table, or -1 outside the table. */
3673
+ private h2Base(v: i32): i32 {
3674
+ const base: i64 = toI64(v) * toI64(this.perConnection2)
3675
+ return v >= 0 && base >= 0 && base < toI64(toI32(this.h2Reqs.length)) ? toI32(base) : -1
2785
3676
  }
2786
3677
 
2787
- /** HTTP/2 slot `v`, just accepted on socket `fd`: an empty stream table, idle, and in the loop. */
2788
- private h2Opened(v: i32, fd: i32): void {
2789
- this.h2Open[v] = true
2790
- this.h2Count[v] = 0
2791
- this.h2IdleSince[v] = this.turnAt
2792
- const base: i32 = v * this.perConnection2
2793
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
2794
- this.h2Reqs[e] = -1
3678
+ /** One past the last entry of HTTP/2 slot `v`'s stream table, or -1 outside the table. */
3679
+ private h2End(v: i32): i32 {
3680
+ const end: i64 = (toI64(v) + toI64(1)) * toI64(this.perConnection2)
3681
+ return v >= 0 && end >= 0 && end <= toI64(toI32(this.h2Reqs.length)) ? toI32(end) : -1
3682
+ }
3683
+
3684
+ /** Marks HTTP/2 slot `v` idle since `since`, or busy for 0; nothing outside the table. */
3685
+ private h2Idle(v: i32, since: i64): void {
3686
+ const idleSince: i64[] = this.h2IdleSince
3687
+ if (v >= 0 && v < toI32(idleSince.length)) {
3688
+ idleSince[v] = since
3689
+ }
3690
+ }
3691
+
3692
+ /**
3693
+ * HTTP/2 slot `v`, just accepted on socket `fd`: an empty stream table,
3694
+ * idle, and in the loop. False, with nothing registered, for a slot past
3695
+ * the tables `h2Grow` sized; the caller then closes the connection.
3696
+ */
3697
+ private h2Opened(v: i32, fd: i32): boolean {
3698
+ const open: boolean[] = this.h2Open
3699
+ const count: i32[] = this.h2Count
3700
+ const idleSince: i64[] = this.h2IdleSince
3701
+ // `h2Grow` sizes the three together, and no listener asks for two billion slots.
3702
+ const wide: i64 = toI64(HTTP_SERVER_H2_BASE) + toI64(v)
3703
+ if (
3704
+ v < 0 ||
3705
+ v >= toI32(open.length) ||
3706
+ v >= toI32(count.length) ||
3707
+ v >= toI32(idleSince.length) ||
3708
+ wide > 2147483647
3709
+ ) {
3710
+ return false
3711
+ }
3712
+ const token: i32 = toI32(wide)
3713
+ open[v] = true
3714
+ count[v] = 0
3715
+ idleSince[v] = this.turnAt
3716
+ const reqs: i32[] = this.h2Reqs
3717
+ const end: i32 = this.h2End(v)
3718
+ for (let e: i32 = this.h2Base(v); e >= 0 && e < end && e < toI32(reqs.length); e++) {
3719
+ reqs[e] = -1
3720
+ }
3721
+ pollAdd(this.loop, fd, H1_WANT_READ, token)
3722
+ return true
3723
+ }
3724
+
3725
+ /** Whether HTTP/2 slot `v` is on the h2c carrier: false outside the slot table. */
3726
+ private h2IsClear(v: i32): boolean {
3727
+ const clear: boolean[] = this.h2Clear
3728
+ return v >= 0 && v < toI32(clear.length) && clear[v]
3729
+ }
3730
+
3731
+ /** HTTP/2 slot `v`'s number in its own carrier's table, or -1 outside the slot table. */
3732
+ private h2Local(v: i32): i32 {
3733
+ const clear: boolean[] = this.h2Clear
3734
+ if (v < 0 || v >= toI32(clear.length)) {
3735
+ return -1
2795
3736
  }
2796
- pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H2_BASE + v)
3737
+ const base: i32 = clear[v] ? this.h2cBase : this.h2TlsBase
3738
+ return base >= 0 && base <= v ? v - base : -1
2797
3739
  }
2798
3740
 
2799
3741
  /** Whether HTTP/2 slot `v`'s carrier holds a connection there. */
2800
3742
  private h2Holds(v: i32): boolean {
2801
- if (this.h2Clear[v]) {
3743
+ const at: i32 = this.h2Local(v)
3744
+ if (this.h2IsClear(v)) {
2802
3745
  const h2c: Http2CleartextServer | null = this.h2c
2803
- return h2c !== null && h2c.holds(v - this.h2cBase)
3746
+ return h2c !== null && h2c.holds(at)
2804
3747
  }
2805
3748
  const h2: Http2TlsServer | null = this.h2
2806
- return h2 !== null && h2.holds(v - this.h2TlsBase)
3749
+ return h2 !== null && h2.holds(at)
2807
3750
  }
2808
3751
 
2809
- /** HTTP/2 slot `v`'s connection. */
2810
- private h2Conn(v: i32): Http2Connection {
2811
- if (this.h2Clear[v]) {
2812
- return this.h2cOf().connection(v - this.h2cBase)
3752
+ /** HTTP/2 slot `v`'s connection, or null with no carrier for its kind of slot or outside the table. */
3753
+ private h2Conn(v: i32): Http2Connection | null {
3754
+ const at: i32 = this.h2Local(v)
3755
+ if (at < 0) {
3756
+ return null
3757
+ }
3758
+ if (this.h2IsClear(v)) {
3759
+ const h2c: Http2CleartextServer | null = this.h2c
3760
+ return h2c === null ? null : h2c.connection(at)
2813
3761
  }
2814
- return this.h2Of().connection(v - this.h2TlsBase)
3762
+ const h2: Http2TlsServer | null = this.h2
3763
+ return h2 === null ? null : h2.connection(at)
2815
3764
  }
2816
3765
 
2817
- /** HTTP/2 slot `v`'s next event, as its carrier's `next` answers. */
3766
+ /** HTTP/2 slot `v`'s connection once it has its preface (a TLS slot has it at once), or null before. */
3767
+ private h2Prefaced(v: i32): Http2Connection | null {
3768
+ const h2c: Http2CleartextServer | null = this.h2c
3769
+ if (this.h2IsClear(v) && (h2c === null || !h2c.prefaced(this.h2Local(v)))) {
3770
+ return null
3771
+ }
3772
+ return this.h2Conn(v)
3773
+ }
3774
+
3775
+ /** HTTP/2 slot `v`'s next event, as its carrier's `next` answers: `H2_ERROR` with no carrier. */
2818
3776
  private h2Next(v: i32): i32 {
2819
- if (this.h2Clear[v]) {
2820
- return this.h2cOf().next(v - this.h2cBase)
3777
+ const at: i32 = this.h2Local(v)
3778
+ if (at < 0) {
3779
+ return H2_ERROR
3780
+ }
3781
+ if (this.h2IsClear(v)) {
3782
+ const h2c: Http2CleartextServer | null = this.h2c
3783
+ return h2c === null ? H2_ERROR : h2c.next(at)
2821
3784
  }
2822
- return this.h2Of().next(v - this.h2TlsBase)
3785
+ const h2: Http2TlsServer | null = this.h2
3786
+ return h2 === null ? H2_ERROR : h2.next(at)
2823
3787
  }
2824
3788
 
2825
- /** Sends what HTTP/2 slot `v` holds: its interest, with `TLS_RECORD_DONE` (`H2C_DONE`) once it is over. */
3789
+ /**
3790
+ * Sends what HTTP/2 slot `v` holds: its interest, with `TLS_RECORD_DONE`
3791
+ * (`H2C_DONE`) once it is over, with no carrier, or outside the slot table.
3792
+ */
2826
3793
  private h2Flush(v: i32): i32 {
2827
- if (this.h2Clear[v]) {
2828
- return this.h2cOf().flush(v - this.h2cBase)
3794
+ const at: i32 = this.h2Local(v)
3795
+ if (at < 0) {
3796
+ return TLS_RECORD_DONE
2829
3797
  }
2830
- return this.h2Of().flush(v - this.h2TlsBase)
3798
+ if (this.h2IsClear(v)) {
3799
+ const h2c: Http2CleartextServer | null = this.h2c
3800
+ return h2c === null ? TLS_RECORD_DONE : h2c.flush(at)
3801
+ }
3802
+ const h2: Http2TlsServer | null = this.h2
3803
+ return h2 === null ? TLS_RECORD_DONE : h2.flush(at)
2831
3804
  }
2832
3805
 
2833
- /** HTTP/2 slot `v`'s socket. */
3806
+ /** HTTP/2 slot `v`'s socket, or -1 with no carrier. */
2834
3807
  private h2FdOf(v: i32): i32 {
2835
- if (this.h2Clear[v]) {
2836
- return this.h2cOf().fd(v - this.h2cBase)
3808
+ const at: i32 = this.h2Local(v)
3809
+ if (this.h2IsClear(v)) {
3810
+ const h2c: Http2CleartextServer | null = this.h2c
3811
+ return h2c === null ? -1 : h2c.fd(at)
2837
3812
  }
2838
- return this.h2Of().fd(v - this.h2TlsBase)
3813
+ const h2: Http2TlsServer | null = this.h2
3814
+ return h2 === null ? -1 : h2.fd(at)
2839
3815
  }
2840
3816
 
2841
3817
  /**
@@ -2852,78 +3828,138 @@ export class HttpServer {
2852
3828
  return
2853
3829
  }
2854
3830
  const read: boolean = (events & HTTP_SERVER_READ_BITS) !== 0
2855
- if (this.h2Clear[v]) {
2856
- const h2c: Http2CleartextServer = this.h2cOf()
2857
- const slot: i32 = v - this.h2cBase
2858
- if (read) {
2859
- h2c.readable(slot)
2860
- } else {
2861
- h2c.writable(slot)
3831
+ const h2c: Http2CleartextServer | null = this.h2c
3832
+ const h2: Http2TlsServer | null = this.h2
3833
+ if (this.h2IsClear(v)) {
3834
+ if (h2c !== null) {
3835
+ const slot: i32 = toI32(toI64(v) - toI64(this.h2cBase))
3836
+ if (read) {
3837
+ h2c.readable(slot)
3838
+ } else {
3839
+ h2c.writable(slot)
3840
+ }
2862
3841
  }
2863
- } else if (key !== null) {
2864
- const h2: Http2TlsServer = this.h2Of()
2865
- const slot: i32 = v - this.h2TlsBase
3842
+ } else if (key !== null && h2 !== null) {
3843
+ const slot: i32 = toI32(toI64(v) - toI64(this.h2TlsBase))
2866
3844
  let wants: i32 = read ? h2.readable(slot) : h2.writable(slot)
2867
3845
  for (let turn: i32 = 0; turn < 4 && h2.holds(slot) && (wants & TLS_RECORD_SIGN) !== 0; turn++) {
2868
3846
  wants = h2.signP256(slot, key)
2869
3847
  }
2870
3848
  }
2871
- if (this.h2Holds(v)) {
2872
- const conn: Http2Connection = this.h2Conn(v)
3849
+ const conn: Http2Connection | null = this.h2Holds(v) ? this.h2Conn(v) : null
3850
+ if (conn !== null) {
2873
3851
  let event: i32 = this.h2Next(v)
2874
3852
  while (event !== H2_NEED_MORE && event !== H2_ERROR) {
2875
3853
  this.h2Event(v, conn, event)
2876
3854
  event = this.h2Next(v)
2877
3855
  }
3856
+ if (conn.inputEnded) {
3857
+ this.h2Ended(v)
3858
+ }
2878
3859
  }
2879
3860
  this.h2Settle(v)
2880
3861
  }
2881
3862
 
3863
+ /**
3864
+ * `slot`'s peer has stopped sending, so no streaming request on it will
3865
+ * get more of its body: each one still waiting is marked hung up and goes
3866
+ * back to the program, once, and its stream is reset CANCEL. A hung-up
3867
+ * request's answer is dropped without touching its stream, so a stream
3868
+ * left open would keep the connection from ever being done. That holds for
3869
+ * an answer already streaming or flushing too: its peer can never close the
3870
+ * stream's other half, so the reset cuts the answer short, by design.
3871
+ */
3872
+ private h2Ended(slot: i32): void {
3873
+ const conn: Http2Connection | null = this.h2Conn(slot)
3874
+ if (conn === null) {
3875
+ return
3876
+ }
3877
+ const reqs: i32[] = this.h2Reqs
3878
+ const end: i32 = this.h2End(slot)
3879
+ for (let e: i32 = this.h2Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
3880
+ const at: i32 = reqs[e]
3881
+ if (at >= 0) {
3882
+ const r: HttpServerRequest | null = this.requestAt(at)
3883
+ if (r !== null && r.streaming && !r.bodyEnded && !r.hung) {
3884
+ this.drop(r)
3885
+ this.requeue(r)
3886
+ conn.reset(toI32(r.stream), H2_CANCEL)
3887
+ }
3888
+ }
3889
+ }
3890
+ }
3891
+
2882
3892
  /** One HTTP/2 event of `slot`'s connection. */
2883
3893
  private h2Event(slot: i32, conn: Http2Connection, event: i32): void {
2884
3894
  const id: i32 = conn.stream
2885
3895
  if (event === H2_REQUEST) {
2886
3896
  const f: HttpFields = conn.fields
2887
3897
  const status: i32 = this.screen(f)
2888
- if (status > 0) {
3898
+ // A declared length past `maxBody` is refused once the route is known, since a streaming route takes any.
3899
+ const long: boolean = status === HTTP_SERVER_TOO_LARGE && this.streamCount > 0
3900
+ if (status > 0 && !long) {
2889
3901
  this.h2Direct(slot, conn, id, status, conn.endStream)
2890
3902
  return
2891
3903
  }
2892
3904
  const e: i32 = this.h2Entry(slot)
2893
- const at: i32 = e >= 0 ? this.obtain() : -1
2894
- if (at < 0) {
3905
+ const r: HttpServerRequest | null = e >= 0 ? this.obtain() : null
3906
+ if (r === null) {
3907
+ conn.reset(id, H2_REFUSED_STREAM)
3908
+ return
3909
+ }
3910
+ if (!this.fromFields(r, f)) {
3911
+ // Still free: `admit` has not taken it.
3912
+ this.free.push(r)
2895
3913
  conn.reset(id, H2_REFUSED_STREAM)
2896
3914
  return
2897
3915
  }
2898
- const r: HttpServerRequest = this.requests[at]
2899
- this.fromFields(r, f)
2900
3916
  r.kind = HTTP_SERVER_H2
2901
3917
  r.version = 2
2902
- r.secure = !this.h2Clear[slot]
3918
+ r.secure = !this.h2IsClear(slot)
2903
3919
  r.stream = toI64(id)
2904
3920
  r.slot = slot
2905
- r.gen = this.h2Gen[slot]
3921
+ r.gen = httpServerAt(this.h2Gen, slot)
2906
3922
  r.entry = e
2907
- this.h2Streams[e] = id
2908
- this.h2Reqs[e] = at
2909
- this.h2Count[slot] = this.h2Count[slot] + 1
2910
- this.h2IdleSince[slot] = 0
2911
- this.admit(r)
2912
- if (conn.endStream) {
3923
+ const streams: i32[] = this.h2Streams
3924
+ const reqs: i32[] = this.h2Reqs
3925
+ // `h2Entry` found `e` in the table, which `h2Grow` sizes with `h2Streams`.
3926
+ if (e >= 0 && e < toI32(streams.length) && e < toI32(reqs.length)) {
3927
+ streams[e] = id
3928
+ reqs[e] = r.index
3929
+ }
3930
+ httpServerAdd(this.h2Count, slot, 1)
3931
+ this.h2Idle(slot, toI64(0))
3932
+ this.admit(r, !conn.endStream)
3933
+ const streaming: boolean = !conn.endStream && this.streamsBody(r.route)
3934
+ if (long && !streaming) {
3935
+ this.release(r)
3936
+ this.h2Direct(slot, conn, id, status, conn.endStream)
3937
+ return
3938
+ }
3939
+ if (streaming) {
3940
+ conn.hold(id)
3941
+ r.streaming = true
3942
+ r.bodyEnded = false
3943
+ this.enqueue(r)
3944
+ } else if (conn.endStream) {
2913
3945
  this.enqueue(r)
2914
3946
  }
2915
3947
  return
2916
3948
  }
2917
3949
  const at: i32 = this.h2Find(slot, id)
2918
- if (at < 0) {
3950
+ const r: HttpServerRequest | null = this.requestAt(at)
3951
+ if (r === null) {
3952
+ return
3953
+ }
3954
+ if (r.streaming) {
3955
+ this.h2Piece(conn, r, event)
2919
3956
  return
2920
3957
  }
2921
- const r: HttpServerRequest = this.requests[at]
2922
3958
  if (event === H2_DATA) {
2923
3959
  if (r.state !== HTTP_SERVER_READING) {
2924
3960
  return
2925
3961
  }
2926
- if (r.bodyLength + conn.dataLength > this.maxBody) {
3962
+ if (toI64(r.bodyLength) + toI64(conn.dataLength) > toI64(this.maxBody)) {
2927
3963
  this.release(r)
2928
3964
  this.h2Direct(slot, conn, id, HTTP_SERVER_TOO_LARGE, conn.endStream)
2929
3965
  return
@@ -2944,6 +3980,60 @@ export class HttpServer {
2944
3980
  }
2945
3981
  }
2946
3982
 
3983
+ /**
3984
+ * One HTTP/2 event of streaming request `r`: a piece of its body is kept for
3985
+ * the program while it holds `r` and credited back at once otherwise, the
3986
+ * end of its body or a reset is marked, and the program is told by `r`
3987
+ * coming out of `take` again. What is kept is bounded by the stream's
3988
+ * window, which only `consume` credits.
3989
+ */
3990
+ private h2Piece(conn: Http2Connection, r: HttpServerRequest, event: i32): void {
3991
+ if (event === H2_DATA) {
3992
+ const n: i32 = conn.dataLength
3993
+ if (
3994
+ r.state === HTTP_SERVER_QUEUED ||
3995
+ r.state === HTTP_SERVER_HELD ||
3996
+ r.state === HTTP_SERVER_STREAMING
3997
+ ) {
3998
+ // The stream window keeps the sum inside an `i32`.
3999
+ const wide: i64 = toI64(r.bodyLength) + toI64(n)
4000
+ const need: i32 = wide > toI64(2147483647) ? 2147483647 : toI32(wide)
4001
+ if (toI32(r.body.length) < need) {
4002
+ const have: i32 = toI32(r.body.length)
4003
+ const cap: i32 = conn.streamWindow()
4004
+ // Doubling past i32 is past `cap` too, which the clamp below gives.
4005
+ let doubled: i32 = cap
4006
+ if (have < 1073741824) {
4007
+ doubled = have * 2
4008
+ }
4009
+ let size: i32 = have < HTTP_SERVER_BODY_FIRST ? HTTP_SERVER_BODY_FIRST : doubled
4010
+ if (size > cap) {
4011
+ size = cap
4012
+ }
4013
+ if (size < need) {
4014
+ size = need
4015
+ }
4016
+ const grown: u8[] = new Array<u8>(size)
4017
+ copyBytes(grown, HTTP_SERVER_ZERO, r.body, HTTP_SERVER_ZERO, r.bodyLength)
4018
+ r.body = grown
4019
+ }
4020
+ copyBytes(r.body, r.bodyLength, conn.data, conn.dataStart, n)
4021
+ r.bodyLength = need
4022
+ } else {
4023
+ conn.consumeBody(conn.stream, n)
4024
+ }
4025
+ r.bodyEnded = conn.endStream
4026
+ } else if (event === H2_TRAILERS) {
4027
+ r.bodyEnded = true
4028
+ } else if (event === H2_RESET) {
4029
+ this.drop(r)
4030
+ this.unlink(r)
4031
+ } else {
4032
+ return
4033
+ }
4034
+ this.requeue(r)
4035
+ }
4036
+
2947
4037
  /**
2948
4038
  * Answers stream `id` with `status` and no body, then resets it with
2949
4039
  * NO_ERROR when the client is still sending (RFC 9113 §8.1); with
@@ -2951,7 +4041,7 @@ export class HttpServer {
2951
4041
  * carries `alt-svc` like every other answer; over h2c it carries none.
2952
4042
  */
2953
4043
  private h2Direct(slot: i32, conn: Http2Connection, id: i32, status: i32, ended: boolean): void {
2954
- const alt: boolean = this.altSvc !== null && !this.h2Clear[slot]
4044
+ const alt: boolean = this.altSvc !== null && !this.h2IsClear(slot)
2955
4045
  const names: u8[][] = alt ? this.altNames : this.noFields
2956
4046
  const values: u8[][] = alt ? this.altValues : this.noFields
2957
4047
  if (conn.respond(id, status, names, values, true) !== 0) {
@@ -2963,9 +4053,10 @@ export class HttpServer {
2963
4053
 
2964
4054
  /** A free entry of `slot`'s stream table, or -1 when it is full. */
2965
4055
  private h2Entry(slot: i32): i32 {
2966
- const base: i32 = slot * this.perConnection2
2967
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
2968
- if (this.h2Reqs[e] === -1) {
4056
+ const reqs: i32[] = this.h2Reqs
4057
+ const end: i32 = this.h2End(slot)
4058
+ for (let e: i32 = this.h2Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4059
+ if (reqs[e] === -1) {
2969
4060
  return e
2970
4061
  }
2971
4062
  }
@@ -2974,10 +4065,12 @@ export class HttpServer {
2974
4065
 
2975
4066
  /** The request `slot` holds for stream `id`, or -1. */
2976
4067
  private h2Find(slot: i32, id: i32): i32 {
2977
- const base: i32 = slot * this.perConnection2
2978
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
2979
- if (this.h2Reqs[e] >= 0 && this.h2Streams[e] === id) {
2980
- return this.h2Reqs[e]
4068
+ const reqs: i32[] = this.h2Reqs
4069
+ const streams: i32[] = this.h2Streams
4070
+ const end: i32 = this.h2End(slot)
4071
+ for (let e: i32 = this.h2Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4072
+ if (reqs[e] >= 0 && e < toI32(streams.length) && streams[e] === id) {
4073
+ return reqs[e]
2981
4074
  }
2982
4075
  }
2983
4076
  return -1
@@ -2989,7 +4082,8 @@ export class HttpServer {
2989
4082
  */
2990
4083
  private h2Settle(slot: i32): void {
2991
4084
  if (!this.h2Holds(slot)) {
2992
- if (this.h2Open[slot]) {
4085
+ const open: boolean[] = this.h2Open
4086
+ if (slot >= 0 && slot < toI32(open.length) && open[slot]) {
2993
4087
  this.h2Forget(slot)
2994
4088
  }
2995
4089
  return
@@ -2998,6 +4092,12 @@ export class HttpServer {
2998
4092
  for (let pass: i32 = 0; pass < 4096 && this.h2Holds(slot) && this.h2PushAll(slot); pass++) {
2999
4093
  wants = this.h2Flush(slot)
3000
4094
  }
4095
+ // A WINDOW_UPDATE `consume` owed while the output was full goes out now:
4096
+ // a peer at a closed window sends nothing that would make `next` send it.
4097
+ const conn: Http2Connection | null = this.h2Holds(slot) ? this.h2Conn(slot) : null
4098
+ if (conn !== null && conn.settleCredit()) {
4099
+ wants = this.h2Flush(slot)
4100
+ }
3001
4101
  if (!this.h2Holds(slot)) {
3002
4102
  this.h2Forget(slot)
3003
4103
  return
@@ -3006,21 +4106,21 @@ export class HttpServer {
3006
4106
  this.h2Close(slot)
3007
4107
  return
3008
4108
  }
3009
- pollModify(
3010
- this.loop,
3011
- this.h2FdOf(slot),
3012
- wants & (H1_WANT_READ | H1_WANT_WRITE),
3013
- HTTP_SERVER_H2_BASE + slot
3014
- )
4109
+ // `h2Opened` registered the slot under this token, so it fits.
4110
+ const token: i64 = toI64(HTTP_SERVER_H2_BASE) + toI64(slot)
4111
+ if (token <= 2147483647) {
4112
+ pollModify(this.loop, this.h2FdOf(slot), wants & (H1_WANT_READ | H1_WANT_WRITE), toI32(token))
4113
+ }
3015
4114
  }
3016
4115
 
3017
4116
  /** Pushes every response `slot` is sending: whether any moved a byte or ended. */
3018
4117
  private h2PushAll(slot: i32): boolean {
3019
4118
  let moved: boolean = false
3020
- const base: i32 = slot * this.perConnection2
3021
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
3022
- const at: i32 = this.h2Reqs[e]
3023
- if (at >= 0 && this.moves(this.requests[at])) {
4119
+ const reqs: i32[] = this.h2Reqs
4120
+ const end: i32 = this.h2End(slot)
4121
+ for (let e: i32 = this.h2Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4122
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4123
+ if (r !== null && this.moves(r)) {
3024
4124
  moved = true
3025
4125
  }
3026
4126
  }
@@ -3032,17 +4132,22 @@ export class HttpServer {
3032
4132
  if (r.state !== HTTP_SERVER_STREAMING && r.state !== HTTP_SERVER_FLUSHING) {
3033
4133
  return false
3034
4134
  }
3035
- const before: i32 = r.pendingEnd - r.pendingStart
4135
+ const before: i64 = toI64(r.pendingEnd) - toI64(r.pendingStart)
3036
4136
  this.push(r)
3037
- return r.state === HTTP_SERVER_FREE || r.pendingEnd - r.pendingStart < before
4137
+ return r.state === HTTP_SERVER_FREE || toI64(r.pendingEnd) - toI64(r.pendingStart) < before
3038
4138
  }
3039
4139
 
3040
4140
  /** Closes `slot` and forgets it. */
3041
4141
  private h2Close(slot: i32): void {
3042
- if (this.h2Clear[slot]) {
3043
- this.h2cOf().close(slot - this.h2cBase)
3044
- } else {
3045
- this.h2Of().close(slot - this.h2TlsBase)
4142
+ const h2c: Http2CleartextServer | null = this.h2c
4143
+ const h2: Http2TlsServer | null = this.h2
4144
+ const at: i32 = this.h2Local(slot)
4145
+ if (at >= 0 && this.h2IsClear(slot)) {
4146
+ if (h2c !== null) {
4147
+ h2c.close(at)
4148
+ }
4149
+ } else if (at >= 0 && h2 !== null) {
4150
+ h2.close(at)
3046
4151
  }
3047
4152
  this.h2Forget(slot)
3048
4153
  }
@@ -3053,17 +4158,29 @@ export class HttpServer {
3053
4158
  * held go back.
3054
4159
  */
3055
4160
  private h2Forget(slot: i32): void {
3056
- this.h2Open[slot] = false
3057
- this.h2Gen[slot] = this.h2Gen[slot] + 1
3058
- this.h2Count[slot] = 0
3059
- const base: i32 = slot * this.perConnection2
3060
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
3061
- const at: i32 = this.h2Reqs[e]
3062
- this.h2Reqs[e] = -1
3063
- if (at >= 0) {
3064
- const r: HttpServerRequest = this.requests[at]
4161
+ const open: boolean[] = this.h2Open
4162
+ const gen: i32[] = this.h2Gen
4163
+ const count: i32[] = this.h2Count
4164
+ // `h2Grow` sizes the three together.
4165
+ if (slot < 0 || slot >= toI32(open.length) || slot >= toI32(gen.length) || slot >= toI32(count.length)) {
4166
+ return
4167
+ }
4168
+ open[slot] = false
4169
+ // The generation only has to differ from the last one, so it wraps.
4170
+ const was: i32 = gen[slot]
4171
+ gen[slot] = was < 2147483647 ? was + 1 : 0
4172
+ count[slot] = 0
4173
+ const reqs: i32[] = this.h2Reqs
4174
+ const end: i32 = this.h2End(slot)
4175
+ for (let e: i32 = this.h2Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4176
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4177
+ reqs[e] = -1
4178
+ if (r !== null) {
3065
4179
  r.entry = -1
3066
4180
  this.drop(r)
4181
+ if (r.streaming) {
4182
+ this.requeue(r)
4183
+ }
3067
4184
  }
3068
4185
  }
3069
4186
  }
@@ -3115,15 +4232,21 @@ export class HttpServer {
3115
4232
  if (n < 0) {
3116
4233
  return count
3117
4234
  }
3118
- const segment: i32 = this.h3Meta[0] > 0 ? this.h3Meta[0] : n
4235
+ const meta: i32[] = this.h3Meta
4236
+ const given: i32 = toI32(meta.length) > 0 ? meta[0] : 0
4237
+ const segment: i32 = given > 0 ? given : n
3119
4238
  let at: i32 = 0
3120
- while (at < n) {
3121
- const length: i32 = n - at < segment ? n - at : segment
4239
+ while (at >= 0 && at < n) {
4240
+ const left: i32 = n - at
4241
+ const length: i32 = left < segment ? left : segment
3122
4242
  if (this.h3Owned(h3, rx, at, length)) {
3123
4243
  h3.datagram(rx, at, length, this.h3From, ms, key)
3124
4244
  }
3125
- count += 1
3126
- at += length
4245
+ if (count < 2147483647) {
4246
+ count += 1
4247
+ }
4248
+ // `length` is at most `n - at`, so `at` stays within `n`.
4249
+ at = toI32(toI64(at) + toI64(length))
3127
4250
  }
3128
4251
  }
3129
4252
  return count
@@ -3131,28 +4254,31 @@ export class HttpServer {
3131
4254
 
3132
4255
  /** Whether a connection of `h3` owns the datagram `buf[at .. at + len)`, by its first packet's Destination Connection ID, read as `Http3Server.datagram` reads it. */
3133
4256
  private h3Owned(h3: Http3Server, buf: u8[], at: i32, len: i32): boolean {
3134
- if (len < 1) {
4257
+ const end: i64 = toI64(at) + toI64(len)
4258
+ if (len < 1 || at < 0 || at >= toI32(buf.length) || end > toI64(buf.length)) {
3135
4259
  return false
3136
4260
  }
3137
- let dcidAt: i32 = at + 1
4261
+ let dcidOffset: i32 = 1
3138
4262
  let dcidLength: i32 = QUIC_CONN_CID_LENGTH
3139
4263
  if ((toI32(buf[at]) & 0x80) !== 0) {
3140
- if (len < 6) {
4264
+ const lengthAt: i32 = toI32(toI64(at) + toI64(5))
4265
+ if (len < 6 || lengthAt < 0 || lengthAt >= toI32(buf.length)) {
3141
4266
  return false
3142
4267
  }
3143
- dcidLength = toI32(buf[at + 5])
3144
- dcidAt = at + 6
4268
+ dcidLength = toI32(buf[lengthAt])
4269
+ dcidOffset = 6
3145
4270
  }
3146
- if (dcidLength > QUIC_MAX_CID_LENGTH || dcidAt + dcidLength > at + len) {
4271
+ if (dcidLength > QUIC_MAX_CID_LENGTH || toI64(dcidOffset) + toI64(dcidLength) > toI64(len)) {
3147
4272
  return false
3148
4273
  }
3149
- return h3.index.find(buf, dcidAt, dcidLength) >= 0
4274
+ // The ID ends within `at + len`, which fits in `buf`.
4275
+ return h3.index.find(buf, toI32(toI64(at) + toI64(dcidOffset)), dcidLength) >= 0
3150
4276
  }
3151
4277
 
3152
4278
  /** Serves `slot`'s HTTP/3 events, then pushes its responses. */
3153
4279
  private h3Serve(h3: Http3Server, slot: i32): void {
3154
4280
  const conn: Http3Connection = h3.connection(slot)
3155
- if (this.h3Gen[slot] !== conn.generation) {
4281
+ if (httpServerAt(this.h3Gen, slot) !== conn.generation) {
3156
4282
  this.h3Clear(slot, conn.generation)
3157
4283
  }
3158
4284
  let event: i32 = conn.next()
@@ -3164,11 +4290,12 @@ export class HttpServer {
3164
4290
  this.h3Clear(slot, conn.generation)
3165
4291
  return
3166
4292
  }
3167
- const base: i32 = slot * this.perConnection3
3168
- for (let e: i32 = base; e < base + this.perConnection3; e++) {
3169
- const at: i32 = this.h3Reqs[e]
3170
- if (at >= 0) {
3171
- this.moves(this.requests[at])
4293
+ const reqs: i32[] = this.h3Reqs
4294
+ const end: i32 = this.h3End(slot)
4295
+ for (let e: i32 = this.h3Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4296
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4297
+ if (r !== null) {
4298
+ this.moves(r)
3172
4299
  }
3173
4300
  }
3174
4301
  }
@@ -3184,13 +4311,17 @@ export class HttpServer {
3184
4311
  return
3185
4312
  }
3186
4313
  const e: i32 = this.h3Entry(slot)
3187
- const at: i32 = e >= 0 ? this.obtain() : -1
3188
- if (at < 0) {
4314
+ const r: HttpServerRequest | null = e >= 0 ? this.obtain() : null
4315
+ if (r === null) {
4316
+ conn.reset(id, H3_REQUEST_REJECTED)
4317
+ return
4318
+ }
4319
+ if (!this.fromFields(r, f)) {
4320
+ // Still free: `admit` has not taken it.
4321
+ this.free.push(r)
3189
4322
  conn.reset(id, H3_REQUEST_REJECTED)
3190
4323
  return
3191
4324
  }
3192
- const r: HttpServerRequest = this.requests[at]
3193
- this.fromFields(r, f)
3194
4325
  r.kind = HTTP_SERVER_H3
3195
4326
  r.version = 3
3196
4327
  r.secure = true
@@ -3198,22 +4329,27 @@ export class HttpServer {
3198
4329
  r.slot = slot
3199
4330
  r.gen = conn.generation
3200
4331
  r.entry = e
3201
- this.h3Streams[e] = id
3202
- this.h3Reqs[e] = at
3203
- this.h3Count[slot] = this.h3Count[slot] + 1
3204
- this.admit(r)
4332
+ const streams: i64[] = this.h3Streams
4333
+ const reqs: i32[] = this.h3Reqs
4334
+ // `h3Entry` found `e` in the table, which `listenHttp3` sizes with `h3Streams`.
4335
+ if (e >= 0 && e < toI32(streams.length) && e < toI32(reqs.length)) {
4336
+ streams[e] = id
4337
+ reqs[e] = r.index
4338
+ }
4339
+ httpServerAdd(this.h3Count, slot, 1)
4340
+ this.admit(r, false)
3205
4341
  return
3206
4342
  }
3207
4343
  const at: i32 = this.h3Find(slot, id)
3208
- if (at < 0) {
4344
+ const r: HttpServerRequest | null = this.requestAt(at)
4345
+ if (r === null) {
3209
4346
  return
3210
4347
  }
3211
- const r: HttpServerRequest = this.requests[at]
3212
4348
  if (event === H3_DATA) {
3213
4349
  if (r.state !== HTTP_SERVER_READING) {
3214
4350
  return
3215
4351
  }
3216
- if (r.bodyLength + conn.dataLength > this.maxBody) {
4352
+ if (toI64(r.bodyLength) + toI64(conn.dataLength) > toI64(this.maxBody)) {
3217
4353
  this.release(r)
3218
4354
  this.h3Direct(conn, id, HTTP_SERVER_TOO_LARGE)
3219
4355
  return
@@ -3244,11 +4380,24 @@ export class HttpServer {
3244
4380
  }
3245
4381
  }
3246
4382
 
4383
+ /** The first entry of HTTP/3 slot `v`'s stream table, or -1 outside the table. */
4384
+ private h3Base(v: i32): i32 {
4385
+ const base: i64 = toI64(v) * toI64(this.perConnection3)
4386
+ return v >= 0 && base >= 0 && base < toI64(toI32(this.h3Reqs.length)) ? toI32(base) : -1
4387
+ }
4388
+
4389
+ /** One past the last entry of HTTP/3 slot `v`'s stream table, or -1 outside the table. */
4390
+ private h3End(v: i32): i32 {
4391
+ const end: i64 = (toI64(v) + toI64(1)) * toI64(this.perConnection3)
4392
+ return v >= 0 && end >= 0 && end <= toI64(toI32(this.h3Reqs.length)) ? toI32(end) : -1
4393
+ }
4394
+
3247
4395
  /** A free entry of `slot`'s stream table, or -1 when it is full. */
3248
4396
  private h3Entry(slot: i32): i32 {
3249
- const base: i32 = slot * this.perConnection3
3250
- for (let e: i32 = base; e < base + this.perConnection3; e++) {
3251
- if (this.h3Reqs[e] === -1) {
4397
+ const reqs: i32[] = this.h3Reqs
4398
+ const end: i32 = this.h3End(slot)
4399
+ for (let e: i32 = this.h3Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4400
+ if (reqs[e] === -1) {
3252
4401
  return e
3253
4402
  }
3254
4403
  }
@@ -3257,10 +4406,12 @@ export class HttpServer {
3257
4406
 
3258
4407
  /** The request `slot` holds for stream `id`, or -1. */
3259
4408
  private h3Find(slot: i32, id: i64): i32 {
3260
- const base: i32 = slot * this.perConnection3
3261
- for (let e: i32 = base; e < base + this.perConnection3; e++) {
3262
- if (this.h3Reqs[e] >= 0 && this.h3Streams[e] === id) {
3263
- return this.h3Reqs[e]
4409
+ const reqs: i32[] = this.h3Reqs
4410
+ const end: i32 = this.h3End(slot)
4411
+ const streams: i64[] = this.h3Streams
4412
+ for (let e: i32 = this.h3Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4413
+ if (reqs[e] >= 0 && e < toI32(streams.length) && streams[e] === id) {
4414
+ return reqs[e]
3264
4415
  }
3265
4416
  }
3266
4417
  return -1
@@ -3271,18 +4422,23 @@ export class HttpServer {
3271
4422
  * generation `gen`: the requests only the server held go back.
3272
4423
  */
3273
4424
  private h3Clear(slot: i32, gen: i32): void {
3274
- const base: i32 = slot * this.perConnection3
3275
- for (let e: i32 = base; e < base + this.perConnection3; e++) {
3276
- const at: i32 = this.h3Reqs[e]
3277
- this.h3Reqs[e] = -1
3278
- if (at >= 0) {
3279
- const r: HttpServerRequest = this.requests[at]
4425
+ const reqs: i32[] = this.h3Reqs
4426
+ const end: i32 = this.h3End(slot)
4427
+ for (let e: i32 = this.h3Base(slot); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4428
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4429
+ reqs[e] = -1
4430
+ if (r !== null) {
3280
4431
  r.entry = -1
3281
4432
  this.drop(r)
3282
4433
  }
3283
4434
  }
3284
- this.h3Count[slot] = 0
3285
- this.h3Gen[slot] = gen
4435
+ const count: i32[] = this.h3Count
4436
+ const gens: i32[] = this.h3Gen
4437
+ // `listenHttp3` sizes the two together.
4438
+ if (slot >= 0 && slot < toI32(count.length) && slot < toI32(gens.length)) {
4439
+ count[slot] = 0
4440
+ gens[slot] = gen
4441
+ }
3286
4442
  }
3287
4443
 
3288
4444
  // ---- Timeouts -------------------------------------------------------------------
@@ -3296,10 +4452,10 @@ export class HttpServer {
3296
4452
  private expireAll(now: i64): void {
3297
4453
  const h1: Http1Server | null = this.h1
3298
4454
  if (h1 !== null) {
3299
- for (let s: i32 = 0; s < toI32(this.h1Current.length); s++) {
3300
- const at: i32 = this.h1Current[s]
3301
- if (at >= 0 && this.late(this.requests[at], now) && h1.holds(s)) {
3302
- this.h1Close(h1, s)
4455
+ for (const s of this.h1Slots) {
4456
+ const r: HttpServerRequest | null = s.current
4457
+ if (r !== null && this.late(r, now) && h1.holds(s.slot)) {
4458
+ this.h1Close(h1, s.slot)
3303
4459
  }
3304
4460
  }
3305
4461
  }
@@ -3326,7 +4482,7 @@ export class HttpServer {
3326
4482
 
3327
4483
  /** Whether `r` is still reading its body past `bodyTimeout`. */
3328
4484
  private late(r: HttpServerRequest, now: i64): boolean {
3329
- return r.state === HTTP_SERVER_READING && now - r.since > this.bodyTimeout
4485
+ return r.state === HTTP_SERVER_READING && httpServerPast(now, r.since, this.bodyTimeout)
3330
4486
  }
3331
4487
 
3332
4488
  /** `expireAll` for HTTP/2 slot `s`. */
@@ -3335,18 +4491,20 @@ export class HttpServer {
3335
4491
  this.h2Forget(s)
3336
4492
  return
3337
4493
  }
3338
- if (this.h2Clear[s] && !this.h2cOf().prefaced(s - this.h2cBase)) {
4494
+ const conn: Http2Connection | null = this.h2Prefaced(s)
4495
+ if (conn === null) {
3339
4496
  // `prefaceTimeout` holds it till then; `idleTimeout` runs from the preface.
3340
- this.h2IdleSince[s] = now
4497
+ this.h2Idle(s, now)
3341
4498
  return
3342
4499
  }
3343
- const conn: Http2Connection = this.h2Conn(s)
3344
- const base: i32 = s * this.perConnection2
3345
- for (let e: i32 = base; e < base + this.perConnection2; e++) {
3346
- const at: i32 = this.h2Reqs[e]
3347
- if (at >= 0 && this.late(this.requests[at], now)) {
3348
- const sent: i32 = conn.reset(this.h2Streams[e], H2_CANCEL)
3349
- this.release(this.requests[at])
4500
+ const streams: i32[] = this.h2Streams
4501
+ const reqs: i32[] = this.h2Reqs
4502
+ const end: i32 = this.h2End(s)
4503
+ for (let e: i32 = this.h2Base(s); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4504
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4505
+ if (r !== null && this.late(r, now) && e < toI32(streams.length)) {
4506
+ const sent: i32 = conn.reset(streams[e], H2_CANCEL)
4507
+ this.release(r)
3350
4508
  if (sent === H2_AGAIN) {
3351
4509
  // No room to send the reset: end the connection, as `abort` does.
3352
4510
  this.h2Close(s)
@@ -3354,7 +4512,9 @@ export class HttpServer {
3354
4512
  }
3355
4513
  }
3356
4514
  }
3357
- if (this.h2Count[s] === 0 && this.h2IdleSince[s] > 0 && now - this.h2IdleSince[s] > this.idleTimeout) {
4515
+ const idleSince: i64[] = this.h2IdleSince
4516
+ const since: i64 = s >= 0 && s < toI32(idleSince.length) ? idleSince[s] : 0
4517
+ if (httpServerAt(this.h2Count, s) === 0 && since > 0 && httpServerPast(now, since, this.idleTimeout)) {
3358
4518
  this.h2Close(s)
3359
4519
  return
3360
4520
  }
@@ -3363,17 +4523,19 @@ export class HttpServer {
3363
4523
 
3364
4524
  /** `expireAll` for HTTP/3 slot `s`. */
3365
4525
  private h3Expire(h3: Http3Server, s: i32, now: i64): void {
3366
- if (!h3.holds(s) || h3.connection(s).generation !== this.h3Gen[s]) {
3367
- this.h3Clear(s, h3.holds(s) ? h3.connection(s).generation : this.h3Gen[s])
4526
+ if (!h3.holds(s) || h3.connection(s).generation !== httpServerAt(this.h3Gen, s)) {
4527
+ this.h3Clear(s, h3.holds(s) ? h3.connection(s).generation : httpServerAt(this.h3Gen, s))
3368
4528
  return
3369
4529
  }
3370
4530
  const conn: Http3Connection = h3.connection(s)
3371
- const base: i32 = s * this.perConnection3
3372
- for (let e: i32 = base; e < base + this.perConnection3; e++) {
3373
- const at: i32 = this.h3Reqs[e]
3374
- if (at >= 0 && this.late(this.requests[at], now)) {
3375
- conn.reset(this.h3Streams[e], H3_REQUEST_CANCELLED)
3376
- this.release(this.requests[at])
4531
+ const streams: i64[] = this.h3Streams
4532
+ const reqs: i32[] = this.h3Reqs
4533
+ const end: i32 = this.h3End(s)
4534
+ for (let e: i32 = this.h3Base(s); e >= 0 && e < end && e < toI32(reqs.length); e++) {
4535
+ const r: HttpServerRequest | null = this.requestAt(reqs[e])
4536
+ if (r !== null && this.late(r, now) && e < toI32(streams.length)) {
4537
+ conn.reset(streams[e], H3_REQUEST_CANCELLED)
4538
+ this.release(r)
3377
4539
  h3.touch(s)
3378
4540
  }
3379
4541
  }