@amritk/nish-aarch64-linux 0.44.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +84 -61
- 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 +3 -4
- package/std/crypto/sha1.ts +107 -65
- 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/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 +1315 -222
- 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 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- 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 +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- 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 +50 -18
- 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 +225 -124
- package/std/net/quic-listener.ts +197 -89
- 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 +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- 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 +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/http1-server.ts
CHANGED
|
@@ -129,7 +129,7 @@
|
|
|
129
129
|
* implementation.
|
|
130
130
|
*/
|
|
131
131
|
import { Secret } from "nish:secret"
|
|
132
|
-
import {
|
|
132
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
133
133
|
import {
|
|
134
134
|
HTTP_CODING_GZIP,
|
|
135
135
|
HTTP_CODING_IDENTITY,
|
|
@@ -288,10 +288,18 @@ const H1_CONN_KEEP_ALIVE: i32 = 2
|
|
|
288
288
|
|
|
289
289
|
/** Whether `kept[at ..)` begins with all of `bytes`, a byte at a time: the path for a range a word load cannot prove inside its array. */
|
|
290
290
|
const http1ServerSameByByte = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
291
|
-
|
|
292
|
-
|
|
291
|
+
const n: i32 = toI32(bytes.length)
|
|
292
|
+
const m: i32 = toI32(kept.length)
|
|
293
|
+
if (at < 0 || at > m - n) {
|
|
294
|
+
return false
|
|
295
|
+
}
|
|
296
|
+
// `j` walks `kept` beside `k`, inside it: `at + n` is at most its length.
|
|
297
|
+
let j: i32 = at
|
|
298
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
299
|
+
if (j < 0 || j >= m || kept[j] !== bytes[k]) {
|
|
293
300
|
return false
|
|
294
301
|
}
|
|
302
|
+
j += 1
|
|
295
303
|
}
|
|
296
304
|
return true
|
|
297
305
|
}
|
|
@@ -304,10 +312,14 @@ const http1ServerSameByByte = (kept: readonly u8[], at: i32, bytes: readonly u8[
|
|
|
304
312
|
*/
|
|
305
313
|
const http1ServerSame8 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
306
314
|
const n: i32 = toI32(bytes.length)
|
|
315
|
+
// Both under 2^30, which every kept buffer is, so `at + n` cannot wrap.
|
|
316
|
+
if (!(at >= 0 && at < 1073741824 && n >= 8 && n < 1073741824)) {
|
|
317
|
+
return http1ServerSameByByte(kept, at, bytes)
|
|
318
|
+
}
|
|
307
319
|
const e: i32 = at + n - 8
|
|
308
320
|
const f: i32 = n - 8
|
|
309
321
|
if (
|
|
310
|
-
!(at
|
|
322
|
+
!(at + 8 <= toI32(kept.length)) ||
|
|
311
323
|
!(e >= 0 && e < 2147483640 && e + 8 <= toI32(kept.length)) ||
|
|
312
324
|
!(toI32(bytes.length) >= 8 && f >= 0 && f < 2147483640 && f + 8 <= toI32(bytes.length))
|
|
313
325
|
) {
|
|
@@ -344,7 +356,8 @@ const http1ServerSame8 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
344
356
|
/** `http1ServerSame8` for four to seven bytes, with four-byte loads. */
|
|
345
357
|
const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
346
358
|
const n: i32 = toI32(bytes.length)
|
|
347
|
-
|
|
359
|
+
// Widened: the range check below holds `e` to `kept` whatever `at` is.
|
|
360
|
+
const e: i32 = toI32(toI64(at) + toI64(n) - toI64(4))
|
|
348
361
|
const f: i32 = n - 4
|
|
349
362
|
if (
|
|
350
363
|
!(at >= 0 && at < 2147483640 && at + 4 <= toI32(kept.length)) ||
|
|
@@ -367,6 +380,34 @@ const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
367
380
|
)
|
|
368
381
|
}
|
|
369
382
|
|
|
383
|
+
/**
|
|
384
|
+
* Whether `kept[at ..)` starts with `bytes`, sixteen or more bytes long:
|
|
385
|
+
* sixteen at a time, with a last load that overlaps the one before. False
|
|
386
|
+
* when `bytes` would run past `kept`. Its own function so that
|
|
387
|
+
* `http1ServerKept`, which most field names reach through the shorter loads,
|
|
388
|
+
* stays small enough to inline them.
|
|
389
|
+
*/
|
|
390
|
+
const http1ServerSame16 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
391
|
+
const n: i32 = toI32(bytes.length)
|
|
392
|
+
const m: i32 = toI32(kept.length)
|
|
393
|
+
let k: i32 = 0
|
|
394
|
+
let j: i32 = at
|
|
395
|
+
while (k < n - 16) {
|
|
396
|
+
if (j < 0 || j > m - 16 || any(ne(loadU8x16(kept, j), loadU8x16(bytes, k)))) {
|
|
397
|
+
return false
|
|
398
|
+
}
|
|
399
|
+
k += 16
|
|
400
|
+
j += 16
|
|
401
|
+
}
|
|
402
|
+
// Widened: `at + n` is only inside `kept` once the checks below hold.
|
|
403
|
+
const tail: i32 = toI32(toI64(at) + toI64(n) - toI64(16))
|
|
404
|
+
const last: i32 = n - 16
|
|
405
|
+
if (tail < 0 || tail > toI32(kept.length) - 16 || last < 0 || last > toI32(bytes.length) - 16) {
|
|
406
|
+
return false
|
|
407
|
+
}
|
|
408
|
+
return !any(ne(loadU8x16(kept, tail), loadU8x16(bytes, last)))
|
|
409
|
+
}
|
|
410
|
+
|
|
370
411
|
/**
|
|
371
412
|
* Where `kept[at ..)` holds `bytes` and then the byte `after`: the offset one
|
|
372
413
|
* past the byte that follows `after` (the space of `: `, the LF of CR LF), or
|
|
@@ -379,34 +420,82 @@ const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
379
420
|
*/
|
|
380
421
|
const http1ServerKept = (kept: u8[], at: i32, limit: i32, bytes: u8[], after: i32): i32 => {
|
|
381
422
|
const n: i32 = toI32(bytes.length)
|
|
382
|
-
|
|
423
|
+
const m: i32 = toI32(kept.length)
|
|
424
|
+
if (at < 0 || limit > m || toI64(n) > toI64(limit) - toI64(at) - toI64(2)) {
|
|
383
425
|
return -1
|
|
384
426
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
k += 16
|
|
392
|
-
}
|
|
393
|
-
return any(ne(loadU8x16(kept, at + n - 16), loadU8x16(bytes, n - 16))) ? -1 : at + n + 2
|
|
427
|
+
// `at + n + 2` is at most `limit`, so `end` and `past` are inside `kept`.
|
|
428
|
+
const end: i32 = toI32(toI64(at) + toI64(n))
|
|
429
|
+
const past: i32 = toI32(toI64(end) + toI64(2))
|
|
430
|
+
if (end < 0 || end >= m || toI32(kept[end]) !== after) {
|
|
431
|
+
return -1
|
|
394
432
|
}
|
|
395
433
|
let same: boolean = false
|
|
396
|
-
if (n >=
|
|
434
|
+
if (n >= 16) {
|
|
435
|
+
same = http1ServerSame16(kept, at, bytes)
|
|
436
|
+
} else if (n >= 8) {
|
|
397
437
|
same = http1ServerSame8(kept, at, bytes)
|
|
398
438
|
} else if (n >= 4) {
|
|
399
439
|
same = http1ServerSame4(kept, at, bytes)
|
|
400
440
|
} else {
|
|
401
441
|
same = http1ServerSameByByte(kept, at, bytes)
|
|
402
442
|
}
|
|
403
|
-
return same ?
|
|
443
|
+
return same ? past : -1
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** Why `value` is outside `low .. high` as the cap `name`, or "" when it is inside. */
|
|
447
|
+
const http1ServerCapFault = (name: string, value: i32, low: i32, high: i32): string =>
|
|
448
|
+
value < low || value > high ? `Http1Config.${name}: ${value} is outside ${low} to ${high}` : ""
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* The first cap of `config` outside what a connection can work with, as the
|
|
452
|
+
* message `Http1Connection`'s constructor panics with, or "" when every cap
|
|
453
|
+
* is inside.
|
|
454
|
+
*/
|
|
455
|
+
const http1ServerConfigFault = (config: Http1Config): string => {
|
|
456
|
+
const big: i32 = 1073741824
|
|
457
|
+
const one: i32 = 1
|
|
458
|
+
const smallest: i32 = 64
|
|
459
|
+
const output: i32 = 1024
|
|
460
|
+
const caps: string[] = [
|
|
461
|
+
http1ServerCapFault("maxTarget", config.maxTarget, one, big),
|
|
462
|
+
http1ServerCapFault("maxHeaderBytes", config.maxHeaderBytes, smallest, big),
|
|
463
|
+
http1ServerCapFault("maxHeaders", config.maxHeaders, one, big),
|
|
464
|
+
http1ServerCapFault("maxBody", config.maxBody, H1_ZERO, big),
|
|
465
|
+
http1ServerCapFault("chunkSize", config.chunkSize, one, big),
|
|
466
|
+
http1ServerCapFault("outputSize", config.outputSize, output, big),
|
|
467
|
+
http1ServerCapFault("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER),
|
|
468
|
+
http1ServerCapFault("idleTimeout", config.idleTimeout, one, big),
|
|
469
|
+
http1ServerCapFault("headTimeout", config.headTimeout, one, big),
|
|
470
|
+
http1ServerCapFault("lingerTimeout", config.lingerTimeout, one, big),
|
|
471
|
+
http1ServerCapFault("lingerBytes", config.lingerBytes, H1_ZERO, big),
|
|
472
|
+
// The buffers the peer's bytes fill grow by doubling and stop at 2^30
|
|
473
|
+
// (`Http1Parser`'s input and head, `WsDecoder`'s input), so a cap whose
|
|
474
|
+
// buffer could need more is refused here rather than reached by a peer:
|
|
475
|
+
// the head holds the request line and the header section together, and
|
|
476
|
+
// the input never holds more than the larger of the two lines or a chunk.
|
|
477
|
+
// Each is at most 2^30 here, so the sum wraps only at 2^31, to a
|
|
478
|
+
// negative the check refuses as it would the sum itself.
|
|
479
|
+
http1ServerCapFault(
|
|
480
|
+
"maxTarget + maxHeaderBytes",
|
|
481
|
+
toI32(toI64(config.maxTarget) + toI64(config.maxHeaderBytes)),
|
|
482
|
+
one,
|
|
483
|
+
big - H1_LINE_OVERHEAD
|
|
484
|
+
),
|
|
485
|
+
]
|
|
486
|
+
for (const fault of caps) {
|
|
487
|
+
if (fault.length > 0) {
|
|
488
|
+
return fault
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return ""
|
|
404
492
|
}
|
|
405
493
|
|
|
406
|
-
/** Panics unless
|
|
407
|
-
const http1ServerCheckCap = (
|
|
408
|
-
|
|
409
|
-
|
|
494
|
+
/** Panics unless every cap of `config` is one a connection can work with: a cap outside is the program's mistake. */
|
|
495
|
+
const http1ServerCheckCap = (config: Http1Config): void => {
|
|
496
|
+
const fault: string = http1ServerConfigFault(config)
|
|
497
|
+
if (fault.length > 0) {
|
|
498
|
+
panic(fault)
|
|
410
499
|
}
|
|
411
500
|
}
|
|
412
501
|
|
|
@@ -418,19 +507,78 @@ const http1ServerCheckWindow = (what: string, buf: readonly u8[], off: i32, len:
|
|
|
418
507
|
}
|
|
419
508
|
}
|
|
420
509
|
|
|
510
|
+
/**
|
|
511
|
+
* `n` zeroed bytes, the length a `Http1Config` cap checked: one `new Array`
|
|
512
|
+
* up to 65535, which a `u16` length proves, and pushes on that past it. A
|
|
513
|
+
* negative `n` answers an empty buffer, by contract rather than by panic. No
|
|
514
|
+
* caller in this module reaches it: they pass `outputSize`, which
|
|
515
|
+
* `http1ServerConfigFault` holds at 1024 or more, and the positive constants
|
|
516
|
+
* `HTTP1_FIRST_INPUT` and `HTTP1_FIRST_HEAD`.
|
|
517
|
+
*/
|
|
518
|
+
const http1ServerBytes = (n: i32): u8[] => {
|
|
519
|
+
if (n < 0) {
|
|
520
|
+
return new Array<u8>(0)
|
|
521
|
+
}
|
|
522
|
+
if (n <= 65535) {
|
|
523
|
+
return new Array<u8>(toU16(n))
|
|
524
|
+
}
|
|
525
|
+
const out: u8[] = new Array<u8>(65535)
|
|
526
|
+
for (let i: i32 = 65535; i < n; i++) {
|
|
527
|
+
out.push(0)
|
|
528
|
+
}
|
|
529
|
+
return out
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* `a + b` worked in 64 bits and narrowed: the connection adds offsets and
|
|
534
|
+
* counts its checked caps bound, each at most 2^30, so the sum fits; one
|
|
535
|
+
* past that wraps to a negative the caller's range check refuses.
|
|
536
|
+
*/
|
|
537
|
+
const http1ServerAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
|
|
538
|
+
|
|
539
|
+
/** `a - b` worked in 64 bits and narrowed, for the same offsets and counts as `http1ServerAdd`. */
|
|
540
|
+
const http1ServerSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Whether more than `ms` milliseconds lie between the `monotonicNanos`
|
|
544
|
+
* instants `since` and `now`. Worked in `u64`, which has the bits of the
|
|
545
|
+
* signed difference, and holds the timeout scaled to nanoseconds; a `now`
|
|
546
|
+
* at or before `since` is no time at all, as the signed test answered.
|
|
547
|
+
*/
|
|
548
|
+
const http1ServerPast = (now: i64, since: i64, ms: i32): boolean =>
|
|
549
|
+
now > since && toU64(now) - toU64(since) > toU64(ms) * toU64(1000000)
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The `monotonicNanos` instant `ms` milliseconds after `at`. Worked in `u64`,
|
|
553
|
+
* whose wrapping sum has the bits of the signed one: an instant is centuries
|
|
554
|
+
* short of 2^63 and the timeout a checked cap, so neither wraps.
|
|
555
|
+
*/
|
|
556
|
+
const http1ServerAfter = (at: i64, ms: i32): i64 => toI64(toU64(at) + toU64(ms) * toU64(1000000))
|
|
557
|
+
|
|
421
558
|
/** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
|
|
422
559
|
const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
|
|
423
560
|
if (n > 0 && at >= 0 && off >= 0 && at <= toI32(out.length) - n && off <= toI32(src.length) - n) {
|
|
424
561
|
// Both windows fit, as they do for every caller: one move, not a byte loop.
|
|
425
|
-
|
|
426
|
-
|
|
562
|
+
if (tryCopyBytes(out, at, src, off, n)) {
|
|
563
|
+
return toI32(toI64(at) + toI64(n))
|
|
564
|
+
}
|
|
427
565
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
566
|
+
// Widened: `at + k` and `off + k` are compared before either is an index.
|
|
567
|
+
const outN: i32 = toI32(out.length)
|
|
568
|
+
const srcN: i32 = toI32(src.length)
|
|
569
|
+
for (let k: i32 = 0; k < n; k++) {
|
|
570
|
+
const i: i64 = toI64(at) + toI64(k)
|
|
571
|
+
const j: i64 = toI64(off) + toI64(k)
|
|
572
|
+
if (i >= toI64(outN) || j >= toI64(srcN)) {
|
|
573
|
+
break
|
|
574
|
+
}
|
|
575
|
+
const to: i32 = toI32(i)
|
|
576
|
+
const from: i32 = toI32(j)
|
|
577
|
+
if (to >= 0 && to < outN && from >= 0 && from < srcN) {
|
|
578
|
+
out[to] = src[from]
|
|
431
579
|
}
|
|
432
580
|
}
|
|
433
|
-
return at + n
|
|
581
|
+
return toI32(toI64(at) + toI64(n))
|
|
434
582
|
}
|
|
435
583
|
|
|
436
584
|
/**
|
|
@@ -455,10 +603,10 @@ export class HttpDate {
|
|
|
455
603
|
private pinnedMs: i64 = 0
|
|
456
604
|
/** The clock reading `hold` took, in milliseconds since 1970, while `held`. */
|
|
457
605
|
private heldMs: i64 = 0
|
|
458
|
-
/** How many times `bytes` has been rewritten. */
|
|
459
|
-
private rewrites:
|
|
460
|
-
/** How many times the wall clock has been read. */
|
|
461
|
-
private reads:
|
|
606
|
+
/** How many times `bytes` has been rewritten: unsigned, so the count wraps where a signed one could overflow. */
|
|
607
|
+
private rewrites: u64 = 0
|
|
608
|
+
/** How many times the wall clock has been read, unsigned as `rewrites` is. */
|
|
609
|
+
private reads: u64 = 0
|
|
462
610
|
private pinned: boolean = false
|
|
463
611
|
private held: boolean = false
|
|
464
612
|
|
|
@@ -490,7 +638,7 @@ export class HttpDate {
|
|
|
490
638
|
if (second !== this.second) {
|
|
491
639
|
httpDateWrite(this.bytes, 0, second)
|
|
492
640
|
this.second = second
|
|
493
|
-
this.rewrites += 1
|
|
641
|
+
this.rewrites += toU64(1)
|
|
494
642
|
}
|
|
495
643
|
return this.bytes
|
|
496
644
|
}
|
|
@@ -536,7 +684,7 @@ export class HttpDate {
|
|
|
536
684
|
|
|
537
685
|
/** The wall clock, in milliseconds since 1970. */
|
|
538
686
|
private now(): i64 {
|
|
539
|
-
this.reads += 1
|
|
687
|
+
this.reads += toU64(1)
|
|
540
688
|
return toI64(Date.now())
|
|
541
689
|
}
|
|
542
690
|
|
|
@@ -546,7 +694,7 @@ export class HttpDate {
|
|
|
546
694
|
* @returns the count of `Date.now()` reads since the date was made, for a test that checks a serve call reads once
|
|
547
695
|
*/
|
|
548
696
|
clockReads(): i64 {
|
|
549
|
-
return this.reads
|
|
697
|
+
return toI64(this.reads)
|
|
550
698
|
}
|
|
551
699
|
|
|
552
700
|
/**
|
|
@@ -555,7 +703,7 @@ export class HttpDate {
|
|
|
555
703
|
* @returns the count of IMF-fixdate formats since the date was made, for a test that checks the once-a-second promise
|
|
556
704
|
*/
|
|
557
705
|
formats(): i64 {
|
|
558
|
-
return this.rewrites
|
|
706
|
+
return toI64(this.rewrites)
|
|
559
707
|
}
|
|
560
708
|
}
|
|
561
709
|
|
|
@@ -613,6 +761,20 @@ export class Http1Config {
|
|
|
613
761
|
idleInput: boolean = true
|
|
614
762
|
}
|
|
615
763
|
|
|
764
|
+
/**
|
|
765
|
+
* `config` when every cap is one a connection can work with, the twin of the
|
|
766
|
+
* check `Http1Connection`'s constructor panics on: a program reading its
|
|
767
|
+
* caps from a file or flags checks them here, and refuses the bad one with
|
|
768
|
+
* the message the constructor would panic with, rather than panicking.
|
|
769
|
+
*
|
|
770
|
+
* @param config the caps to check, returned as they are when they pass
|
|
771
|
+
* @returns `config`, or the first cap outside its range, named with the range it must lie in
|
|
772
|
+
*/
|
|
773
|
+
export const http1ConfigChecked = (config: Http1Config): Result<Http1Config, string> => {
|
|
774
|
+
const fault: string = http1ServerConfigFault(config)
|
|
775
|
+
return fault.length > 0 ? Err(fault) : Ok(config)
|
|
776
|
+
}
|
|
777
|
+
|
|
616
778
|
/** The reason phrase a status is sent with; RFC 9112 §4 lets a client ignore it. */
|
|
617
779
|
const http1ServerReason = (status: i32): string => {
|
|
618
780
|
switch (status) {
|
|
@@ -684,10 +846,18 @@ const http1ServerReserved = (name: u8[]): boolean =>
|
|
|
684
846
|
*/
|
|
685
847
|
export class Http1OutputPool {
|
|
686
848
|
// Widest first, so the layout packs without padding.
|
|
687
|
-
/**
|
|
849
|
+
/**
|
|
850
|
+
* The buffers given back and not yet taken again; `made - free.length` is
|
|
851
|
+
* how many are out, while `made` is below 2^31 - 1 (see `made`).
|
|
852
|
+
*/
|
|
688
853
|
free: u8[][]
|
|
689
854
|
private size: i32 = 0
|
|
690
|
-
/**
|
|
855
|
+
/**
|
|
856
|
+
* How many buffers the pool has made, up to 2^31 - 1, where it stays: past
|
|
857
|
+
* that it undercounts, and so does `made - free.length`. This module's
|
|
858
|
+
* pools make buffers of 256 bytes or more, so reaching it takes 512 GiB of
|
|
859
|
+
* buffers.
|
|
860
|
+
*/
|
|
691
861
|
made: i32 = 0
|
|
692
862
|
/** How many buffers `free` has room for without growing. */
|
|
693
863
|
private room: i32 = 0
|
|
@@ -708,18 +878,24 @@ export class Http1OutputPool {
|
|
|
708
878
|
* @internal Called by `Http1Connection` in this module, when it first needs room to write and when it unparks its input and head buffers.
|
|
709
879
|
*/
|
|
710
880
|
take(): u8[] {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
881
|
+
const free: u8[][] = this.free
|
|
882
|
+
const n: i32 = toI32(free.length)
|
|
883
|
+
// `n` proves the index and the length test the `pop`.
|
|
884
|
+
if (n > 0 && free.length > 0) {
|
|
885
|
+
const buf: u8[] = free[n - 1]
|
|
886
|
+
free.pop()
|
|
714
887
|
return buf
|
|
715
888
|
}
|
|
716
|
-
|
|
717
|
-
const
|
|
889
|
+
// Held at 2^31 - 1 rather than wrapped: see `made`.
|
|
890
|
+
const was: i32 = this.made
|
|
891
|
+
const made: i32 = was < 2147483647 ? was + 1 : was
|
|
892
|
+
this.made = made
|
|
893
|
+
const buf: u8[] = http1ServerBytes(this.size)
|
|
718
894
|
// `free` gets room for every buffer made here, while this allocates
|
|
719
895
|
// anyway, so `give` never grows it: a slot's first park can come long
|
|
720
896
|
// after its take, when the arena is meant to be flat.
|
|
721
|
-
if (
|
|
722
|
-
const want: i32 =
|
|
897
|
+
if (made > this.room) {
|
|
898
|
+
const want: i32 = made >= 0 && made < 1073741823 ? made * 2 : 2147483647
|
|
723
899
|
for (let i: i32 = 0; i < want; i++) {
|
|
724
900
|
this.free.push(buf)
|
|
725
901
|
}
|
|
@@ -833,6 +1009,8 @@ export class Http1Connection {
|
|
|
833
1009
|
private inputEnded: boolean = false
|
|
834
1010
|
/** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
|
|
835
1011
|
headTimed: boolean = false
|
|
1012
|
+
/** Whether a reader holds the parser's head and spans past the exchange: `restart` hands it them rather than reuse them. */
|
|
1013
|
+
private lentParsed: boolean = false
|
|
836
1014
|
/** Where the bytes `H1_BODY` or `H1_WS_MESSAGE` handed over start in `data`. */
|
|
837
1015
|
dataStart: i32 = 0
|
|
838
1016
|
/** How many bytes `H1_BODY` or `H1_WS_MESSAGE` handed over, from `data[dataStart]`. */
|
|
@@ -931,40 +1109,22 @@ export class Http1Connection {
|
|
|
931
1109
|
* @internal Read by `tests/link/net_http1_server_head_reuse` to tell a copied head from a written one.
|
|
932
1110
|
*/
|
|
933
1111
|
headCopies: i64 = 0
|
|
1112
|
+
/**
|
|
1113
|
+
* The head and spans `restart` gives the parser while `lentParsed`, in
|
|
1114
|
+
* place of the ones a reader still holds: set by `lendParsed`.
|
|
1115
|
+
*/
|
|
1116
|
+
private spareHead: u8[]
|
|
1117
|
+
private spareSpans: i32[]
|
|
934
1118
|
|
|
935
1119
|
/**
|
|
936
|
-
* A connection under `config`. A cap outside what the connection can work with panics
|
|
1120
|
+
* A connection under `config`. A cap outside what the connection can work with panics;
|
|
1121
|
+
* `http1ConfigChecked` refuses the same caps as a `Result`.
|
|
937
1122
|
*
|
|
938
1123
|
* @param config the caps the connection is held to, read now and kept
|
|
939
1124
|
* @param date the `Date` its responses carry, shared with other connections; null makes it one of its own
|
|
940
1125
|
*/
|
|
941
1126
|
constructor(config: Http1Config, date: HttpDate | null = null) {
|
|
942
|
-
|
|
943
|
-
const one: i32 = 1
|
|
944
|
-
const smallest: i32 = 64
|
|
945
|
-
const output: i32 = 1024
|
|
946
|
-
http1ServerCheckCap("maxTarget", config.maxTarget, one, big)
|
|
947
|
-
http1ServerCheckCap("maxHeaderBytes", config.maxHeaderBytes, smallest, big)
|
|
948
|
-
http1ServerCheckCap("maxHeaders", config.maxHeaders, one, big)
|
|
949
|
-
http1ServerCheckCap("maxBody", config.maxBody, H1_ZERO, big)
|
|
950
|
-
http1ServerCheckCap("chunkSize", config.chunkSize, one, big)
|
|
951
|
-
http1ServerCheckCap("outputSize", config.outputSize, output, big)
|
|
952
|
-
http1ServerCheckCap("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER)
|
|
953
|
-
http1ServerCheckCap("idleTimeout", config.idleTimeout, one, big)
|
|
954
|
-
http1ServerCheckCap("headTimeout", config.headTimeout, one, big)
|
|
955
|
-
http1ServerCheckCap("lingerTimeout", config.lingerTimeout, one, big)
|
|
956
|
-
http1ServerCheckCap("lingerBytes", config.lingerBytes, H1_ZERO, big)
|
|
957
|
-
// The buffers the peer's bytes fill grow by doubling and stop at 2^30
|
|
958
|
-
// (`Http1Parser`'s input and head, `WsDecoder`'s input), so a cap whose
|
|
959
|
-
// buffer could need more is refused here rather than reached by a peer:
|
|
960
|
-
// the head holds the request line and the header section together, and
|
|
961
|
-
// the input never holds more than the larger of the two lines or a chunk.
|
|
962
|
-
http1ServerCheckCap(
|
|
963
|
-
"maxTarget + maxHeaderBytes",
|
|
964
|
-
config.maxTarget + config.maxHeaderBytes,
|
|
965
|
-
one,
|
|
966
|
-
big - H1_LINE_OVERHEAD
|
|
967
|
-
)
|
|
1127
|
+
http1ServerCheckCap(config)
|
|
968
1128
|
this.config = config
|
|
969
1129
|
// The parser is made first: the arena places each block after the last,
|
|
970
1130
|
// so its front fields sit on the lines just behind the connection's own.
|
|
@@ -976,6 +1136,8 @@ export class Http1Connection {
|
|
|
976
1136
|
this.acceptKey = null
|
|
977
1137
|
this.noOutput = []
|
|
978
1138
|
this.output = this.noOutput
|
|
1139
|
+
this.spareHead = this.noOutput
|
|
1140
|
+
this.spareSpans = []
|
|
979
1141
|
this.pool = null
|
|
980
1142
|
this.inputs = null
|
|
981
1143
|
this.heads = null
|
|
@@ -987,19 +1149,30 @@ export class Http1Connection {
|
|
|
987
1149
|
this.headLengths = []
|
|
988
1150
|
// A line must fit whole before the parser can read it, so the buffer
|
|
989
1151
|
// holds the longest request line or field line however small a chunk is.
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1152
|
+
// Widened, though every cap is checked to 2^30 above: the sums stay i32.
|
|
1153
|
+
let limit: i64 = toI64(config.chunkSize)
|
|
1154
|
+
const head: i64 = toI64(config.maxHeaderBytes) + toI64(2)
|
|
1155
|
+
const line: i64 = toI64(config.maxTarget) + toI64(H1_LINE_OVERHEAD)
|
|
1156
|
+
if (head > limit) {
|
|
1157
|
+
limit = head
|
|
993
1158
|
}
|
|
994
|
-
if (
|
|
995
|
-
limit =
|
|
1159
|
+
if (line > limit) {
|
|
1160
|
+
limit = line
|
|
996
1161
|
}
|
|
997
|
-
this.bufferLimit = limit
|
|
1162
|
+
this.bufferLimit = toI32(limit)
|
|
998
1163
|
this.restart()
|
|
999
1164
|
}
|
|
1000
1165
|
|
|
1001
1166
|
/** Puts the connection back as the constructor left it, for the next peer. It allocates nothing. */
|
|
1002
1167
|
restart(): void {
|
|
1168
|
+
if (this.lentParsed) {
|
|
1169
|
+
// A reader still holds the head and spans: the next peer's go in the
|
|
1170
|
+
// ones it gave, and the reader's stay its own.
|
|
1171
|
+
this.lentParsed = false
|
|
1172
|
+
this.parser.head = this.spareHead
|
|
1173
|
+
this.parser.spans = this.spareSpans
|
|
1174
|
+
this.spareHead = this.noOutput
|
|
1175
|
+
}
|
|
1003
1176
|
this.parser.restart()
|
|
1004
1177
|
const ws: WsDecoder | null = this.ws
|
|
1005
1178
|
if (ws !== null) {
|
|
@@ -1043,10 +1216,42 @@ export class Http1Connection {
|
|
|
1043
1216
|
this.park()
|
|
1044
1217
|
}
|
|
1045
1218
|
|
|
1219
|
+
/**
|
|
1220
|
+
* Lends the parser's head and spans to a reader that may hold them past
|
|
1221
|
+
* the connection: until `unlendParsed`, a `restart` gives the parser `head`
|
|
1222
|
+
* and `spans` in their place and leaves its own with the reader. While the
|
|
1223
|
+
* request is open they stay the parser's as well, unchanged.
|
|
1224
|
+
*
|
|
1225
|
+
* @param head an empty buffer, or one the reader is done with, that the parser takes as its head on a restart
|
|
1226
|
+
* @param spans the same for the parser's spans
|
|
1227
|
+
*/
|
|
1228
|
+
lendParsed(head: u8[], spans: i32[]): void {
|
|
1229
|
+
this.spareHead = head
|
|
1230
|
+
this.spareSpans = spans
|
|
1231
|
+
this.lentParsed = true
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/**
|
|
1235
|
+
* Ends `lendParsed`: the reader is done, and the parser's head and spans
|
|
1236
|
+
* are its own again. A connection resting meanwhile parks them now, as
|
|
1237
|
+
* `park` gives no lent head to the pool.
|
|
1238
|
+
*
|
|
1239
|
+
* @returns false when the connection restarted meanwhile, so the reader keeps what it was lent and the parser has the buffers it gave
|
|
1240
|
+
*/
|
|
1241
|
+
unlendParsed(): boolean {
|
|
1242
|
+
const held: boolean = this.lentParsed
|
|
1243
|
+
this.lentParsed = false
|
|
1244
|
+
// `park` kept the head back while it was lent: a resting connection
|
|
1245
|
+
// gives it to the pool now.
|
|
1246
|
+
this.park()
|
|
1247
|
+
return held
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1046
1250
|
/**
|
|
1047
1251
|
* Gives the parser's input and head buffers to `inputs` and `heads` while
|
|
1048
1252
|
* the connection rests between exchanges: speaking HTTP, no request open,
|
|
1049
|
-
* nothing fed and unread. Does nothing without the pools or otherwise
|
|
1253
|
+
* nothing fed and unread. Does nothing without the pools or otherwise,
|
|
1254
|
+
* and keeps a head lent by `lendParsed`.
|
|
1050
1255
|
*/
|
|
1051
1256
|
park(): void {
|
|
1052
1257
|
const inputs: Http1OutputPool | null = this.inputs
|
|
@@ -1065,7 +1270,9 @@ export class Http1Connection {
|
|
|
1065
1270
|
p.lineStart = 0
|
|
1066
1271
|
p.lineEnd = 0
|
|
1067
1272
|
}
|
|
1068
|
-
|
|
1273
|
+
// A lent head is the reader's too: it goes to the pool only once
|
|
1274
|
+
// `unlendParsed` ends the loan, or never, if a restart hands it over.
|
|
1275
|
+
if (toI32(p.head.length) > 0 && !this.lentParsed) {
|
|
1069
1276
|
heads.give(p.head)
|
|
1070
1277
|
p.head = this.noOutput
|
|
1071
1278
|
}
|
|
@@ -1099,7 +1306,7 @@ export class Http1Connection {
|
|
|
1099
1306
|
* @returns the cap in bytes on unread input: the buffer limit for HTTP, the message cap plus a frame header for WebSocket
|
|
1100
1307
|
*/
|
|
1101
1308
|
inputLimit(): i32 {
|
|
1102
|
-
return this.state === H1_S_WEBSOCKET ? this.config.maxMessage
|
|
1309
|
+
return this.state === H1_S_WEBSOCKET ? http1ServerAdd(this.config.maxMessage, 14) : this.bufferLimit
|
|
1103
1310
|
}
|
|
1104
1311
|
|
|
1105
1312
|
/**
|
|
@@ -1116,9 +1323,12 @@ export class Http1Connection {
|
|
|
1116
1323
|
// Only a WebSocket reads the decoder and the configured message cap,
|
|
1117
1324
|
// so a request's check stays on the connection's front fields.
|
|
1118
1325
|
const ws: WsDecoder | null = this.ws
|
|
1119
|
-
room =
|
|
1326
|
+
room = http1ServerSub(
|
|
1327
|
+
this.inputLimit(),
|
|
1328
|
+
ws !== null ? http1ServerSub(ws.end, ws.start) : this.parser.buffered()
|
|
1329
|
+
)
|
|
1120
1330
|
} else {
|
|
1121
|
-
room = this.bufferLimit
|
|
1331
|
+
room = http1ServerSub(this.bufferLimit, this.parser.buffered())
|
|
1122
1332
|
}
|
|
1123
1333
|
return room > 0 ? room : 0
|
|
1124
1334
|
}
|
|
@@ -1187,7 +1397,7 @@ export class Http1Connection {
|
|
|
1187
1397
|
return
|
|
1188
1398
|
}
|
|
1189
1399
|
const pool: Http1OutputPool | null = this.pool
|
|
1190
|
-
const output: u8[] = pool !== null ? pool.take() :
|
|
1400
|
+
const output: u8[] = pool !== null ? pool.take() : http1ServerBytes(this.config.outputSize)
|
|
1191
1401
|
this.output = output
|
|
1192
1402
|
this.outputCap = toI32(output.length)
|
|
1193
1403
|
this.outputStart = 0
|
|
@@ -1217,8 +1427,8 @@ export class Http1Connection {
|
|
|
1217
1427
|
* @param n how many bytes from `outputStart` the carrier sent; more than are waiting counts as all of them
|
|
1218
1428
|
*/
|
|
1219
1429
|
consume(n: i32): void {
|
|
1220
|
-
const pending: i32 = this.outputEnd
|
|
1221
|
-
this.outputStart = this.outputStart
|
|
1430
|
+
const pending: i32 = http1ServerSub(this.outputEnd, this.outputStart)
|
|
1431
|
+
this.outputStart = http1ServerAdd(this.outputStart, n < pending ? n : pending)
|
|
1222
1432
|
if (this.outputStart === this.outputEnd) {
|
|
1223
1433
|
this.outputStart = 0
|
|
1224
1434
|
this.outputEnd = 0
|
|
@@ -1323,20 +1533,19 @@ export class Http1Connection {
|
|
|
1323
1533
|
this.acquire()
|
|
1324
1534
|
}
|
|
1325
1535
|
const output: u8[] = this.output
|
|
1326
|
-
const live: i32 = this.outputEnd
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
}
|
|
1536
|
+
const live: i32 = http1ServerSub(this.outputEnd, this.outputStart)
|
|
1537
|
+
// The live bytes lie inside the output, so the move always succeeds; were
|
|
1538
|
+
// they not, the output stays as it is and only its end is room.
|
|
1539
|
+
if (this.outputStart > 0 && (live <= 0 || tryCopyBytes(output, 0, output, this.outputStart, live))) {
|
|
1331
1540
|
this.outputStart = 0
|
|
1332
|
-
this.outputEnd = live
|
|
1541
|
+
this.outputEnd = live > 0 ? live : 0
|
|
1333
1542
|
}
|
|
1334
|
-
return toI32(output.length)
|
|
1543
|
+
return http1ServerSub(toI32(output.length), this.outputEnd)
|
|
1335
1544
|
}
|
|
1336
1545
|
|
|
1337
1546
|
/** The room a response or a frame may use: all but the control reserve. */
|
|
1338
1547
|
private bodyRoom(): i32 {
|
|
1339
|
-
const r: i32 = this.room()
|
|
1548
|
+
const r: i32 = http1ServerSub(this.room(), H1_CONTROL_RESERVE)
|
|
1340
1549
|
return r > 0 ? r : 0
|
|
1341
1550
|
}
|
|
1342
1551
|
|
|
@@ -1354,7 +1563,7 @@ export class Http1Connection {
|
|
|
1354
1563
|
if (this.writeHeld) {
|
|
1355
1564
|
// Half the output free before the program is asked to write again, so
|
|
1356
1565
|
// that a slow reader is not answered one small write at a time.
|
|
1357
|
-
if (this.bodyRoom() * 2 < this.config.outputSize - H1_CONTROL_RESERVE) {
|
|
1566
|
+
if (toI64(this.bodyRoom()) * toI64(2) < toI64(this.config.outputSize) - toI64(H1_CONTROL_RESERVE)) {
|
|
1358
1567
|
return H1_NEED_MORE
|
|
1359
1568
|
}
|
|
1360
1569
|
this.writeHeld = false
|
|
@@ -1640,12 +1849,17 @@ export class Http1Connection {
|
|
|
1640
1849
|
// program whose heads differ pays this test and no more.
|
|
1641
1850
|
if (!interim && this.date.formats() !== this.headOff) {
|
|
1642
1851
|
if (this.sameHead(status, names, values, framing, code)) {
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
this.
|
|
1852
|
+
// `bodyRoom` stops short of the output's end, so the copy fits; a
|
|
1853
|
+
// copy that did not would write the head out afresh below.
|
|
1854
|
+
if (
|
|
1855
|
+
this.headLength <= this.bodyRoom() &&
|
|
1856
|
+
tryCopyBytes(this.output, this.outputEnd, this.headBytes, H1_ZERO, this.headLength)
|
|
1857
|
+
) {
|
|
1858
|
+
this.outputEnd = http1ServerAdd(this.outputEnd, this.headLength)
|
|
1647
1859
|
this.headMisses = 0
|
|
1648
|
-
|
|
1860
|
+
// A count past 2^31 - 2 heads stays there: no server lives that long.
|
|
1861
|
+
const copies: i64 = this.headCopies
|
|
1862
|
+
this.headCopies = copies < 2147483646 ? copies + 1 : copies
|
|
1649
1863
|
return this.begin(close, framing, empty)
|
|
1650
1864
|
}
|
|
1651
1865
|
}
|
|
@@ -1673,9 +1887,9 @@ export class Http1Connection {
|
|
|
1673
1887
|
if (end === HTTP1_REFUSED) {
|
|
1674
1888
|
return H1_INVALID
|
|
1675
1889
|
}
|
|
1676
|
-
if (end < 0 || end
|
|
1890
|
+
if (end < 0 || http1ServerSub(end, start) > room) {
|
|
1677
1891
|
// The head was not written, or ate into the reserve: take it back.
|
|
1678
|
-
if (end
|
|
1892
|
+
if (http1ServerSub(end, start) > http1ServerSub(this.config.outputSize, H1_CONTROL_RESERVE)) {
|
|
1679
1893
|
return H1_INVALID
|
|
1680
1894
|
}
|
|
1681
1895
|
if (start === 0) {
|
|
@@ -1690,8 +1904,10 @@ export class Http1Connection {
|
|
|
1690
1904
|
}
|
|
1691
1905
|
if (this.date.formats() !== this.headOff) {
|
|
1692
1906
|
// A head written here is a miss: the kept one did not match.
|
|
1693
|
-
this.headMisses
|
|
1694
|
-
|
|
1907
|
+
const was: i32 = this.headMisses
|
|
1908
|
+
const misses: i32 = was < H1_HEAD_MISSES ? was + 1 : H1_HEAD_MISSES
|
|
1909
|
+
this.headMisses = misses
|
|
1910
|
+
if (misses < H1_HEAD_MISSES) {
|
|
1695
1911
|
this.keepHead(start, end, status, reason, names, values, framing, code)
|
|
1696
1912
|
} else {
|
|
1697
1913
|
this.headMisses = 0
|
|
@@ -1736,17 +1952,24 @@ export class Http1Connection {
|
|
|
1736
1952
|
const limit: i32 = this.headLength
|
|
1737
1953
|
let at: i32 = this.headFieldsAt
|
|
1738
1954
|
const lengths: i32[] = this.headLengths
|
|
1739
|
-
|
|
1955
|
+
const kinds: i32 = toI32(lengths.length)
|
|
1956
|
+
// `count` is both arrays' length; testing each proves every access, and
|
|
1957
|
+
// `j` walks `lengths` two to a field, inside it.
|
|
1958
|
+
let j: i32 = 0
|
|
1740
1959
|
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length) && at >= 0; i += 1) {
|
|
1960
|
+
if (j < 0 || j >= kinds - 1) {
|
|
1961
|
+
return false
|
|
1962
|
+
}
|
|
1741
1963
|
// Both lengths, so that a value cannot match the kept bytes past its
|
|
1742
1964
|
// own field and pass for the kept head without its text checks.
|
|
1743
1965
|
const name: u8[] = names[i]
|
|
1744
1966
|
const value: u8[] = values[i]
|
|
1745
|
-
if (toI32(name.length) !== lengths[
|
|
1967
|
+
if (toI32(name.length) !== lengths[j] || toI32(value.length) !== lengths[j + 1]) {
|
|
1746
1968
|
return false
|
|
1747
1969
|
}
|
|
1748
1970
|
at = http1ServerKept(kept, at, limit, name, 58)
|
|
1749
1971
|
at = http1ServerKept(kept, at, limit, value, 13)
|
|
1972
|
+
j += 2
|
|
1750
1973
|
}
|
|
1751
1974
|
return at >= 0
|
|
1752
1975
|
}
|
|
@@ -1776,7 +1999,7 @@ export class Http1Connection {
|
|
|
1776
1999
|
framing: i32,
|
|
1777
2000
|
connection: i32
|
|
1778
2001
|
): void {
|
|
1779
|
-
const n: i32 = end
|
|
2002
|
+
const n: i32 = http1ServerSub(end, start)
|
|
1780
2003
|
const fields: i32 = toI32(names.length)
|
|
1781
2004
|
const again: boolean =
|
|
1782
2005
|
status === this.headStatus && connection === this.headConnection && fields === this.headFields
|
|
@@ -1785,25 +2008,41 @@ export class Http1Connection {
|
|
|
1785
2008
|
this.headConnection = connection
|
|
1786
2009
|
this.headFields = fields
|
|
1787
2010
|
this.headLength = 0
|
|
1788
|
-
if (n > H1_HEAD_KEEP || fields > H1_HEAD_FIELDS) {
|
|
2011
|
+
if (n < 0 || n > H1_HEAD_KEEP || fields > H1_HEAD_FIELDS) {
|
|
1789
2012
|
return
|
|
1790
2013
|
}
|
|
1791
2014
|
if (!again) {
|
|
1792
2015
|
return
|
|
1793
2016
|
}
|
|
1794
2017
|
if (toI32(this.headBytes.length) < n) {
|
|
1795
|
-
|
|
2018
|
+
// `n` is at most `H1_HEAD_KEEP`, so the rounded length is a `u16`.
|
|
2019
|
+
// A power of two: rounding up is adding one short of it and masking.
|
|
2020
|
+
this.headBytes = new Array<u8>(
|
|
2021
|
+
toU16((toU32(n) + toU32(H1_HEAD_ROUND) - toU32(1)) & (toU32(0) - toU32(H1_HEAD_ROUND)))
|
|
2022
|
+
)
|
|
2023
|
+
}
|
|
2024
|
+
// `fields` is at most `H1_HEAD_FIELDS`, so twice it is a `u16`.
|
|
2025
|
+
const kinds: u16 = toU16(toU32(fields) * toU32(2))
|
|
2026
|
+
if (toI32(this.headLengths.length) < toI32(kinds)) {
|
|
2027
|
+
this.headLengths = new Array<i32>(kinds)
|
|
1796
2028
|
}
|
|
1797
|
-
|
|
1798
|
-
|
|
2029
|
+
const lengths: i32[] = this.headLengths
|
|
2030
|
+
const room: i32 = toI32(lengths.length)
|
|
2031
|
+
let j: i32 = 0
|
|
2032
|
+
for (let i: i32 = 0; i < fields && i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
2033
|
+
if (j < 0 || j >= room - 1) {
|
|
2034
|
+
break
|
|
2035
|
+
}
|
|
2036
|
+
lengths[j] = toI32(names[i].length)
|
|
2037
|
+
lengths[j + 1] = toI32(values[i].length)
|
|
2038
|
+
j += 2
|
|
1799
2039
|
}
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
2040
|
+
// A head that will not copy is not kept: `headLength` stays 0.
|
|
2041
|
+
if (!tryCopyBytes(this.headBytes, H1_ZERO, this.output, start, n)) {
|
|
2042
|
+
return
|
|
1803
2043
|
}
|
|
1804
|
-
copyBytes(this.headBytes, H1_ZERO, this.output, start, n)
|
|
1805
2044
|
// The field lines start past "HTTP/1.1 NNN " reason CRLF.
|
|
1806
|
-
this.headFieldsAt = 15
|
|
2045
|
+
this.headFieldsAt = http1ServerAdd(15, toI32(reason.length))
|
|
1807
2046
|
this.headLength = n
|
|
1808
2047
|
this.headDate = this.date
|
|
1809
2048
|
this.headFormats = this.date.formats()
|
|
@@ -1839,8 +2078,9 @@ export class Http1Connection {
|
|
|
1839
2078
|
const room: i32 = this.bodyRoom()
|
|
1840
2079
|
let take: i32 = len
|
|
1841
2080
|
if (chunked) {
|
|
1842
|
-
|
|
1843
|
-
|
|
2081
|
+
const fits: i32 = http1ServerSub(room, H1_CHUNK_OVERHEAD)
|
|
2082
|
+
if (take > fits) {
|
|
2083
|
+
take = fits
|
|
1844
2084
|
}
|
|
1845
2085
|
} else if (take > room) {
|
|
1846
2086
|
take = room
|
|
@@ -1854,7 +2094,7 @@ export class Http1Connection {
|
|
|
1854
2094
|
} else {
|
|
1855
2095
|
this.outputEnd = http1ServerCopy(this.output, this.outputEnd, buf, off, take)
|
|
1856
2096
|
if (this.responseFraming >= 0) {
|
|
1857
|
-
this.responseLeft = this.responseLeft
|
|
2097
|
+
this.responseLeft = http1ServerSub(this.responseLeft, take)
|
|
1858
2098
|
}
|
|
1859
2099
|
}
|
|
1860
2100
|
if (take < len) {
|
|
@@ -1901,8 +2141,17 @@ export class Http1Connection {
|
|
|
1901
2141
|
) {
|
|
1902
2142
|
return this.write(buf, off, len)
|
|
1903
2143
|
}
|
|
1904
|
-
const
|
|
1905
|
-
const
|
|
2144
|
+
const start: i32 = this.outputStart
|
|
2145
|
+
const end: i32 = this.outputEnd
|
|
2146
|
+
// `0 <= start <= end <= output.length` holds for every queued window; one
|
|
2147
|
+
// that broke it would make `netWrite2` panic, so it ends the connection.
|
|
2148
|
+
// `buf`'s window is checked above.
|
|
2149
|
+
if (start < 0 || start > end || end > toI32(this.output.length)) {
|
|
2150
|
+
this.abort()
|
|
2151
|
+
return H1_CLOSED
|
|
2152
|
+
}
|
|
2153
|
+
const pending: i32 = http1ServerSub(end, start)
|
|
2154
|
+
const n: i32 = netWrite2(this.fd, this.output, start, pending, buf, off, len)
|
|
1906
2155
|
if (n < 0 && n !== H1_AGAIN) {
|
|
1907
2156
|
this.abort()
|
|
1908
2157
|
return H1_CLOSED
|
|
@@ -1912,13 +2161,13 @@ export class Http1Connection {
|
|
|
1912
2161
|
if (n > 0) {
|
|
1913
2162
|
this.lastActive = monotonicNanos()
|
|
1914
2163
|
this.consume(n)
|
|
1915
|
-
sent = n > pending ? n
|
|
1916
|
-
this.responseLeft = this.responseLeft
|
|
2164
|
+
sent = n > pending ? http1ServerSub(n, pending) : 0
|
|
2165
|
+
this.responseLeft = http1ServerSub(this.responseLeft, sent)
|
|
1917
2166
|
}
|
|
1918
2167
|
if (sent < len) {
|
|
1919
|
-
const copied: i32 = this.write(buf, off
|
|
2168
|
+
const copied: i32 = this.write(buf, http1ServerAdd(off, sent), http1ServerSub(len, sent))
|
|
1920
2169
|
if (copied > 0) {
|
|
1921
|
-
sent = sent
|
|
2170
|
+
sent = http1ServerAdd(sent, copied)
|
|
1922
2171
|
} else if (sent === 0) {
|
|
1923
2172
|
return copied
|
|
1924
2173
|
}
|
|
@@ -2000,11 +2249,14 @@ export class Http1Connection {
|
|
|
2000
2249
|
const parser: Http1Parser = this.parser
|
|
2001
2250
|
const i: i32 = parser.headerIndex("accept-encoding")
|
|
2002
2251
|
const spans: i32[] = parser.spans
|
|
2003
|
-
|
|
2252
|
+
// Widened: a header's value span is `spans[4i + 2 .. 4i + 3]`, tested
|
|
2253
|
+
// against the array before either is read.
|
|
2254
|
+
const k: i32 = toI32(toI64(i) * toI64(4) + toI64(2))
|
|
2255
|
+
if (i < 0 || k < 0 || k >= toI32(spans.length) - 1) {
|
|
2004
2256
|
return HTTP_CODING_IDENTITY
|
|
2005
2257
|
}
|
|
2006
|
-
const start: i32 = spans[
|
|
2007
|
-
const end: i32 = spans[
|
|
2258
|
+
const start: i32 = spans[k]
|
|
2259
|
+
const end: i32 = spans[k + 1]
|
|
2008
2260
|
if (start < 0 || start > end || end > toI32(parser.head.length)) {
|
|
2009
2261
|
return HTTP_CODING_IDENTITY
|
|
2010
2262
|
}
|
|
@@ -2049,8 +2301,10 @@ export class Http1Connection {
|
|
|
2049
2301
|
* The whole response is written or none of it: `H1_AGAIN` when the output has
|
|
2050
2302
|
* no room for it yet (`H1_WRITE` says when to try again, compressing again),
|
|
2051
2303
|
* `H1_INVALID` for a response larger than the output could ever hold or one
|
|
2052
|
-
* `respond` refuses, `H1_CLOSED` with no request to answer
|
|
2053
|
-
*
|
|
2304
|
+
* `respond` refuses, `H1_CLOSED` with no request to answer (or, never for a
|
|
2305
|
+
* body the room was checked for, when `write` takes less than all of it, which
|
|
2306
|
+
* aborts the connection); 0 once it is queued. A window outside `body` is
|
|
2307
|
+
* `H1_INVALID` too, before anything is queued.
|
|
2054
2308
|
*
|
|
2055
2309
|
* @param status the HTTP status code to send, as `respond` takes it
|
|
2056
2310
|
* @param names the program's field names, without `Content-Encoding` (or `Vary` under `enc.vary`)
|
|
@@ -2072,7 +2326,9 @@ export class Http1Connection {
|
|
|
2072
2326
|
enc: HttpCompressor,
|
|
2073
2327
|
coding: i32
|
|
2074
2328
|
): i32 {
|
|
2075
|
-
|
|
2329
|
+
if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(body.length)) {
|
|
2330
|
+
return H1_INVALID
|
|
2331
|
+
}
|
|
2076
2332
|
if (!this.answerable()) {
|
|
2077
2333
|
return H1_CLOSED
|
|
2078
2334
|
}
|
|
@@ -2098,7 +2354,7 @@ export class Http1Connection {
|
|
|
2098
2354
|
if (r !== 0) {
|
|
2099
2355
|
return r
|
|
2100
2356
|
}
|
|
2101
|
-
queued = this.outputEnd - outputEnd + len <= enc.segmentSize(mss)
|
|
2357
|
+
queued = toI64(this.outputEnd) - toI64(outputEnd) + toI64(len) <= toI64(enc.segmentSize(mss))
|
|
2102
2358
|
if (!queued) {
|
|
2103
2359
|
this.unrespond(outputEnd, closeAfter)
|
|
2104
2360
|
}
|
|
@@ -2118,9 +2374,9 @@ export class Http1Connection {
|
|
|
2118
2374
|
}
|
|
2119
2375
|
if (packed >= 0 && enc.segmentMode && !required) {
|
|
2120
2376
|
// The head just written, and the one the body as it is would get.
|
|
2121
|
-
const head: i32 = this.outputEnd
|
|
2377
|
+
const head: i32 = http1ServerSub(this.outputEnd, outputEnd)
|
|
2122
2378
|
const plain: i32 = enc.http1IdentityHead(head, packed, len)
|
|
2123
|
-
if (!enc.fewerSegments(plain
|
|
2379
|
+
if (!enc.fewerSegments(http1ServerAdd(plain, len), http1ServerAdd(head, packed), mss)) {
|
|
2124
2380
|
this.unrespond(outputEnd, closeAfter)
|
|
2125
2381
|
enc.identityFields(names, values)
|
|
2126
2382
|
packed = -1
|
|
@@ -2132,7 +2388,9 @@ export class Http1Connection {
|
|
|
2132
2388
|
}
|
|
2133
2389
|
}
|
|
2134
2390
|
if (!this.responseEmpty && n > this.bodyRoom()) {
|
|
2135
|
-
const fits: boolean =
|
|
2391
|
+
const fits: boolean =
|
|
2392
|
+
toI64(this.outputEnd) - toI64(outputEnd) + toI64(n) <=
|
|
2393
|
+
toI64(this.config.outputSize) - toI64(H1_CONTROL_RESERVE)
|
|
2136
2394
|
this.unrespond(outputEnd, closeAfter)
|
|
2137
2395
|
if (!fits) {
|
|
2138
2396
|
return H1_INVALID
|
|
@@ -2142,7 +2400,11 @@ export class Http1Connection {
|
|
|
2142
2400
|
}
|
|
2143
2401
|
const w: i32 = packed >= 0 ? this.write(enc.output, toI32(0), n) : this.write(body, off, n)
|
|
2144
2402
|
if (w !== n) {
|
|
2145
|
-
|
|
2403
|
+
// `n` fit `bodyRoom` above, so `write` takes all of it; were it short,
|
|
2404
|
+
// the head would promise bytes the output does not hold, and the
|
|
2405
|
+
// connection is dropped rather than sent a body cut short.
|
|
2406
|
+
this.abort()
|
|
2407
|
+
return H1_CLOSED
|
|
2146
2408
|
}
|
|
2147
2409
|
return this.end()
|
|
2148
2410
|
}
|
|
@@ -2177,11 +2439,13 @@ export class Http1Connection {
|
|
|
2177
2439
|
const p: Http1Parser = this.parser
|
|
2178
2440
|
const field: i32 = websocketRequestKeyField(p)
|
|
2179
2441
|
const spans: i32[] = p.spans
|
|
2180
|
-
|
|
2442
|
+
// Widened as in `acceptEncodingOf`: the key's span is `spans[4f + 2 .. 4f + 3]`.
|
|
2443
|
+
const k: i32 = toI32(toI64(field) * toI64(4) + toI64(2))
|
|
2444
|
+
if (field < 0 || k < 0 || k >= toI32(spans.length) - 1) {
|
|
2181
2445
|
return H1_INVALID
|
|
2182
2446
|
}
|
|
2183
2447
|
const accept: WsAcceptKey = this.acceptStorage()
|
|
2184
|
-
if (!accept.compute(p.head, spans[
|
|
2448
|
+
if (!accept.compute(p.head, spans[k], spans[k + 1])) {
|
|
2185
2449
|
return H1_INVALID
|
|
2186
2450
|
}
|
|
2187
2451
|
// Written past `outputEnd`, where nothing live is, and kept only when it
|
|
@@ -2191,7 +2455,7 @@ export class Http1Connection {
|
|
|
2191
2455
|
if (end === HTTP1_REFUSED) {
|
|
2192
2456
|
return H1_INVALID
|
|
2193
2457
|
}
|
|
2194
|
-
if (end < 0 || end
|
|
2458
|
+
if (end < 0 || http1ServerSub(end, this.outputEnd) > room) {
|
|
2195
2459
|
this.writeHeld = true
|
|
2196
2460
|
return H1_AGAIN
|
|
2197
2461
|
}
|
|
@@ -2233,7 +2497,10 @@ export class Http1Connection {
|
|
|
2233
2497
|
if (this.state !== H1_S_WEBSOCKET) {
|
|
2234
2498
|
return H1_CLOSED
|
|
2235
2499
|
}
|
|
2236
|
-
if (
|
|
2500
|
+
if (
|
|
2501
|
+
opcode === WS_OP_CLOSE ||
|
|
2502
|
+
toI64(len) > toI64(this.config.outputSize) - toI64(H1_CONTROL_RESERVE) - toI64(14)
|
|
2503
|
+
) {
|
|
2237
2504
|
return H1_INVALID
|
|
2238
2505
|
}
|
|
2239
2506
|
if (websocketFrameSize(len, false) > this.bodyRoom()) {
|
|
@@ -2280,7 +2547,7 @@ export class Http1Connection {
|
|
|
2280
2547
|
|
|
2281
2548
|
/** Whether nothing has moved for longer than the idle timeout, at `now` (`monotonicNanos`). */
|
|
2282
2549
|
private idle(now: i64): boolean {
|
|
2283
|
-
return now
|
|
2550
|
+
return http1ServerPast(now, this.lastActive, this.config.idleTimeout)
|
|
2284
2551
|
}
|
|
2285
2552
|
|
|
2286
2553
|
/**
|
|
@@ -2317,7 +2584,7 @@ export class Http1Connection {
|
|
|
2317
2584
|
|
|
2318
2585
|
/** Whether a head has been arriving for longer than `headTimeout` at `now` (`monotonicNanos`). */
|
|
2319
2586
|
private headLate(now: i64): boolean {
|
|
2320
|
-
return this.headTimed && now
|
|
2587
|
+
return this.headTimed && http1ServerPast(now, this.headSince, this.config.headTimeout)
|
|
2321
2588
|
}
|
|
2322
2589
|
|
|
2323
2590
|
/**
|
|
@@ -2364,13 +2631,13 @@ export class Http1Connection {
|
|
|
2364
2631
|
* @internal Called by `Http1Server` in this module as it drains a refused slot.
|
|
2365
2632
|
*/
|
|
2366
2633
|
drained(n: i32): boolean {
|
|
2367
|
-
this.lingered = this.lingered
|
|
2634
|
+
this.lingered = http1ServerAdd(this.lingered, n)
|
|
2368
2635
|
return this.lingered < this.config.lingerBytes
|
|
2369
2636
|
}
|
|
2370
2637
|
|
|
2371
2638
|
/** Whether the lingering close has lasted longer than `lingerTimeout` at `now` (`monotonicNanos`). */
|
|
2372
2639
|
private lingerLate(now: i64): boolean {
|
|
2373
|
-
return this.lingered >= 0 && now
|
|
2640
|
+
return this.lingered >= 0 && http1ServerPast(now, this.lingerSince, this.config.lingerTimeout)
|
|
2374
2641
|
}
|
|
2375
2642
|
|
|
2376
2643
|
/**
|
|
@@ -2397,17 +2664,17 @@ export class Http1Connection {
|
|
|
2397
2664
|
* @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to skip the slots that cannot expire yet.
|
|
2398
2665
|
*/
|
|
2399
2666
|
expiresNoSooner(now: i64): i64 {
|
|
2400
|
-
const idle: i64 = this.lastActive
|
|
2667
|
+
const idle: i64 = http1ServerAfter(this.lastActive, this.config.idleTimeout)
|
|
2401
2668
|
let start: i64 = now
|
|
2402
2669
|
if (this.headTimed) {
|
|
2403
2670
|
start = this.headSince
|
|
2404
2671
|
} else if (this.requestOpen) {
|
|
2405
2672
|
start = this.lastActive
|
|
2406
2673
|
}
|
|
2407
|
-
const head: i64 = start
|
|
2674
|
+
const head: i64 = http1ServerAfter(start, this.config.headTimeout)
|
|
2408
2675
|
const sooner: i64 = head < idle ? head : idle
|
|
2409
2676
|
if (this.lingered >= 0) {
|
|
2410
|
-
const linger: i64 = this.lingerSince
|
|
2677
|
+
const linger: i64 = http1ServerAfter(this.lingerSince, this.config.lingerTimeout)
|
|
2411
2678
|
return linger < sooner ? linger : sooner
|
|
2412
2679
|
}
|
|
2413
2680
|
return sooner
|
|
@@ -2420,7 +2687,7 @@ export class Http1Connection {
|
|
|
2420
2687
|
*/
|
|
2421
2688
|
const http1ExpireFloor = (config: Http1Config): i64 => {
|
|
2422
2689
|
const shorter: i32 = config.headTimeout < config.idleTimeout ? config.headTimeout : config.idleTimeout
|
|
2423
|
-
return monotonicNanos()
|
|
2690
|
+
return http1ServerAfter(monotonicNanos(), shorter)
|
|
2424
2691
|
}
|
|
2425
2692
|
|
|
2426
2693
|
// ---- Carriers -------------------------------------------------------------------
|
|
@@ -2531,7 +2798,7 @@ export class Http1Server {
|
|
|
2531
2798
|
this.inputs = new Http1OutputPool(HTTP1_FIRST_INPUT)
|
|
2532
2799
|
this.heads = new Http1OutputPool(HTTP1_FIRST_HEAD)
|
|
2533
2800
|
this.date = new HttpDate()
|
|
2534
|
-
this.scratch =
|
|
2801
|
+
this.scratch = http1ServerBytes(config.chunkSize)
|
|
2535
2802
|
this.peer = new Array<u8>(H1_ADDRESS_SIZE)
|
|
2536
2803
|
}
|
|
2537
2804
|
|
|
@@ -2562,13 +2829,15 @@ export class Http1Server {
|
|
|
2562
2829
|
* @returns the count of slots accepted into and not yet closed
|
|
2563
2830
|
*/
|
|
2564
2831
|
busy(): i32 {
|
|
2565
|
-
|
|
2832
|
+
// Counted in u32, which wraps where an i32 would be a site: there are no
|
|
2833
|
+
// more busy slots than `fds` has entries, so the count fits an i32.
|
|
2834
|
+
let n: u32 = 0
|
|
2566
2835
|
for (const fd of this.fds) {
|
|
2567
2836
|
if (fd >= 0) {
|
|
2568
|
-
n = n + 1
|
|
2837
|
+
n = n + toU32(1)
|
|
2569
2838
|
}
|
|
2570
2839
|
}
|
|
2571
|
-
return n
|
|
2840
|
+
return toI32(n)
|
|
2572
2841
|
}
|
|
2573
2842
|
|
|
2574
2843
|
/**
|
|
@@ -2588,7 +2857,12 @@ export class Http1Server {
|
|
|
2588
2857
|
* @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
|
|
2589
2858
|
*/
|
|
2590
2859
|
fd(slot: i32): i32 {
|
|
2591
|
-
|
|
2860
|
+
const fds: i32[] = this.fds
|
|
2861
|
+
if (slot < 0 || slot >= toI32(fds.length)) {
|
|
2862
|
+
return -1
|
|
2863
|
+
}
|
|
2864
|
+
const fd: i32 = fds[slot]
|
|
2865
|
+
return fd >= 0 ? fd : -1
|
|
2592
2866
|
}
|
|
2593
2867
|
|
|
2594
2868
|
/**
|
|
@@ -2603,10 +2877,12 @@ export class Http1Server {
|
|
|
2603
2877
|
* @returns the slot's connection, which the pool keeps; read its events and write its response through it
|
|
2604
2878
|
*/
|
|
2605
2879
|
connection(slot: i32): Http1Connection {
|
|
2606
|
-
|
|
2607
|
-
|
|
2880
|
+
const conn: Http1Connection | null = this.attach(slot)
|
|
2881
|
+
if (conn !== null) {
|
|
2882
|
+
return conn
|
|
2608
2883
|
}
|
|
2609
|
-
|
|
2884
|
+
const connections: Http1Connection[] = this.connections
|
|
2885
|
+
return connections.length > 0 ? connections[0] : this.build()
|
|
2610
2886
|
}
|
|
2611
2887
|
|
|
2612
2888
|
/**
|
|
@@ -2619,7 +2895,8 @@ export class Http1Server {
|
|
|
2619
2895
|
* @returns true when the slot is accepted into and resting; false when it is free, out of range or busy
|
|
2620
2896
|
*/
|
|
2621
2897
|
resting(slot: i32): boolean {
|
|
2622
|
-
|
|
2898
|
+
const at: i32[] = this.at
|
|
2899
|
+
return this.holds(slot) && slot >= 0 && slot < toI32(at.length) && at[slot] < 0
|
|
2623
2900
|
}
|
|
2624
2901
|
|
|
2625
2902
|
/**
|
|
@@ -2631,40 +2908,110 @@ export class Http1Server {
|
|
|
2631
2908
|
return toI32(this.spare.length)
|
|
2632
2909
|
}
|
|
2633
2910
|
|
|
2634
|
-
/**
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
const
|
|
2641
|
-
if (
|
|
2642
|
-
|
|
2911
|
+
/**
|
|
2912
|
+
* The connection `slot` holds now, without giving a resting slot one: null
|
|
2913
|
+
* for a resting slot, and for one that is free or out of range.
|
|
2914
|
+
*/
|
|
2915
|
+
private lent(slot: i32): Http1Connection | null {
|
|
2916
|
+
const fds: i32[] = this.fds
|
|
2917
|
+
const at: i32[] = this.at
|
|
2918
|
+
if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(at.length) || fds[slot] < 0) {
|
|
2919
|
+
return null
|
|
2643
2920
|
}
|
|
2644
|
-
const
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2921
|
+
const k: i32 = at[slot]
|
|
2922
|
+
const connections: Http1Connection[] = this.connections
|
|
2923
|
+
return k >= 0 && k < toI32(connections.length) ? connections[k] : null
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2926
|
+
/**
|
|
2927
|
+
* `slot`'s connection, given one when it rests, carrying its descriptor,
|
|
2928
|
+
* `lastActive` and MSS; null for a slot that is free or out of range, so
|
|
2929
|
+
* a caller that checked neither answers it as closed.
|
|
2930
|
+
*/
|
|
2931
|
+
private attach(slot: i32): Http1Connection | null {
|
|
2932
|
+
const fds: i32[] = this.fds
|
|
2933
|
+
const at: i32[] = this.at
|
|
2934
|
+
const rested: i64[] = this.rested
|
|
2935
|
+
const restedMss: i32[] = this.restedMss
|
|
2936
|
+
const owner: i64[] = this.owner
|
|
2937
|
+
// `accept` grows the five together, so a slot it answered is in range of each.
|
|
2938
|
+
if (
|
|
2939
|
+
slot < 0 ||
|
|
2940
|
+
slot >= toI32(fds.length) ||
|
|
2941
|
+
slot >= toI32(at.length) ||
|
|
2942
|
+
slot >= toI32(rested.length) ||
|
|
2943
|
+
slot >= toI32(restedMss.length) ||
|
|
2944
|
+
slot >= toI32(owner.length) ||
|
|
2945
|
+
fds[slot] < 0
|
|
2946
|
+
) {
|
|
2947
|
+
return null
|
|
2948
|
+
}
|
|
2949
|
+
const held: Http1Connection | null = this.lent(slot)
|
|
2950
|
+
if (held !== null) {
|
|
2951
|
+
return held
|
|
2952
|
+
}
|
|
2953
|
+
const fd: i32 = fds[slot]
|
|
2954
|
+
const lastActive: i64 = rested[slot]
|
|
2955
|
+
const mss: i32 = restedMss[slot]
|
|
2956
|
+
const serial: i64 = owner[slot]
|
|
2957
|
+
const spare: i32[] = this.spare
|
|
2958
|
+
const connections: Http1Connection[] = this.connections
|
|
2959
|
+
const top: i32 = toI32(spare.length) - 1
|
|
2960
|
+
let k: i32 = -1
|
|
2961
|
+
if (top >= 0 && top < toI32(spare.length) && spare.length > 0) {
|
|
2962
|
+
k = spare[top]
|
|
2963
|
+
spare.pop()
|
|
2964
|
+
}
|
|
2965
|
+
const taken: Http1Connection | null = k >= 0 && k < toI32(connections.length) ? connections[k] : null
|
|
2966
|
+
// No spare: a new one, at the end of `connections`.
|
|
2967
|
+
const conn: Http1Connection = taken !== null ? taken : this.build()
|
|
2968
|
+
if (taken === null) {
|
|
2969
|
+
k = toI32(this.connections.length) - 1
|
|
2970
|
+
}
|
|
2971
|
+
const lent: i32[] = this.at
|
|
2972
|
+
if (slot < toI32(lent.length)) {
|
|
2973
|
+
lent[slot] = k
|
|
2974
|
+
}
|
|
2975
|
+
conn.fd = fd
|
|
2976
|
+
conn.lastActive = lastActive
|
|
2977
|
+
conn.socketMss = mss
|
|
2978
|
+
conn.lendTo(serial)
|
|
2650
2979
|
return conn
|
|
2651
2980
|
}
|
|
2652
2981
|
|
|
2653
2982
|
/** Gives back `slot`'s connection when it rests, keeping what the slot needs of it. */
|
|
2654
2983
|
private rest(slot: i32): void {
|
|
2655
|
-
const
|
|
2656
|
-
|
|
2984
|
+
const at: i32[] = this.at
|
|
2985
|
+
const rested: i64[] = this.rested
|
|
2986
|
+
const restedMss: i32[] = this.restedMss
|
|
2987
|
+
if (
|
|
2988
|
+
slot < 0 ||
|
|
2989
|
+
slot >= toI32(at.length) ||
|
|
2990
|
+
slot >= toI32(rested.length) ||
|
|
2991
|
+
slot >= toI32(restedMss.length)
|
|
2992
|
+
) {
|
|
2993
|
+
return
|
|
2994
|
+
}
|
|
2995
|
+
const k: i32 = at[slot]
|
|
2996
|
+
const connections: Http1Connection[] = this.connections
|
|
2997
|
+
if (k < 0 || k >= toI32(connections.length)) {
|
|
2657
2998
|
return
|
|
2658
2999
|
}
|
|
2659
|
-
const conn: Http1Connection =
|
|
3000
|
+
const conn: Http1Connection = connections[k]
|
|
2660
3001
|
if (!conn.rests()) {
|
|
2661
3002
|
return
|
|
2662
3003
|
}
|
|
2663
|
-
|
|
2664
|
-
|
|
3004
|
+
const fresh: i32[] = this.at
|
|
3005
|
+
const freshRested: i64[] = this.rested
|
|
3006
|
+
const freshMss: i32[] = this.restedMss
|
|
3007
|
+
if (slot >= toI32(fresh.length) || slot >= toI32(freshRested.length) || slot >= toI32(freshMss.length)) {
|
|
3008
|
+
return
|
|
3009
|
+
}
|
|
3010
|
+
freshRested[slot] = conn.lastActive
|
|
3011
|
+
freshMss[slot] = conn.socketMss
|
|
2665
3012
|
conn.fd = -1
|
|
2666
3013
|
conn.socketMss = 0
|
|
2667
|
-
|
|
3014
|
+
fresh[slot] = -1
|
|
2668
3015
|
this.spare.push(k)
|
|
2669
3016
|
}
|
|
2670
3017
|
|
|
@@ -2685,9 +3032,11 @@ export class Http1Server {
|
|
|
2685
3032
|
// first accepts take slots 0, 1, 2 ... and a pool that never fills never
|
|
2686
3033
|
// builds its last slots.
|
|
2687
3034
|
let k: i32 = H1_POOL_FULL
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
3035
|
+
const free: i32[] = this.free
|
|
3036
|
+
const top: i32 = toI32(free.length) - 1
|
|
3037
|
+
if (top >= 0 && top < toI32(free.length) && free.length > 0) {
|
|
3038
|
+
k = free[top]
|
|
3039
|
+
free.pop()
|
|
2691
3040
|
} else if (toI32(this.fds.length) < this.slots) {
|
|
2692
3041
|
k = toI32(this.fds.length)
|
|
2693
3042
|
this.fds.push(H1_FREE)
|
|
@@ -2715,7 +3064,9 @@ export class Http1Server {
|
|
|
2715
3064
|
this.at[k] = -1
|
|
2716
3065
|
this.rested[k] = monotonicNanos()
|
|
2717
3066
|
this.owner[k] = this.accepts
|
|
2718
|
-
|
|
3067
|
+
// A serial only keys a kept head to its peer: in u64, which wraps where
|
|
3068
|
+
// an i64 would be a site, after more accepts than a server ever sees.
|
|
3069
|
+
this.accepts = toI64(toU64(this.accepts) + toU64(1))
|
|
2719
3070
|
// Once a connection, not a request, so a keep-alive request costs no
|
|
2720
3071
|
// syscall: a path whose MSS moves after the handshake is not followed.
|
|
2721
3072
|
const mss: i32 = netMss(fd)
|
|
@@ -2733,12 +3084,12 @@ export class Http1Server {
|
|
|
2733
3084
|
* @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
|
|
2734
3085
|
*/
|
|
2735
3086
|
readable(slot: i32): i32 {
|
|
2736
|
-
|
|
3087
|
+
const conn: Http1Connection | null = this.attach(slot)
|
|
3088
|
+
if (conn === null) {
|
|
2737
3089
|
return H1_DONE
|
|
2738
3090
|
}
|
|
2739
3091
|
this.date.hold()
|
|
2740
|
-
const
|
|
2741
|
-
const fd: i32 = this.fds[slot]
|
|
3092
|
+
const fd: i32 = conn.fd
|
|
2742
3093
|
let moved: boolean = false
|
|
2743
3094
|
while (true) {
|
|
2744
3095
|
const room: i32 = conn.inputRoom()
|
|
@@ -2801,17 +3152,17 @@ export class Http1Server {
|
|
|
2801
3152
|
if (!this.holds(slot)) {
|
|
2802
3153
|
return H1_ERROR
|
|
2803
3154
|
}
|
|
2804
|
-
|
|
3155
|
+
const conn: Http1Connection | null = this.lent(slot)
|
|
3156
|
+
if (conn === null) {
|
|
2805
3157
|
// A resting slot has read nothing since it was answered.
|
|
2806
3158
|
return H1_NEED_MORE
|
|
2807
3159
|
}
|
|
2808
|
-
const conn: Http1Connection = this.connections[this.at[slot]]
|
|
2809
3160
|
if (conn.lingering()) {
|
|
2810
3161
|
// Its `H1_ERROR` was answered before the lingering close began.
|
|
2811
3162
|
return H1_NEED_MORE
|
|
2812
3163
|
}
|
|
2813
3164
|
if (conn.writeHeld) {
|
|
2814
|
-
this.send(
|
|
3165
|
+
this.send(conn)
|
|
2815
3166
|
}
|
|
2816
3167
|
return conn.next()
|
|
2817
3168
|
}
|
|
@@ -2847,28 +3198,32 @@ export class Http1Server {
|
|
|
2847
3198
|
// Closed with those bytes unread, the socket would answer them with a
|
|
2848
3199
|
// reset that can destroy the response before the client reads it (H1-2).
|
|
2849
3200
|
const now: i64 = monotonicNanos()
|
|
2850
|
-
const conn: Http1Connection = this.attach(slot)
|
|
2851
|
-
if (!conn.lingerStart(now) || netShutdown(
|
|
3201
|
+
const conn: Http1Connection | null = this.attach(slot)
|
|
3202
|
+
if (conn === null || !conn.lingerStart(now) || netShutdown(conn.fd, H1_SHUT_WRITE) < 0) {
|
|
2852
3203
|
return H1_DONE
|
|
2853
3204
|
}
|
|
2854
3205
|
const deadline: i64 = conn.expiresNoSooner(now)
|
|
2855
3206
|
if (deadline < this.expireAfter) {
|
|
2856
3207
|
this.expireAfter = deadline
|
|
2857
3208
|
}
|
|
2858
|
-
return this.drain(
|
|
3209
|
+
return this.drain(conn)
|
|
2859
3210
|
}
|
|
2860
3211
|
|
|
2861
3212
|
/**
|
|
2862
|
-
* Reads and throws away what `
|
|
3213
|
+
* Reads and throws away what `conn`'s lingering peer sends until the
|
|
2863
3214
|
* socket would block, answering `H1_WANT_READ`; `H1_DONE` at its end of
|
|
2864
3215
|
* stream, an error, or past `lingerBytes`. What it drains is not activity:
|
|
2865
3216
|
* `lastActive` stays, and `expire` closes the slot past `lingerTimeout`.
|
|
2866
3217
|
*/
|
|
2867
|
-
private drain(
|
|
2868
|
-
const
|
|
2869
|
-
const fd: i32 = this.fds[slot]
|
|
3218
|
+
private drain(conn: Http1Connection): i32 {
|
|
3219
|
+
const fd: i32 = conn.fd
|
|
2870
3220
|
while (true) {
|
|
2871
|
-
const
|
|
3221
|
+
const size: i32 = toI32(this.scratch.length)
|
|
3222
|
+
// `0 <= 0 <= size <= scratch.length`: a scratch with no room drains nothing.
|
|
3223
|
+
if (size <= 0) {
|
|
3224
|
+
return H1_DONE
|
|
3225
|
+
}
|
|
3226
|
+
const n: i32 = netRead(fd, this.scratch, H1_ZERO, size)
|
|
2872
3227
|
if (n === H1_AGAIN) {
|
|
2873
3228
|
return H1_WANT_READ
|
|
2874
3229
|
}
|
|
@@ -2883,15 +3238,15 @@ export class Http1Server {
|
|
|
2883
3238
|
if (!this.holds(slot)) {
|
|
2884
3239
|
return H1_DONE
|
|
2885
3240
|
}
|
|
2886
|
-
|
|
3241
|
+
const conn: Http1Connection | null = this.lent(slot)
|
|
3242
|
+
if (conn === null) {
|
|
2887
3243
|
// A resting slot holds nothing to send and has room to read.
|
|
2888
3244
|
return H1_WANT_READ
|
|
2889
3245
|
}
|
|
2890
|
-
const conn: Http1Connection = this.connections[this.at[slot]]
|
|
2891
3246
|
if (conn.lingering()) {
|
|
2892
|
-
return this.drain(
|
|
3247
|
+
return this.drain(conn)
|
|
2893
3248
|
}
|
|
2894
|
-
this.send(
|
|
3249
|
+
this.send(conn)
|
|
2895
3250
|
if (conn.isDone()) {
|
|
2896
3251
|
return H1_DONE
|
|
2897
3252
|
}
|
|
@@ -2911,13 +3266,20 @@ export class Http1Server {
|
|
|
2911
3266
|
return wants
|
|
2912
3267
|
}
|
|
2913
3268
|
|
|
2914
|
-
/** Writes what
|
|
2915
|
-
private send(
|
|
2916
|
-
const
|
|
2917
|
-
const
|
|
2918
|
-
const pending: i32 = conn.outputEnd - conn.outputStart
|
|
3269
|
+
/** Writes what a slot's connection `conn` holds until the socket would block. */
|
|
3270
|
+
private send(conn: Http1Connection): void {
|
|
3271
|
+
const fd: i32 = conn.fd
|
|
3272
|
+
const pending: i32 = http1ServerSub(conn.outputEnd, conn.outputStart)
|
|
2919
3273
|
while (conn.wantsWrite()) {
|
|
2920
|
-
const
|
|
3274
|
+
const start: i32 = conn.outputStart
|
|
3275
|
+
const end: i32 = conn.outputEnd
|
|
3276
|
+
// `0 <= start <= end <= output.length` holds for every queued window;
|
|
3277
|
+
// one that broke it would make `netWrite` panic, so it ends the connection.
|
|
3278
|
+
if (start < 0 || start > end || end > toI32(conn.output.length)) {
|
|
3279
|
+
conn.abort()
|
|
3280
|
+
break
|
|
3281
|
+
}
|
|
3282
|
+
const n: i32 = netWrite(fd, conn.output, start, http1ServerSub(end, start))
|
|
2921
3283
|
if (n <= 0) {
|
|
2922
3284
|
if (n !== H1_AGAIN) {
|
|
2923
3285
|
conn.abort()
|
|
@@ -2926,7 +3288,7 @@ export class Http1Server {
|
|
|
2926
3288
|
}
|
|
2927
3289
|
conn.consume(n)
|
|
2928
3290
|
}
|
|
2929
|
-
if (conn.outputEnd
|
|
3291
|
+
if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
|
|
2930
3292
|
conn.lastActive = monotonicNanos()
|
|
2931
3293
|
}
|
|
2932
3294
|
conn.release()
|
|
@@ -2947,10 +3309,14 @@ export class Http1Server {
|
|
|
2947
3309
|
*/
|
|
2948
3310
|
rearm(slot: i32, wants: i32): boolean {
|
|
2949
3311
|
const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
|
|
2950
|
-
if (!this.holds(slot)
|
|
3312
|
+
if (!this.holds(slot)) {
|
|
2951
3313
|
return false
|
|
2952
3314
|
}
|
|
2953
|
-
|
|
3315
|
+
const armed: i32[] = this.armed
|
|
3316
|
+
if (slot < 0 || slot >= toI32(armed.length) || armed[slot] === interest) {
|
|
3317
|
+
return false
|
|
3318
|
+
}
|
|
3319
|
+
armed[slot] = interest
|
|
2954
3320
|
return true
|
|
2955
3321
|
}
|
|
2956
3322
|
|
|
@@ -2961,18 +3327,22 @@ export class Http1Server {
|
|
|
2961
3327
|
* @param slot the slot to close; a free or unknown one is ignored
|
|
2962
3328
|
*/
|
|
2963
3329
|
close(slot: i32): void {
|
|
2964
|
-
|
|
3330
|
+
const fds: i32[] = this.fds
|
|
3331
|
+
const at: i32[] = this.at
|
|
3332
|
+
if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(at.length) || fds[slot] < 0) {
|
|
2965
3333
|
return
|
|
2966
3334
|
}
|
|
2967
|
-
|
|
2968
|
-
|
|
3335
|
+
const fd: i32 = fds[slot]
|
|
3336
|
+
const k: i32 = at[slot]
|
|
3337
|
+
fds[slot] = H1_FREE
|
|
3338
|
+
at[slot] = -1
|
|
3339
|
+
netClose(fd)
|
|
2969
3340
|
this.free.push(slot)
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
const conn: Http1Connection =
|
|
3341
|
+
const connections: Http1Connection[] = this.connections
|
|
3342
|
+
if (k >= 0 && k < toI32(connections.length)) {
|
|
3343
|
+
const conn: Http1Connection = connections[k]
|
|
2973
3344
|
conn.abort()
|
|
2974
3345
|
conn.restart()
|
|
2975
|
-
this.at[slot] = -1
|
|
2976
3346
|
this.spare.push(k)
|
|
2977
3347
|
}
|
|
2978
3348
|
}
|
|
@@ -2988,41 +3358,44 @@ export class Http1Server {
|
|
|
2988
3358
|
if (now < this.expireAfter) {
|
|
2989
3359
|
return 0
|
|
2990
3360
|
}
|
|
2991
|
-
|
|
3361
|
+
const scans: i32 = this.expireScans
|
|
3362
|
+
this.expireScans = scans < 2147483647 ? scans + 1 : scans
|
|
2992
3363
|
let soonest: i64 = http1ExpireFloor(this.config)
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3364
|
+
// Counted in u32, which wraps where an i32 would be a site: no more slots
|
|
3365
|
+
// close than there are, so the count fits an i32.
|
|
3366
|
+
let n: u32 = 0
|
|
3367
|
+
const idle: i32 = this.config.idleTimeout
|
|
3368
|
+
const head: i64 = http1ServerAfter(now, this.config.headTimeout)
|
|
3369
|
+
// `close` changes neither array's length, but each pass checks `k` again.
|
|
3370
|
+
for (let k: i32 = 0; k >= 0 && k < toI32(this.fds.length) && k < toI32(this.rested.length); k++) {
|
|
3371
|
+
const fds: i32[] = this.fds
|
|
3372
|
+
const rested: i64[] = this.rested
|
|
3373
|
+
if (k < toI32(fds.length) && k < toI32(rested.length) && fds[k] >= 0) {
|
|
3374
|
+
const since: i64 = rested[k]
|
|
3375
|
+
const conn: Http1Connection | null = this.lent(k)
|
|
3376
|
+
if (conn === null) {
|
|
3004
3377
|
// A resting slot is idle past `idleTimeout`, and a head that
|
|
3005
3378
|
// starts arriving no sooner than now has `headTimeout` from now.
|
|
3006
|
-
|
|
3007
|
-
if (now - since > idle) {
|
|
3379
|
+
if (http1ServerPast(now, since, idle)) {
|
|
3008
3380
|
this.close(k)
|
|
3009
|
-
n = n + 1
|
|
3381
|
+
n = n + toU32(1)
|
|
3010
3382
|
} else {
|
|
3011
|
-
const quiet: i64 = since
|
|
3012
|
-
|
|
3013
|
-
|
|
3383
|
+
const quiet: i64 = http1ServerAfter(since, idle)
|
|
3384
|
+
const sooner: i64 = quiet < head ? quiet : head
|
|
3385
|
+
if (sooner < soonest) {
|
|
3386
|
+
soonest = sooner
|
|
3014
3387
|
}
|
|
3015
3388
|
}
|
|
3016
|
-
} else if (
|
|
3389
|
+
} else if (conn.expired(now)) {
|
|
3017
3390
|
this.close(k)
|
|
3018
|
-
n = n + 1
|
|
3019
|
-
} else if (
|
|
3020
|
-
soonest =
|
|
3391
|
+
n = n + toU32(1)
|
|
3392
|
+
} else if (conn.expiresNoSooner(now) < soonest) {
|
|
3393
|
+
soonest = conn.expiresNoSooner(now)
|
|
3021
3394
|
}
|
|
3022
3395
|
}
|
|
3023
3396
|
}
|
|
3024
3397
|
this.expireAfter = soonest
|
|
3025
|
-
return n
|
|
3398
|
+
return toI32(n)
|
|
3026
3399
|
}
|
|
3027
3400
|
}
|
|
3028
3401
|
|
|
@@ -3086,7 +3459,7 @@ export class Http1TlsServer {
|
|
|
3086
3459
|
this.inputs = new Http1OutputPool(HTTP1_FIRST_INPUT)
|
|
3087
3460
|
this.heads = new Http1OutputPool(HTTP1_FIRST_HEAD)
|
|
3088
3461
|
this.date = new HttpDate()
|
|
3089
|
-
this.scratch =
|
|
3462
|
+
this.scratch = http1ServerBytes(config.chunkSize)
|
|
3090
3463
|
}
|
|
3091
3464
|
|
|
3092
3465
|
/**
|
|
@@ -3150,12 +3523,17 @@ export class Http1TlsServer {
|
|
|
3150
3523
|
}
|
|
3151
3524
|
|
|
3152
3525
|
/**
|
|
3153
|
-
*
|
|
3154
|
-
*
|
|
3155
|
-
* TLS session and no `Http1Connection`, and reads as free
|
|
3526
|
+
* `slot`'s HTTP/1.1 connection when it holds one and has it built, as
|
|
3527
|
+
* `Http2TlsServer.live` asks; otherwise null: a slot accepted on `tls`
|
|
3528
|
+
* directly holds a TLS session and no `Http1Connection`, and reads as free
|
|
3529
|
+
* here.
|
|
3156
3530
|
*/
|
|
3157
|
-
private live(slot: i32):
|
|
3158
|
-
|
|
3531
|
+
private live(slot: i32): Http1Connection | null {
|
|
3532
|
+
if (!this.tls.holds(slot)) {
|
|
3533
|
+
return null
|
|
3534
|
+
}
|
|
3535
|
+
const connections: Http1Connection[] = this.connections
|
|
3536
|
+
return slot >= 0 && slot < toI32(connections.length) ? connections[slot] : null
|
|
3159
3537
|
}
|
|
3160
3538
|
|
|
3161
3539
|
/**
|
|
@@ -3219,9 +3597,12 @@ export class Http1TlsServer {
|
|
|
3219
3597
|
while (toI32(this.connections.length) <= slot) {
|
|
3220
3598
|
this.build()
|
|
3221
3599
|
}
|
|
3222
|
-
const
|
|
3223
|
-
|
|
3224
|
-
|
|
3600
|
+
const connections: Http1Connection[] = this.connections
|
|
3601
|
+
if (slot < toI32(connections.length)) {
|
|
3602
|
+
const conn: Http1Connection = connections[slot]
|
|
3603
|
+
conn.restart()
|
|
3604
|
+
conn.lastActive = monotonicNanos()
|
|
3605
|
+
}
|
|
3225
3606
|
}
|
|
3226
3607
|
return slot
|
|
3227
3608
|
}
|
|
@@ -3233,20 +3614,20 @@ export class Http1TlsServer {
|
|
|
3233
3614
|
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
3234
3615
|
*/
|
|
3235
3616
|
readable(slot: i32): i32 {
|
|
3236
|
-
const live:
|
|
3617
|
+
const live: Http1Connection | null = this.live(slot)
|
|
3237
3618
|
const now: i64 = monotonicNanos()
|
|
3238
3619
|
// What a lingering slot drains is not activity, so `idleTimeout` bounds
|
|
3239
3620
|
// how long a client that trickles bytes after the close can hold it.
|
|
3240
|
-
if (live && !this.tls.lingering(slot)) {
|
|
3241
|
-
|
|
3621
|
+
if (live !== null && !this.tls.lingering(slot)) {
|
|
3622
|
+
live.lastActive = now
|
|
3242
3623
|
}
|
|
3243
3624
|
this.tls.readable(slot)
|
|
3244
3625
|
// Ciphertext after the handshake is the start of a head, or of a record
|
|
3245
3626
|
// that will hold one, so the head's clock starts here; a record trickled
|
|
3246
3627
|
// a byte at a time would hand `next` no plaintext until it is whole. The
|
|
3247
3628
|
// handshake itself is bounded by `idleTimeout` alone (TLS-4).
|
|
3248
|
-
if (live && this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN) {
|
|
3249
|
-
|
|
3629
|
+
if (live !== null && this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN) {
|
|
3630
|
+
live.headAwaited(now)
|
|
3250
3631
|
}
|
|
3251
3632
|
return this.flush(slot)
|
|
3252
3633
|
}
|
|
@@ -3283,10 +3664,10 @@ export class Http1TlsServer {
|
|
|
3283
3664
|
* @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
|
|
3284
3665
|
*/
|
|
3285
3666
|
next(slot: i32): i32 {
|
|
3286
|
-
|
|
3667
|
+
const conn: Http1Connection | null = this.live(slot)
|
|
3668
|
+
if (conn === null || this.refused(slot)) {
|
|
3287
3669
|
return H1_ERROR
|
|
3288
3670
|
}
|
|
3289
|
-
const conn: Http1Connection = this.connections[slot]
|
|
3290
3671
|
while (true) {
|
|
3291
3672
|
const room: i32 = conn.inputRoom()
|
|
3292
3673
|
const want: i32 = room < toI32(this.scratch.length) ? room : toI32(this.scratch.length)
|
|
@@ -3304,7 +3685,7 @@ export class Http1TlsServer {
|
|
|
3304
3685
|
}
|
|
3305
3686
|
// A held-back write finds its room here, as in `Http1Server.next`.
|
|
3306
3687
|
if (conn.writeHeld) {
|
|
3307
|
-
this.send(slot)
|
|
3688
|
+
this.send(slot, conn)
|
|
3308
3689
|
}
|
|
3309
3690
|
return conn.next()
|
|
3310
3691
|
}
|
|
@@ -3319,14 +3700,14 @@ export class Http1TlsServer {
|
|
|
3319
3700
|
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
3320
3701
|
*/
|
|
3321
3702
|
flush(slot: i32): i32 {
|
|
3322
|
-
|
|
3703
|
+
const conn: Http1Connection | null = this.live(slot)
|
|
3704
|
+
if (conn === null) {
|
|
3323
3705
|
return TLS_RECORD_DONE
|
|
3324
3706
|
}
|
|
3325
3707
|
if (this.refused(slot)) {
|
|
3326
3708
|
return this.tls.shutdown(slot)
|
|
3327
3709
|
}
|
|
3328
|
-
|
|
3329
|
-
this.send(slot)
|
|
3710
|
+
this.send(slot, conn)
|
|
3330
3711
|
if (conn.isDone()) {
|
|
3331
3712
|
return this.tls.shutdown(slot)
|
|
3332
3713
|
}
|
|
@@ -3338,24 +3719,22 @@ export class Http1TlsServer {
|
|
|
3338
3719
|
: wants
|
|
3339
3720
|
}
|
|
3340
3721
|
|
|
3341
|
-
/**
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
*/
|
|
3345
|
-
private send(slot: i32): void {
|
|
3346
|
-
if (slot < 0 || slot >= toI32(this.connections.length)) {
|
|
3347
|
-
return
|
|
3348
|
-
}
|
|
3349
|
-
const conn: Http1Connection = this.connections[slot]
|
|
3350
|
-
const pending: i32 = conn.outputEnd - conn.outputStart
|
|
3722
|
+
/** Writes what `slot`'s connection `conn` holds into TLS, as far as TLS takes it. */
|
|
3723
|
+
private send(slot: i32, conn: Http1Connection): void {
|
|
3724
|
+
const pending: i32 = http1ServerSub(conn.outputEnd, conn.outputStart)
|
|
3351
3725
|
while (conn.wantsWrite()) {
|
|
3352
|
-
const n: i32 = this.tls.write(
|
|
3726
|
+
const n: i32 = this.tls.write(
|
|
3727
|
+
slot,
|
|
3728
|
+
conn.output,
|
|
3729
|
+
conn.outputStart,
|
|
3730
|
+
http1ServerSub(conn.outputEnd, conn.outputStart)
|
|
3731
|
+
)
|
|
3353
3732
|
if (n <= 0) {
|
|
3354
3733
|
break
|
|
3355
3734
|
}
|
|
3356
3735
|
conn.consume(n)
|
|
3357
3736
|
}
|
|
3358
|
-
if (conn.outputEnd
|
|
3737
|
+
if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
|
|
3359
3738
|
conn.lastActive = monotonicNanos()
|
|
3360
3739
|
}
|
|
3361
3740
|
conn.release()
|
|
@@ -3368,8 +3747,9 @@ export class Http1TlsServer {
|
|
|
3368
3747
|
*/
|
|
3369
3748
|
close(slot: i32): void {
|
|
3370
3749
|
this.tls.close(slot)
|
|
3371
|
-
|
|
3372
|
-
|
|
3750
|
+
const connections: Http1Connection[] = this.connections
|
|
3751
|
+
if (slot >= 0 && slot < toI32(connections.length)) {
|
|
3752
|
+
connections[slot].abort()
|
|
3373
3753
|
}
|
|
3374
3754
|
}
|
|
3375
3755
|
|
|
@@ -3384,21 +3764,23 @@ export class Http1TlsServer {
|
|
|
3384
3764
|
if (now < this.expireAfter) {
|
|
3385
3765
|
return 0
|
|
3386
3766
|
}
|
|
3387
|
-
|
|
3767
|
+
const scans: i32 = this.expireScans
|
|
3768
|
+
this.expireScans = scans < 2147483647 ? scans + 1 : scans
|
|
3388
3769
|
let soonest: i64 = http1ExpireFloor(this.config)
|
|
3389
|
-
|
|
3770
|
+
// In u32, as `Http1Server.expire` counts.
|
|
3771
|
+
let n: u32 = 0
|
|
3390
3772
|
for (let k: i32 = 0; k < this.tls.size() && k < toI32(this.connections.length); k++) {
|
|
3391
|
-
|
|
3392
|
-
|
|
3773
|
+
const conn: Http1Connection | null = this.live(k)
|
|
3774
|
+
if (conn !== null) {
|
|
3393
3775
|
if (conn.expired(now)) {
|
|
3394
3776
|
this.tls.close(k)
|
|
3395
|
-
n = n + 1
|
|
3777
|
+
n = n + toU32(1)
|
|
3396
3778
|
} else if (conn.expiresNoSooner(now) < soonest) {
|
|
3397
3779
|
soonest = conn.expiresNoSooner(now)
|
|
3398
3780
|
}
|
|
3399
3781
|
}
|
|
3400
3782
|
}
|
|
3401
3783
|
this.expireAfter = soonest
|
|
3402
|
-
return n
|
|
3784
|
+
return toI32(n)
|
|
3403
3785
|
}
|
|
3404
3786
|
}
|