nish 0.47.0 → 0.49.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.
@@ -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,
@@ -132,6 +134,7 @@ import { TLS_RECORD_DONE, TLS_RECORD_SIGN } from "nish/net/tls/record-server"
132
134
  import { TLS_TCP_POOL_FULL } from "nish/net/tls-tcp"
133
135
  import { URL_DECODE_ALLOW_NUL, URL_DECODE_ERR_SPACE, URL_DECODE_FORM, percentDecode } from "nish/net/url"
134
136
  import { netLocalPort, pollAdd, pollCreate, pollModify, pollRemove, pollWait, udpRecvFrom } from "nish:net"
137
+ import { netRead } from "nish:net"
135
138
  import { Secret } from "nish:secret"
136
139
 
137
140
  /** The base `digits` writes a content-length in, and the byte of its digit 0. */
@@ -143,9 +146,8 @@ const HTTP_SERVER_TOO_LARGE: i32 = 413
143
146
  /** The status a request gets when no request record is free. */
144
147
  const HTTP_SERVER_UNAVAILABLE: i32 = 503
145
148
 
146
- // A request's life: free, reading its body, whole but parked behind a full
147
- // output, queued for `take`, held by the program, streaming its response,
148
- // and flushing the response's last bytes.
149
+ // A request's life: free, reading its body, whole but parked behind a full output, queued for `take`,
150
+ // held by the program, streaming its response, and flushing the response's last bytes.
149
151
  const HTTP_SERVER_FREE: i32 = 0
150
152
  const HTTP_SERVER_READING: i32 = 1
151
153
  const HTTP_SERVER_PARKED: i32 = 2
@@ -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
@@ -900,22 +900,45 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
900
900
  }
901
901
  }
902
902
 
903
+ // The HTTP/2 connection preface (RFC 9113 §3.4), which `listenHttp1`'s `h2c` reads a connection's first bytes against.
904
+ const HTTP_SERVER_H2C_PREFACE: string = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
905
+ const HTTP_SERVER_H2C_PREFACE_SIZE: i32 = 24
906
+
907
+ /**
908
+ * Feeds `conn` the `[off, end)` of `buf` that an h2c sniff read ahead, behind
909
+ * the window's range test, spelled so the prover reads it: the sniff stops at
910
+ * its head's 24 bytes, so the test always holds. -1 should it ever fail.
911
+ */
912
+ const httpServerFeedHead = (conn: Http1Connection, buf: u8[], off: i32, end: i32): i32 => {
913
+ if (off >= 0 && off <= end && end <= toI32(buf.length)) {
914
+ return conn.feed(buf, off, end - off)
915
+ }
916
+ return toI32(-1)
917
+ }
918
+
903
919
  /**
904
920
  * 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.
921
+ * slot number, its carrier, its generation, its request, and whether it is
922
+ * open, waits for a free request or is being driven. `listenHttp1` and
923
+ * `listenHttp1Tls` make one per carrier slot.
907
924
  */
908
925
  class HttpServerH1Slot {
909
- /** The carrier's slot number. */
926
+ /** The request the slot is reading or answering, null for none. */
927
+ current: HttpServerRequest | null = null
928
+ /** The slot's first bytes while `sniff`: 24 on a listener that takes h2c peers, none on any other. */
929
+ head: u8[] = []
930
+ /** When the slot was accepted, in `monotonicNanos`: while `sniff`, its `prefaceTimeout` starts here. */
931
+ since: i64 = 0
932
+ /** The slot's index in the server's `h1Slots`, which is also its poll token. */
910
933
  slot: i32
934
+ /** The slot's number in its own carrier. */
935
+ carrier: i32
936
+ /** Its carrier's `outputSize`. */
937
+ outputSize: i32
938
+ /** The interest a TLS slot is armed for, which `h1Arm` compares against; the cleartext carrier keeps its own. */
939
+ armed: i32 = 0
911
940
  /** Bumped each time the slot closes, so an answer still owed to the connection before is dropped; it wraps. */
912
941
  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
942
  /**
920
943
  * 0 while no `drive` of it runs, 1 while one does, 2 once a call inside it
921
944
  * (an answer screened while it reads) asked for another pass. The nested
@@ -923,17 +946,35 @@ class HttpServerH1Slot {
923
946
  * requests one write pipelines.
924
947
  */
925
948
  driving: i32 = 0
949
+ /** How many of `head` are read. */
950
+ got: i32 = 0
951
+ /** Whether the slot is on the TLS carrier (`listenHttp1Tls`) rather than the cleartext one. */
952
+ tls: boolean
953
+ /** Whether the carrier's connection in the slot is open. */
954
+ open: boolean = false
955
+ /** Whether it stopped reading for want of a free request and waits on `starved`. */
956
+ starved: boolean = false
957
+ /**
958
+ * Whether the slot is on a listener that takes h2c peers and has not yet
959
+ * read the byte that decides (`listenHttp1`'s `h2c`): `head` holds what it
960
+ * has read of the preface so far.
961
+ */
962
+ sniff: boolean = false
926
963
 
927
- constructor(slot: i32) {
964
+ constructor(slot: i32, tls: boolean, carrier: i32, outputSize: i32) {
928
965
  this.slot = slot
966
+ this.tls = tls
967
+ this.carrier = carrier
968
+ this.outputSize = outputSize
929
969
  }
930
970
  }
931
971
 
932
972
  /**
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`.
973
+ * One handler for HTTP/1.1 (cleartext and over TLS), HTTP/2 (over TLS and
974
+ * cleartext) and HTTP/3: configure up to one listener of each kind, then
975
+ * loop on `waitSigned` (or `wait`), `ready` and `take`, answering each
976
+ * request with `send`, `json` and `sendJson`, `problem`, `refuse`, or
977
+ * `start`, `write` and `end`.
937
978
  */
938
979
  export class HttpServer {
939
980
  private router: Router
@@ -985,8 +1026,12 @@ export class HttpServer {
985
1026
  /** When `wait` next looks for timeouts. */
986
1027
  private sweepAt: i64 = 0
987
1028
 
988
- // HTTP/1.1: the carrier, its listener, and a record per slot.
1029
+ // HTTP/1.1: the two carriers, and a record per slot of either. Cleartext
1030
+ // slot `s` is record `h1Base + s`, TLS slot `s` is record `h1TlsBase + s`.
989
1031
  private h1: Http1Server | null = null
1032
+ private h1Tls: Http1TlsServer | null = null
1033
+ private h1Base: i32 = 0
1034
+ private h1TlsBase: i32 = 0
990
1035
  /** Per listener (`-100 - token`): when its rest for want of descriptors ends, 0 while it is not resting. */
991
1036
  private restUntil: i64[]
992
1037
  private h1Slots: HttpServerH1Slot[]
@@ -1042,7 +1087,8 @@ export class HttpServer {
1042
1087
  /** The readiness loop's descriptor, from `pollCreate`. */
1043
1088
  private loop: i32 = 0
1044
1089
  private h1Fd: i32 = -1
1045
- private h1OutputSize: i32 = 0
1090
+ /** The HTTP/1.1 over TLS listener, -1 until `listenHttp1Tls`. */
1091
+ private h1TlsFd: i32 = -1
1046
1092
  /** Where `feedStarved` takes the next slot of `starved`; the slots before it are spent. */
1047
1093
  private starvedHead: i32 = 0
1048
1094
  /** Where `fromFields` writes next in a request's head. */
@@ -1063,7 +1109,8 @@ export class HttpServer {
1063
1109
 
1064
1110
  /**
1065
1111
  * A server routing through `router`, held to `limits`. It has no listener
1066
- * yet: add them with `listenHttp1`, `listenHttp2Tls` and `listenHttp3`.
1112
+ * yet: add them with `listenHttp1`, `listenHttp1Tls`, `listenH2c`,
1113
+ * `listenHttp2Tls` and `listenHttp3`.
1067
1114
  * Panics on a limit outside its range.
1068
1115
  * @param limits the caps and timeouts, read once here
1069
1116
  * @param router the routes, which the server keeps and consults at each request head
@@ -1142,7 +1189,7 @@ export class HttpServer {
1142
1189
  this.outValues = []
1143
1190
  this.h1Slots = []
1144
1191
  this.starved = []
1145
- this.restUntil = new Array<i64>(4)
1192
+ this.restUntil = new Array<i64>(5)
1146
1193
  this.h2Clear = []
1147
1194
  this.h2Gen = []
1148
1195
  this.h2Open = []
@@ -1166,42 +1213,112 @@ export class HttpServer {
1166
1213
 
1167
1214
  /**
1168
1215
  * 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`.
1216
+ * to `slots` connections. Panics on a second cleartext HTTP/1.1 listener,
1217
+ * when `config` would let in a head past `limits.maxHead` (its `maxTarget`,
1218
+ * its `maxHeaderBytes` and 44 bytes of request line) or more fields than
1219
+ * `limits.maxFields`, or when `slots`, with the TLS listener's if it came
1220
+ * first, pass 1048576. `config.maxBody` is set to `limits.maxBody`.
1221
+ *
1222
+ * With `h2c`, the listener also takes cleartext HTTP/2 peers by prior
1223
+ * knowledge: each connection's first bytes are read, up to 24, against the
1224
+ * HTTP/2 preface. One whose 24 bytes are the preface is served as h2c
1225
+ * with `h2c`'s caps, as `listenH2c` serves it (up to `slots` such
1226
+ * connections, beside the HTTP/1.1 ones); one whose first byte to differ
1227
+ * comes sooner, or that ends first, is served as HTTP/1.1, with what was
1228
+ * read as the start of its input. One that sends part of the preface and
1229
+ * then nothing is closed `limits.prefaceTimeout` after it was accepted.
1230
+ * There is no `Upgrade: h2c`. Without `h2c`, nothing is read ahead and
1231
+ * every connection is HTTP/1.1. Panics also, with `h2c`, as `listenH2c`
1232
+ * does on its `config` and `slots`, and when an h2c listener is already
1233
+ * configured.
1173
1234
  * @param fd the listening socket's descriptor, still the program's to close
1174
1235
  * @param config the carrier's caps and timeouts, kept by the carrier
1175
1236
  * @param slots the most connections held at once
1237
+ * @param h2c the h2c carrier's caps, to take h2c peers on this listener; null, the default, for none
1176
1238
  */
1177
- listenHttp1(fd: i32, config: Http1Config, slots: i32): void {
1239
+ listenHttp1(fd: i32, config: Http1Config, slots: i32, h2c: Http2Config | null = null): void {
1178
1240
  if (this.h1Fd >= 0) {
1179
1241
  panic("HttpServer.listenHttp1: an HTTP/1.1 listener is already configured")
1180
1242
  }
1243
+ this.checkHttp1("listenHttp1", config, slots)
1244
+ if (h2c !== null) {
1245
+ this.clearHttp2("listenHttp1", fd, h2c, slots)
1246
+ }
1247
+ this.h1 = new Http1Server(config, fd, slots)
1248
+ this.h1Fd = fd
1249
+ this.h1Base = this.h1Grow(slots, false, config.outputSize)
1250
+ if (h2c !== null) {
1251
+ const table: HttpServerH1Slot[] = this.h1Slots
1252
+ for (let k: i32 = this.h1Base; k >= 0 && k < toI32(table.length); k++) {
1253
+ table[k].head = new Array<u8>(HTTP_SERVER_H2C_PREFACE_SIZE)
1254
+ }
1255
+ }
1256
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
1257
+ }
1258
+
1259
+ /**
1260
+ * Serves HTTP/1.1 over TLS (ALPN `http/1.1`, or none) on `fd`, a listening
1261
+ * TCP socket the program made, with up to `slots` connections, on a port of
1262
+ * its own: a client that offers only another protocol is shut down, never
1263
+ * handed to the HTTP/2 listener. Its requests say `secure` true and its
1264
+ * responses carry Alt-Svc as HTTP/2 over TLS ones do. Panics as
1265
+ * `listenHttp1` does, on a second listener of this kind, or when `slots`,
1266
+ * with the cleartext listener's if it came first, pass 1048576.
1267
+ * `config.maxBody` is set to `limits.maxBody`. Serve it with `waitSigned`.
1268
+ * @param fd the listening socket's descriptor, still the program's to close
1269
+ * @param tls the certificate chain and TLS settings
1270
+ * @param config the carrier's caps and timeouts, kept by the carrier
1271
+ * @param slots the most connections held at once
1272
+ */
1273
+ listenHttp1Tls(fd: i32, tls: TlsServerConfig, config: Http1Config, slots: i32): void {
1274
+ if (this.h1TlsFd >= 0) {
1275
+ panic("HttpServer.listenHttp1Tls: an HTTP/1.1 over TLS listener is already configured")
1276
+ }
1277
+ this.checkHttp1("listenHttp1Tls", config, slots)
1278
+ this.h1Tls = new Http1TlsServer(tls, config, fd, slots)
1279
+ this.h1TlsFd = fd
1280
+ this.h1TlsBase = this.h1Grow(slots, true, config.outputSize)
1281
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_TLS_LISTENER)
1282
+ }
1283
+
1284
+ /**
1285
+ * Panics, naming `name`, when `config` would let in a head past
1286
+ * `limits.maxHead` or more fields than `limits.maxFields`, on fewer than
1287
+ * one slot, or on more than the HTTP/1.1 tokens hold; then sets
1288
+ * `config.maxBody` to `limits.maxBody`.
1289
+ */
1290
+ private checkHttp1(name: string, config: Http1Config, slots: i32): void {
1181
1291
  const head: i64 = toI64(config.maxTarget) + toI64(config.maxHeaderBytes) + HTTP_SERVER_H1_LINE_OVERHEAD
1182
1292
  if (head > toI64(this.maxHead)) {
1183
1293
  panic(
1184
- `HttpServer.listenHttp1: maxTarget ${config.maxTarget} + maxHeaderBytes ${config.maxHeaderBytes} + 44 is more than limits.maxHead ${this.maxHead}`
1294
+ `HttpServer.${name}: maxTarget ${config.maxTarget} + maxHeaderBytes ${config.maxHeaderBytes} + 44 is more than limits.maxHead ${this.maxHead}`
1185
1295
  )
1186
1296
  }
1187
1297
  if (config.maxHeaders > this.maxFields) {
1188
1298
  panic(
1189
- `HttpServer.listenHttp1: maxHeaders ${config.maxHeaders} is more than limits.maxFields ${this.maxFields}`
1299
+ `HttpServer.${name}: maxHeaders ${config.maxHeaders} is more than limits.maxFields ${this.maxFields}`
1190
1300
  )
1191
1301
  }
1192
1302
  if (slots < 1) {
1193
- panic(`HttpServer.listenHttp1: slots ${slots} is below 1`)
1303
+ panic(`HttpServer.${name}: slots ${slots} is below 1`)
1304
+ }
1305
+ // A slot's record index is its poll token, which must stay below the HTTP/2 tokens.
1306
+ if (toI64(toI32(this.h1Slots.length)) + toI64(slots) > toI64(HTTP_SERVER_H2_BASE)) {
1307
+ panic(
1308
+ `HttpServer.${name}: slots ${slots} and the ${this.h1Slots.length} HTTP/1.1 slots listened before are more than 1048576`
1309
+ )
1194
1310
  }
1195
1311
  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[] = []
1312
+ }
1313
+
1314
+ /** Appends a record for each of a new carrier's `slots` and answers the index of its first. */
1315
+ private h1Grow(slots: i32, tls: boolean, outputSize: i32): i32 {
1316
+ const table: HttpServerH1Slot[] = this.h1Slots
1317
+ const base: i32 = toI32(table.length)
1200
1318
  for (let s: i32 = 0; s < slots; s++) {
1201
- table.push(new HttpServerH1Slot(s))
1319
+ table.push(new HttpServerH1Slot(toI32(table.length), tls, s, outputSize))
1202
1320
  }
1203
- this.h1Slots = table
1204
- pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
1321
+ return base
1205
1322
  }
1206
1323
 
1207
1324
  /**
@@ -1221,10 +1338,20 @@ export class HttpServer {
1221
1338
  * @param slots the most connections held at once
1222
1339
  */
1223
1340
  listenH2c(fd: i32, config: Http2Config, slots: i32): void {
1224
- if (this.h2cFd >= 0) {
1225
- panic("HttpServer.listenH2c: an h2c listener is already configured")
1341
+ this.clearHttp2("listenH2c", fd, config, slots)
1342
+ this.h2cFd = fd
1343
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H2C_LISTENER)
1344
+ }
1345
+
1346
+ /**
1347
+ * The h2c carrier, on `fd` and with `slots` slots, for `listenH2c` or for
1348
+ * `listenHttp1`'s `h2c`: there is one at most, so a second panics.
1349
+ */
1350
+ private clearHttp2(name: string, fd: i32, config: Http2Config, slots: i32): void {
1351
+ if (this.h2c !== null) {
1352
+ panic(`HttpServer.${name}: an h2c listener is already configured`)
1226
1353
  }
1227
- this.checkHttp2("listenH2c", config, slots)
1354
+ this.checkHttp2(name, config, slots)
1228
1355
  const base: i32 = this.h2Grow(slots, this.perOf(config))
1229
1356
  this.h2cBase = base
1230
1357
  const clear: boolean[] = this.h2Clear
@@ -1232,8 +1359,6 @@ export class HttpServer {
1232
1359
  clear[v] = true
1233
1360
  }
1234
1361
  this.h2c = new Http2CleartextServer(config, fd, slots, this.prefaceTimeout)
1235
- this.h2cFd = fd
1236
- pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H2C_LISTENER)
1237
1362
  }
1238
1363
 
1239
1364
  /** The checks every HTTP/2 listener's configuration passes. */
@@ -1424,14 +1549,14 @@ export class HttpServer {
1424
1549
 
1425
1550
  /**
1426
1551
  * 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.
1552
+ * `waitSigned`, with no key. It serves cleartext HTTP/1.1 and h2c
1553
+ * (`listenH2c`). Panics when an HTTP/1.1 or HTTP/2 over TLS or an HTTP/3
1554
+ * listener is configured, since a handshake there needs the key.
1430
1555
  * @param timeoutMs the longest to block, in milliseconds, or -1 to block until something is ready
1431
1556
  * @returns how many whole requests are queued for `take` after this turn
1432
1557
  */
1433
1558
  wait(timeoutMs: i32): i32 {
1434
- if (this.h2Fd >= 0 || this.h3 !== null) {
1559
+ if (this.h2Fd >= 0 || this.h1TlsFd >= 0 || this.h3 !== null) {
1435
1560
  panic("HttpServer.wait: a TLS or QUIC listener needs the certificate's key; call waitSigned")
1436
1561
  }
1437
1562
  return this.turnWith(timeoutMs, null)
@@ -1465,6 +1590,7 @@ export class HttpServer {
1465
1590
  timeout = this.restListener(this.h1Fd, HTTP_SERVER_H1_LISTENER, now, timeout)
1466
1591
  timeout = this.restListener(this.h2Fd, HTTP_SERVER_H2_LISTENER, now, timeout)
1467
1592
  timeout = this.restListener(this.h2cFd, HTTP_SERVER_H2C_LISTENER, now, timeout)
1593
+ timeout = this.restListener(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER, now, timeout)
1468
1594
  const h3: Http3Server | null = this.h3
1469
1595
  if (h3 !== null) {
1470
1596
  h3.flush(now / HTTP_SERVER_NANOS_PER_MS)
@@ -1494,6 +1620,8 @@ export class HttpServer {
1494
1620
  this.acceptHttp2()
1495
1621
  } else if (token === HTTP_SERVER_H2C_LISTENER) {
1496
1622
  this.acceptH2c()
1623
+ } else if (token === HTTP_SERVER_H1_TLS_LISTENER) {
1624
+ this.acceptHttp1Tls()
1497
1625
  } else if (token === HTTP_SERVER_H3_SOCKET) {
1498
1626
  this.h3Readable = true
1499
1627
  } else if (token <= -2 && token > HTTP_SERVER_H1_LISTENER) {
@@ -1504,15 +1632,19 @@ export class HttpServer {
1504
1632
  } else if (token >= HTTP_SERVER_H2_BASE) {
1505
1633
  this.h2Serve(toI32(toI64(token) - toI64(HTTP_SERVER_H2_BASE)), events, key)
1506
1634
  } else if (token >= 0) {
1507
- this.h1Serve(token, events)
1635
+ this.h1Serve(token, events, key)
1508
1636
  }
1509
1637
  }
1510
1638
  if (h3 !== null && key !== null) {
1511
1639
  this.h3Turn(h3, now, key)
1512
1640
  }
1513
1641
  const h1: Http1Server | null = this.h1
1514
- if (h1 !== null && h1.expire(now) > 0) {
1515
- this.h1Sweep(h1)
1642
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1643
+ // Both carriers expire before the one sweep that forgets what either closed.
1644
+ const closed: i32 = h1 !== null ? h1.expire(now) : 0
1645
+ const closedTls: i32 = h1Tls !== null ? h1Tls.expire(now) : 0
1646
+ if (closed > 0 || closedTls > 0) {
1647
+ this.h1Sweep()
1516
1648
  }
1517
1649
  this.feedStarved()
1518
1650
  if (now >= this.sweepAt) {
@@ -1593,15 +1725,17 @@ export class HttpServer {
1593
1725
  this.unlisten(this.h1Fd, HTTP_SERVER_H1_LISTENER)
1594
1726
  this.unlisten(this.h2Fd, HTTP_SERVER_H2_LISTENER)
1595
1727
  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
- }
1728
+ this.unlisten(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER)
1729
+ for (const s of this.h1Slots) {
1730
+ const conn: Http1Connection | null = s.open ? this.h1Conn(s) : null
1731
+ if (conn !== null && this.h1Holds(s)) {
1732
+ if (s.tls && s.current === null && !conn.wantsWrite()) {
1733
+ // TLS may still hold sealed records: `settle` closes once they are sent.
1734
+ this.settle(s, conn)
1735
+ } else if (s.current === null && (this.h1Resting(s) || !conn.wantsWrite())) {
1736
+ this.h1Close(s)
1737
+ } else {
1738
+ conn.closeAfter = true
1605
1739
  }
1606
1740
  }
1607
1741
  }
@@ -1643,15 +1777,17 @@ export class HttpServer {
1643
1777
  */
1644
1778
  connections(): i32 {
1645
1779
  const h1: Http1Server | null = this.h1
1780
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1646
1781
  const h2: Http2TlsServer | null = this.h2
1647
1782
  const h2c: Http2CleartextServer | null = this.h2c
1648
1783
  const h3: Http3Server | null = this.h3
1649
1784
  const one: i32 = h1 !== null ? h1.busy() : 0
1785
+ const oneTls: i32 = h1Tls !== null ? h1Tls.busy() : 0
1650
1786
  const two: i32 = h2 !== null ? h2.busy() : 0
1651
1787
  const clear: i32 = h2c !== null ? h2c.busy() : 0
1652
1788
  const three: i32 = h3 !== null ? h3.busy() : 0
1653
1789
  // 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))
1790
+ const n: i64 = toI64(one) + toI64(oneTls) + toI64(two) + (toI64(clear) + toI64(three))
1655
1791
  return n > toI64(2147483647) ? 2147483647 : toI32(n)
1656
1792
  }
1657
1793
 
@@ -1666,16 +1802,14 @@ export class HttpServer {
1666
1802
  let slot: i32 = h1.accept()
1667
1803
  while (slot >= 0 || slot === H1_POOL_FULL || slot === HTTP_SERVER_ACCEPT_ABORTED) {
1668
1804
  if (slot >= 0) {
1669
- const s: HttpServerH1Slot | null = this.h1Slot(slot)
1805
+ const s: HttpServerH1Slot | null = this.h1Record(this.h1Base, slot)
1670
1806
  if (s === null) {
1671
1807
  // `listenHttp1` makes a record for every carrier slot, so a slot
1672
1808
  // without one is no connection the server can serve: it is
1673
1809
  // closed, never left open and unwatched.
1674
1810
  h1.close(slot)
1675
1811
  } else {
1676
- s.open = true
1677
- s.current = null
1678
- pollAdd(this.loop, h1.fd(slot), H1_WANT_READ, slot)
1812
+ this.h1Opened(s, h1.fd(slot))
1679
1813
  }
1680
1814
  }
1681
1815
  slot = h1.accept()
@@ -1687,24 +1821,262 @@ export class HttpServer {
1687
1821
  }
1688
1822
  }
1689
1823
 
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)) {
1824
+ /** Takes every waiting HTTP/1.1 over TLS connection, each handshake with a fresh random and key share. */
1825
+ private acceptHttp1Tls(): void {
1826
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1827
+ if (h1Tls === null || this.draining) {
1694
1828
  return
1695
1829
  }
1830
+ while (true) {
1831
+ crypto.getRandomValues(this.rnd)
1832
+ crypto.getRandomValues(this.keyBuf)
1833
+ const slot: i32 = h1Tls.accept(this.rnd, this.keyBuf)
1834
+ if (slot >= 0) {
1835
+ const s: HttpServerH1Slot | null = this.h1Record(this.h1TlsBase, slot)
1836
+ if (s === null) {
1837
+ // As in `acceptHttp1`: a carrier slot without a record is closed.
1838
+ h1Tls.close(slot)
1839
+ } else {
1840
+ this.h1Opened(s, h1Tls.fd(slot))
1841
+ }
1842
+ } else if (slot === HTTP_SERVER_ACCEPT_NONE) {
1843
+ return
1844
+ } else if (slot !== TLS_TCP_POOL_FULL && slot !== HTTP_SERVER_ACCEPT_ABORTED) {
1845
+ // Out of descriptors or memory: rest the listener, as `acceptHttp1` does.
1846
+ this.rest(this.h1TlsFd, HTTP_SERVER_H1_TLS_LISTENER)
1847
+ return
1848
+ }
1849
+ }
1850
+ }
1851
+
1852
+ /** The record of carrier slot `slot` on the carrier whose records start at `base`, or null past the table. */
1853
+ private h1Record(base: i32, slot: i32): HttpServerH1Slot | null {
1854
+ const v: i64 = toI64(base) + toI64(slot)
1855
+ return this.h1Slot(v <= toI64(2147483647) ? toI32(v) : toI32(-1))
1856
+ }
1857
+
1858
+ /** A slot its carrier has just accepted on `fd`: open, with no request, and watched for reading. */
1859
+ private h1Opened(s: HttpServerH1Slot, fd: i32): void {
1860
+ s.open = true
1861
+ s.current = null
1862
+ s.armed = H1_WANT_READ
1863
+ s.sniff = toI32(s.head.length) === HTTP_SERVER_H2C_PREFACE_SIZE
1864
+ s.got = 0
1865
+ s.since = this.turnAt
1866
+ pollAdd(this.loop, fd, H1_WANT_READ, s.slot)
1867
+ }
1868
+
1869
+ /** A ready HTTP/1.1 socket: read or written, a TLS handshake signed with `key`, then driven. */
1870
+ private h1Serve(slot: i32, events: i32, key: Secret<u8[]> | null): void {
1696
1871
  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)
1872
+ if (s === null || !this.h1Holds(s)) {
1873
+ return
1874
+ }
1875
+ const read: boolean = (events & HTTP_SERVER_READ_BITS) !== 0
1876
+ const h1Tls: Http1TlsServer | null = this.h1Tls
1877
+ if (s.tls && h1Tls !== null) {
1878
+ let wants: i32 = read ? h1Tls.readable(s.carrier) : h1Tls.writable(s.carrier)
1879
+ // A handshake signs once; the bound only stops a carrier that kept asking.
1880
+ for (
1881
+ let turn: i32 = 0;
1882
+ turn < 4 && key !== null && (wants & TLS_RECORD_SIGN) !== 0 && h1Tls.holds(s.carrier);
1883
+ turn++
1884
+ ) {
1885
+ wants = h1Tls.signP256(s.carrier, key)
1886
+ }
1887
+ } else {
1888
+ const h1: Http1Server | null = this.h1
1889
+ if (h1 !== null && s.sniff && !this.h1Sniff(s, h1)) {
1890
+ return
1891
+ }
1892
+ if (h1 !== null) {
1893
+ if (read) {
1894
+ h1.readable(s.carrier)
1895
+ } else {
1896
+ h1.writable(s.carrier)
1897
+ }
1898
+ }
1899
+ }
1900
+ this.drive(s)
1901
+ }
1902
+
1903
+ /**
1904
+ * A ready socket on a listener that takes h2c peers, not yet decided:
1905
+ * reads into `s.head` up to the preface's 24 bytes. The first byte that
1906
+ * differs from the preface, the peer's end or a failed read gives the slot
1907
+ * to HTTP/1.1, with what was read as its first input; 24 that match move
1908
+ * the socket to the h2c carrier. Answers whether the slot is HTTP/1.1's
1909
+ * now, for `h1Serve` to read on; false while it waits for more, or once it
1910
+ * is h2c's or closed.
1911
+ */
1912
+ private h1Sniff(s: HttpServerH1Slot, h1: Http1Server): boolean {
1913
+ const fd: i32 = h1.fd(s.carrier)
1914
+ const head: u8[] = s.head
1915
+ const want: string = HTTP_SERVER_H2C_PREFACE
1916
+ let got: i32 = s.got
1917
+ while (got >= 0 && got < toI32(head.length)) {
1918
+ const n: i32 = netRead(fd, head, got, toI32(head.length) - got)
1919
+ if (n === HTTP_SERVER_ACCEPT_NONE) {
1920
+ return false
1921
+ }
1922
+ if (n <= 0) {
1923
+ // The peer's end or a failure: HTTP/1.1 reads it again and answers it as it does today.
1924
+ return this.h1Decided(s, h1)
1925
+ }
1926
+ // `netRead` answers at most the bytes it was asked for, so the unsigned sum is at most 24.
1927
+ const end: i32 = toI32(toU32(got) + toU32(n))
1928
+ for (let k: i32 = got; k < end && k < toI32(head.length) && k < toI32(want.length); k++) {
1929
+ if (toI32(head[k]) !== toI32(want.charCodeAt(k))) {
1930
+ s.got = end
1931
+ return this.h1Decided(s, h1)
1932
+ }
1933
+ }
1934
+ got = end
1935
+ s.got = end
1936
+ }
1937
+ this.h1ToH2c(s, h1)
1938
+ return false
1939
+ }
1940
+
1941
+ /**
1942
+ * Slot `s` is HTTP/1.1's: what it read ahead is its connection's first
1943
+ * input, stamped as `readable` stamps what it reads. False, with the slot
1944
+ * closed, should the connection not take it all.
1945
+ */
1946
+ private h1Decided(s: HttpServerH1Slot, h1: Http1Server): boolean {
1947
+ s.sniff = false
1948
+ const head: u8[] = s.head
1949
+ const got: i32 = s.got
1950
+ if (got <= 0) {
1951
+ return true
1952
+ }
1953
+ const conn: Http1Connection = h1.connection(s.carrier)
1954
+ // A fresh connection has room for a head, past the 24 bytes this hands it.
1955
+ const taken: i32 = httpServerFeedHead(conn, head, toI32(0), got)
1956
+ conn.lastActive = this.turnAt
1957
+ conn.headArrived(this.turnAt)
1958
+ if (taken !== got) {
1959
+ this.h1Close(s)
1960
+ return false
1961
+ }
1962
+ return true
1963
+ }
1964
+
1965
+ /**
1966
+ * Slot `s` read the whole preface: its socket leaves HTTP/1.1's loop and
1967
+ * carrier, unclosed, for the h2c carrier, which takes the 24 bytes as
1968
+ * `listenH2c`'s accept would have read them, and is served at once.
1969
+ */
1970
+ private h1ToH2c(s: HttpServerH1Slot, h1: Http1Server): void {
1971
+ s.sniff = false
1972
+ const h2c: Http2CleartextServer | null = this.h2c
1973
+ // `listenHttp1` makes the h2c carrier with the sniffing records, and an
1974
+ // unread slot is released, so neither close is reached; each keeps the socket owned.
1975
+ if (h2c === null) {
1976
+ this.h1Close(s)
1977
+ return
1978
+ }
1979
+ const freed: i32 = h1.release(s.carrier)
1980
+ if (freed < 0) {
1981
+ this.h1Close(s)
1700
1982
  return
1701
1983
  }
1702
- if ((events & HTTP_SERVER_READ_BITS) !== 0) {
1703
- h1.readable(slot)
1984
+ pollRemove(this.loop, freed)
1985
+ this.h1Forget(s)
1986
+ const slot: i32 = h2c.adopt(freed, s.head)
1987
+ if (slot < 0) {
1988
+ return
1989
+ }
1990
+ const v: i64 = toI64(this.h2cBase) + toI64(slot)
1991
+ const at: i32 = v <= 2147483647 ? toI32(v) : toI32(-1)
1992
+ if (this.h2Opened(at, freed)) {
1993
+ this.h2Serve(at, H1_WANT_READ, null)
1704
1994
  } else {
1705
- h1.writable(slot)
1995
+ h2c.close(slot)
1996
+ }
1997
+ }
1998
+
1999
+ /** Whether `s`'s carrier holds a connection in it. */
2000
+ private h1Holds(s: HttpServerH1Slot): boolean {
2001
+ if (s.tls) {
2002
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2003
+ return h1Tls !== null && h1Tls.holds(s.carrier)
2004
+ }
2005
+ const h1: Http1Server | null = this.h1
2006
+ return h1 !== null && h1.holds(s.carrier)
2007
+ }
2008
+
2009
+ /** The connection in `s`, which a TLS carrier shares while the slot has read nothing; null with no carrier. */
2010
+ private h1Conn(s: HttpServerH1Slot): Http1Connection | null {
2011
+ if (s.tls) {
2012
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2013
+ return h1Tls !== null ? h1Tls.connection(s.carrier) : null
2014
+ }
2015
+ const h1: Http1Server | null = this.h1
2016
+ return h1 !== null ? h1.connection(s.carrier) : null
2017
+ }
2018
+
2019
+ /**
2020
+ * `s`'s carrier's next event; `H1_ERROR` with no carrier, or for a TLS slot
2021
+ * whose handshake chose another protocol: the carrier's own `refused` reads
2022
+ * false once its shutdown has moved TLS past open, and would then hand on a
2023
+ * request the client sent after its Finished.
2024
+ */
2025
+ private h1Next(s: HttpServerH1Slot): i32 {
2026
+ if (s.tls) {
2027
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2028
+ if (h1Tls === null) {
2029
+ return H1_ERROR
2030
+ }
2031
+ const chosen: string = h1Tls.alpn(s.carrier)
2032
+ return chosen === "" || chosen === H1_ALPN ? h1Tls.next(s.carrier) : H1_ERROR
2033
+ }
2034
+ const h1: Http1Server | null = this.h1
2035
+ return h1 !== null ? h1.next(s.carrier) : H1_ERROR
2036
+ }
2037
+
2038
+ /** What `s`'s carrier sends now and the interest it answers; `H1_DONE` with no carrier. */
2039
+ private h1Flush(s: HttpServerH1Slot): i32 {
2040
+ if (s.tls) {
2041
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2042
+ return h1Tls !== null ? h1Tls.flush(s.carrier) : H1_DONE
2043
+ }
2044
+ const h1: Http1Server | null = this.h1
2045
+ return h1 !== null ? h1.flush(s.carrier) : H1_DONE
2046
+ }
2047
+
2048
+ /**
2049
+ * Whether TLS slot `s`, whose flush answered `wants`, still has sealed
2050
+ * records to send or is shutting down after `conn` ended: closing it now
2051
+ * would drop what `TlsTcpServer.close` cannot send in one attempt, so a
2052
+ * draining server waits for `H1_DONE`. Always false for a cleartext slot,
2053
+ * whose unsent bytes are `conn`'s own.
2054
+ */
2055
+ private h1Sealing(s: HttpServerH1Slot, conn: Http1Connection, wants: i32): boolean {
2056
+ return s.tls && ((wants & H1_WANT_WRITE) !== 0 || conn.isDone())
2057
+ }
2058
+
2059
+ /** Whether `s` rests its buffers while idle; a TLS slot never does. */
2060
+ private h1Resting(s: HttpServerH1Slot): boolean {
2061
+ const h1: Http1Server | null = this.h1
2062
+ return !s.tls && h1 !== null && h1.resting(s.carrier)
2063
+ }
2064
+
2065
+ /** Arms `s`'s socket for `wants`, unless it is armed for it already. */
2066
+ private h1Arm(s: HttpServerH1Slot, wants: i32): void {
2067
+ const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
2068
+ if (s.tls) {
2069
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2070
+ if (h1Tls !== null && h1Tls.holds(s.carrier) && s.armed !== interest) {
2071
+ s.armed = interest
2072
+ pollModify(this.loop, h1Tls.fd(s.carrier), interest, s.slot)
2073
+ }
2074
+ return
2075
+ }
2076
+ const h1: Http1Server | null = this.h1
2077
+ if (h1 !== null && h1.rearm(s.carrier, wants)) {
2078
+ pollModify(this.loop, h1.fd(s.carrier), interest, s.slot)
1706
2079
  }
1707
- this.drive(h1, s)
1708
2080
  }
1709
2081
 
1710
2082
  /**
@@ -1712,49 +2084,56 @@ export class HttpServer {
1712
2084
  * settles it. A head with no free request left waits: the slot stops
1713
2085
  * reading and goes on `starved` until a request is handed back.
1714
2086
  */
1715
- private drive(h1: Http1Server, s: HttpServerH1Slot): void {
2087
+ private drive(s: HttpServerH1Slot): void {
1716
2088
  if (s.driving !== 0) {
1717
2089
  s.driving = 2
1718
2090
  return
1719
2091
  }
1720
2092
  s.driving = 1
1721
- this.drivePass(h1, s)
2093
+ this.drivePass(s)
1722
2094
  while (s.driving === 2 && s.open) {
1723
2095
  s.driving = 1
1724
- this.drivePass(h1, s)
2096
+ this.drivePass(s)
1725
2097
  }
1726
2098
  s.driving = 0
1727
2099
  }
1728
2100
 
1729
2101
  /** `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 {
2102
+ private driveOf(r: HttpServerRequest): void {
1731
2103
  const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
1732
2104
  if (s !== null) {
1733
- this.drive(h1, s)
2105
+ this.drive(s)
1734
2106
  }
1735
2107
  }
1736
2108
 
1737
2109
  /** 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)
2110
+ private drivePass(s: HttpServerH1Slot): void {
2111
+ const conn: Http1Connection | null = this.h1Conn(s)
2112
+ if (conn === null) {
2113
+ return
2114
+ }
1741
2115
  let event: i32 = H1_NEED_MORE
1742
2116
  if (!this.starve(s) && !this.unended(s)) {
1743
- event = h1.next(slot)
2117
+ event = this.h1Next(s)
1744
2118
  while (event !== H1_NEED_MORE && event !== H1_ERROR) {
2119
+ // A TLS slot's connection is built by the read that brought its first plaintext.
2120
+ const live: Http1Connection | null = this.h1Conn(s)
2121
+ if (live === null) {
2122
+ break
2123
+ }
1745
2124
  if (event === H1_REQUEST) {
1746
- this.begin(s, conn)
2125
+ this.begin(s, live)
1747
2126
  } else if (event === H1_BODY) {
1748
- this.gather(s, conn)
2127
+ this.gather(s, live)
1749
2128
  } else if (event === H1_END) {
1750
- this.whole(s, conn)
2129
+ this.whole(s, live)
1751
2130
  } else if (event === H1_WRITE) {
1752
- this.resume(s, conn)
2131
+ this.resume(s, live)
1753
2132
  }
1754
2133
  if (this.starve(s) || this.unended(s)) {
1755
2134
  break
1756
2135
  }
1757
- event = h1.next(slot)
2136
+ event = this.h1Next(s)
1758
2137
  }
1759
2138
  }
1760
2139
  if (event === H1_ERROR) {
@@ -1762,7 +2141,8 @@ export class HttpServer {
1762
2141
  // request it was reading is nobody's.
1763
2142
  this.releaseReading(s)
1764
2143
  }
1765
- this.settle(h1, s, conn)
2144
+ const after: Http1Connection | null = this.h1Conn(s)
2145
+ this.settle(s, after !== null ? after : conn)
1766
2146
  }
1767
2147
 
1768
2148
  /**
@@ -1794,10 +2174,6 @@ export class HttpServer {
1794
2174
 
1795
2175
  /** Drives the starved slots again while requests are free. */
1796
2176
  private feedStarved(): void {
1797
- const h1: Http1Server | null = this.h1
1798
- if (h1 === null) {
1799
- return
1800
- }
1801
2177
  const starved: HttpServerH1Slot[] = this.starved
1802
2178
  while (toI32(this.free.length) > 0) {
1803
2179
  const head: i32 = this.starvedHead
@@ -1807,8 +2183,8 @@ export class HttpServer {
1807
2183
  const s: HttpServerH1Slot = starved[head]
1808
2184
  this.starvedHead = head + 1
1809
2185
  s.starved = false
1810
- if (s.open && h1.holds(s.slot)) {
1811
- this.drive(h1, s)
2186
+ if (s.open && this.h1Holds(s)) {
2187
+ this.drive(s)
1812
2188
  }
1813
2189
  }
1814
2190
  // Drop the spent slots once they are half the list, so each is moved O(1) times.
@@ -1828,32 +2204,29 @@ export class HttpServer {
1828
2204
  }
1829
2205
 
1830
2206
  /** 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)
2207
+ private settle(s: HttpServerH1Slot, conn: Http1Connection): void {
2208
+ let wants: i32 = this.h1Flush(s)
1834
2209
  if ((wants & H1_DONE) !== 0) {
1835
- this.h1Close(h1, slot)
2210
+ this.h1Close(s)
1836
2211
  return
1837
2212
  }
1838
- if (this.draining && s.current === null && !conn.wantsWrite()) {
1839
- this.h1Close(h1, slot)
2213
+ if (this.draining && s.current === null && !conn.wantsWrite() && !this.h1Sealing(s, conn, wants)) {
2214
+ this.h1Close(s)
1840
2215
  return
1841
2216
  }
1842
2217
  if (s.starved) {
1843
2218
  wants = wants & ~H1_WANT_READ
1844
2219
  }
1845
- if (h1.rearm(slot, wants)) {
1846
- pollModify(this.loop, h1.fd(slot), wants & (H1_WANT_READ | H1_WANT_WRITE), slot)
1847
- }
2220
+ this.h1Arm(s, wants)
1848
2221
  const r: HttpServerRequest | null = s.current
1849
- if (r !== null && r.state === HTTP_SERVER_PARKED && this.roomy(conn)) {
2222
+ if (r !== null && r.state === HTTP_SERVER_PARKED && this.roomy(s, conn)) {
1850
2223
  this.h1Ready(r)
1851
2224
  }
1852
2225
  }
1853
2226
 
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)
2227
+ /** Whether `conn`'s output holds at most half of slot `s`'s output size unsent, so a response head fits behind it. */
2228
+ private roomy(s: HttpServerH1Slot, conn: Http1Connection): boolean {
2229
+ return toI64(conn.outputEnd) - toI64(conn.outputStart) <= toI64(s.outputSize) / toI64(2)
1857
2230
  }
1858
2231
 
1859
2232
  /** A head: a free request, the head copied into it, and its route. */
@@ -1899,7 +2272,7 @@ export class HttpServer {
1899
2272
  r.targetEnd = p.targetEnd
1900
2273
  r.kind = HTTP_SERVER_H1
1901
2274
  r.version = 1
1902
- r.secure = false
2275
+ r.secure = s.tls
1903
2276
  r.slot = s.slot
1904
2277
  r.gen = s.gen
1905
2278
  if (r.methodIs("CONNECT")) {
@@ -1925,7 +2298,7 @@ export class HttpServer {
1925
2298
  if (r === null) {
1926
2299
  return
1927
2300
  }
1928
- if (this.roomy(conn)) {
2301
+ if (this.roomy(s, conn)) {
1929
2302
  this.h1Ready(r)
1930
2303
  } else {
1931
2304
  r.state = HTTP_SERVER_PARKED
@@ -2020,16 +2393,23 @@ export class HttpServer {
2020
2393
  }
2021
2394
 
2022
2395
  /**
2023
- * Closes `slot` and bumps its generation, so any answer still owed for it is
2396
+ * Closes slot `s` and bumps its generation, so any answer still owed for it is
2024
2397
  * dropped. A request the server alone holds goes back now; one queued or
2025
2398
  * taken goes back when `ready` passes it or the program answers it.
2026
2399
  */
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)
2400
+ private h1Close(s: HttpServerH1Slot): void {
2401
+ if (s.tls) {
2402
+ const h1Tls: Http1TlsServer | null = this.h1Tls
2403
+ if (h1Tls !== null) {
2404
+ h1Tls.close(s.carrier)
2405
+ }
2406
+ } else {
2407
+ const h1: Http1Server | null = this.h1
2408
+ if (h1 !== null) {
2409
+ h1.close(s.carrier)
2410
+ }
2032
2411
  }
2412
+ this.h1Forget(s)
2033
2413
  }
2034
2414
 
2035
2415
  /** The bookkeeping of a slot the carrier has closed. */
@@ -2044,9 +2424,9 @@ export class HttpServer {
2044
2424
  }
2045
2425
 
2046
2426
  /** Every slot the carrier's `expire` closed. */
2047
- private h1Sweep(h1: Http1Server): void {
2427
+ private h1Sweep(): void {
2048
2428
  for (const s of this.h1Slots) {
2049
- if (s.open && !h1.holds(s.slot)) {
2429
+ if (s.open && !this.h1Holds(s)) {
2050
2430
  this.h1Forget(s)
2051
2431
  }
2052
2432
  }
@@ -2625,8 +3005,8 @@ export class HttpServer {
2625
3005
  const conn: Http2Connection | null = this.h2Conn(r.slot)
2626
3006
  return conn === null || conn.inputEnded
2627
3007
  }
2628
- const h1: Http1Server | null = this.h1
2629
- return r.kind === HTTP_SERVER_H1 && h1 !== null && h1.connection(r.slot).ended()
3008
+ const conn: Http1Connection | null = this.h1ConnOf(r)
3009
+ return r.kind === HTTP_SERVER_H1 && conn !== null && conn.ended()
2630
3010
  }
2631
3011
 
2632
3012
  /** Panics, naming `who`, unless `r` is one of this server's own requests. */
@@ -2637,7 +3017,13 @@ export class HttpServer {
2637
3017
  }
2638
3018
  }
2639
3019
 
2640
- /** HTTP/1.1 slot `slot`'s record, or null for a number outside the table `listenHttp1` made one per carrier slot. */
3020
+ /** The connection of the HTTP/1.1 slot request `r` was read on, or null when that slot has no record or no carrier. */
3021
+ private h1ConnOf(r: HttpServerRequest): Http1Connection | null {
3022
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
3023
+ return s !== null ? this.h1Conn(s) : null
3024
+ }
3025
+
3026
+ /** 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
3027
  private h1Slot(slot: i32): HttpServerH1Slot | null {
2642
3028
  const slots: HttpServerH1Slot[] = this.h1Slots
2643
3029
  return slot >= 0 && slot < toI32(slots.length) ? slots[slot] : null
@@ -2652,9 +3038,8 @@ export class HttpServer {
2652
3038
  /** Whether `r`'s connection is the one it was read on, still open, and its stream not reset. */
2653
3039
  private live(r: HttpServerRequest): boolean {
2654
3040
  if (r.kind === HTTP_SERVER_H1) {
2655
- const h1: Http1Server | null = this.h1
2656
3041
  const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
2657
- return h1 !== null && s !== null && h1.holds(r.slot) && s.gen === r.gen
3042
+ return s !== null && this.h1Holds(s) && s.gen === r.gen
2658
3043
  }
2659
3044
  if (r.hung) {
2660
3045
  return false
@@ -2737,11 +3122,10 @@ export class HttpServer {
2737
3122
  return HANDLER_DROPPED
2738
3123
  }
2739
3124
  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
3125
+ const conn: Http1Connection | null = this.h1ConnOf(r)
3126
+ if (conn === null) {
3127
+ return HANDLER_DROPPED // `live` holds no HTTP/1.1 request without its slot's carrier
2743
3128
  }
2744
- const conn: Http1Connection = h1.connection(r.slot)
2745
3129
  const sent: i32 = conn.respond(
2746
3130
  status,
2747
3131
  this.outNames,
@@ -2936,9 +3320,9 @@ export class HttpServer {
2936
3320
  private abort(r: HttpServerRequest): void {
2937
3321
  r.hung = true
2938
3322
  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)
3323
+ const s: HttpServerH1Slot | null = this.h1Slot(r.slot)
3324
+ if (s !== null && this.live(r)) {
3325
+ this.h1Close(s)
2942
3326
  }
2943
3327
  return
2944
3328
  }
@@ -3032,9 +3416,9 @@ export class HttpServer {
3032
3416
  if (r.kind === HTTP_SERVER_H1) {
3033
3417
  // `write` keeps `len` within what the head declared, so the carrier
3034
3418
  // 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)
3419
+ const conn: Http1Connection | null = this.h1ConnOf(r)
3420
+ if (conn !== null) {
3421
+ taken = conn.write(buf, off, len)
3038
3422
  }
3039
3423
  if (taken === H1_AGAIN) {
3040
3424
  return 0
@@ -3061,12 +3445,9 @@ export class HttpServer {
3061
3445
 
3062
3446
  /** 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
3447
  private nudge(r: HttpServerRequest): void {
3064
- const h1: Http1Server | null = this.h1
3065
3448
  const h3: Http3Server | null = this.h3
3066
3449
  if (r.kind === HTTP_SERVER_H1) {
3067
- if (h1 !== null) {
3068
- this.driveOf(h1, r)
3069
- }
3450
+ this.driveOf(r)
3070
3451
  } else if (r.kind === HTTP_SERVER_H2) {
3071
3452
  if (this.h2Holds(r.slot)) {
3072
3453
  this.h2Settle(r.slot)
@@ -3088,17 +3469,17 @@ export class HttpServer {
3088
3469
  if (r.state !== HTTP_SERVER_STREAMING) {
3089
3470
  panic(`HttpServer.end: the response is not started (state ${r.state})`)
3090
3471
  }
3091
- const h1: Http1Server | null = this.h1
3092
- if (!this.live(r) || (r.kind === HTTP_SERVER_H1 && h1 === null)) {
3472
+ const conn: Http1Connection | null = r.kind === HTTP_SERVER_H1 ? this.h1ConnOf(r) : null
3473
+ if (!this.live(r) || (r.kind === HTTP_SERVER_H1 && conn === null)) {
3093
3474
  this.release(r)
3094
3475
  return HANDLER_DROPPED
3095
3476
  }
3096
3477
  if (r.left > 0 && !r.quiet) {
3097
3478
  panic(`HttpServer.end: the body is ${r.left} byte(s) short of the length the response declared`)
3098
3479
  }
3099
- if (r.kind === HTTP_SERVER_H1 && h1 !== null) {
3100
- this.finishH1(r, h1.connection(r.slot))
3101
- this.driveOf(h1, r)
3480
+ if (r.kind === HTTP_SERVER_H1 && conn !== null) {
3481
+ this.finishH1(r, conn)
3482
+ this.driveOf(r)
3102
3483
  return HANDLER_SENT
3103
3484
  }
3104
3485
  r.state = HTTP_SERVER_FLUSHING
@@ -4450,13 +4831,19 @@ export class HttpServer {
4450
4831
  * the carriers dropped are forgotten.
4451
4832
  */
4452
4833
  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
- }
4834
+ for (const s of this.h1Slots) {
4835
+ if (
4836
+ s.open &&
4837
+ s.sniff &&
4838
+ s.got > 0 &&
4839
+ httpServerAfter(s.since, toI64(this.prefaceTimeout) * HTTP_SERVER_NANOS_PER_MS) <= now
4840
+ ) {
4841
+ // Part of the preface and then silence: the slot has held its 24 bytes long enough.
4842
+ this.h1Close(s)
4843
+ }
4844
+ const r: HttpServerRequest | null = s.current
4845
+ if (r !== null && this.late(r, now) && this.h1Holds(s)) {
4846
+ this.h1Close(s)
4460
4847
  }
4461
4848
  }
4462
4849
  const h2c: Http2CleartextServer | null = this.h2c