@amritk/nish-aarch64-linux 0.46.0 → 0.48.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 (46) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +29 -18
  3. package/package.json +1 -1
  4. package/runtime/gfx/README.md +55 -0
  5. package/runtime/gfx/nish-gfx.c +1063 -0
  6. package/runtime/gfx/nish-gfx.h +221 -0
  7. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  8. package/runtime/nish.d.ts +22 -5
  9. package/runtime/nish.h +1 -0
  10. package/runtime/nish.mjs +1 -0
  11. package/runtime/runtime-bytes.c +167 -7
  12. package/runtime/runtime-net.c +122 -52
  13. package/runtime/runtime-uring.c +150 -69
  14. package/runtime/sqlite/nish-sqlite.c +166 -39
  15. package/scripts/build.sh +88 -3
  16. package/std/README.md +2 -2
  17. package/std/base64.ts +28 -30
  18. package/std/compress/deflate.ts +1371 -704
  19. package/std/crypto/aes.ts +81 -45
  20. package/std/crypto/bcrypt-batch.ts +23 -4
  21. package/std/crypto/bcrypt.ts +36 -11
  22. package/std/crypto/chacha20poly1305.ts +93 -52
  23. package/std/crypto/x25519.ts +7 -4
  24. package/std/host/game.ts +52 -20
  25. package/std/host/ring.ts +115 -31
  26. package/std/host/text.ts +9 -3
  27. package/std/json.ts +83 -0
  28. package/std/math.ts +161 -131
  29. package/std/net/handler.ts +24 -6
  30. package/std/net/http-server.ts +358 -141
  31. package/std/net/http1-server.ts +44 -13
  32. package/std/net/prefork.ts +1 -2
  33. package/std/net/static.ts +7 -2
  34. package/std/random.ts +34 -51
  35. package/std/raster.ts +377 -113
  36. package/std/regex.ts +46 -15
  37. package/std/release.ts +19 -13
  38. package/std/sqlite.ts +380 -16
  39. package/std/synth.ts +20 -3
  40. package/std/temporal.ts +740 -363
  41. package/std/term-io.ts +81 -33
  42. package/std/term-pixels.ts +287 -112
  43. package/std/term-probe.ts +7 -3
  44. package/std/term-rate.ts +142 -66
  45. package/std/term.ts +782 -406
  46. package/std/testing.ts +14 -7
@@ -1,9 +1,9 @@
1
1
  /**
2
- * `nish/net/http-server` — one handler for HTTP/1.1, HTTP/2 (over TLS and
3
- * cleartext) and HTTP/3, with Alt-Svc advertising the HTTP/3 port.
2
+ * `nish/net/http-server` — one handler for HTTP/1.1 and HTTP/2 (each over TLS
3
+ * or cleartext) and HTTP/3, with Alt-Svc advertising the HTTP/3 port.
4
4
  *
5
5
  * `HttpServer` is `HttpHandler`'s pull loop (`nish/net/handler`) over up to
6
- * four listeners at once. The program builds a `Router`, configures the
6
+ * five listeners at once. The program builds a `Router`, configures the
7
7
  * listeners, and loops on `waitSigned` (or `wait`, when every listener is
8
8
  * cleartext), `ready` and `take`, switching on `r.route`; one route body
9
9
  * answers every version.
@@ -70,6 +70,7 @@ import {
70
70
  import { HTTP1_CHUNKED, HTTP1_NO_BODY, http1HexValue } from "nish/net/http1-codec"
71
71
  import {
72
72
  H1_AGAIN,
73
+ H1_ALPN,
73
74
  H1_BODY,
74
75
  H1_DONE,
75
76
  H1_END,
@@ -83,6 +84,7 @@ import {
83
84
  Http1Config,
84
85
  Http1Connection,
85
86
  Http1Server,
87
+ Http1TlsServer,
86
88
  } from "nish/net/http1-server"
87
89
  import {
88
90
  H2_AGAIN,
@@ -166,16 +168,14 @@ const HTTP_SERVER_VALUE_END: i32 = 3
166
168
  const HTTP_SERVER_H1: i32 = 1
167
169
  const HTTP_SERVER_H2: i32 = 2
168
170
  const HTTP_SERVER_H3: i32 = 3
169
-
170
- // Poll tokens: an HTTP/1.1 slot is its own number, an HTTP/2 slot is past
171
- // `HTTP_SERVER_H2_BASE`, and a watched descriptor is `-2 - id`. A listener's
172
- // token, less -100, is also its entry in `restUntil`.
171
+ // Poll tokens: an HTTP/1.1 slot is its `h1Slots` index, an HTTP/2 slot is past `HTTP_SERVER_H2_BASE`,
172
+ // a watched descriptor is `-2 - id`, and a listener's token less -100 is its entry in `restUntil`.
173
173
  const HTTP_SERVER_H1_LISTENER: i32 = -100
174
174
  const HTTP_SERVER_H2_LISTENER: i32 = -101
175
175
  const HTTP_SERVER_H3_SOCKET: i32 = -102
176
176
  const HTTP_SERVER_H2C_LISTENER: i32 = -103
177
+ const HTTP_SERVER_H1_TLS_LISTENER: i32 = -104
177
178
  const HTTP_SERVER_H2_BASE: i32 = 1048576
178
-
179
179
  const HTTP_SERVER_READ_BITS: i32 = 5
180
180
  const HTTP_SERVER_ZERO: i32 = 0
181
181
  const HTTP_SERVER_ACCEPT_NONE: i32 = -11
@@ -902,12 +902,21 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
902
902
 
903
903
  /**
904
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.
905
+ * slot number, its carrier, its generation, its request, and whether it is
906
+ * open, waits for a free request or is being driven. `listenHttp1` and
907
+ * `listenHttp1Tls` make one per carrier slot.
907
908
  */
908
909
  class HttpServerH1Slot {
909
- /** The carrier's slot number. */
910
+ /** The slot's index in the server's `h1Slots`, which is also its poll token. */
910
911
  slot: i32
912
+ /** Whether the slot is on the TLS carrier (`listenHttp1Tls`) rather than the cleartext one. */
913
+ tls: boolean
914
+ /** The slot's number in its own carrier. */
915
+ carrier: i32
916
+ /** Its carrier's `outputSize`. */
917
+ outputSize: i32
918
+ /** The interest a TLS slot is armed for, which `h1Arm` compares against; the cleartext carrier keeps its own. */
919
+ armed: i32 = 0
911
920
  /** Bumped each time the slot closes, so an answer still owed to the connection before is dropped; it wraps. */
912
921
  gen: i32 = 0
913
922
  /** The request the slot is reading or answering, null for none. */
@@ -924,16 +933,20 @@ class HttpServerH1Slot {
924
933
  */
925
934
  driving: i32 = 0
926
935
 
927
- constructor(slot: i32) {
936
+ constructor(slot: i32, tls: boolean, carrier: i32, outputSize: i32) {
928
937
  this.slot = slot
938
+ this.tls = tls
939
+ this.carrier = carrier
940
+ this.outputSize = outputSize
929
941
  }
930
942
  }
931
943
 
932
944
  /**
933
- * One handler for HTTP/1.1, HTTP/2 over TLS and HTTP/3: configure up to one
934
- * listener of each kind, then loop on `waitSigned` (or `wait`), `ready` and
935
- * `take`, answering each request with `send`, `json` and `sendJson`,
936
- * `problem`, `refuse`, or `start`, `write` and `end`.
945
+ * One handler for HTTP/1.1 (cleartext and over TLS), HTTP/2 (over TLS and
946
+ * cleartext) and HTTP/3: configure up to one listener of each kind, then
947
+ * loop on `waitSigned` (or `wait`), `ready` and `take`, answering each
948
+ * request with `send`, `json` and `sendJson`, `problem`, `refuse`, or
949
+ * `start`, `write` and `end`.
937
950
  */
938
951
  export class HttpServer {
939
952
  private router: Router
@@ -985,8 +998,12 @@ export class HttpServer {
985
998
  /** When `wait` next looks for timeouts. */
986
999
  private sweepAt: i64 = 0
987
1000
 
988
- // HTTP/1.1: the carrier, its listener, and a record per slot.
1001
+ // HTTP/1.1: the two carriers, and a record per slot of either. Cleartext
1002
+ // slot `s` is record `h1Base + s`, TLS slot `s` is record `h1TlsBase + s`.
989
1003
  private h1: Http1Server | null = null
1004
+ private h1Tls: Http1TlsServer | null = null
1005
+ private h1Base: i32 = 0
1006
+ private h1TlsBase: i32 = 0
990
1007
  /** Per listener (`-100 - token`): when its rest for want of descriptors ends, 0 while it is not resting. */
991
1008
  private restUntil: i64[]
992
1009
  private h1Slots: HttpServerH1Slot[]
@@ -1042,7 +1059,8 @@ export class HttpServer {
1042
1059
  /** The readiness loop's descriptor, from `pollCreate`. */
1043
1060
  private loop: i32 = 0
1044
1061
  private h1Fd: i32 = -1
1045
- private h1OutputSize: i32 = 0
1062
+ /** The HTTP/1.1 over TLS listener, -1 until `listenHttp1Tls`. */
1063
+ private h1TlsFd: i32 = -1
1046
1064
  /** Where `feedStarved` takes the next slot of `starved`; the slots before it are spent. */
1047
1065
  private starvedHead: i32 = 0
1048
1066
  /** Where `fromFields` writes next in a request's head. */
@@ -1063,7 +1081,8 @@ export class HttpServer {
1063
1081
 
1064
1082
  /**
1065
1083
  * A server routing through `router`, held to `limits`. It has no listener
1066
- * yet: add them with `listenHttp1`, `listenHttp2Tls` and `listenHttp3`.
1084
+ * yet: add them with `listenHttp1`, `listenHttp1Tls`, `listenH2c`,
1085
+ * `listenHttp2Tls` and `listenHttp3`.
1067
1086
  * Panics on a limit outside its range.
1068
1087
  * @param limits the caps and timeouts, read once here
1069
1088
  * @param router the routes, which the server keeps and consults at each request head
@@ -1142,7 +1161,7 @@ export class HttpServer {
1142
1161
  this.outValues = []
1143
1162
  this.h1Slots = []
1144
1163
  this.starved = []
1145
- this.restUntil = new Array<i64>(4)
1164
+ this.restUntil = new Array<i64>(5)
1146
1165
  this.h2Clear = []
1147
1166
  this.h2Gen = []
1148
1167
  this.h2Open = []
@@ -1166,10 +1185,11 @@ export class HttpServer {
1166
1185
 
1167
1186
  /**
1168
1187
  * Serves HTTP/1.1 on `fd`, a listening TCP socket the program made, with up
1169
- * to `slots` connections. Panics on a second HTTP/1.1 listener, or when
1170
- * `config` would let in a head past `limits.maxHead` (its `maxTarget`, its
1171
- * `maxHeaderBytes` and 44 bytes of request line) or more fields than
1172
- * `limits.maxFields`. `config.maxBody` is set to `limits.maxBody`.
1188
+ * to `slots` connections. Panics on a second cleartext HTTP/1.1 listener,
1189
+ * when `config` would let in a head past `limits.maxHead` (its `maxTarget`,
1190
+ * its `maxHeaderBytes` and 44 bytes of request line) or more fields than
1191
+ * `limits.maxFields`, or when `slots`, with the TLS listener's if it came
1192
+ * first, pass 1048576. `config.maxBody` is set to `limits.maxBody`.
1173
1193
  * @param fd the listening socket's descriptor, still the program's to close
1174
1194
  * @param config the carrier's caps and timeouts, kept by the carrier
1175
1195
  * @param slots the most connections held at once
@@ -1178,30 +1198,76 @@ export class HttpServer {
1178
1198
  if (this.h1Fd >= 0) {
1179
1199
  panic("HttpServer.listenHttp1: an HTTP/1.1 listener is already configured")
1180
1200
  }
1201
+ this.checkHttp1("listenHttp1", config, slots)
1202
+ this.h1 = new Http1Server(config, fd, slots)
1203
+ this.h1Fd = fd
1204
+ this.h1Base = this.h1Grow(slots, false, config.outputSize)
1205
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
1206
+ }
1207
+
1208
+ /**
1209
+ * Serves HTTP/1.1 over TLS (ALPN `http/1.1`, or none) on `fd`, a listening
1210
+ * TCP socket the program made, with up to `slots` connections, on a port of
1211
+ * its own: a client that offers only another protocol is shut down, never
1212
+ * handed to the HTTP/2 listener. Its requests say `secure` true and its
1213
+ * responses carry Alt-Svc as HTTP/2 over TLS ones do. Panics as
1214
+ * `listenHttp1` does, on a second listener of this kind, or when `slots`,
1215
+ * with the cleartext listener's if it came first, pass 1048576.
1216
+ * `config.maxBody` is set to `limits.maxBody`. Serve it with `waitSigned`.
1217
+ * @param fd the listening socket's descriptor, still the program's to close
1218
+ * @param tls the certificate chain and TLS settings
1219
+ * @param config the carrier's caps and timeouts, kept by the carrier
1220
+ * @param slots the most connections held at once
1221
+ */
1222
+ listenHttp1Tls(fd: i32, tls: TlsServerConfig, config: Http1Config, slots: i32): void {
1223
+ if (this.h1TlsFd >= 0) {
1224
+ panic("HttpServer.listenHttp1Tls: an HTTP/1.1 over TLS listener is already configured")
1225
+ }
1226
+ this.checkHttp1("listenHttp1Tls", config, slots)
1227
+ this.h1Tls = new Http1TlsServer(tls, config, fd, slots)
1228
+ this.h1TlsFd = fd
1229
+ this.h1TlsBase = this.h1Grow(slots, true, config.outputSize)
1230
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_TLS_LISTENER)
1231
+ }
1232
+
1233
+ /**
1234
+ * Panics, naming `name`, when `config` would let in a head past
1235
+ * `limits.maxHead` or more fields than `limits.maxFields`, on fewer than
1236
+ * one slot, or on more than the HTTP/1.1 tokens hold; then sets
1237
+ * `config.maxBody` to `limits.maxBody`.
1238
+ */
1239
+ private checkHttp1(name: string, config: Http1Config, slots: i32): void {
1181
1240
  const head: i64 = toI64(config.maxTarget) + toI64(config.maxHeaderBytes) + HTTP_SERVER_H1_LINE_OVERHEAD
1182
1241
  if (head > toI64(this.maxHead)) {
1183
1242
  panic(
1184
- `HttpServer.listenHttp1: maxTarget ${config.maxTarget} + maxHeaderBytes ${config.maxHeaderBytes} + 44 is more than limits.maxHead ${this.maxHead}`
1243
+ `HttpServer.${name}: maxTarget ${config.maxTarget} + maxHeaderBytes ${config.maxHeaderBytes} + 44 is more than limits.maxHead ${this.maxHead}`
1185
1244
  )
1186
1245
  }
1187
1246
  if (config.maxHeaders > this.maxFields) {
1188
1247
  panic(
1189
- `HttpServer.listenHttp1: maxHeaders ${config.maxHeaders} is more than limits.maxFields ${this.maxFields}`
1248
+ `HttpServer.${name}: maxHeaders ${config.maxHeaders} is more than limits.maxFields ${this.maxFields}`
1190
1249
  )
1191
1250
  }
1192
1251
  if (slots < 1) {
1193
- panic(`HttpServer.listenHttp1: slots ${slots} is below 1`)
1252
+ panic(`HttpServer.${name}: slots ${slots} is below 1`)
1253
+ }
1254
+ // A slot's record index is its poll token, which must stay below the HTTP/2 tokens.
1255
+ if (toI64(toI32(this.h1Slots.length)) + toI64(slots) > toI64(HTTP_SERVER_H2_BASE)) {
1256
+ panic(
1257
+ `HttpServer.${name}: slots ${slots} and the ${this.h1Slots.length} HTTP/1.1 slots listened before are more than 1048576`
1258
+ )
1194
1259
  }
1195
1260
  config.maxBody = this.maxBody
1196
- this.h1 = new Http1Server(config, fd, slots)
1197
- this.h1Fd = fd
1198
- this.h1OutputSize = config.outputSize
1199
- const table: HttpServerH1Slot[] = []
1261
+ }
1262
+
1263
+ /** Appends a record for each of a new carrier's `slots` and answers the index of its first. */
1264
+ private h1Grow(slots: i32, tls: boolean, outputSize: i32): i32 {
1265
+ const table: HttpServerH1Slot[] = this.h1Slots
1266
+ const base: i32 = toI32(table.length)
1200
1267
  for (let s: i32 = 0; s < slots; s++) {
1201
- table.push(new HttpServerH1Slot(s))
1268
+ table.push(new HttpServerH1Slot(toI32(table.length), tls, s, outputSize))
1202
1269
  }
1203
- this.h1Slots = table
1204
- pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
1270
+ return base
1205
1271
  }
1206
1272
 
1207
1273
  /**
@@ -1424,14 +1490,14 @@ export class HttpServer {
1424
1490
 
1425
1491
  /**
1426
1492
  * One turn of the loop for a server with no TLS or QUIC listener: as
1427
- * `waitSigned`, with no key. It serves HTTP/1.1 and h2c (`listenH2c`).
1428
- * Panics when an HTTP/2 over TLS or HTTP/3 listener is configured, since a
1429
- * handshake there needs the key.
1493
+ * `waitSigned`, with no key. It serves cleartext HTTP/1.1 and h2c
1494
+ * (`listenH2c`). Panics when an HTTP/1.1 or HTTP/2 over TLS or an HTTP/3
1495
+ * listener is configured, since a handshake there needs the key.
1430
1496
  * @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
1431
1497
  * @returns how many whole requests are queued for `take` after this turn
1432
1498
  */
1433
1499
  wait(timeoutMs: i32): i32 {
1434
- if (this.h2Fd >= 0 || this.h3 !== null) {
1500
+ if (this.h2Fd >= 0 || this.h1TlsFd >= 0 || this.h3 !== null) {
1435
1501
  panic("HttpServer.wait: a TLS or QUIC listener needs the certificate's key; call waitSigned")
1436
1502
  }
1437
1503
  return this.turnWith(timeoutMs, null)
@@ -1465,6 +1531,7 @@ export class HttpServer {
1465
1531
  timeout = this.restListener(this.h1Fd, HTTP_SERVER_H1_LISTENER, now, timeout)
1466
1532
  timeout = this.restListener(this.h2Fd, HTTP_SERVER_H2_LISTENER, now, timeout)
1467
1533
  timeout = this.restListener(this.h2cFd, HTTP_SERVER_H2C_LISTENER, now, timeout)
1534
+ timeout = this.restListener(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER, now, timeout)
1468
1535
  const h3: Http3Server | null = this.h3
1469
1536
  if (h3 !== null) {
1470
1537
  h3.flush(now / HTTP_SERVER_NANOS_PER_MS)
@@ -1494,6 +1561,8 @@ export class HttpServer {
1494
1561
  this.acceptHttp2()
1495
1562
  } else if (token === HTTP_SERVER_H2C_LISTENER) {
1496
1563
  this.acceptH2c()
1564
+ } else if (token === HTTP_SERVER_H1_TLS_LISTENER) {
1565
+ this.acceptHttp1Tls()
1497
1566
  } else if (token === HTTP_SERVER_H3_SOCKET) {
1498
1567
  this.h3Readable = true
1499
1568
  } else if (token <= -2 && token > HTTP_SERVER_H1_LISTENER) {
@@ -1504,15 +1573,19 @@ export class HttpServer {
1504
1573
  } else if (token >= HTTP_SERVER_H2_BASE) {
1505
1574
  this.h2Serve(toI32(toI64(token) - toI64(HTTP_SERVER_H2_BASE)), events, key)
1506
1575
  } else if (token >= 0) {
1507
- this.h1Serve(token, events)
1576
+ this.h1Serve(token, events, key)
1508
1577
  }
1509
1578
  }
1510
1579
  if (h3 !== null && key !== null) {
1511
1580
  this.h3Turn(h3, now, key)
1512
1581
  }
1513
1582
  const h1: Http1Server | null = this.h1
1514
- if (h1 !== null && h1.expire(now) > 0) {
1515
- this.h1Sweep(h1)
1583
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1584
+ // Both carriers expire before the one sweep that forgets what either closed.
1585
+ const closed: i32 = h1 !== null ? h1.expire(now) : 0
1586
+ const closedTls: i32 = h1Tls !== null ? h1Tls.expire(now) : 0
1587
+ if (closed > 0 || closedTls > 0) {
1588
+ this.h1Sweep()
1516
1589
  }
1517
1590
  this.feedStarved()
1518
1591
  if (now >= this.sweepAt) {
@@ -1593,15 +1666,17 @@ export class HttpServer {
1593
1666
  this.unlisten(this.h1Fd, HTTP_SERVER_H1_LISTENER)
1594
1667
  this.unlisten(this.h2Fd, HTTP_SERVER_H2_LISTENER)
1595
1668
  this.unlisten(this.h2cFd, HTTP_SERVER_H2C_LISTENER)
1596
- const h1: Http1Server | null = this.h1
1597
- if (h1 !== null) {
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)
1602
- } else {
1603
- h1.connection(s.slot).closeAfter = true
1604
- }
1669
+ this.unlisten(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER)
1670
+ for (const s of this.h1Slots) {
1671
+ const conn: Http1Connection | null = s.open ? this.h1Conn(s) : null
1672
+ if (conn !== null && this.h1Holds(s)) {
1673
+ if (s.tls && s.current === null && !conn.wantsWrite()) {
1674
+ // TLS may still hold sealed records: `settle` closes once they are sent.
1675
+ this.settle(s, conn)
1676
+ } else if (s.current === null && (this.h1Resting(s) || !conn.wantsWrite())) {
1677
+ this.h1Close(s)
1678
+ } else {
1679
+ conn.closeAfter = true
1605
1680
  }
1606
1681
  }
1607
1682
  }
@@ -1643,15 +1718,17 @@ export class HttpServer {
1643
1718
  */
1644
1719
  connections(): i32 {
1645
1720
  const h1: Http1Server | null = this.h1
1721
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1646
1722
  const h2: Http2TlsServer | null = this.h2
1647
1723
  const h2c: Http2CleartextServer | null = this.h2c
1648
1724
  const h3: Http3Server | null = this.h3
1649
1725
  const one: i32 = h1 !== null ? h1.busy() : 0
1726
+ const oneTls: i32 = h1Tls !== null ? h1Tls.busy() : 0
1650
1727
  const two: i32 = h2 !== null ? h2.busy() : 0
1651
1728
  const clear: i32 = h2c !== null ? h2c.busy() : 0
1652
1729
  const three: i32 = h3 !== null ? h3.busy() : 0
1653
1730
  // 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))
1731
+ const n: i64 = toI64(one) + toI64(oneTls) + toI64(two) + (toI64(clear) + toI64(three))
1655
1732
  return n > toI64(2147483647) ? 2147483647 : toI32(n)
1656
1733
  }
1657
1734
 
@@ -1666,16 +1743,14 @@ export class HttpServer {
1666
1743
  let slot: i32 = h1.accept()
1667
1744
  while (slot >= 0 || slot === H1_POOL_FULL || slot === HTTP_SERVER_ACCEPT_ABORTED) {
1668
1745
  if (slot >= 0) {
1669
- const s: HttpServerH1Slot | null = this.h1Slot(slot)
1746
+ const s: HttpServerH1Slot | null = this.h1Record(this.h1Base, slot)
1670
1747
  if (s === null) {
1671
1748
  // `listenHttp1` makes a record for every carrier slot, so a slot
1672
1749
  // without one is no connection the server can serve: it is
1673
1750
  // closed, never left open and unwatched.
1674
1751
  h1.close(slot)
1675
1752
  } else {
1676
- s.open = true
1677
- s.current = null
1678
- pollAdd(this.loop, h1.fd(slot), H1_WANT_READ, slot)
1753
+ this.h1Opened(s, h1.fd(slot))
1679
1754
  }
1680
1755
  }
1681
1756
  slot = h1.accept()
@@ -1687,24 +1762,160 @@ export class HttpServer {
1687
1762
  }
1688
1763
  }
1689
1764
 
1690
- /** A ready HTTP/1.1 socket: read or written, then driven. */
1691
- private h1Serve(slot: i32, events: i32): void {
1692
- const h1: Http1Server | null = this.h1
1693
- if (h1 === null || !h1.holds(slot)) {
1765
+ /** Takes every waiting HTTP/1.1 over TLS connection, each handshake with a fresh random and key share. */
1766
+ private acceptHttp1Tls(): void {
1767
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1768
+ if (h1Tls === null || this.draining) {
1694
1769
  return
1695
1770
  }
1771
+ while (true) {
1772
+ crypto.getRandomValues(this.rnd)
1773
+ crypto.getRandomValues(this.keyBuf)
1774
+ const slot: i32 = h1Tls.accept(this.rnd, this.keyBuf)
1775
+ if (slot >= 0) {
1776
+ const s: HttpServerH1Slot | null = this.h1Record(this.h1TlsBase, slot)
1777
+ if (s === null) {
1778
+ // As in `acceptHttp1`: a carrier slot without a record is closed.
1779
+ h1Tls.close(slot)
1780
+ } else {
1781
+ this.h1Opened(s, h1Tls.fd(slot))
1782
+ }
1783
+ } else if (slot === HTTP_SERVER_ACCEPT_NONE) {
1784
+ return
1785
+ } else if (slot !== TLS_TCP_POOL_FULL && slot !== HTTP_SERVER_ACCEPT_ABORTED) {
1786
+ // Out of descriptors or memory: rest the listener, as `acceptHttp1` does.
1787
+ this.rest(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER)
1788
+ return
1789
+ }
1790
+ }
1791
+ }
1792
+
1793
+ /** The record of carrier slot `slot` on the carrier whose records start at `base`, or null past the table. */
1794
+ private h1Record(base: i32, slot: i32): HttpServerH1Slot | null {
1795
+ const v: i64 = toI64(base) + toI64(slot)
1796
+ return this.h1Slot(v <= toI64(2147483647) ? toI32(v) : toI32(-1))
1797
+ }
1798
+
1799
+ /** A slot its carrier has just accepted on `fd`: open, with no request, and watched for reading. */
1800
+ private h1Opened(s: HttpServerH1Slot, fd: i32): void {
1801
+ s.open = true
1802
+ s.current = null
1803
+ s.armed = H1_WANT_READ
1804
+ pollAdd(this.loop, fd, H1_WANT_READ, s.slot)
1805
+ }
1806
+
1807
+ /** A ready HTTP/1.1 socket: read or written, a TLS handshake signed with `key`, then driven. */
1808
+ private h1Serve(slot: i32, events: i32, key: Secret<u8[]> | null): void {
1696
1809
  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)
1810
+ if (s === null || !this.h1Holds(s)) {
1700
1811
  return
1701
1812
  }
1702
- if ((events & HTTP_SERVER_READ_BITS) !== 0) {
1703
- h1.readable(slot)
1813
+ const read: boolean = (events & HTTP_SERVER_READ_BITS) !== 0
1814
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1815
+ if (s.tls && h1Tls !== null) {
1816
+ let wants: i32 = read ? h1Tls.readable(s.carrier) : h1Tls.writable(s.carrier)
1817
+ // A handshake signs once; the bound only stops a carrier that kept asking.
1818
+ for (
1819
+ let turn: i32 = 0;
1820
+ turn < 4 && key !== null && (wants & TLS_RECORD_SIGN) !== 0 && h1Tls.holds(s.carrier);
1821
+ turn++
1822
+ ) {
1823
+ wants = h1Tls.signP256(s.carrier, key)
1824
+ }
1704
1825
  } else {
1705
- h1.writable(slot)
1826
+ const h1: Http1Server | null = this.h1
1827
+ if (h1 !== null) {
1828
+ if (read) {
1829
+ h1.readable(s.carrier)
1830
+ } else {
1831
+ h1.writable(s.carrier)
1832
+ }
1833
+ }
1834
+ }
1835
+ this.drive(s)
1836
+ }
1837
+
1838
+ /** Whether `s`'s carrier holds a connection in it. */
1839
+ private h1Holds(s: HttpServerH1Slot): boolean {
1840
+ if (s.tls) {
1841
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1842
+ return h1Tls !== null && h1Tls.holds(s.carrier)
1843
+ }
1844
+ const h1: Http1Server | null = this.h1
1845
+ return h1 !== null && h1.holds(s.carrier)
1846
+ }
1847
+
1848
+ /** The connection in `s`, which a TLS carrier shares while the slot has read nothing; null with no carrier. */
1849
+ private h1Conn(s: HttpServerH1Slot): Http1Connection | null {
1850
+ if (s.tls) {
1851
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1852
+ return h1Tls !== null ? h1Tls.connection(s.carrier) : null
1853
+ }
1854
+ const h1: Http1Server | null = this.h1
1855
+ return h1 !== null ? h1.connection(s.carrier) : null
1856
+ }
1857
+
1858
+ /**
1859
+ * `s`'s carrier's next event; `H1_ERROR` with no carrier, or for a TLS slot
1860
+ * whose handshake chose another protocol: the carrier's own `refused` reads
1861
+ * false once its shutdown has moved TLS past open, and would then hand on a
1862
+ * request the client sent after its Finished.
1863
+ */
1864
+ private h1Next(s: HttpServerH1Slot): i32 {
1865
+ if (s.tls) {
1866
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1867
+ if (h1Tls === null) {
1868
+ return H1_ERROR
1869
+ }
1870
+ const chosen: string = h1Tls.alpn(s.carrier)
1871
+ return chosen === "" || chosen === H1_ALPN ? h1Tls.next(s.carrier) : H1_ERROR
1872
+ }
1873
+ const h1: Http1Server | null = this.h1
1874
+ return h1 !== null ? h1.next(s.carrier) : H1_ERROR
1875
+ }
1876
+
1877
+ /** What `s`'s carrier sends now and the interest it answers; `H1_DONE` with no carrier. */
1878
+ private h1Flush(s: HttpServerH1Slot): i32 {
1879
+ if (s.tls) {
1880
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1881
+ return h1Tls !== null ? h1Tls.flush(s.carrier) : H1_DONE
1882
+ }
1883
+ const h1: Http1Server | null = this.h1
1884
+ return h1 !== null ? h1.flush(s.carrier) : H1_DONE
1885
+ }
1886
+
1887
+ /**
1888
+ * Whether TLS slot `s`, whose flush answered `wants`, still has sealed
1889
+ * records to send or is shutting down after `conn` ended: closing it now
1890
+ * would drop what `TlsTcpServer.close` cannot send in one attempt, so a
1891
+ * draining server waits for `H1_DONE`. Always false for a cleartext slot,
1892
+ * whose unsent bytes are `conn`'s own.
1893
+ */
1894
+ private h1Sealing(s: HttpServerH1Slot, conn: Http1Connection, wants: i32): boolean {
1895
+ return s.tls && ((wants & H1_WANT_WRITE) !== 0 || conn.isDone())
1896
+ }
1897
+
1898
+ /** Whether `s` rests its buffers while idle; a TLS slot never does. */
1899
+ private h1Resting(s: HttpServerH1Slot): boolean {
1900
+ const h1: Http1Server | null = this.h1
1901
+ return !s.tls && h1 !== null && h1.resting(s.carrier)
1902
+ }
1903
+
1904
+ /** Arms `s`'s socket for `wants`, unless it is armed for it already. */
1905
+ private h1Arm(s: HttpServerH1Slot, wants: i32): void {
1906
+ const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
1907
+ if (s.tls) {
1908
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1909
+ if (h1Tls !== null && h1Tls.holds(s.carrier) && s.armed !== interest) {
1910
+ s.armed = interest
1911
+ pollModify(this.loop, h1Tls.fd(s.carrier), interest, s.slot)
1912
+ }
1913
+ return
1914
+ }
1915
+ const h1: Http1Server | null = this.h1
1916
+ if (h1 !== null && h1.rearm(s.carrier, wants)) {
1917
+ pollModify(this.loop, h1.fd(s.carrier), interest, s.slot)
1706
1918
  }
1707
- this.drive(h1, s)
1708
1919
  }
1709
1920
 
1710
1921
  /**
@@ -1712,49 +1923,56 @@ export class HttpServer {
1712
1923
  * settles it. A head with no free request left waits: the slot stops
1713
1924
  * reading and goes on `starved` until a request is handed back.
1714
1925
  */
1715
- private drive(h1: Http1Server, s: HttpServerH1Slot): void {
1926
+ private drive(s: HttpServerH1Slot): void {
1716
1927
  if (s.driving !== 0) {
1717
1928
  s.driving = 2
1718
1929
  return
1719
1930
  }
1720
1931
  s.driving = 1
1721
- this.drivePass(h1, s)
1932
+ this.drivePass(s)
1722
1933
  while (s.driving === 2 && s.open) {
1723
1934
  s.driving = 1
1724
- this.drivePass(h1, s)
1935
+ this.drivePass(s)
1725
1936
  }
1726
1937
  s.driving = 0
1727
1938
  }
1728
1939
 
1729
1940
  /** `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 {
1941
+ private driveOf(r: HttpServerRequest): void {
1731
1942
  const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
1732
1943
  if (s !== null) {
1733
- this.drive(h1, s)
1944
+ this.drive(s)
1734
1945
  }
1735
1946
  }
1736
1947
 
1737
1948
  /** One pass of `drive`: the slot's events until it needs more input, then `settle`. */
1738
- private drivePass(h1: Http1Server, s: HttpServerH1Slot): void {
1739
- const slot: i32 = s.slot
1740
- const conn: Http1Connection = h1.connection(slot)
1949
+ private drivePass(s: HttpServerH1Slot): void {
1950
+ const conn: Http1Connection | null = this.h1Conn(s)
1951
+ if (conn === null) {
1952
+ return
1953
+ }
1741
1954
  let event: i32 = H1_NEED_MORE
1742
1955
  if (!this.starve(s) && !this.unended(s)) {
1743
- event = h1.next(slot)
1956
+ event = this.h1Next(s)
1744
1957
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
1958
+ // A TLS slot's connection is built by the read that brought its first plaintext.
1959
+ const live: Http1Connection | null = this.h1Conn(s)
1960
+ if (live === null) {
1961
+ break
1962
+ }
1745
1963
  if (event === H1_REQUEST) {
1746
- this.begin(s, conn)
1964
+ this.begin(s, live)
1747
1965
  } else if (event === H1_BODY) {
1748
- this.gather(s, conn)
1966
+ this.gather(s, live)
1749
1967
  } else if (event === H1_END) {
1750
- this.whole(s, conn)
1968
+ this.whole(s, live)
1751
1969
  } else if (event === H1_WRITE) {
1752
- this.resume(s, conn)
1970
+ this.resume(s, live)
1753
1971
  }
1754
1972
  if (this.starve(s) || this.unended(s)) {
1755
1973
  break
1756
1974
  }
1757
- event = h1.next(slot)
1975
+ event = this.h1Next(s)
1758
1976
  }
1759
1977
  }
1760
1978
  if (event === H1_ERROR) {
@@ -1762,7 +1980,8 @@ export class HttpServer {
1762
1980
  // request it was reading is nobody's.
1763
1981
  this.releaseReading(s)
1764
1982
  }
1765
- this.settle(h1, s, conn)
1983
+ const after: Http1Connection | null = this.h1Conn(s)
1984
+ this.settle(s, after !== null ? after : conn)
1766
1985
  }
1767
1986
 
1768
1987
  /**
@@ -1794,10 +2013,6 @@ export class HttpServer {
1794
2013
 
1795
2014
  /** Drives the starved slots again while requests are free. */
1796
2015
  private feedStarved(): void {
1797
- const h1: Http1Server | null = this.h1
1798
- if (h1 === null) {
1799
- return
1800
- }
1801
2016
  const starved: HttpServerH1Slot[] = this.starved
1802
2017
  while (toI32(this.free.length) > 0) {
1803
2018
  const head: i32 = this.starvedHead
@@ -1807,8 +2022,8 @@ export class HttpServer {
1807
2022
  const s: HttpServerH1Slot = starved[head]
1808
2023
  this.starvedHead = head + 1
1809
2024
  s.starved = false
1810
- if (s.open && h1.holds(s.slot)) {
1811
- this.drive(h1, s)
2025
+ if (s.open && this.h1Holds(s)) {
2026
+ this.drive(s)
1812
2027
  }
1813
2028
  }
1814
2029
  // Drop the spent slots once they are half the list, so each is moved O(1) times.
@@ -1828,32 +2043,29 @@ export class HttpServer {
1828
2043
  }
1829
2044
 
1830
2045
  /** Sends what `slot` holds and re-arms it, or closes it once it is over; enqueues a parked request once there is room. */
1831
- private settle(h1: Http1Server, s: HttpServerH1Slot, conn: Http1Connection): void {
1832
- const slot: i32 = s.slot
1833
- let wants: i32 = h1.flush(slot)
2046
+ private settle(s: HttpServerH1Slot, conn: Http1Connection): void {
2047
+ let wants: i32 = this.h1Flush(s)
1834
2048
  if ((wants & H1_DONE) !== 0) {
1835
- this.h1Close(h1, slot)
2049
+ this.h1Close(s)
1836
2050
  return
1837
2051
  }
1838
- if (this.draining && s.current === null && !conn.wantsWrite()) {
1839
- this.h1Close(h1, slot)
2052
+ if (this.draining && s.current === null && !conn.wantsWrite() && !this.h1Sealing(s, conn, wants)) {
2053
+ this.h1Close(s)
1840
2054
  return
1841
2055
  }
1842
2056
  if (s.starved) {
1843
2057
  wants = wants & ~H1_WANT_READ
1844
2058
  }
1845
- if (h1.rearm(slot, wants)) {
1846
- pollModify(this.loop, h1.fd(slot), wants & (H1_WANT_READ | H1_WANT_WRITE), slot)
1847
- }
2059
+ this.h1Arm(s, wants)
1848
2060
  const r: HttpServerRequest | null = s.current
1849
- if (r !== null && r.state === HTTP_SERVER_PARKED && this.roomy(conn)) {
2061
+ if (r !== null && r.state === HTTP_SERVER_PARKED && this.roomy(s, conn)) {
1850
2062
  this.h1Ready(r)
1851
2063
  }
1852
2064
  }
1853
2065
 
1854
- /** Whether `conn`'s output holds at most half its size unsent, so a response head fits behind it. */
1855
- private roomy(conn: Http1Connection): boolean {
1856
- return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(this.h1OutputSize) / toI64(2)
2066
+ /** Whether `conn`'s output holds at most half of slot `s`'s output size unsent, so a response head fits behind it. */
2067
+ private roomy(s: HttpServerH1Slot, conn: Http1Connection): boolean {
2068
+ return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(s.outputSize) / toI64(2)
1857
2069
  }
1858
2070
 
1859
2071
  /** A head: a free request, the head copied into it, and its route. */
@@ -1899,7 +2111,7 @@ export class HttpServer {
1899
2111
  r.targetEnd = p.targetEnd
1900
2112
  r.kind = HTTP_SERVER_H1
1901
2113
  r.version = 1
1902
- r.secure = false
2114
+ r.secure = s.tls
1903
2115
  r.slot = s.slot
1904
2116
  r.gen = s.gen
1905
2117
  if (r.methodIs("CONNECT")) {
@@ -1925,7 +2137,7 @@ export class HttpServer {
1925
2137
  if (r === null) {
1926
2138
  return
1927
2139
  }
1928
- if (this.roomy(conn)) {
2140
+ if (this.roomy(s, conn)) {
1929
2141
  this.h1Ready(r)
1930
2142
  } else {
1931
2143
  r.state = HTTP_SERVER_PARKED
@@ -2020,16 +2232,23 @@ export class HttpServer {
2020
2232
  }
2021
2233
 
2022
2234
  /**
2023
- * Closes `slot` and bumps its generation, so any answer still owed for it is
2235
+ * Closes slot `s` and bumps its generation, so any answer still owed for it is
2024
2236
  * dropped. A request the server alone holds goes back now; one queued or
2025
2237
  * taken goes back when `ready` passes it or the program answers it.
2026
2238
  */
2027
- private h1Close(h1: Http1Server, slot: i32): void {
2028
- h1.close(slot)
2029
- const s: HttpServerH1Slot | null = this.h1Slot(slot)
2030
- if (s !== null) {
2031
- this.h1Forget(s)
2239
+ private h1Close(s: HttpServerH1Slot): void {
2240
+ if (s.tls) {
2241
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2242
+ if (h1Tls !== null) {
2243
+ h1Tls.close(s.carrier)
2244
+ }
2245
+ } else {
2246
+ const h1: Http1Server | null = this.h1
2247
+ if (h1 !== null) {
2248
+ h1.close(s.carrier)
2249
+ }
2032
2250
  }
2251
+ this.h1Forget(s)
2033
2252
  }
2034
2253
 
2035
2254
  /** The bookkeeping of a slot the carrier has closed. */
@@ -2044,9 +2263,9 @@ export class HttpServer {
2044
2263
  }
2045
2264
 
2046
2265
  /** Every slot the carrier's `expire` closed. */
2047
- private h1Sweep(h1: Http1Server): void {
2266
+ private h1Sweep(): void {
2048
2267
  for (const s of this.h1Slots) {
2049
- if (s.open && !h1.holds(s.slot)) {
2268
+ if (s.open && !this.h1Holds(s)) {
2050
2269
  this.h1Forget(s)
2051
2270
  }
2052
2271
  }
@@ -2625,8 +2844,8 @@ export class HttpServer {
2625
2844
  const conn: Http2Connection | null = this.h2Conn(r.slot)
2626
2845
  return conn === null || conn.inputEnded
2627
2846
  }
2628
- const h1: Http1Server | null = this.h1
2629
- return r.kind === HTTP_SERVER_H1 && h1 !== null && h1.connection(r.slot).ended()
2847
+ const conn: Http1Connection | null = this.h1ConnOf(r)
2848
+ return r.kind === HTTP_SERVER_H1 && conn !== null && conn.ended()
2630
2849
  }
2631
2850
 
2632
2851
  /** Panics, naming `who`, unless `r` is one of this server's own requests. */
@@ -2637,7 +2856,13 @@ export class HttpServer {
2637
2856
  }
2638
2857
  }
2639
2858
 
2640
- /** HTTP/1.1 slot `slot`'s record, or null for a number outside the table `listenHttp1` made one per carrier slot. */
2859
+ /** The connection of the HTTP/1.1 slot request `r` was read on, or null when that slot has no record or no carrier. */
2860
+ private h1ConnOf(r: HttpServerRequest): Http1Connection | null {
2861
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
2862
+ return s !== null ? this.h1Conn(s) : null
2863
+ }
2864
+
2865
+ /** HTTP/1.1 slot `slot`'s record, or null for a number outside the table the HTTP/1.1 listeners made one per carrier slot. */
2641
2866
  private h1Slot(slot: i32): HttpServerH1Slot | null {
2642
2867
  const slots: HttpServerH1Slot[] = this.h1Slots
2643
2868
  return slot >= 0 && slot < toI32(slots.length) ? slots[slot] : null
@@ -2652,9 +2877,8 @@ export class HttpServer {
2652
2877
  /** Whether `r`'s connection is the one it was read on, still open, and its stream not reset. */
2653
2878
  private live(r: HttpServerRequest): boolean {
2654
2879
  if (r.kind === HTTP_SERVER_H1) {
2655
- const h1: Http1Server | null = this.h1
2656
2880
  const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
2657
- return h1 !== null && s !== null && h1.holds(r.slot) && s.gen === r.gen
2881
+ return s !== null && this.h1Holds(s) && s.gen === r.gen
2658
2882
  }
2659
2883
  if (r.hung) {
2660
2884
  return false
@@ -2737,11 +2961,10 @@ export class HttpServer {
2737
2961
  return HANDLER_DROPPED
2738
2962
  }
2739
2963
  if (r.kind === HTTP_SERVER_H1) {
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
2964
+ const conn: Http1Connection | null = this.h1ConnOf(r)
2965
+ if (conn === null) {
2966
+ return HANDLER_DROPPED // `live` holds no HTTP/1.1 request without its slot's carrier
2743
2967
  }
2744
- const conn: Http1Connection = h1.connection(r.slot)
2745
2968
  const sent: i32 = conn.respond(
2746
2969
  status,
2747
2970
  this.outNames,
@@ -2936,9 +3159,9 @@ export class HttpServer {
2936
3159
  private abort(r: HttpServerRequest): void {
2937
3160
  r.hung = true
2938
3161
  if (r.kind === HTTP_SERVER_H1) {
2939
- const h1: Http1Server | null = this.h1
2940
- if (h1 !== null && this.live(r)) {
2941
- this.h1Close(h1, r.slot)
3162
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
3163
+ if (s !== null && this.live(r)) {
3164
+ this.h1Close(s)
2942
3165
  }
2943
3166
  return
2944
3167
  }
@@ -3032,9 +3255,9 @@ export class HttpServer {
3032
3255
  if (r.kind === HTTP_SERVER_H1) {
3033
3256
  // `write` keeps `len` within what the head declared, so the carrier
3034
3257
  // 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)
3258
+ const conn: Http1Connection | null = this.h1ConnOf(r)
3259
+ if (conn !== null) {
3260
+ taken = conn.write(buf, off, len)
3038
3261
  }
3039
3262
  if (taken === H1_AGAIN) {
3040
3263
  return 0
@@ -3061,12 +3284,9 @@ export class HttpServer {
3061
3284
 
3062
3285
  /** 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. */
3063
3286
  private nudge(r: HttpServerRequest): void {
3064
- const h1: Http1Server | null = this.h1
3065
3287
  const h3: Http3Server | null = this.h3
3066
3288
  if (r.kind === HTTP_SERVER_H1) {
3067
- if (h1 !== null) {
3068
- this.driveOf(h1, r)
3069
- }
3289
+ this.driveOf(r)
3070
3290
  } else if (r.kind === HTTP_SERVER_H2) {
3071
3291
  if (this.h2Holds(r.slot)) {
3072
3292
  this.h2Settle(r.slot)
@@ -3088,17 +3308,17 @@ export class HttpServer {
3088
3308
  if (r.state !== HTTP_SERVER_STREAMING) {
3089
3309
  panic(`HttpServer.end: the response is not started (state ${r.state})`)
3090
3310
  }
3091
- const h1: Http1Server | null = this.h1
3092
- if (!this.live(r) || (r.kind === HTTP_SERVER_H1 && h1 === null)) {
3311
+ const conn: Http1Connection | null = r.kind === HTTP_SERVER_H1 ? this.h1ConnOf(r) : null
3312
+ if (!this.live(r) || (r.kind === HTTP_SERVER_H1 && conn === null)) {
3093
3313
  this.release(r)
3094
3314
  return HANDLER_DROPPED
3095
3315
  }
3096
3316
  if (r.left > 0 && !r.quiet) {
3097
3317
  panic(`HttpServer.end: the body is ${r.left} byte(s) short of the length the response declared`)
3098
3318
  }
3099
- if (r.kind === HTTP_SERVER_H1 && h1 !== null) {
3100
- this.finishH1(r, h1.connection(r.slot))
3101
- this.driveOf(h1, r)
3319
+ if (r.kind === HTTP_SERVER_H1 && conn !== null) {
3320
+ this.finishH1(r, conn)
3321
+ this.driveOf(r)
3102
3322
  return HANDLER_SENT
3103
3323
  }
3104
3324
  r.state = HTTP_SERVER_FLUSHING
@@ -4450,13 +4670,10 @@ export class HttpServer {
4450
4670
  * the carriers dropped are forgotten.
4451
4671
  */
4452
4672
  private expireAll(now: i64): void {
4453
- const h1: Http1Server | null = this.h1
4454
- if (h1 !== null) {
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)
4459
- }
4673
+ for (const s of this.h1Slots) {
4674
+ const r: HttpServerRequest | null = s.current
4675
+ if (r !== null && this.late(r, now) && this.h1Holds(s)) {
4676
+ this.h1Close(s)
4460
4677
  }
4461
4678
  }
4462
4679
  const h2c: Http2CleartextServer | null = this.h2c