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