@amritk/nish-aarch64-linux 0.39.0 → 0.41.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 (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
@@ -89,6 +89,7 @@ import {
89
89
  HTTP1_REFUSED,
90
90
  HTTP1_UPGRADE,
91
91
  HTTP1_FIRST_INPUT,
92
+ http1ChunkExtValid,
92
93
  http1HexValue,
93
94
  http1IsFieldText,
94
95
  http1IsOws,
@@ -1428,8 +1429,8 @@ const http1ClientEndOfHead = (p: Http1ResponseParser): i32 => {
1428
1429
  }
1429
1430
 
1430
1431
  /**
1431
- * Reads a chunk-size line (RFC 9112 §7.1): 1*HEXDIG, then optional
1432
- * whitespace and a `;` extension that is read past but not interpreted. Sets
1432
+ * Reads a chunk-size line (RFC 9112 §7.1): 1*HEXDIG, then the chunk
1433
+ * extensions `http1ChunkExtValid` reads, parsed but not interpreted. Sets
1433
1434
  * `left` and answers `HTTP1_NEED_MORE`, or the refusal.
1434
1435
  */
1435
1436
  const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
@@ -1475,6 +1476,9 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1475
1476
  return http1ClientFail(p, HTTP1_E_CHUNK, "a control character in a chunk extension")
1476
1477
  }
1477
1478
  }
1479
+ if (!http1ChunkExtValid(buf, digitsEnd, b)) {
1480
+ return http1ClientFail(p, HTTP1_E_CHUNK, "a malformed chunk extension")
1481
+ }
1478
1482
  }
1479
1483
  if (over) {
1480
1484
  return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a chunked body over the limit")
@@ -35,7 +35,8 @@
35
35
  * (§2.2); whitespace before the first field or an obs-fold (§2.2, §5.2);
36
36
  * whitespace between a field name and its colon (§5.1); a field value with a
37
37
  * control character; a 1.1 request without exactly one `Host`, or a 1.0
38
- * request with more than one (§3.2, RFC 9110 §7.2); `Content-Length` that is
38
+ * request with more than one (§3.2, RFC 9110 §7.2), or a `Host` that is not
39
+ * `uri-host [":" port]` (§3.2, by `urlIsHostPort`); `Content-Length` that is
39
40
  * not one run of digits, or that appears twice (§6.3); `Transfer-Encoding`
40
41
  * together with `Content-Length`, in a 1.0 request, or whose last coding is
41
42
  * not `chunked` (§6.1, §6.3); a malformed chunk size, chunk extension or
@@ -81,6 +82,7 @@
81
82
  * Written from RFC 9112 and RFC 9110, not ported from another implementation.
82
83
  */
83
84
  import { copyBytes } from "nish/bytes"
85
+ import { urlIsHostPort } from "nish/net/url"
84
86
  import {
85
87
  and,
86
88
  any,
@@ -301,6 +303,11 @@ export class Http1Parser {
301
303
  * @internal Counted by the field-line parser in this module as each name is lowered, and read by `http1EndOfHead`.
302
304
  */
303
305
  hostFields: i32 = 0
306
+ /**
307
+ * The index of the last Host field, or -1.
308
+ * @internal Set by the field-line parser in this module, and read by `http1EndOfHead`.
309
+ */
310
+ hostField: i32 = -1
304
311
  /**
305
312
  * Content-Length fields the request's head has kept so far.
306
313
  * @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
@@ -422,15 +429,18 @@ export class Http1Parser {
422
429
  // head kept, at HTTP1_FIRST_INPUT and HTTP1_FIRST_HEAD bytes: a parser
423
430
  // that is built and never fed (a pool's spare slot) costs neither, and a
424
431
  // caller that lends them out between requests puts empty ones here.
425
- // The spans are made first: the arena places each block after the last,
426
- // so their header and first entries sit just behind the parser's fields
427
- // rather than behind four empty arrays.
428
- this.spans = new Array<i32>(64)
432
+ // The spans likewise wait for the first field line (`http1SpanSet`). An
433
+ // empty array cannot be written, only replaced, so one serves for both
434
+ // buffers. The lists share one only until the `restart` below, which
435
+ // replaces both while `keepText` is on, as it is here even for a parser
436
+ // `Http1Connection` turns it off on: the sharing saves one empty array,
437
+ // not both.
438
+ this.spans = []
429
439
  this.buf = []
430
440
  this.body = this.buf
431
- this.head = []
441
+ this.head = this.buf
432
442
  this.names = []
433
- this.values = []
443
+ this.values = this.names
434
444
  this.tally = new Http1ListTally()
435
445
  this.restart()
436
446
  }
@@ -643,6 +653,8 @@ export class Http1Parser {
643
653
  export const HTTP1_FIRST_INPUT: i32 = 1024
644
654
  /** The head buffer a parser makes for its first head. */
645
655
  export const HTTP1_FIRST_HEAD: i32 = 256
656
+ /** The spans a parser makes at its first field line: four per field, room for sixteen. */
657
+ const HTTP1_FIRST_SPANS: i32 = 64
646
658
 
647
659
  /** Makes room for `len` more bytes at `p.end`, moving the live bytes down first. */
648
660
  const http1Reserve = (p: Http1Parser, len: i32): void => {
@@ -710,6 +722,7 @@ const http1StartRequest = (p: Http1Parser): void => {
710
722
  p.sectionBytes = 0
711
723
  p.fieldCount = 0
712
724
  p.hostFields = 0
725
+ p.hostField = -1
713
726
  p.lengthFields = 0
714
727
  p.lengthField = -1
715
728
  p.codingFields = 0
@@ -1043,11 +1056,11 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
1043
1056
  return at
1044
1057
  }
1045
1058
 
1046
- /** Sets `spans[at]`, growing the array when it must. */
1059
+ /** Sets `spans[at]`, growing the array when it must: first to `HTTP1_FIRST_SPANS`, then by doubling. */
1047
1060
  const http1SpanSet = (p: Http1Parser, at: i32, value: i32): void => {
1048
1061
  if (at >= toI32(p.spans.length)) {
1049
1062
  const old: i32[] = p.spans
1050
- const grown: i32[] = new Array<i32>(2 * at + 4)
1063
+ const grown: i32[] = new Array<i32>(at < HTTP1_FIRST_SPANS ? HTTP1_FIRST_SPANS : 2 * at + 4)
1051
1064
  for (let k: i32 = 0; k < toI32(old.length) && k < toI32(grown.length); k += 1) {
1052
1065
  grown[k] = old[k]
1053
1066
  }
@@ -1442,6 +1455,7 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
1442
1455
  const kind: i32 = http1FramingField(p.head, at, nameEnd - a)
1443
1456
  if (kind === HTTP1_FIELD_HOST) {
1444
1457
  p.hostFields += 1
1458
+ p.hostField = p.headerCount
1445
1459
  } else if (kind === HTTP1_FIELD_LENGTH) {
1446
1460
  p.lengthFields += 1
1447
1461
  p.lengthField = p.headerCount
@@ -1487,6 +1501,18 @@ const http1EndOfHead = (p: Http1Parser): i32 => {
1487
1501
  if ((p.minor === 1 && hosts !== 1) || hosts > 1) {
1488
1502
  return http1Fail(p, 400, "a request without exactly one Host")
1489
1503
  }
1504
+ // The one Host must be a host and an optional port (§3.2): a value such as
1505
+ // `a@b`, `a/b` or `a, b` is read one way by this program and another by a
1506
+ // proxy or a virtual-host table in front of it. An empty Host is allowed.
1507
+ const hostField: i32 = p.hostField
1508
+ if (
1509
+ hosts === 1 &&
1510
+ hostField >= 0 &&
1511
+ 4 * hostField + 3 < toI32(spans.length) &&
1512
+ !urlIsHostPort(head, spans[4 * hostField + 2], spans[4 * hostField + 3])
1513
+ ) {
1514
+ return http1Fail(p, 400, "a Host that is not a host and port")
1515
+ }
1490
1516
  if (p.codingFields > 0) {
1491
1517
  if (lengths > 0) {
1492
1518
  return http1Fail(p, 400, "both Content-Length and Transfer-Encoding")
@@ -1600,9 +1626,105 @@ export const http1HexValue = (c: i32): i32 => {
1600
1626
  }
1601
1627
 
1602
1628
  /**
1603
- * Reads a chunk-size line (§7.1): 1*HEXDIG, then optional whitespace and a
1604
- * `;` extension that is read past but not interpreted. Sets `left` and answers
1605
- * `HTTP1_NEED_MORE`, or the refusal.
1629
+ * Where the quoted-string that opens at `bytes[from]` ends (RFC 9110 §5.6.4):
1630
+ * the index just past its closing DQUOTE, or -1 when the line ends first or
1631
+ * a byte inside is neither qdtext nor a quoted-pair. A backslash takes the
1632
+ * byte after it whatever it is, a DQUOTE included, as long as that byte is
1633
+ * HTAB, SP, VCHAR or obs-text.
1634
+ */
1635
+ const http1QuotedStringEnd = (bytes: u8[], from: i32, to: i32): i32 => {
1636
+ let i: i32 = from + 1
1637
+ while (i >= 0 && i < to && i < toI32(bytes.length)) {
1638
+ const c: i32 = toI32(bytes[i])
1639
+ if (c === 34) {
1640
+ return i + 1
1641
+ }
1642
+ if (c === 92) {
1643
+ i += 1
1644
+ if (i < 0 || i >= to || i >= toI32(bytes.length) || !http1IsValueByte(toI32(bytes[i]))) {
1645
+ return -1
1646
+ }
1647
+ } else if (!http1IsValueByte(c)) {
1648
+ return -1
1649
+ }
1650
+ i += 1
1651
+ }
1652
+ return -1
1653
+ }
1654
+
1655
+ /** The first index from `from` on, before `to`, that is not OWS. */
1656
+ const http1SkipOws = (bytes: u8[], from: i32, to: i32): i32 => {
1657
+ let i: i32 = from
1658
+ while (i >= 0 && i < to && i < toI32(bytes.length) && http1IsOws(toI32(bytes[i]))) {
1659
+ i += 1
1660
+ }
1661
+ return i
1662
+ }
1663
+
1664
+ /**
1665
+ * Whether `bytes[from .. to)`, what follows a chunk size on its line, is the
1666
+ * chunk-ext of RFC 9112 §7.1.1 and nothing else:
1667
+ * `*( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )`, the name
1668
+ * a token and the value a token or a quoted-string. An empty window holds no
1669
+ * extensions and is valid. The extensions are not interpreted, but they are
1670
+ * parsed whole: a parser that skips to the line's end and one that reads a
1671
+ * quoted-string across it disagree about where the chunk's data starts (the
1672
+ * chunk-extension smuggling of Jetty's CVE-2026-2332 and Netty's
1673
+ * CVE-2026-33870), so an unclosed quote, an empty name or value, a stray `;`
1674
+ * or `=`, and whitespace that leads nowhere are each refused. The server's
1675
+ * parser and the client's both read their chunk-size lines through this.
1676
+ * @param bytes the buffer holding the chunk-size line
1677
+ * @param from the index just past the chunk size's last hex digit
1678
+ * @param to the index of the line's CR, exclusive
1679
+ * @returns true when the window is zero or more well-formed chunk extensions
1680
+ */
1681
+ export const http1ChunkExtValid = (bytes: u8[], from: i32, to: i32): boolean => {
1682
+ const end: i32 = to < toI32(bytes.length) ? to : toI32(bytes.length)
1683
+ let i: i32 = from
1684
+ while (i >= 0 && i < end) {
1685
+ i = http1SkipOws(bytes, i, end)
1686
+ if (i < 0 || i >= end || i >= toI32(bytes.length) || toI32(bytes[i]) !== 59) {
1687
+ return false
1688
+ }
1689
+ i = http1SkipOws(bytes, i + 1, end)
1690
+ const nameStart: i32 = i
1691
+ while (i >= 0 && i < end && i < toI32(bytes.length) && http1IsTokenByte(toI32(bytes[i]))) {
1692
+ i += 1
1693
+ }
1694
+ if (i === nameStart) {
1695
+ return false
1696
+ }
1697
+ const nameEnd: i32 = i
1698
+ i = http1SkipOws(bytes, i, end)
1699
+ if (i >= 0 && i < end && i < toI32(bytes.length) && toI32(bytes[i]) === 61) {
1700
+ i = http1SkipOws(bytes, i + 1, end)
1701
+ if (i >= 0 && i < end && i < toI32(bytes.length) && toI32(bytes[i]) === 34) {
1702
+ i = http1QuotedStringEnd(bytes, i, end)
1703
+ if (i < 0) {
1704
+ return false
1705
+ }
1706
+ } else {
1707
+ const valueStart: i32 = i
1708
+ while (i >= 0 && i < end && i < toI32(bytes.length) && http1IsTokenByte(toI32(bytes[i]))) {
1709
+ i += 1
1710
+ }
1711
+ if (i === valueStart) {
1712
+ return false
1713
+ }
1714
+ }
1715
+ } else {
1716
+ // Whitespace after a name is BWS only before the next `;`, which the
1717
+ // loop's next turn reads from the name's end.
1718
+ i = nameEnd
1719
+ }
1720
+ }
1721
+ return true
1722
+ }
1723
+
1724
+ /**
1725
+ * Reads a chunk-size line (§7.1): 1*HEXDIG, then the chunk extensions
1726
+ * `http1ChunkExtValid` reads, parsed but not interpreted. Sets `left` and
1727
+ * answers `HTTP1_NEED_MORE`, or the refusal.
1606
1728
  */
1607
1729
  const http1ChunkSize = (p: Http1Parser): i32 => {
1608
1730
  const buf: u8[] = p.buf
@@ -1648,6 +1770,9 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
1648
1770
  return http1Fail(p, 400, "a control character in a chunk extension")
1649
1771
  }
1650
1772
  }
1773
+ if (!http1ChunkExtValid(buf, digitsEnd, b)) {
1774
+ return http1Fail(p, 400, "a malformed chunk extension")
1775
+ }
1651
1776
  }
1652
1777
  if (over) {
1653
1778
  return http1Fail(p, 413, "a chunked body over the limit")
@@ -35,10 +35,15 @@
35
35
  * response it owes.
36
36
  *
37
37
  * **One exchange at a time.** A request is answered before the next one on
38
- * the connection is read: `respond`, any number of `write`s, `end`. The
39
- * program may answer at once (before the body has arrived) or after
40
- * `H1_END`; either way the request's body is read to its end before the next
41
- * request starts, which is what keeps pipelined requests in order (§9.3.2).
38
+ * the connection is read: `respond`, any number of `write`s, `end`. A
39
+ * response with a length is over once its last byte is written, and one with
40
+ * no body (`Content-Length: 0`, HEAD, 204, 304) once its head is, so `end` is
41
+ * optional for both; a chunked body, and an unframed one to HTTP/1.0, has no
42
+ * length to count and is over only at `end`, without which the connection
43
+ * waits and the next request is never answered. The program may answer at
44
+ * once (before the body has arrived) or after `H1_END`; either way the
45
+ * request's body is read to its end before the next request starts, which is
46
+ * what keeps pipelined requests in order (§9.3.2).
42
47
  * Pipelined bytes wait in the parser's buffer, and once it holds
43
48
  * `inputLimit()` bytes the connection stops asking for input, so the client's
44
49
  * TCP window closes: back-pressure on the way in.
@@ -251,11 +256,18 @@ const H1_R_DONE: i32 = 2
251
256
 
252
257
  /**
253
258
  * The longest final head `respond` keeps to write again (`Http1Connection`'s
254
- * `headBytes`): one buffer of this size per connection, made at its first
255
- * kept head. A longer head is written fresh every time.
259
+ * `headBytes`, one buffer per connection made at its first kept head). A
260
+ * longer head is written fresh every time.
256
261
  */
257
262
  const H1_HEAD_KEEP: i32 = 1024
258
263
 
264
+ /**
265
+ * What a kept head's buffer is rounded up to: a cache line, so that a head a
266
+ * few bytes longer than the last (a `Content-Length` with one more digit)
267
+ * fits the buffer it has rather than making another.
268
+ */
269
+ const H1_HEAD_ROUND: i32 = 64
270
+
259
271
  /** The most program fields a kept head carries: a head with more is written fresh every time. */
260
272
  const H1_HEAD_FIELDS: i32 = 16
261
273
 
@@ -592,9 +604,10 @@ export class Http1Config {
592
604
  * come, the slot gives both to pools the server's slots share, and takes
593
605
  * them again with the next byte. The memory they hold then follows the
594
606
  * connections mid-request rather than every keep-alive connection open,
595
- * for two pool operations a request. Off by default.
607
+ * for two pool operations a request. On by default; false keeps each
608
+ * slot's buffers across the rest, about 1.9 KB more an idle connection.
596
609
  */
597
- idleInput: boolean = false
610
+ idleInput: boolean = true
598
611
  }
599
612
 
600
613
  /** The reason phrase a status is sent with; RFC 9112 §4 lets a client ignore it. */
@@ -673,6 +686,8 @@ export class Http1OutputPool {
673
686
  private size: i32 = 0
674
687
  /** How many buffers the pool has made. */
675
688
  made: i32 = 0
689
+ /** How many buffers `free` has room for without growing. */
690
+ private room: i32 = 0
676
691
 
677
692
  /**
678
693
  * An empty pool: no buffer is made until the first `take`.
@@ -696,11 +711,25 @@ export class Http1OutputPool {
696
711
  return buf
697
712
  }
698
713
  this.made = this.made + 1
699
- return new Array<u8>(this.size)
714
+ const buf: u8[] = new Array<u8>(this.size)
715
+ // `free` gets room for every buffer made here, while this allocates
716
+ // anyway, so `give` never grows it: a slot's first park can come long
717
+ // after its take, when the arena is meant to be flat.
718
+ if (this.made > this.room) {
719
+ const want: i32 = this.made * 2
720
+ for (let i: i32 = 0; i < want; i++) {
721
+ this.free.push(buf)
722
+ }
723
+ while (this.free.length > 0) {
724
+ this.free.pop()
725
+ }
726
+ this.room = want
727
+ }
728
+ return buf
700
729
  }
701
730
 
702
731
  /**
703
- * Takes back a buffer `take` gave.
732
+ * Takes back a buffer `take` gave, into the room `take` made: it allocates nothing.
704
733
  *
705
734
  * @internal Called by `Http1Connection` in this module, from `release` and `park`, once a borrowed buffer holds nothing it still needs.
706
735
  */
@@ -743,10 +772,11 @@ export class Http1Connection {
743
772
  /** Where the parser's head buffer goes while the slot rests, beside `inputs`; null otherwise. */
744
773
  heads: Http1OutputPool | null
745
774
  /**
746
- * The `Date` every final response carries. The connection makes its own;
747
- * `Http1Server` and `Http1TlsServer` put theirs here, so that every slot of a
748
- * pool shares one and the pool formats at most once a second. A program may
749
- * put another, to share it or to pin its clock.
775
+ * The `Date` every final response carries: the constructor's, or one the
776
+ * connection makes when given none. `Http1Server` and `Http1TlsServer` give
777
+ * theirs, so that every slot of a pool shares one, formats at most once a
778
+ * second and makes no `Date` of its own. A program may put another, to
779
+ * share it or to pin its clock.
750
780
  */
751
781
  date: HttpDate
752
782
  /** When the carrier last moved a byte for the connection, in `monotonicNanos`. */
@@ -847,9 +877,10 @@ export class Http1Connection {
847
877
  * status, the same field names and values byte for byte, the same framing
848
878
  * and `Connection`, and the same `date` not reformatted since (`formats`).
849
879
  * Empty until the first head that is kept (one of at most `H1_HEAD_KEEP`
850
- * bytes, after a head of the same shape); then made once and kept for the
851
- * slot's later peers, since what it holds is a function of those inputs
852
- * alone.
880
+ * bytes, after a head of the same shape); then made at that head's length
881
+ * rounded up to `H1_HEAD_ROUND`, remade only for a longer one, and kept for
882
+ * the slot's later peers, since what it holds is a function of those inputs
883
+ * alone. An idle slot holds what its heads need, not `H1_HEAD_KEEP`.
853
884
  */
854
885
  private headBytes: u8[]
855
886
  /** How many bytes of `headBytes` the kept head is; 0 while none is kept. */
@@ -866,7 +897,8 @@ export class Http1Connection {
866
897
  private headFields: i32 = 0
867
898
  /**
868
899
  * The length of each of the kept head's field names and values, in order,
869
- * name then value: `2 * H1_HEAD_FIELDS` of them, made with `headBytes`.
900
+ * name then value: two per field, made at the first head kept with that
901
+ * many fields and remade only for one with more.
870
902
  */
871
903
  private headLengths: i32[]
872
904
  /** The `HttpDate` the kept head's `Date` came from. */
@@ -891,8 +923,9 @@ export class Http1Connection {
891
923
  * A connection under `config`. A cap outside what the connection can work with panics.
892
924
  *
893
925
  * @param config the caps the connection is held to, read now and kept
926
+ * @param date the `Date` its responses carry, shared with other connections; null makes it one of its own
894
927
  */
895
- constructor(config: Http1Config) {
928
+ constructor(config: Http1Config, date: HttpDate | null = null) {
896
929
  const big: i32 = 1073741824
897
930
  const one: i32 = 1
898
931
  const smallest: i32 = 64
@@ -923,7 +956,7 @@ export class Http1Connection {
923
956
  // The parser is made first: the arena places each block after the last,
924
957
  // so its front fields sit on the lines just behind the connection's own.
925
958
  this.parser = new Http1Parser(config.maxTarget, config.maxHeaderBytes, config.maxHeaders, config.maxBody)
926
- this.date = new HttpDate()
959
+ this.date = date === null ? new HttpDate() : date
927
960
  this.parser.keepText = false
928
961
  this.parser.maxChunk = config.chunkSize
929
962
  this.ws = null
@@ -1254,6 +1287,13 @@ export class Http1Connection {
1254
1287
  private nextRequest(): i32 {
1255
1288
  while (true) {
1256
1289
  if (this.requestEnded) {
1290
+ // A body with nothing left to send is over, `end()` or not: no body
1291
+ // at all, or a length met in full. Chunked and unframed bodies have
1292
+ // no length to count, so only `end()` finishes them.
1293
+ const counted: boolean = this.responseFraming >= 0 && this.responseLeft === 0
1294
+ if (this.response === H1_R_BODY && (this.responseEmpty || counted)) {
1295
+ this.response = H1_R_DONE
1296
+ }
1257
1297
  if (this.response !== H1_R_DONE) {
1258
1298
  return H1_NEED_MORE
1259
1299
  }
@@ -1468,6 +1508,11 @@ export class Http1Connection {
1468
1508
  * output; `H1_AGAIN` when the output has no room for it yet (`H1_WRITE`
1469
1509
  * says when to try again); `H1_CLOSED` with no request to answer.
1470
1510
  *
1511
+ * A response with no body (a `bodyLength` of 0, a HEAD, a 204 or a 304) is
1512
+ * finished once this returns 0, and one with a length once `write` or
1513
+ * `sendBody` has taken all of it: `end` is optional for both. A chunked
1514
+ * body is finished only by `end`.
1515
+ *
1471
1516
  * @param status the HTTP status code to send, 100 to 599 but not 101
1472
1517
  * @param names the program's field names, one per field; nothing checks their
1473
1518
  * case, and the reserved names are refused in any ASCII case
@@ -1660,9 +1705,11 @@ export class Http1Connection {
1660
1705
  if (!again) {
1661
1706
  return
1662
1707
  }
1663
- if (toI32(this.headBytes.length) < H1_HEAD_KEEP) {
1664
- this.headBytes = new Array<u8>(H1_HEAD_KEEP)
1665
- this.headLengths = new Array<i32>(2 * H1_HEAD_FIELDS)
1708
+ if (toI32(this.headBytes.length) < n) {
1709
+ this.headBytes = new Array<u8>(((n + H1_HEAD_ROUND - 1) / H1_HEAD_ROUND) * H1_HEAD_ROUND)
1710
+ }
1711
+ if (toI32(this.headLengths.length) < 2 * fields) {
1712
+ this.headLengths = new Array<i32>(2 * fields)
1666
1713
  }
1667
1714
  for (let i: i32 = 0; i < fields && i < toI32(values.length); i += 1) {
1668
1715
  this.headLengths[2 * i] = toI32(names[i].length)
@@ -1683,7 +1730,8 @@ export class Http1Connection {
1683
1730
  * everything was taken, `H1_WRITE` says when to try the rest; `H1_AGAIN`
1684
1731
  * when nothing was. A response with no body takes everything and sends
1685
1732
  * nothing. `H1_CLOSED` with no response under way. A window outside `buf`
1686
- * panics.
1733
+ * panics. Once the bytes taken reach the `Content-Length`, the response is
1734
+ * finished whether or not `end` follows; a chunked body still needs `end`.
1687
1735
  *
1688
1736
  * @param buf the body bytes to send, which stay the caller's
1689
1737
  * @param off where in `buf` the window starts
@@ -1745,7 +1793,8 @@ export class Http1Connection {
1745
1793
  * carrier) copy all of it: it is `write`, with the same bytes on the wire.
1746
1794
  * Answers what `write` answers, `H1_INVALID` for more than the response still
1747
1795
  * owes; `H1_CLOSED` also when the socket failed during the send, which aborts
1748
- * the connection. A window outside `buf` panics.
1796
+ * the connection. A window outside `buf` panics. As with `write`, a
1797
+ * `Content-Length` body sent in full needs no `end`.
1749
1798
  *
1750
1799
  * @param buf the body bytes to send, free for the caller to reuse once this returns
1751
1800
  * @param off where in `buf` the window starts
@@ -1798,6 +1847,13 @@ export class Http1Connection {
1798
1847
  * `H1_AGAIN` when the last chunk does not fit yet; `H1_CLOSED` with no
1799
1848
  * response under way.
1800
1849
  *
1850
+ * Required for a chunked body, and for an unframed one (a chunked response
1851
+ * to HTTP/1.0), which only `end` can say is over: without it the connection
1852
+ * waits and the next request is never read. Optional for a response with
1853
+ * no body or a `Content-Length` written in full, which the connection
1854
+ * finishes itself when it moves to the next request; called before then it
1855
+ * answers 0, and after then `H1_CLOSED`.
1856
+ *
1801
1857
  * @returns 0 once the response is complete, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
1802
1858
  */
1803
1859
  end(): i32 {
@@ -2349,9 +2405,8 @@ export class Http1Server {
2349
2405
 
2350
2406
  /** A new connection on this pool's outputs and `Date`, for the next slot built. */
2351
2407
  private build(): Http1Connection {
2352
- const conn: Http1Connection = new Http1Connection(this.config)
2408
+ const conn: Http1Connection = new Http1Connection(this.config, this.date)
2353
2409
  conn.pool = this.outputs
2354
- conn.date = this.date
2355
2410
  if (this.config.idleInput) {
2356
2411
  conn.inputs = this.inputs
2357
2412
  conn.heads = this.heads
@@ -2804,9 +2859,8 @@ export class Http1TlsServer {
2804
2859
 
2805
2860
  /** A connection on this pool's outputs and `Date` that no slot holds yet. */
2806
2861
  private make(): Http1Connection {
2807
- const conn: Http1Connection = new Http1Connection(this.config)
2862
+ const conn: Http1Connection = new Http1Connection(this.config, this.date)
2808
2863
  conn.pool = this.outputs
2809
- conn.date = this.date
2810
2864
  if (this.config.idleInput) {
2811
2865
  conn.inputs = this.inputs
2812
2866
  conn.heads = this.heads
package/std/net/http2.ts CHANGED
@@ -1603,13 +1603,23 @@ export class Http2Connection {
1603
1603
  return H2_NEED_MORE
1604
1604
  }
1605
1605
  let opened: boolean = false
1606
+ // INITIAL_WINDOW_SIZE moves every stream's window, so the streams are
1607
+ // walked once, after every entry has been checked: a walk per entry let
1608
+ // one frame of 2,730 of them cost 2,730 walks (H2-5). The windows end at
1609
+ // the frame's last value, as they would entry by entry, and since every
1610
+ // window moves by the same amount, one passes 2^31 - 1 at some entry
1611
+ // (§6.9.2, the entries taken in order, §6.5.3) exactly when it would at
1612
+ // the frame's largest value.
1613
+ let window: i64 = this.peerInitialWindow
1614
+ let highest: i64 = this.peerInitialWindow
1606
1615
  const n: i32 = http2SettingCount(f)
1607
1616
  for (let k: i32 = 0; k < n; k++) {
1608
1617
  const id: i32 = http2SettingId(f, this.input, k)
1609
1618
  const value: i64 = http2SettingValue(f, this.input, k)
1610
1619
  const error: i32 = http2SettingError(id, value)
1611
1620
  if (error !== H2_NO_ERROR) {
1612
- return this.fail(error)
1621
+ // An earlier entry that took a window past 2^31 - 1 was the first error.
1622
+ return this.fail(this.windowPassesMax(highest) ? H2_FLOW_CONTROL_ERROR : error)
1613
1623
  }
1614
1624
  if (id === H2_SETTINGS_HEADER_TABLE_SIZE) {
1615
1625
  const size: i32 = toI32(Math.min(value, toI64(HPACK_DEFAULT_TABLE_SIZE)))
@@ -1617,19 +1627,9 @@ export class Http2Connection {
1617
1627
  this.encoder.setMaxTableSize(size)
1618
1628
  }
1619
1629
  } else if (id === H2_SETTINGS_INITIAL_WINDOW_SIZE) {
1620
- const delta: i64 = value - this.peerInitialWindow
1621
- this.peerInitialWindow = value
1622
- for (const s of this.streams) {
1623
- if (s.id !== 0) {
1624
- s.sendWindow = s.sendWindow + delta
1625
- if (s.sendWindow > toI64(H2_MAX_WINDOW)) {
1626
- return this.fail(H2_FLOW_CONTROL_ERROR)
1627
- }
1628
- if (delta > toI64(0) && s.blocked) {
1629
- s.blocked = false
1630
- opened = true
1631
- }
1632
- }
1630
+ window = value
1631
+ if (value > highest) {
1632
+ highest = value
1633
1633
  }
1634
1634
  } else if (id === H2_SETTINGS_MAX_FRAME_SIZE) {
1635
1635
  this.peerMaxFrameSize = toI32(value)
@@ -1637,6 +1637,22 @@ export class Http2Connection {
1637
1637
  this.peerMaxHeaderList = value
1638
1638
  }
1639
1639
  }
1640
+ if (this.windowPassesMax(highest)) {
1641
+ return this.fail(H2_FLOW_CONTROL_ERROR)
1642
+ }
1643
+ if (window !== this.peerInitialWindow) {
1644
+ const delta: i64 = window - this.peerInitialWindow
1645
+ this.peerInitialWindow = window
1646
+ for (const s of this.streams) {
1647
+ if (s.id !== 0) {
1648
+ s.sendWindow = s.sendWindow + delta
1649
+ if (delta > toI64(0) && s.blocked) {
1650
+ s.blocked = false
1651
+ opened = true
1652
+ }
1653
+ }
1654
+ }
1655
+ }
1640
1656
  this.wrote(http2WriteSettingsAck(this.output, this.outputEnd))
1641
1657
  if (opened) {
1642
1658
  this.stream = 0
@@ -1645,6 +1661,23 @@ export class Http2Connection {
1645
1661
  return H2_NEED_MORE
1646
1662
  }
1647
1663
 
1664
+ /**
1665
+ * Whether SETTINGS_INITIAL_WINDOW_SIZE at `initial` would take an open
1666
+ * stream's send window past 2^31 - 1, the connection error of §6.9.2.
1667
+ */
1668
+ private windowPassesMax(initial: i64): boolean {
1669
+ if (initial <= this.peerInitialWindow) {
1670
+ return false
1671
+ }
1672
+ const delta: i64 = initial - this.peerInitialWindow
1673
+ for (const s of this.streams) {
1674
+ if (s.id !== 0 && s.sendWindow + delta > toI64(H2_MAX_WINDOW)) {
1675
+ return true
1676
+ }
1677
+ }
1678
+ return false
1679
+ }
1680
+
1648
1681
  /** WINDOW_UPDATE (§6.9): a send window grows, and a write it held back may go on. */
1649
1682
  private handleWindowUpdate(f: Http2Frame): i32 {
1650
1683
  const inc: i64 = toI64(f.increment)
@@ -86,9 +86,21 @@
86
86
  * bucket from a bitmap. A deadline past the wheel's horizon is filed at the
87
87
  * horizon and filed again when it comes round. So no call scans the slots.
88
88
  *
89
+ * The HTTP/3 side's clock (`Http3Connection.clock`, H3-5) runs on every
90
+ * flush and files its deadline with the rest: a slot that goes
91
+ * `idleTimeout` without progress while no request is in hand is closed with
92
+ * H3_NO_ERROR, and one whose request head has waited `headTimeout` (a
93
+ * client trickling its HEADERS, the slowloris of HTTP/3) with
94
+ * H3_EXCESSIVE_LOAD, as the program writes no timer of its own. A client
95
+ * that only acknowledges or sends PING keeps its QUIC connection alive but
96
+ * not its slot. The clock times a slot from the first `next` that finds the
97
+ * handshake chose `h3`, whether or not the client has let this side's
98
+ * streams open; a slot the program runs another protocol on, such as
99
+ * `hq-interop`, is left to QUIC's idle timeout.
100
+ *
89
101
  * **Caps, fixed at start-up**: the slots, and inside each the QUIC
90
102
  * configuration's streams and buffers and the `Http3Config`'s field section,
91
- * body chunk and DATA frame.
103
+ * body chunk, DATA frame and two timeouts.
92
104
  *
93
105
  * A slot's connections and address are built on the first Initial accepted into
94
106
  * it and kept, so a pool sized for its peak costs at start-up only its per-slot
@@ -616,13 +628,21 @@ export class Http3Server {
616
628
  * @param config The HTTP/3 configuration, kept by reference; its `bodyChunk` and `maxFieldSectionSize`
617
629
  * size the shared buffer pools.
618
630
  * @param fd The bound UDP socket, kept; the server does not close it.
619
- * @param size How many slots, 1 to 65536; any other panics.
631
+ * @param size How many slots, 1 to 65536; any other panics, as does an `idleTimeout` or `headTimeout`
632
+ * under 1.
620
633
  * @param listenerEntropy `QUIC_LISTENER_ENTROPY_SIZE` random bytes, copied and then zeroed in this array.
621
634
  */
622
635
  constructor(quicConfig: QuicServerConfig, config: Http3Config, fd: i32, size: i32, listenerEntropy: u8[]) {
623
636
  if (size < 1 || size > 65536) {
624
637
  panic(`Http3Server: ${size} slots, outside 1 to 65536`)
625
638
  }
639
+ // Checked here as well as by each slot's connection, which is built only on its first Initial.
640
+ if (config.idleTimeout < 1) {
641
+ panic(`Http3Server: idleTimeout of ${config.idleTimeout}, outside 1 to 2147483647`)
642
+ }
643
+ if (config.headTimeout < 1) {
644
+ panic(`Http3Server: headTimeout of ${config.headTimeout}, outside 1 to 2147483647`)
645
+ }
626
646
  this.quicConfig = quicConfig
627
647
  this.config = config
628
648
  this.fd = fd
@@ -1139,9 +1159,12 @@ export class Http3Server {
1139
1159
  private flushSlot(slot: i32, now: i64): void {
1140
1160
  const quic: QuicConnection = this.quics[slot]
1141
1161
  quic.recovery.micros = this.micros
1142
- if (this.conns[slot].isDone() && !quic.closed()) {
1162
+ const conn: Http3Connection = this.conns[slot]
1163
+ if (conn.isDone() && !quic.closed()) {
1143
1164
  quic.close(H3_NO_ERROR)
1144
1165
  }
1166
+ // An idle client, or one trickling a request's head, loses the slot here (H3-5).
1167
+ const clocked: i64 = conn.clock(now)
1145
1168
  // A PATH_RESPONSE owed on a path the connection did not move to goes
1146
1169
  // there, alone (RFC 9000 §9.1); asking for it runs the connection's
1147
1170
  // timers first, so a validation that ran out has moved it back before
@@ -1199,6 +1222,9 @@ export class Http3Server {
1199
1222
  if (paced > now && (due < 0 || paced < due)) {
1200
1223
  due = paced
1201
1224
  }
1225
+ if (clocked >= 0 && (due < 0 || clocked < due)) {
1226
+ due = clocked
1227
+ }
1202
1228
  this.wheel.file(slot, due, now)
1203
1229
  }
1204
1230