nish 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/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- 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/skills/writing-nish/SKILL.md +12 -4
- 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/http1-server.ts
CHANGED
|
@@ -104,14 +104,16 @@
|
|
|
104
104
|
* a whole request head (`headTimeout`, against slowloris); the slots are the
|
|
105
105
|
* pool's size.
|
|
106
106
|
*
|
|
107
|
-
* `Http1Server`
|
|
108
|
-
*
|
|
109
|
-
* depend on the size, and holds
|
|
110
|
-
*
|
|
111
|
-
*
|
|
107
|
+
* `Http1Server` lends a connection to a slot only while it is busy and builds
|
|
108
|
+
* one only when none is spare, so a pool sized for its peak costs at start-up
|
|
109
|
+
* only what does not depend on the size, and holds as many connections as were
|
|
110
|
+
* busy at once; `Http1TlsServer` builds a slot's connection on its first
|
|
111
|
+
* accept and keeps it, with its TLS state through
|
|
112
|
+
* `TlsTcpServer`. Every buffer is allocated with its slot or
|
|
112
113
|
* grown to its cap on first use and kept, so a warmed slot serves request after
|
|
113
|
-
* request with `Arena.used()` flat. Accepting a WebSocket
|
|
114
|
-
* key
|
|
114
|
+
* request with `Arena.used()` flat. Accepting a WebSocket computes the accept
|
|
115
|
+
* key and writes the 101 into storage the slot keeps, built on its first
|
|
116
|
+
* accept, so later handshakes leave the arena flat too.
|
|
115
117
|
*
|
|
116
118
|
* **Output is borrowed.** The pools' slots share an `Http1OutputPool`: a
|
|
117
119
|
* connection takes an `outputSize` buffer when it first needs room to write
|
|
@@ -127,7 +129,7 @@
|
|
|
127
129
|
* implementation.
|
|
128
130
|
*/
|
|
129
131
|
import { Secret } from "nish:secret"
|
|
130
|
-
import {
|
|
132
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
131
133
|
import {
|
|
132
134
|
HTTP_CODING_GZIP,
|
|
133
135
|
HTTP_CODING_IDENTITY,
|
|
@@ -168,9 +170,10 @@ import {
|
|
|
168
170
|
WsDecoder,
|
|
169
171
|
websocketClosePayload,
|
|
170
172
|
websocketFrameSize,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
WsAcceptKey,
|
|
174
|
+
websocketRequestKeyField,
|
|
173
175
|
websocketWriteFrame,
|
|
176
|
+
websocketWriteUpgradeResponse,
|
|
174
177
|
} from "nish/net/websocket"
|
|
175
178
|
import { TlsServerConfig } from "nish/net/tls"
|
|
176
179
|
import { HTTP_DATE_LAST_SECOND, HTTP_DATE_SIZE, httpDateWrite } from "nish/net/http-fields"
|
|
@@ -285,10 +288,18 @@ const H1_CONN_KEEP_ALIVE: i32 = 2
|
|
|
285
288
|
|
|
286
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. */
|
|
287
290
|
const http1ServerSameByByte = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
288
|
-
|
|
289
|
-
|
|
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]) {
|
|
290
300
|
return false
|
|
291
301
|
}
|
|
302
|
+
j += 1
|
|
292
303
|
}
|
|
293
304
|
return true
|
|
294
305
|
}
|
|
@@ -301,10 +312,14 @@ const http1ServerSameByByte = (kept: readonly u8[], at: i32, bytes: readonly u8[
|
|
|
301
312
|
*/
|
|
302
313
|
const http1ServerSame8 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
303
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
|
+
}
|
|
304
319
|
const e: i32 = at + n - 8
|
|
305
320
|
const f: i32 = n - 8
|
|
306
321
|
if (
|
|
307
|
-
!(at
|
|
322
|
+
!(at + 8 <= toI32(kept.length)) ||
|
|
308
323
|
!(e >= 0 && e < 2147483640 && e + 8 <= toI32(kept.length)) ||
|
|
309
324
|
!(toI32(bytes.length) >= 8 && f >= 0 && f < 2147483640 && f + 8 <= toI32(bytes.length))
|
|
310
325
|
) {
|
|
@@ -341,7 +356,8 @@ const http1ServerSame8 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
341
356
|
/** `http1ServerSame8` for four to seven bytes, with four-byte loads. */
|
|
342
357
|
const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): boolean => {
|
|
343
358
|
const n: i32 = toI32(bytes.length)
|
|
344
|
-
|
|
359
|
+
// Widened: the range check below holds `e` to `kept` whatever `at` is.
|
|
360
|
+
const e: i32 = toI32(toI64(at) + toI64(n) - toI64(4))
|
|
345
361
|
const f: i32 = n - 4
|
|
346
362
|
if (
|
|
347
363
|
!(at >= 0 && at < 2147483640 && at + 4 <= toI32(kept.length)) ||
|
|
@@ -364,6 +380,34 @@ const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
364
380
|
)
|
|
365
381
|
}
|
|
366
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
|
+
|
|
367
411
|
/**
|
|
368
412
|
* Where `kept[at ..)` holds `bytes` and then the byte `after`: the offset one
|
|
369
413
|
* past the byte that follows `after` (the space of `: `, the LF of CR LF), or
|
|
@@ -376,34 +420,82 @@ const http1ServerSame4 = (kept: readonly u8[], at: i32, bytes: readonly u8[]): b
|
|
|
376
420
|
*/
|
|
377
421
|
const http1ServerKept = (kept: u8[], at: i32, limit: i32, bytes: u8[], after: i32): i32 => {
|
|
378
422
|
const n: i32 = toI32(bytes.length)
|
|
379
|
-
|
|
423
|
+
const m: i32 = toI32(kept.length)
|
|
424
|
+
if (at < 0 || limit > m || toI64(n) > toI64(limit) - toI64(at) - toI64(2)) {
|
|
380
425
|
return -1
|
|
381
426
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
k += 16
|
|
389
|
-
}
|
|
390
|
-
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
|
|
391
432
|
}
|
|
392
433
|
let same: boolean = false
|
|
393
|
-
if (n >=
|
|
434
|
+
if (n >= 16) {
|
|
435
|
+
same = http1ServerSame16(kept, at, bytes)
|
|
436
|
+
} else if (n >= 8) {
|
|
394
437
|
same = http1ServerSame8(kept, at, bytes)
|
|
395
438
|
} else if (n >= 4) {
|
|
396
439
|
same = http1ServerSame4(kept, at, bytes)
|
|
397
440
|
} else {
|
|
398
441
|
same = http1ServerSameByByte(kept, at, bytes)
|
|
399
442
|
}
|
|
400
|
-
return same ?
|
|
443
|
+
return same ? past : -1
|
|
401
444
|
}
|
|
402
445
|
|
|
403
|
-
/**
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
|
|
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 ""
|
|
492
|
+
}
|
|
493
|
+
|
|
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)
|
|
407
499
|
}
|
|
408
500
|
}
|
|
409
501
|
|
|
@@ -415,19 +507,78 @@ const http1ServerCheckWindow = (what: string, buf: readonly u8[], off: i32, len:
|
|
|
415
507
|
}
|
|
416
508
|
}
|
|
417
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
|
+
|
|
418
558
|
/** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
|
|
419
559
|
const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
|
|
420
560
|
if (n > 0 && at >= 0 && off >= 0 && at <= toI32(out.length) - n && off <= toI32(src.length) - n) {
|
|
421
561
|
// Both windows fit, as they do for every caller: one move, not a byte loop.
|
|
422
|
-
|
|
423
|
-
|
|
562
|
+
if (tryCopyBytes(out, at, src, off, n)) {
|
|
563
|
+
return toI32(toI64(at) + toI64(n))
|
|
564
|
+
}
|
|
424
565
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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]
|
|
428
579
|
}
|
|
429
580
|
}
|
|
430
|
-
return at + n
|
|
581
|
+
return toI32(toI64(at) + toI64(n))
|
|
431
582
|
}
|
|
432
583
|
|
|
433
584
|
/**
|
|
@@ -452,10 +603,10 @@ export class HttpDate {
|
|
|
452
603
|
private pinnedMs: i64 = 0
|
|
453
604
|
/** The clock reading `hold` took, in milliseconds since 1970, while `held`. */
|
|
454
605
|
private heldMs: i64 = 0
|
|
455
|
-
/** How many times `bytes` has been rewritten. */
|
|
456
|
-
private rewrites:
|
|
457
|
-
/** How many times the wall clock has been read. */
|
|
458
|
-
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
|
|
459
610
|
private pinned: boolean = false
|
|
460
611
|
private held: boolean = false
|
|
461
612
|
|
|
@@ -487,7 +638,7 @@ export class HttpDate {
|
|
|
487
638
|
if (second !== this.second) {
|
|
488
639
|
httpDateWrite(this.bytes, 0, second)
|
|
489
640
|
this.second = second
|
|
490
|
-
this.rewrites += 1
|
|
641
|
+
this.rewrites += toU64(1)
|
|
491
642
|
}
|
|
492
643
|
return this.bytes
|
|
493
644
|
}
|
|
@@ -533,7 +684,7 @@ export class HttpDate {
|
|
|
533
684
|
|
|
534
685
|
/** The wall clock, in milliseconds since 1970. */
|
|
535
686
|
private now(): i64 {
|
|
536
|
-
this.reads += 1
|
|
687
|
+
this.reads += toU64(1)
|
|
537
688
|
return toI64(Date.now())
|
|
538
689
|
}
|
|
539
690
|
|
|
@@ -543,7 +694,7 @@ export class HttpDate {
|
|
|
543
694
|
* @returns the count of `Date.now()` reads since the date was made, for a test that checks a serve call reads once
|
|
544
695
|
*/
|
|
545
696
|
clockReads(): i64 {
|
|
546
|
-
return this.reads
|
|
697
|
+
return toI64(this.reads)
|
|
547
698
|
}
|
|
548
699
|
|
|
549
700
|
/**
|
|
@@ -552,7 +703,7 @@ export class HttpDate {
|
|
|
552
703
|
* @returns the count of IMF-fixdate formats since the date was made, for a test that checks the once-a-second promise
|
|
553
704
|
*/
|
|
554
705
|
formats(): i64 {
|
|
555
|
-
return this.rewrites
|
|
706
|
+
return toI64(this.rewrites)
|
|
556
707
|
}
|
|
557
708
|
}
|
|
558
709
|
|
|
@@ -610,6 +761,20 @@ export class Http1Config {
|
|
|
610
761
|
idleInput: boolean = true
|
|
611
762
|
}
|
|
612
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
|
+
|
|
613
778
|
/** The reason phrase a status is sent with; RFC 9112 §4 lets a client ignore it. */
|
|
614
779
|
const http1ServerReason = (status: i32): string => {
|
|
615
780
|
switch (status) {
|
|
@@ -681,10 +846,18 @@ const http1ServerReserved = (name: u8[]): boolean =>
|
|
|
681
846
|
*/
|
|
682
847
|
export class Http1OutputPool {
|
|
683
848
|
// Widest first, so the layout packs without padding.
|
|
684
|
-
/**
|
|
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
|
+
*/
|
|
685
853
|
free: u8[][]
|
|
686
854
|
private size: i32 = 0
|
|
687
|
-
/**
|
|
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
|
+
*/
|
|
688
861
|
made: i32 = 0
|
|
689
862
|
/** How many buffers `free` has room for without growing. */
|
|
690
863
|
private room: i32 = 0
|
|
@@ -705,18 +878,24 @@ export class Http1OutputPool {
|
|
|
705
878
|
* @internal Called by `Http1Connection` in this module, when it first needs room to write and when it unparks its input and head buffers.
|
|
706
879
|
*/
|
|
707
880
|
take(): u8[] {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
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()
|
|
711
887
|
return buf
|
|
712
888
|
}
|
|
713
|
-
|
|
714
|
-
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)
|
|
715
894
|
// `free` gets room for every buffer made here, while this allocates
|
|
716
895
|
// anyway, so `give` never grows it: a slot's first park can come long
|
|
717
896
|
// after its take, when the arena is meant to be flat.
|
|
718
|
-
if (
|
|
719
|
-
const want: i32 =
|
|
897
|
+
if (made > this.room) {
|
|
898
|
+
const want: i32 = made >= 0 && made < 1073741823 ? made * 2 : 2147483647
|
|
720
899
|
for (let i: i32 = 0; i < want; i++) {
|
|
721
900
|
this.free.push(buf)
|
|
722
901
|
}
|
|
@@ -830,6 +1009,8 @@ export class Http1Connection {
|
|
|
830
1009
|
private inputEnded: boolean = false
|
|
831
1010
|
/** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
|
|
832
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
|
|
833
1014
|
/** Where the bytes `H1_BODY` or `H1_WS_MESSAGE` handed over start in `data`. */
|
|
834
1015
|
dataStart: i32 = 0
|
|
835
1016
|
/** How many bytes `H1_BODY` or `H1_WS_MESSAGE` handed over, from `data[dataStart]`. */
|
|
@@ -865,6 +1046,11 @@ export class Http1Connection {
|
|
|
865
1046
|
* the slot's later peers.
|
|
866
1047
|
*/
|
|
867
1048
|
ws: WsDecoder | null
|
|
1049
|
+
/**
|
|
1050
|
+
* The accept key's storage: made on the slot's first WebSocket accept, like
|
|
1051
|
+
* `ws`, then kept, so a later handshake allocates nothing.
|
|
1052
|
+
*/
|
|
1053
|
+
private acceptKey: WsAcceptKey | null
|
|
868
1054
|
/** `H1_BODY`'s and `H1_WS_MESSAGE`'s bytes. */
|
|
869
1055
|
data: u8[]
|
|
870
1056
|
/** A ping's payload, kept until its pong fits: empty until the first ping. */
|
|
@@ -912,46 +1098,33 @@ export class Http1Connection {
|
|
|
912
1098
|
* still has that count, `respond` neither compares nor keeps. -1 while on.
|
|
913
1099
|
*/
|
|
914
1100
|
private headOff: i64 = -1
|
|
1101
|
+
/**
|
|
1102
|
+
* The accept serial of the peer whose responses the kept head was kept
|
|
1103
|
+
* for, -1 for none: `lendTo` drops a kept head lent to another peer.
|
|
1104
|
+
*/
|
|
1105
|
+
private keptFor: i64 = -1
|
|
915
1106
|
/**
|
|
916
1107
|
* How many heads `respond` has copied from the kept one rather than written.
|
|
917
1108
|
*
|
|
918
1109
|
* @internal Read by `tests/link/net_http1_server_head_reuse` to tell a copied head from a written one.
|
|
919
1110
|
*/
|
|
920
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[]
|
|
921
1118
|
|
|
922
1119
|
/**
|
|
923
|
-
* 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`.
|
|
924
1122
|
*
|
|
925
1123
|
* @param config the caps the connection is held to, read now and kept
|
|
926
1124
|
* @param date the `Date` its responses carry, shared with other connections; null makes it one of its own
|
|
927
1125
|
*/
|
|
928
1126
|
constructor(config: Http1Config, date: HttpDate | null = null) {
|
|
929
|
-
|
|
930
|
-
const one: i32 = 1
|
|
931
|
-
const smallest: i32 = 64
|
|
932
|
-
const output: i32 = 1024
|
|
933
|
-
http1ServerCheckCap("maxTarget", config.maxTarget, one, big)
|
|
934
|
-
http1ServerCheckCap("maxHeaderBytes", config.maxHeaderBytes, smallest, big)
|
|
935
|
-
http1ServerCheckCap("maxHeaders", config.maxHeaders, one, big)
|
|
936
|
-
http1ServerCheckCap("maxBody", config.maxBody, H1_ZERO, big)
|
|
937
|
-
http1ServerCheckCap("chunkSize", config.chunkSize, one, big)
|
|
938
|
-
http1ServerCheckCap("outputSize", config.outputSize, output, big)
|
|
939
|
-
http1ServerCheckCap("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER)
|
|
940
|
-
http1ServerCheckCap("idleTimeout", config.idleTimeout, one, big)
|
|
941
|
-
http1ServerCheckCap("headTimeout", config.headTimeout, one, big)
|
|
942
|
-
http1ServerCheckCap("lingerTimeout", config.lingerTimeout, one, big)
|
|
943
|
-
http1ServerCheckCap("lingerBytes", config.lingerBytes, H1_ZERO, big)
|
|
944
|
-
// The buffers the peer's bytes fill grow by doubling and stop at 2^30
|
|
945
|
-
// (`Http1Parser`'s input and head, `WsDecoder`'s input), so a cap whose
|
|
946
|
-
// buffer could need more is refused here rather than reached by a peer:
|
|
947
|
-
// the head holds the request line and the header section together, and
|
|
948
|
-
// the input never holds more than the larger of the two lines or a chunk.
|
|
949
|
-
http1ServerCheckCap(
|
|
950
|
-
"maxTarget + maxHeaderBytes",
|
|
951
|
-
config.maxTarget + config.maxHeaderBytes,
|
|
952
|
-
one,
|
|
953
|
-
big - H1_LINE_OVERHEAD
|
|
954
|
-
)
|
|
1127
|
+
http1ServerCheckCap(config)
|
|
955
1128
|
this.config = config
|
|
956
1129
|
// The parser is made first: the arena places each block after the last,
|
|
957
1130
|
// so its front fields sit on the lines just behind the connection's own.
|
|
@@ -960,8 +1133,11 @@ export class Http1Connection {
|
|
|
960
1133
|
this.parser.keepText = false
|
|
961
1134
|
this.parser.maxChunk = config.chunkSize
|
|
962
1135
|
this.ws = null
|
|
1136
|
+
this.acceptKey = null
|
|
963
1137
|
this.noOutput = []
|
|
964
1138
|
this.output = this.noOutput
|
|
1139
|
+
this.spareHead = this.noOutput
|
|
1140
|
+
this.spareSpans = []
|
|
965
1141
|
this.pool = null
|
|
966
1142
|
this.inputs = null
|
|
967
1143
|
this.heads = null
|
|
@@ -973,19 +1149,30 @@ export class Http1Connection {
|
|
|
973
1149
|
this.headLengths = []
|
|
974
1150
|
// A line must fit whole before the parser can read it, so the buffer
|
|
975
1151
|
// holds the longest request line or field line however small a chunk is.
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
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
|
|
979
1158
|
}
|
|
980
|
-
if (
|
|
981
|
-
limit =
|
|
1159
|
+
if (line > limit) {
|
|
1160
|
+
limit = line
|
|
982
1161
|
}
|
|
983
|
-
this.bufferLimit = limit
|
|
1162
|
+
this.bufferLimit = toI32(limit)
|
|
984
1163
|
this.restart()
|
|
985
1164
|
}
|
|
986
1165
|
|
|
987
1166
|
/** Puts the connection back as the constructor left it, for the next peer. It allocates nothing. */
|
|
988
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
|
+
}
|
|
989
1176
|
this.parser.restart()
|
|
990
1177
|
const ws: WsDecoder | null = this.ws
|
|
991
1178
|
if (ws !== null) {
|
|
@@ -1019,6 +1206,7 @@ export class Http1Connection {
|
|
|
1019
1206
|
this.headStatus = 0
|
|
1020
1207
|
this.headMisses = 0
|
|
1021
1208
|
this.headOff = -1
|
|
1209
|
+
this.keptFor = -1
|
|
1022
1210
|
this.lingerSince = 0
|
|
1023
1211
|
this.lingered = -1
|
|
1024
1212
|
this.mss = 0
|
|
@@ -1028,10 +1216,42 @@ export class Http1Connection {
|
|
|
1028
1216
|
this.park()
|
|
1029
1217
|
}
|
|
1030
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
|
+
|
|
1031
1250
|
/**
|
|
1032
1251
|
* Gives the parser's input and head buffers to `inputs` and `heads` while
|
|
1033
1252
|
* the connection rests between exchanges: speaking HTTP, no request open,
|
|
1034
|
-
* 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`.
|
|
1035
1255
|
*/
|
|
1036
1256
|
park(): void {
|
|
1037
1257
|
const inputs: Http1OutputPool | null = this.inputs
|
|
@@ -1050,7 +1270,9 @@ export class Http1Connection {
|
|
|
1050
1270
|
p.lineStart = 0
|
|
1051
1271
|
p.lineEnd = 0
|
|
1052
1272
|
}
|
|
1053
|
-
|
|
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) {
|
|
1054
1276
|
heads.give(p.head)
|
|
1055
1277
|
p.head = this.noOutput
|
|
1056
1278
|
}
|
|
@@ -1084,7 +1306,7 @@ export class Http1Connection {
|
|
|
1084
1306
|
* @returns the cap in bytes on unread input: the buffer limit for HTTP, the message cap plus a frame header for WebSocket
|
|
1085
1307
|
*/
|
|
1086
1308
|
inputLimit(): i32 {
|
|
1087
|
-
return this.state === H1_S_WEBSOCKET ? this.config.maxMessage
|
|
1309
|
+
return this.state === H1_S_WEBSOCKET ? http1ServerAdd(this.config.maxMessage, 14) : this.bufferLimit
|
|
1088
1310
|
}
|
|
1089
1311
|
|
|
1090
1312
|
/**
|
|
@@ -1101,9 +1323,12 @@ export class Http1Connection {
|
|
|
1101
1323
|
// Only a WebSocket reads the decoder and the configured message cap,
|
|
1102
1324
|
// so a request's check stays on the connection's front fields.
|
|
1103
1325
|
const ws: WsDecoder | null = this.ws
|
|
1104
|
-
room =
|
|
1326
|
+
room = http1ServerSub(
|
|
1327
|
+
this.inputLimit(),
|
|
1328
|
+
ws !== null ? http1ServerSub(ws.end, ws.start) : this.parser.buffered()
|
|
1329
|
+
)
|
|
1105
1330
|
} else {
|
|
1106
|
-
room = this.bufferLimit
|
|
1331
|
+
room = http1ServerSub(this.bufferLimit, this.parser.buffered())
|
|
1107
1332
|
}
|
|
1108
1333
|
return room > 0 ? room : 0
|
|
1109
1334
|
}
|
|
@@ -1172,7 +1397,7 @@ export class Http1Connection {
|
|
|
1172
1397
|
return
|
|
1173
1398
|
}
|
|
1174
1399
|
const pool: Http1OutputPool | null = this.pool
|
|
1175
|
-
const output: u8[] = pool !== null ? pool.take() :
|
|
1400
|
+
const output: u8[] = pool !== null ? pool.take() : http1ServerBytes(this.config.outputSize)
|
|
1176
1401
|
this.output = output
|
|
1177
1402
|
this.outputCap = toI32(output.length)
|
|
1178
1403
|
this.outputStart = 0
|
|
@@ -1202,8 +1427,8 @@ export class Http1Connection {
|
|
|
1202
1427
|
* @param n how many bytes from `outputStart` the carrier sent; more than are waiting counts as all of them
|
|
1203
1428
|
*/
|
|
1204
1429
|
consume(n: i32): void {
|
|
1205
|
-
const pending: i32 = this.outputEnd
|
|
1206
|
-
this.outputStart = this.outputStart
|
|
1430
|
+
const pending: i32 = http1ServerSub(this.outputEnd, this.outputStart)
|
|
1431
|
+
this.outputStart = http1ServerAdd(this.outputStart, n < pending ? n : pending)
|
|
1207
1432
|
if (this.outputStart === this.outputEnd) {
|
|
1208
1433
|
this.outputStart = 0
|
|
1209
1434
|
this.outputEnd = 0
|
|
@@ -1243,6 +1468,64 @@ export class Http1Connection {
|
|
|
1243
1468
|
return this.state === H1_S_HTTP && !this.requestOpen && !this.headTimed && !this.wantsWrite()
|
|
1244
1469
|
}
|
|
1245
1470
|
|
|
1471
|
+
/**
|
|
1472
|
+
* Whether the connection holds nothing its peer needs kept between
|
|
1473
|
+
* requests: `betweenRequests`, with no output borrowed, no write held back
|
|
1474
|
+
* or pong owed, the peer's stream not ended, no close asked for, no
|
|
1475
|
+
* lingering close, no program-set `mss`, and no byte fed and unread. Such a
|
|
1476
|
+
* connection is the same for any peer, so `Http1Server` lends it to the
|
|
1477
|
+
* next slot that reads and keeps only the slot's `lastActive` and
|
|
1478
|
+
* `socketMss` while it rests.
|
|
1479
|
+
*
|
|
1480
|
+
* @returns true when the connection may serve another peer's next request as it is
|
|
1481
|
+
* @internal Called by `Http1Server.flush` in this module to free a resting slot's connection.
|
|
1482
|
+
*/
|
|
1483
|
+
/**
|
|
1484
|
+
* Takes the connection for the peer accepted as `owner`, dropping its kept
|
|
1485
|
+
* head unless that peer's own responses wrote it, so that how long
|
|
1486
|
+
* `respond` takes tells one peer nothing of another's heads (H1-7).
|
|
1487
|
+
*
|
|
1488
|
+
* @param owner the accept serial of the peer the connection serves next
|
|
1489
|
+
* @internal Called by `Http1Server` in this module as it lends a connection to a slot.
|
|
1490
|
+
*/
|
|
1491
|
+
lendTo(owner: i64): void {
|
|
1492
|
+
if (this.keptFor !== owner) {
|
|
1493
|
+
this.headLength = 0
|
|
1494
|
+
this.headStatus = 0
|
|
1495
|
+
this.headMisses = 0
|
|
1496
|
+
this.headOff = -1
|
|
1497
|
+
this.keptFor = owner
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* Whether the connection holds nothing a resting keep-alive slot must keep:
|
|
1503
|
+
* speaking HTTP with no request open, no head clock started, no output
|
|
1504
|
+
* borrowed or unsent, no write held or pong owed, the peer's stream not
|
|
1505
|
+
* ended, no close asked for or lingering, no program-set `mss`, and its
|
|
1506
|
+
* parser at rest. `Http1Server` then gives the connection back for another
|
|
1507
|
+
* busy slot.
|
|
1508
|
+
*
|
|
1509
|
+
* @returns true when the connection may be lent to another slot.
|
|
1510
|
+
* @internal Called by `Http1Server` in this module at `flush`.
|
|
1511
|
+
*/
|
|
1512
|
+
rests(): boolean {
|
|
1513
|
+
return (
|
|
1514
|
+
this.state === H1_S_HTTP &&
|
|
1515
|
+
!this.requestOpen &&
|
|
1516
|
+
!this.headTimed &&
|
|
1517
|
+
this.outputCap === 0 &&
|
|
1518
|
+
this.outputEnd === this.outputStart &&
|
|
1519
|
+
!this.writeHeld &&
|
|
1520
|
+
!this.pongPending &&
|
|
1521
|
+
!this.inputEnded &&
|
|
1522
|
+
!this.closeAfter &&
|
|
1523
|
+
this.lingered < 0 &&
|
|
1524
|
+
this.mss === 0 &&
|
|
1525
|
+
this.parser.resting()
|
|
1526
|
+
)
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1246
1529
|
/** The room at the end of the output, once what was sent is moved out of the way. */
|
|
1247
1530
|
private room(): i32 {
|
|
1248
1531
|
// Tested here, so that the usual case, an output already held, is no call.
|
|
@@ -1250,20 +1533,19 @@ export class Http1Connection {
|
|
|
1250
1533
|
this.acquire()
|
|
1251
1534
|
}
|
|
1252
1535
|
const output: u8[] = this.output
|
|
1253
|
-
const live: i32 = this.outputEnd
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
}
|
|
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))) {
|
|
1258
1540
|
this.outputStart = 0
|
|
1259
|
-
this.outputEnd = live
|
|
1541
|
+
this.outputEnd = live > 0 ? live : 0
|
|
1260
1542
|
}
|
|
1261
|
-
return toI32(output.length)
|
|
1543
|
+
return http1ServerSub(toI32(output.length), this.outputEnd)
|
|
1262
1544
|
}
|
|
1263
1545
|
|
|
1264
1546
|
/** The room a response or a frame may use: all but the control reserve. */
|
|
1265
1547
|
private bodyRoom(): i32 {
|
|
1266
|
-
const r: i32 = this.room()
|
|
1548
|
+
const r: i32 = http1ServerSub(this.room(), H1_CONTROL_RESERVE)
|
|
1267
1549
|
return r > 0 ? r : 0
|
|
1268
1550
|
}
|
|
1269
1551
|
|
|
@@ -1281,7 +1563,7 @@ export class Http1Connection {
|
|
|
1281
1563
|
if (this.writeHeld) {
|
|
1282
1564
|
// Half the output free before the program is asked to write again, so
|
|
1283
1565
|
// that a slow reader is not answered one small write at a time.
|
|
1284
|
-
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)) {
|
|
1285
1567
|
return H1_NEED_MORE
|
|
1286
1568
|
}
|
|
1287
1569
|
this.writeHeld = false
|
|
@@ -1567,12 +1849,17 @@ export class Http1Connection {
|
|
|
1567
1849
|
// program whose heads differ pays this test and no more.
|
|
1568
1850
|
if (!interim && this.date.formats() !== this.headOff) {
|
|
1569
1851
|
if (this.sameHead(status, names, values, framing, code)) {
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
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)
|
|
1574
1859
|
this.headMisses = 0
|
|
1575
|
-
|
|
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
|
|
1576
1863
|
return this.begin(close, framing, empty)
|
|
1577
1864
|
}
|
|
1578
1865
|
}
|
|
@@ -1600,9 +1887,9 @@ export class Http1Connection {
|
|
|
1600
1887
|
if (end === HTTP1_REFUSED) {
|
|
1601
1888
|
return H1_INVALID
|
|
1602
1889
|
}
|
|
1603
|
-
if (end < 0 || end
|
|
1890
|
+
if (end < 0 || http1ServerSub(end, start) > room) {
|
|
1604
1891
|
// The head was not written, or ate into the reserve: take it back.
|
|
1605
|
-
if (end
|
|
1892
|
+
if (http1ServerSub(end, start) > http1ServerSub(this.config.outputSize, H1_CONTROL_RESERVE)) {
|
|
1606
1893
|
return H1_INVALID
|
|
1607
1894
|
}
|
|
1608
1895
|
if (start === 0) {
|
|
@@ -1617,8 +1904,10 @@ export class Http1Connection {
|
|
|
1617
1904
|
}
|
|
1618
1905
|
if (this.date.formats() !== this.headOff) {
|
|
1619
1906
|
// A head written here is a miss: the kept one did not match.
|
|
1620
|
-
this.headMisses
|
|
1621
|
-
|
|
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) {
|
|
1622
1911
|
this.keepHead(start, end, status, reason, names, values, framing, code)
|
|
1623
1912
|
} else {
|
|
1624
1913
|
this.headMisses = 0
|
|
@@ -1663,17 +1952,24 @@ export class Http1Connection {
|
|
|
1663
1952
|
const limit: i32 = this.headLength
|
|
1664
1953
|
let at: i32 = this.headFieldsAt
|
|
1665
1954
|
const lengths: i32[] = this.headLengths
|
|
1666
|
-
|
|
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
|
|
1667
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
|
+
}
|
|
1668
1963
|
// Both lengths, so that a value cannot match the kept bytes past its
|
|
1669
1964
|
// own field and pass for the kept head without its text checks.
|
|
1670
1965
|
const name: u8[] = names[i]
|
|
1671
1966
|
const value: u8[] = values[i]
|
|
1672
|
-
if (toI32(name.length) !== lengths[
|
|
1967
|
+
if (toI32(name.length) !== lengths[j] || toI32(value.length) !== lengths[j + 1]) {
|
|
1673
1968
|
return false
|
|
1674
1969
|
}
|
|
1675
1970
|
at = http1ServerKept(kept, at, limit, name, 58)
|
|
1676
1971
|
at = http1ServerKept(kept, at, limit, value, 13)
|
|
1972
|
+
j += 2
|
|
1677
1973
|
}
|
|
1678
1974
|
return at >= 0
|
|
1679
1975
|
}
|
|
@@ -1703,7 +1999,7 @@ export class Http1Connection {
|
|
|
1703
1999
|
framing: i32,
|
|
1704
2000
|
connection: i32
|
|
1705
2001
|
): void {
|
|
1706
|
-
const n: i32 = end
|
|
2002
|
+
const n: i32 = http1ServerSub(end, start)
|
|
1707
2003
|
const fields: i32 = toI32(names.length)
|
|
1708
2004
|
const again: boolean =
|
|
1709
2005
|
status === this.headStatus && connection === this.headConnection && fields === this.headFields
|
|
@@ -1712,25 +2008,41 @@ export class Http1Connection {
|
|
|
1712
2008
|
this.headConnection = connection
|
|
1713
2009
|
this.headFields = fields
|
|
1714
2010
|
this.headLength = 0
|
|
1715
|
-
if (n > H1_HEAD_KEEP || fields > H1_HEAD_FIELDS) {
|
|
2011
|
+
if (n < 0 || n > H1_HEAD_KEEP || fields > H1_HEAD_FIELDS) {
|
|
1716
2012
|
return
|
|
1717
2013
|
}
|
|
1718
2014
|
if (!again) {
|
|
1719
2015
|
return
|
|
1720
2016
|
}
|
|
1721
2017
|
if (toI32(this.headBytes.length) < n) {
|
|
1722
|
-
|
|
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
|
+
)
|
|
1723
2023
|
}
|
|
1724
|
-
|
|
1725
|
-
|
|
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)
|
|
1726
2028
|
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
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
|
|
2039
|
+
}
|
|
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
|
|
1730
2043
|
}
|
|
1731
|
-
copyBytes(this.headBytes, H1_ZERO, this.output, start, n)
|
|
1732
2044
|
// The field lines start past "HTTP/1.1 NNN " reason CRLF.
|
|
1733
|
-
this.headFieldsAt = 15
|
|
2045
|
+
this.headFieldsAt = http1ServerAdd(15, toI32(reason.length))
|
|
1734
2046
|
this.headLength = n
|
|
1735
2047
|
this.headDate = this.date
|
|
1736
2048
|
this.headFormats = this.date.formats()
|
|
@@ -1766,8 +2078,9 @@ export class Http1Connection {
|
|
|
1766
2078
|
const room: i32 = this.bodyRoom()
|
|
1767
2079
|
let take: i32 = len
|
|
1768
2080
|
if (chunked) {
|
|
1769
|
-
|
|
1770
|
-
|
|
2081
|
+
const fits: i32 = http1ServerSub(room, H1_CHUNK_OVERHEAD)
|
|
2082
|
+
if (take > fits) {
|
|
2083
|
+
take = fits
|
|
1771
2084
|
}
|
|
1772
2085
|
} else if (take > room) {
|
|
1773
2086
|
take = room
|
|
@@ -1781,7 +2094,7 @@ export class Http1Connection {
|
|
|
1781
2094
|
} else {
|
|
1782
2095
|
this.outputEnd = http1ServerCopy(this.output, this.outputEnd, buf, off, take)
|
|
1783
2096
|
if (this.responseFraming >= 0) {
|
|
1784
|
-
this.responseLeft = this.responseLeft
|
|
2097
|
+
this.responseLeft = http1ServerSub(this.responseLeft, take)
|
|
1785
2098
|
}
|
|
1786
2099
|
}
|
|
1787
2100
|
if (take < len) {
|
|
@@ -1828,8 +2141,17 @@ export class Http1Connection {
|
|
|
1828
2141
|
) {
|
|
1829
2142
|
return this.write(buf, off, len)
|
|
1830
2143
|
}
|
|
1831
|
-
const
|
|
1832
|
-
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)
|
|
1833
2155
|
if (n < 0 && n !== H1_AGAIN) {
|
|
1834
2156
|
this.abort()
|
|
1835
2157
|
return H1_CLOSED
|
|
@@ -1839,13 +2161,13 @@ export class Http1Connection {
|
|
|
1839
2161
|
if (n > 0) {
|
|
1840
2162
|
this.lastActive = monotonicNanos()
|
|
1841
2163
|
this.consume(n)
|
|
1842
|
-
sent = n > pending ? n
|
|
1843
|
-
this.responseLeft = this.responseLeft
|
|
2164
|
+
sent = n > pending ? http1ServerSub(n, pending) : 0
|
|
2165
|
+
this.responseLeft = http1ServerSub(this.responseLeft, sent)
|
|
1844
2166
|
}
|
|
1845
2167
|
if (sent < len) {
|
|
1846
|
-
const copied: i32 = this.write(buf, off
|
|
2168
|
+
const copied: i32 = this.write(buf, http1ServerAdd(off, sent), http1ServerSub(len, sent))
|
|
1847
2169
|
if (copied > 0) {
|
|
1848
|
-
sent = sent
|
|
2170
|
+
sent = http1ServerAdd(sent, copied)
|
|
1849
2171
|
} else if (sent === 0) {
|
|
1850
2172
|
return copied
|
|
1851
2173
|
}
|
|
@@ -1927,11 +2249,14 @@ export class Http1Connection {
|
|
|
1927
2249
|
const parser: Http1Parser = this.parser
|
|
1928
2250
|
const i: i32 = parser.headerIndex("accept-encoding")
|
|
1929
2251
|
const spans: i32[] = parser.spans
|
|
1930
|
-
|
|
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) {
|
|
1931
2256
|
return HTTP_CODING_IDENTITY
|
|
1932
2257
|
}
|
|
1933
|
-
const start: i32 = spans[
|
|
1934
|
-
const end: i32 = spans[
|
|
2258
|
+
const start: i32 = spans[k]
|
|
2259
|
+
const end: i32 = spans[k + 1]
|
|
1935
2260
|
if (start < 0 || start > end || end > toI32(parser.head.length)) {
|
|
1936
2261
|
return HTTP_CODING_IDENTITY
|
|
1937
2262
|
}
|
|
@@ -1976,8 +2301,10 @@ export class Http1Connection {
|
|
|
1976
2301
|
* The whole response is written or none of it: `H1_AGAIN` when the output has
|
|
1977
2302
|
* no room for it yet (`H1_WRITE` says when to try again, compressing again),
|
|
1978
2303
|
* `H1_INVALID` for a response larger than the output could ever hold or one
|
|
1979
|
-
* `respond` refuses, `H1_CLOSED` with no request to answer
|
|
1980
|
-
*
|
|
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.
|
|
1981
2308
|
*
|
|
1982
2309
|
* @param status the HTTP status code to send, as `respond` takes it
|
|
1983
2310
|
* @param names the program's field names, without `Content-Encoding` (or `Vary` under `enc.vary`)
|
|
@@ -1999,7 +2326,9 @@ export class Http1Connection {
|
|
|
1999
2326
|
enc: HttpCompressor,
|
|
2000
2327
|
coding: i32
|
|
2001
2328
|
): i32 {
|
|
2002
|
-
|
|
2329
|
+
if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(body.length)) {
|
|
2330
|
+
return H1_INVALID
|
|
2331
|
+
}
|
|
2003
2332
|
if (!this.answerable()) {
|
|
2004
2333
|
return H1_CLOSED
|
|
2005
2334
|
}
|
|
@@ -2025,7 +2354,7 @@ export class Http1Connection {
|
|
|
2025
2354
|
if (r !== 0) {
|
|
2026
2355
|
return r
|
|
2027
2356
|
}
|
|
2028
|
-
queued = this.outputEnd - outputEnd + len <= enc.segmentSize(mss)
|
|
2357
|
+
queued = toI64(this.outputEnd) - toI64(outputEnd) + toI64(len) <= toI64(enc.segmentSize(mss))
|
|
2029
2358
|
if (!queued) {
|
|
2030
2359
|
this.unrespond(outputEnd, closeAfter)
|
|
2031
2360
|
}
|
|
@@ -2045,9 +2374,9 @@ export class Http1Connection {
|
|
|
2045
2374
|
}
|
|
2046
2375
|
if (packed >= 0 && enc.segmentMode && !required) {
|
|
2047
2376
|
// The head just written, and the one the body as it is would get.
|
|
2048
|
-
const head: i32 = this.outputEnd
|
|
2377
|
+
const head: i32 = http1ServerSub(this.outputEnd, outputEnd)
|
|
2049
2378
|
const plain: i32 = enc.http1IdentityHead(head, packed, len)
|
|
2050
|
-
if (!enc.fewerSegments(plain
|
|
2379
|
+
if (!enc.fewerSegments(http1ServerAdd(plain, len), http1ServerAdd(head, packed), mss)) {
|
|
2051
2380
|
this.unrespond(outputEnd, closeAfter)
|
|
2052
2381
|
enc.identityFields(names, values)
|
|
2053
2382
|
packed = -1
|
|
@@ -2059,7 +2388,9 @@ export class Http1Connection {
|
|
|
2059
2388
|
}
|
|
2060
2389
|
}
|
|
2061
2390
|
if (!this.responseEmpty && n > this.bodyRoom()) {
|
|
2062
|
-
const fits: boolean =
|
|
2391
|
+
const fits: boolean =
|
|
2392
|
+
toI64(this.outputEnd) - toI64(outputEnd) + toI64(n) <=
|
|
2393
|
+
toI64(this.config.outputSize) - toI64(H1_CONTROL_RESERVE)
|
|
2063
2394
|
this.unrespond(outputEnd, closeAfter)
|
|
2064
2395
|
if (!fits) {
|
|
2065
2396
|
return H1_INVALID
|
|
@@ -2069,7 +2400,11 @@ export class Http1Connection {
|
|
|
2069
2400
|
}
|
|
2070
2401
|
const w: i32 = packed >= 0 ? this.write(enc.output, toI32(0), n) : this.write(body, off, n)
|
|
2071
2402
|
if (w !== n) {
|
|
2072
|
-
|
|
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
|
|
2073
2408
|
}
|
|
2074
2409
|
return this.end()
|
|
2075
2410
|
}
|
|
@@ -2101,26 +2436,47 @@ export class Http1Connection {
|
|
|
2101
2436
|
if (!this.answerable()) {
|
|
2102
2437
|
return H1_CLOSED
|
|
2103
2438
|
}
|
|
2104
|
-
const
|
|
2105
|
-
|
|
2439
|
+
const p: Http1Parser = this.parser
|
|
2440
|
+
const field: i32 = websocketRequestKeyField(p)
|
|
2441
|
+
const spans: i32[] = p.spans
|
|
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) {
|
|
2106
2445
|
return H1_INVALID
|
|
2107
2446
|
}
|
|
2108
|
-
const
|
|
2109
|
-
if (head
|
|
2447
|
+
const accept: WsAcceptKey = this.acceptStorage()
|
|
2448
|
+
if (!accept.compute(p.head, spans[k], spans[k + 1])) {
|
|
2110
2449
|
return H1_INVALID
|
|
2111
2450
|
}
|
|
2112
|
-
|
|
2113
|
-
|
|
2451
|
+
// Written past `outputEnd`, where nothing live is, and kept only when it
|
|
2452
|
+
// fits beside the control reserve.
|
|
2453
|
+
const room: i32 = this.bodyRoom()
|
|
2454
|
+
const end: i32 = websocketWriteUpgradeResponse(this.output, this.outputEnd, accept.value, protocol)
|
|
2455
|
+
if (end === HTTP1_REFUSED) {
|
|
2456
|
+
return H1_INVALID
|
|
2457
|
+
}
|
|
2458
|
+
if (end < 0 || http1ServerSub(end, this.outputEnd) > room) {
|
|
2114
2459
|
this.writeHeld = true
|
|
2115
2460
|
return H1_AGAIN
|
|
2116
2461
|
}
|
|
2117
|
-
this.outputEnd =
|
|
2462
|
+
this.outputEnd = end
|
|
2118
2463
|
this.response = H1_R_DONE
|
|
2119
2464
|
this.upgraded = true
|
|
2120
2465
|
this.closeAfter = false
|
|
2121
2466
|
return 0
|
|
2122
2467
|
}
|
|
2123
2468
|
|
|
2469
|
+
/** The slot's `WsAcceptKey`, made on its first WebSocket accept and kept. */
|
|
2470
|
+
private acceptStorage(): WsAcceptKey {
|
|
2471
|
+
const made: WsAcceptKey | null = this.acceptKey
|
|
2472
|
+
if (made !== null) {
|
|
2473
|
+
return made
|
|
2474
|
+
}
|
|
2475
|
+
const fresh: WsAcceptKey = new WsAcceptKey()
|
|
2476
|
+
this.acceptKey = fresh
|
|
2477
|
+
return fresh
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2124
2480
|
/**
|
|
2125
2481
|
* Sends one WebSocket frame, unmasked as a server's are (§5.1): a whole
|
|
2126
2482
|
* text or binary message, a fragment of one (`fin` false, then
|
|
@@ -2141,7 +2497,10 @@ export class Http1Connection {
|
|
|
2141
2497
|
if (this.state !== H1_S_WEBSOCKET) {
|
|
2142
2498
|
return H1_CLOSED
|
|
2143
2499
|
}
|
|
2144
|
-
if (
|
|
2500
|
+
if (
|
|
2501
|
+
opcode === WS_OP_CLOSE ||
|
|
2502
|
+
toI64(len) > toI64(this.config.outputSize) - toI64(H1_CONTROL_RESERVE) - toI64(14)
|
|
2503
|
+
) {
|
|
2145
2504
|
return H1_INVALID
|
|
2146
2505
|
}
|
|
2147
2506
|
if (websocketFrameSize(len, false) > this.bodyRoom()) {
|
|
@@ -2188,7 +2547,7 @@ export class Http1Connection {
|
|
|
2188
2547
|
|
|
2189
2548
|
/** Whether nothing has moved for longer than the idle timeout, at `now` (`monotonicNanos`). */
|
|
2190
2549
|
private idle(now: i64): boolean {
|
|
2191
|
-
return now
|
|
2550
|
+
return http1ServerPast(now, this.lastActive, this.config.idleTimeout)
|
|
2192
2551
|
}
|
|
2193
2552
|
|
|
2194
2553
|
/**
|
|
@@ -2225,7 +2584,7 @@ export class Http1Connection {
|
|
|
2225
2584
|
|
|
2226
2585
|
/** Whether a head has been arriving for longer than `headTimeout` at `now` (`monotonicNanos`). */
|
|
2227
2586
|
private headLate(now: i64): boolean {
|
|
2228
|
-
return this.headTimed && now
|
|
2587
|
+
return this.headTimed && http1ServerPast(now, this.headSince, this.config.headTimeout)
|
|
2229
2588
|
}
|
|
2230
2589
|
|
|
2231
2590
|
/**
|
|
@@ -2272,13 +2631,13 @@ export class Http1Connection {
|
|
|
2272
2631
|
* @internal Called by `Http1Server` in this module as it drains a refused slot.
|
|
2273
2632
|
*/
|
|
2274
2633
|
drained(n: i32): boolean {
|
|
2275
|
-
this.lingered = this.lingered
|
|
2634
|
+
this.lingered = http1ServerAdd(this.lingered, n)
|
|
2276
2635
|
return this.lingered < this.config.lingerBytes
|
|
2277
2636
|
}
|
|
2278
2637
|
|
|
2279
2638
|
/** Whether the lingering close has lasted longer than `lingerTimeout` at `now` (`monotonicNanos`). */
|
|
2280
2639
|
private lingerLate(now: i64): boolean {
|
|
2281
|
-
return this.lingered >= 0 && now
|
|
2640
|
+
return this.lingered >= 0 && http1ServerPast(now, this.lingerSince, this.config.lingerTimeout)
|
|
2282
2641
|
}
|
|
2283
2642
|
|
|
2284
2643
|
/**
|
|
@@ -2305,17 +2664,17 @@ export class Http1Connection {
|
|
|
2305
2664
|
* @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to skip the slots that cannot expire yet.
|
|
2306
2665
|
*/
|
|
2307
2666
|
expiresNoSooner(now: i64): i64 {
|
|
2308
|
-
const idle: i64 = this.lastActive
|
|
2667
|
+
const idle: i64 = http1ServerAfter(this.lastActive, this.config.idleTimeout)
|
|
2309
2668
|
let start: i64 = now
|
|
2310
2669
|
if (this.headTimed) {
|
|
2311
2670
|
start = this.headSince
|
|
2312
2671
|
} else if (this.requestOpen) {
|
|
2313
2672
|
start = this.lastActive
|
|
2314
2673
|
}
|
|
2315
|
-
const head: i64 = start
|
|
2674
|
+
const head: i64 = http1ServerAfter(start, this.config.headTimeout)
|
|
2316
2675
|
const sooner: i64 = head < idle ? head : idle
|
|
2317
2676
|
if (this.lingered >= 0) {
|
|
2318
|
-
const linger: i64 = this.lingerSince
|
|
2677
|
+
const linger: i64 = http1ServerAfter(this.lingerSince, this.config.lingerTimeout)
|
|
2319
2678
|
return linger < sooner ? linger : sooner
|
|
2320
2679
|
}
|
|
2321
2680
|
return sooner
|
|
@@ -2328,7 +2687,7 @@ export class Http1Connection {
|
|
|
2328
2687
|
*/
|
|
2329
2688
|
const http1ExpireFloor = (config: Http1Config): i64 => {
|
|
2330
2689
|
const shorter: i32 = config.headTimeout < config.idleTimeout ? config.headTimeout : config.idleTimeout
|
|
2331
|
-
return monotonicNanos()
|
|
2690
|
+
return http1ServerAfter(monotonicNanos(), shorter)
|
|
2332
2691
|
}
|
|
2333
2692
|
|
|
2334
2693
|
// ---- Carriers -------------------------------------------------------------------
|
|
@@ -2337,14 +2696,36 @@ const http1ExpireFloor = (config: Http1Config): i64 => {
|
|
|
2337
2696
|
* A pool of HTTP/1.1 connections over plain TCP on one listening socket.
|
|
2338
2697
|
* `slot`s are the indices `accept` answers; a slot is the program's from
|
|
2339
2698
|
* `accept` until it calls `close`, and its descriptor is `fd(slot)`.
|
|
2699
|
+
*
|
|
2700
|
+
* A slot holds an `Http1Connection` only while it has something to keep: a
|
|
2701
|
+
* request read or answered, bytes fed and unread, output to send, or a
|
|
2702
|
+
* WebSocket. One that rests between requests (`Http1Connection.rests`) gives
|
|
2703
|
+
* its connection back at `flush` and keeps only its descriptor, its
|
|
2704
|
+
* interest, its `lastActive` and its socket's MSS; the next slot that reads
|
|
2705
|
+
* takes a resting connection, keeping its response head only if it was kept
|
|
2706
|
+
* for that slot's own peer (H1-7). So the pool builds
|
|
2707
|
+
* as many connections as are busy at once, not one per open slot, and an
|
|
2708
|
+
* idle keep-alive costs its slot a few words.
|
|
2340
2709
|
*/
|
|
2341
2710
|
export class Http1Server {
|
|
2342
2711
|
private config: Http1Config
|
|
2343
2712
|
/**
|
|
2344
|
-
* The connections built so far,
|
|
2345
|
-
*
|
|
2713
|
+
* The connections built so far, as many as were ever busy at once: each is
|
|
2714
|
+
* a slot's (`at`) or spare.
|
|
2346
2715
|
*/
|
|
2347
2716
|
connections: Http1Connection[]
|
|
2717
|
+
/** Each used slot's connection, an index into `connections`, or -1 while the slot rests. */
|
|
2718
|
+
private at: i32[]
|
|
2719
|
+
/** The indices in `connections` of the connections no slot holds, a stack. */
|
|
2720
|
+
private spare: i32[]
|
|
2721
|
+
/** Each resting slot's `lastActive` (`monotonicNanos`), which its connection carried. */
|
|
2722
|
+
private rested: i64[]
|
|
2723
|
+
/** Each resting slot's `socketMss`, which its connection carried. */
|
|
2724
|
+
private restedMss: i32[]
|
|
2725
|
+
/** Each used slot's accept serial, which keys the kept head of a connection lent to it (H1-7). */
|
|
2726
|
+
private owner: i64[]
|
|
2727
|
+
/** How many connections `accept` has taken: the next one's serial. */
|
|
2728
|
+
private accepts: i64 = 0
|
|
2348
2729
|
/**
|
|
2349
2730
|
* The `Date` every slot's responses carry, formatted once a second for all
|
|
2350
2731
|
* of them and read from the clock once per serve call: `readable` and
|
|
@@ -2391,10 +2772,10 @@ export class Http1Server {
|
|
|
2391
2772
|
|
|
2392
2773
|
/**
|
|
2393
2774
|
* A pool of `size` slots, at least one, serving `config` on `listener`.
|
|
2394
|
-
* No slot is built here: a
|
|
2395
|
-
*
|
|
2396
|
-
*
|
|
2397
|
-
*
|
|
2775
|
+
* No slot is built here: a connection is built the first time a slot
|
|
2776
|
+
* reads and none rests spare, and kept, so a pool sized for a peak costs
|
|
2777
|
+
* at start-up only the read scratch, the shared `Date` and the output
|
|
2778
|
+
* pool, and at most as many connections as were ever busy at once.
|
|
2398
2779
|
*
|
|
2399
2780
|
* @param config the caps every slot's connection is held to
|
|
2400
2781
|
* @param listener the listening socket from `tcpListen`, which stays the program's to close
|
|
@@ -2404,6 +2785,11 @@ export class Http1Server {
|
|
|
2404
2785
|
this.config = config
|
|
2405
2786
|
this.listener = listener
|
|
2406
2787
|
this.connections = []
|
|
2788
|
+
this.at = []
|
|
2789
|
+
this.spare = []
|
|
2790
|
+
this.rested = []
|
|
2791
|
+
this.restedMss = []
|
|
2792
|
+
this.owner = []
|
|
2407
2793
|
this.fds = []
|
|
2408
2794
|
this.armed = []
|
|
2409
2795
|
this.free = []
|
|
@@ -2412,11 +2798,11 @@ export class Http1Server {
|
|
|
2412
2798
|
this.inputs = new Http1OutputPool(HTTP1_FIRST_INPUT)
|
|
2413
2799
|
this.heads = new Http1OutputPool(HTTP1_FIRST_HEAD)
|
|
2414
2800
|
this.date = new HttpDate()
|
|
2415
|
-
this.scratch =
|
|
2801
|
+
this.scratch = http1ServerBytes(config.chunkSize)
|
|
2416
2802
|
this.peer = new Array<u8>(H1_ADDRESS_SIZE)
|
|
2417
2803
|
}
|
|
2418
2804
|
|
|
2419
|
-
/** A new connection on this pool's outputs and `Date`,
|
|
2805
|
+
/** A new connection on this pool's outputs and `Date`, appended to `connections`. */
|
|
2420
2806
|
private build(): Http1Connection {
|
|
2421
2807
|
const conn: Http1Connection = new Http1Connection(this.config, this.date)
|
|
2422
2808
|
conn.pool = this.outputs
|
|
@@ -2443,13 +2829,15 @@ export class Http1Server {
|
|
|
2443
2829
|
* @returns the count of slots accepted into and not yet closed
|
|
2444
2830
|
*/
|
|
2445
2831
|
busy(): i32 {
|
|
2446
|
-
|
|
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
|
|
2447
2835
|
for (const fd of this.fds) {
|
|
2448
2836
|
if (fd >= 0) {
|
|
2449
|
-
n = n + 1
|
|
2837
|
+
n = n + toU32(1)
|
|
2450
2838
|
}
|
|
2451
2839
|
}
|
|
2452
|
-
return n
|
|
2840
|
+
return toI32(n)
|
|
2453
2841
|
}
|
|
2454
2842
|
|
|
2455
2843
|
/**
|
|
@@ -2469,22 +2857,162 @@ export class Http1Server {
|
|
|
2469
2857
|
* @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
|
|
2470
2858
|
*/
|
|
2471
2859
|
fd(slot: i32): i32 {
|
|
2472
|
-
|
|
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
|
|
2473
2866
|
}
|
|
2474
2867
|
|
|
2475
2868
|
/**
|
|
2476
|
-
* The connection of `slot`. A slot
|
|
2477
|
-
*
|
|
2478
|
-
*
|
|
2869
|
+
* The connection of `slot`. A resting slot is given one here, a spare or
|
|
2870
|
+
* a new one, which it keeps until it rests again at a `flush`: so a
|
|
2871
|
+
* program holds the answer only within a serve call, and asks again in
|
|
2872
|
+
* the next. A free slot, or one out of range, names the first connection
|
|
2873
|
+
* built, which this builds when none has been; it is another slot's or
|
|
2874
|
+
* spare, so read nothing from it.
|
|
2479
2875
|
*
|
|
2480
2876
|
* @param slot the slot `accept` answered
|
|
2481
2877
|
* @returns the slot's connection, which the pool keeps; read its events and write its response through it
|
|
2482
2878
|
*/
|
|
2483
2879
|
connection(slot: i32): Http1Connection {
|
|
2484
|
-
|
|
2485
|
-
|
|
2880
|
+
const conn: Http1Connection | null = this.attach(slot)
|
|
2881
|
+
if (conn !== null) {
|
|
2882
|
+
return conn
|
|
2883
|
+
}
|
|
2884
|
+
const connections: Http1Connection[] = this.connections
|
|
2885
|
+
return connections.length > 0 ? connections[0] : this.build()
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
/**
|
|
2889
|
+
* Whether `slot` holds a connection that rests between requests and has
|
|
2890
|
+
* given its `Http1Connection` back: nothing is open, unread or unsent on
|
|
2891
|
+
* it, so closing it loses nothing. It does not give the slot a connection,
|
|
2892
|
+
* as `connection` does.
|
|
2893
|
+
*
|
|
2894
|
+
* @param slot the slot index to ask about, any value
|
|
2895
|
+
* @returns true when the slot is accepted into and resting; false when it is free, out of range or busy
|
|
2896
|
+
*/
|
|
2897
|
+
resting(slot: i32): boolean {
|
|
2898
|
+
const at: i32[] = this.at
|
|
2899
|
+
return this.holds(slot) && slot >= 0 && slot < toI32(at.length) && at[slot] < 0
|
|
2900
|
+
}
|
|
2901
|
+
|
|
2902
|
+
/**
|
|
2903
|
+
* How many connections no slot holds now, of the `connections` built.
|
|
2904
|
+
*
|
|
2905
|
+
* @returns the count of spare connections, which a resting slot that reads takes before one is built
|
|
2906
|
+
*/
|
|
2907
|
+
spares(): i32 {
|
|
2908
|
+
return toI32(this.spare.length)
|
|
2909
|
+
}
|
|
2910
|
+
|
|
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
|
|
2920
|
+
}
|
|
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)
|
|
2979
|
+
return conn
|
|
2980
|
+
}
|
|
2981
|
+
|
|
2982
|
+
/** Gives back `slot`'s connection when it rests, keeping what the slot needs of it. */
|
|
2983
|
+
private rest(slot: i32): void {
|
|
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
|
|
2486
2994
|
}
|
|
2487
|
-
|
|
2995
|
+
const k: i32 = at[slot]
|
|
2996
|
+
const connections: Http1Connection[] = this.connections
|
|
2997
|
+
if (k < 0 || k >= toI32(connections.length)) {
|
|
2998
|
+
return
|
|
2999
|
+
}
|
|
3000
|
+
const conn: Http1Connection = connections[k]
|
|
3001
|
+
if (!conn.rests()) {
|
|
3002
|
+
return
|
|
3003
|
+
}
|
|
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
|
|
3012
|
+
conn.fd = -1
|
|
3013
|
+
conn.socketMss = 0
|
|
3014
|
+
fresh[slot] = -1
|
|
3015
|
+
this.spare.push(k)
|
|
2488
3016
|
}
|
|
2489
3017
|
|
|
2490
3018
|
/**
|
|
@@ -2504,30 +3032,45 @@ export class Http1Server {
|
|
|
2504
3032
|
// first accepts take slots 0, 1, 2 ... and a pool that never fills never
|
|
2505
3033
|
// builds its last slots.
|
|
2506
3034
|
let k: i32 = H1_POOL_FULL
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
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()
|
|
2510
3040
|
} else if (toI32(this.fds.length) < this.slots) {
|
|
2511
3041
|
k = toI32(this.fds.length)
|
|
2512
3042
|
this.fds.push(H1_FREE)
|
|
2513
3043
|
this.armed.push(H1_WANT_READ)
|
|
3044
|
+
this.at.push(toI32(-1))
|
|
3045
|
+
this.rested.push(toI64(0))
|
|
3046
|
+
this.restedMss.push(toI32(0))
|
|
3047
|
+
this.owner.push(toI64(0))
|
|
2514
3048
|
}
|
|
2515
|
-
if (
|
|
3049
|
+
if (
|
|
3050
|
+
k < 0 ||
|
|
3051
|
+
k >= toI32(this.fds.length) ||
|
|
3052
|
+
k >= toI32(this.armed.length) ||
|
|
3053
|
+
k >= toI32(this.at.length) ||
|
|
3054
|
+
k >= toI32(this.rested.length) ||
|
|
3055
|
+
k >= toI32(this.restedMss.length) ||
|
|
3056
|
+
k >= toI32(this.owner.length)
|
|
3057
|
+
) {
|
|
2516
3058
|
netClose(fd)
|
|
2517
3059
|
return H1_POOL_FULL
|
|
2518
3060
|
}
|
|
2519
|
-
const conn: Http1Connection = k < toI32(this.connections.length) ? this.connections[k] : this.build()
|
|
2520
3061
|
this.fds[k] = fd
|
|
2521
3062
|
this.armed[k] = H1_WANT_READ
|
|
2522
|
-
|
|
2523
|
-
|
|
3063
|
+
// The slot starts resting: it is given a connection when it first reads.
|
|
3064
|
+
this.at[k] = -1
|
|
3065
|
+
this.rested[k] = monotonicNanos()
|
|
3066
|
+
this.owner[k] = this.accepts
|
|
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))
|
|
2524
3070
|
// Once a connection, not a request, so a keep-alive request costs no
|
|
2525
3071
|
// syscall: a path whose MSS moves after the handshake is not followed.
|
|
2526
3072
|
const mss: i32 = netMss(fd)
|
|
2527
|
-
|
|
2528
|
-
conn.socketMss = mss
|
|
2529
|
-
}
|
|
2530
|
-
conn.lastActive = monotonicNanos()
|
|
3073
|
+
this.restedMss[k] = mss > 0 ? mss : 0
|
|
2531
3074
|
return k
|
|
2532
3075
|
}
|
|
2533
3076
|
|
|
@@ -2541,12 +3084,12 @@ export class Http1Server {
|
|
|
2541
3084
|
* @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
|
|
2542
3085
|
*/
|
|
2543
3086
|
readable(slot: i32): i32 {
|
|
2544
|
-
|
|
3087
|
+
const conn: Http1Connection | null = this.attach(slot)
|
|
3088
|
+
if (conn === null) {
|
|
2545
3089
|
return H1_DONE
|
|
2546
3090
|
}
|
|
2547
3091
|
this.date.hold()
|
|
2548
|
-
const
|
|
2549
|
-
const fd: i32 = this.fds[slot]
|
|
3092
|
+
const fd: i32 = conn.fd
|
|
2550
3093
|
let moved: boolean = false
|
|
2551
3094
|
while (true) {
|
|
2552
3095
|
const room: i32 = conn.inputRoom()
|
|
@@ -2609,13 +3152,17 @@ export class Http1Server {
|
|
|
2609
3152
|
if (!this.holds(slot)) {
|
|
2610
3153
|
return H1_ERROR
|
|
2611
3154
|
}
|
|
2612
|
-
const conn: Http1Connection = this.
|
|
3155
|
+
const conn: Http1Connection | null = this.lent(slot)
|
|
3156
|
+
if (conn === null) {
|
|
3157
|
+
// A resting slot has read nothing since it was answered.
|
|
3158
|
+
return H1_NEED_MORE
|
|
3159
|
+
}
|
|
2613
3160
|
if (conn.lingering()) {
|
|
2614
3161
|
// Its `H1_ERROR` was answered before the lingering close began.
|
|
2615
3162
|
return H1_NEED_MORE
|
|
2616
3163
|
}
|
|
2617
3164
|
if (conn.writeHeld) {
|
|
2618
|
-
this.send(
|
|
3165
|
+
this.send(conn)
|
|
2619
3166
|
}
|
|
2620
3167
|
return conn.next()
|
|
2621
3168
|
}
|
|
@@ -2631,6 +3178,7 @@ export class Http1Server {
|
|
|
2631
3178
|
* shut and what the client still sends is drained, answering
|
|
2632
3179
|
* `H1_WANT_READ` until the client's end of stream, `lingerBytes`, or
|
|
2633
3180
|
* `expire` past `lingerTimeout`; `next` answers `H1_NEED_MORE` meanwhile.
|
|
3181
|
+
* A slot whose connection then rests gives it back (see the class comment).
|
|
2634
3182
|
*
|
|
2635
3183
|
* @param slot the slot `accept` answered
|
|
2636
3184
|
* @returns the `H1_WANT_` bits to arm the slot with, or `H1_DONE` when the program should `close` it
|
|
@@ -2638,7 +3186,11 @@ export class Http1Server {
|
|
|
2638
3186
|
flush(slot: i32): i32 {
|
|
2639
3187
|
this.date.unhold()
|
|
2640
3188
|
const wants: i32 = this.settle(slot)
|
|
2641
|
-
if (
|
|
3189
|
+
if (!this.holds(slot)) {
|
|
3190
|
+
return wants
|
|
3191
|
+
}
|
|
3192
|
+
if (wants !== H1_DONE) {
|
|
3193
|
+
this.rest(slot)
|
|
2642
3194
|
return wants
|
|
2643
3195
|
}
|
|
2644
3196
|
// A refusal is sent: shut the write side and drain what the client still
|
|
@@ -2646,28 +3198,32 @@ export class Http1Server {
|
|
|
2646
3198
|
// Closed with those bytes unread, the socket would answer them with a
|
|
2647
3199
|
// reset that can destroy the response before the client reads it (H1-2).
|
|
2648
3200
|
const now: i64 = monotonicNanos()
|
|
2649
|
-
const conn: Http1Connection = this.
|
|
2650
|
-
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) {
|
|
2651
3203
|
return H1_DONE
|
|
2652
3204
|
}
|
|
2653
3205
|
const deadline: i64 = conn.expiresNoSooner(now)
|
|
2654
3206
|
if (deadline < this.expireAfter) {
|
|
2655
3207
|
this.expireAfter = deadline
|
|
2656
3208
|
}
|
|
2657
|
-
return this.drain(
|
|
3209
|
+
return this.drain(conn)
|
|
2658
3210
|
}
|
|
2659
3211
|
|
|
2660
3212
|
/**
|
|
2661
|
-
* Reads and throws away what `
|
|
3213
|
+
* Reads and throws away what `conn`'s lingering peer sends until the
|
|
2662
3214
|
* socket would block, answering `H1_WANT_READ`; `H1_DONE` at its end of
|
|
2663
3215
|
* stream, an error, or past `lingerBytes`. What it drains is not activity:
|
|
2664
3216
|
* `lastActive` stays, and `expire` closes the slot past `lingerTimeout`.
|
|
2665
3217
|
*/
|
|
2666
|
-
private drain(
|
|
2667
|
-
const
|
|
2668
|
-
const fd: i32 = this.fds[slot]
|
|
3218
|
+
private drain(conn: Http1Connection): i32 {
|
|
3219
|
+
const fd: i32 = conn.fd
|
|
2669
3220
|
while (true) {
|
|
2670
|
-
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)
|
|
2671
3227
|
if (n === H1_AGAIN) {
|
|
2672
3228
|
return H1_WANT_READ
|
|
2673
3229
|
}
|
|
@@ -2682,11 +3238,15 @@ export class Http1Server {
|
|
|
2682
3238
|
if (!this.holds(slot)) {
|
|
2683
3239
|
return H1_DONE
|
|
2684
3240
|
}
|
|
2685
|
-
const conn: Http1Connection = this.
|
|
3241
|
+
const conn: Http1Connection | null = this.lent(slot)
|
|
3242
|
+
if (conn === null) {
|
|
3243
|
+
// A resting slot holds nothing to send and has room to read.
|
|
3244
|
+
return H1_WANT_READ
|
|
3245
|
+
}
|
|
2686
3246
|
if (conn.lingering()) {
|
|
2687
|
-
return this.drain(
|
|
3247
|
+
return this.drain(conn)
|
|
2688
3248
|
}
|
|
2689
|
-
this.send(
|
|
3249
|
+
this.send(conn)
|
|
2690
3250
|
if (conn.isDone()) {
|
|
2691
3251
|
return H1_DONE
|
|
2692
3252
|
}
|
|
@@ -2706,13 +3266,20 @@ export class Http1Server {
|
|
|
2706
3266
|
return wants
|
|
2707
3267
|
}
|
|
2708
3268
|
|
|
2709
|
-
/** Writes what
|
|
2710
|
-
private send(
|
|
2711
|
-
const
|
|
2712
|
-
const
|
|
2713
|
-
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)
|
|
2714
3273
|
while (conn.wantsWrite()) {
|
|
2715
|
-
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))
|
|
2716
3283
|
if (n <= 0) {
|
|
2717
3284
|
if (n !== H1_AGAIN) {
|
|
2718
3285
|
conn.abort()
|
|
@@ -2721,7 +3288,7 @@ export class Http1Server {
|
|
|
2721
3288
|
}
|
|
2722
3289
|
conn.consume(n)
|
|
2723
3290
|
}
|
|
2724
|
-
if (conn.outputEnd
|
|
3291
|
+
if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
|
|
2725
3292
|
conn.lastActive = monotonicNanos()
|
|
2726
3293
|
}
|
|
2727
3294
|
conn.release()
|
|
@@ -2742,27 +3309,41 @@ export class Http1Server {
|
|
|
2742
3309
|
*/
|
|
2743
3310
|
rearm(slot: i32, wants: i32): boolean {
|
|
2744
3311
|
const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
|
|
2745
|
-
if (!this.holds(slot)
|
|
3312
|
+
if (!this.holds(slot)) {
|
|
3313
|
+
return false
|
|
3314
|
+
}
|
|
3315
|
+
const armed: i32[] = this.armed
|
|
3316
|
+
if (slot < 0 || slot >= toI32(armed.length) || armed[slot] === interest) {
|
|
2746
3317
|
return false
|
|
2747
3318
|
}
|
|
2748
|
-
|
|
3319
|
+
armed[slot] = interest
|
|
2749
3320
|
return true
|
|
2750
3321
|
}
|
|
2751
3322
|
|
|
2752
3323
|
/**
|
|
2753
|
-
* Closes `slot`'s socket, whatever is left unsent, and frees the slot.
|
|
3324
|
+
* Closes `slot`'s socket, whatever is left unsent, and frees the slot. Its
|
|
3325
|
+
* connection, when it holds one, is aborted and restarted, and is spare.
|
|
2754
3326
|
*
|
|
2755
3327
|
* @param slot the slot to close; a free or unknown one is ignored
|
|
2756
3328
|
*/
|
|
2757
3329
|
close(slot: i32): void {
|
|
2758
|
-
|
|
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) {
|
|
2759
3333
|
return
|
|
2760
3334
|
}
|
|
2761
|
-
|
|
2762
|
-
|
|
3335
|
+
const fd: i32 = fds[slot]
|
|
3336
|
+
const k: i32 = at[slot]
|
|
3337
|
+
fds[slot] = H1_FREE
|
|
3338
|
+
at[slot] = -1
|
|
3339
|
+
netClose(fd)
|
|
2763
3340
|
this.free.push(slot)
|
|
2764
|
-
|
|
2765
|
-
|
|
3341
|
+
const connections: Http1Connection[] = this.connections
|
|
3342
|
+
if (k >= 0 && k < toI32(connections.length)) {
|
|
3343
|
+
const conn: Http1Connection = connections[k]
|
|
3344
|
+
conn.abort()
|
|
3345
|
+
conn.restart()
|
|
3346
|
+
this.spare.push(k)
|
|
2766
3347
|
}
|
|
2767
3348
|
}
|
|
2768
3349
|
|
|
@@ -2777,22 +3358,44 @@ export class Http1Server {
|
|
|
2777
3358
|
if (now < this.expireAfter) {
|
|
2778
3359
|
return 0
|
|
2779
3360
|
}
|
|
2780
|
-
|
|
3361
|
+
const scans: i32 = this.expireScans
|
|
3362
|
+
this.expireScans = scans < 2147483647 ? scans + 1 : scans
|
|
2781
3363
|
let soonest: i64 = http1ExpireFloor(this.config)
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
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) {
|
|
3377
|
+
// A resting slot is idle past `idleTimeout`, and a head that
|
|
3378
|
+
// starts arriving no sooner than now has `headTimeout` from now.
|
|
3379
|
+
if (http1ServerPast(now, since, idle)) {
|
|
3380
|
+
this.close(k)
|
|
3381
|
+
n = n + toU32(1)
|
|
3382
|
+
} else {
|
|
3383
|
+
const quiet: i64 = http1ServerAfter(since, idle)
|
|
3384
|
+
const sooner: i64 = quiet < head ? quiet : head
|
|
3385
|
+
if (sooner < soonest) {
|
|
3386
|
+
soonest = sooner
|
|
3387
|
+
}
|
|
3388
|
+
}
|
|
3389
|
+
} else if (conn.expired(now)) {
|
|
2787
3390
|
this.close(k)
|
|
2788
|
-
n = n + 1
|
|
3391
|
+
n = n + toU32(1)
|
|
2789
3392
|
} else if (conn.expiresNoSooner(now) < soonest) {
|
|
2790
3393
|
soonest = conn.expiresNoSooner(now)
|
|
2791
3394
|
}
|
|
2792
3395
|
}
|
|
2793
3396
|
}
|
|
2794
3397
|
this.expireAfter = soonest
|
|
2795
|
-
return n
|
|
3398
|
+
return toI32(n)
|
|
2796
3399
|
}
|
|
2797
3400
|
}
|
|
2798
3401
|
|
|
@@ -2856,7 +3459,7 @@ export class Http1TlsServer {
|
|
|
2856
3459
|
this.inputs = new Http1OutputPool(HTTP1_FIRST_INPUT)
|
|
2857
3460
|
this.heads = new Http1OutputPool(HTTP1_FIRST_HEAD)
|
|
2858
3461
|
this.date = new HttpDate()
|
|
2859
|
-
this.scratch =
|
|
3462
|
+
this.scratch = http1ServerBytes(config.chunkSize)
|
|
2860
3463
|
}
|
|
2861
3464
|
|
|
2862
3465
|
/**
|
|
@@ -2920,12 +3523,17 @@ export class Http1TlsServer {
|
|
|
2920
3523
|
}
|
|
2921
3524
|
|
|
2922
3525
|
/**
|
|
2923
|
-
*
|
|
2924
|
-
*
|
|
2925
|
-
* 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.
|
|
2926
3530
|
*/
|
|
2927
|
-
private live(slot: i32):
|
|
2928
|
-
|
|
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
|
|
2929
3537
|
}
|
|
2930
3538
|
|
|
2931
3539
|
/**
|
|
@@ -2989,9 +3597,12 @@ export class Http1TlsServer {
|
|
|
2989
3597
|
while (toI32(this.connections.length) <= slot) {
|
|
2990
3598
|
this.build()
|
|
2991
3599
|
}
|
|
2992
|
-
const
|
|
2993
|
-
|
|
2994
|
-
|
|
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
|
+
}
|
|
2995
3606
|
}
|
|
2996
3607
|
return slot
|
|
2997
3608
|
}
|
|
@@ -3003,20 +3614,20 @@ export class Http1TlsServer {
|
|
|
3003
3614
|
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
3004
3615
|
*/
|
|
3005
3616
|
readable(slot: i32): i32 {
|
|
3006
|
-
const live:
|
|
3617
|
+
const live: Http1Connection | null = this.live(slot)
|
|
3007
3618
|
const now: i64 = monotonicNanos()
|
|
3008
3619
|
// What a lingering slot drains is not activity, so `idleTimeout` bounds
|
|
3009
3620
|
// how long a client that trickles bytes after the close can hold it.
|
|
3010
|
-
if (live && !this.tls.lingering(slot)) {
|
|
3011
|
-
|
|
3621
|
+
if (live !== null && !this.tls.lingering(slot)) {
|
|
3622
|
+
live.lastActive = now
|
|
3012
3623
|
}
|
|
3013
3624
|
this.tls.readable(slot)
|
|
3014
3625
|
// Ciphertext after the handshake is the start of a head, or of a record
|
|
3015
3626
|
// that will hold one, so the head's clock starts here; a record trickled
|
|
3016
3627
|
// a byte at a time would hand `next` no plaintext until it is whole. The
|
|
3017
3628
|
// handshake itself is bounded by `idleTimeout` alone (TLS-4).
|
|
3018
|
-
if (live && this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN) {
|
|
3019
|
-
|
|
3629
|
+
if (live !== null && this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN) {
|
|
3630
|
+
live.headAwaited(now)
|
|
3020
3631
|
}
|
|
3021
3632
|
return this.flush(slot)
|
|
3022
3633
|
}
|
|
@@ -3053,10 +3664,10 @@ export class Http1TlsServer {
|
|
|
3053
3664
|
* @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
|
|
3054
3665
|
*/
|
|
3055
3666
|
next(slot: i32): i32 {
|
|
3056
|
-
|
|
3667
|
+
const conn: Http1Connection | null = this.live(slot)
|
|
3668
|
+
if (conn === null || this.refused(slot)) {
|
|
3057
3669
|
return H1_ERROR
|
|
3058
3670
|
}
|
|
3059
|
-
const conn: Http1Connection = this.connections[slot]
|
|
3060
3671
|
while (true) {
|
|
3061
3672
|
const room: i32 = conn.inputRoom()
|
|
3062
3673
|
const want: i32 = room < toI32(this.scratch.length) ? room : toI32(this.scratch.length)
|
|
@@ -3074,7 +3685,7 @@ export class Http1TlsServer {
|
|
|
3074
3685
|
}
|
|
3075
3686
|
// A held-back write finds its room here, as in `Http1Server.next`.
|
|
3076
3687
|
if (conn.writeHeld) {
|
|
3077
|
-
this.send(slot)
|
|
3688
|
+
this.send(slot, conn)
|
|
3078
3689
|
}
|
|
3079
3690
|
return conn.next()
|
|
3080
3691
|
}
|
|
@@ -3089,14 +3700,14 @@ export class Http1TlsServer {
|
|
|
3089
3700
|
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
3090
3701
|
*/
|
|
3091
3702
|
flush(slot: i32): i32 {
|
|
3092
|
-
|
|
3703
|
+
const conn: Http1Connection | null = this.live(slot)
|
|
3704
|
+
if (conn === null) {
|
|
3093
3705
|
return TLS_RECORD_DONE
|
|
3094
3706
|
}
|
|
3095
3707
|
if (this.refused(slot)) {
|
|
3096
3708
|
return this.tls.shutdown(slot)
|
|
3097
3709
|
}
|
|
3098
|
-
|
|
3099
|
-
this.send(slot)
|
|
3710
|
+
this.send(slot, conn)
|
|
3100
3711
|
if (conn.isDone()) {
|
|
3101
3712
|
return this.tls.shutdown(slot)
|
|
3102
3713
|
}
|
|
@@ -3108,24 +3719,22 @@ export class Http1TlsServer {
|
|
|
3108
3719
|
: wants
|
|
3109
3720
|
}
|
|
3110
3721
|
|
|
3111
|
-
/**
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
*/
|
|
3115
|
-
private send(slot: i32): void {
|
|
3116
|
-
if (slot < 0 || slot >= toI32(this.connections.length)) {
|
|
3117
|
-
return
|
|
3118
|
-
}
|
|
3119
|
-
const conn: Http1Connection = this.connections[slot]
|
|
3120
|
-
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)
|
|
3121
3725
|
while (conn.wantsWrite()) {
|
|
3122
|
-
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
|
+
)
|
|
3123
3732
|
if (n <= 0) {
|
|
3124
3733
|
break
|
|
3125
3734
|
}
|
|
3126
3735
|
conn.consume(n)
|
|
3127
3736
|
}
|
|
3128
|
-
if (conn.outputEnd
|
|
3737
|
+
if (http1ServerSub(conn.outputEnd, conn.outputStart) < pending) {
|
|
3129
3738
|
conn.lastActive = monotonicNanos()
|
|
3130
3739
|
}
|
|
3131
3740
|
conn.release()
|
|
@@ -3138,8 +3747,9 @@ export class Http1TlsServer {
|
|
|
3138
3747
|
*/
|
|
3139
3748
|
close(slot: i32): void {
|
|
3140
3749
|
this.tls.close(slot)
|
|
3141
|
-
|
|
3142
|
-
|
|
3750
|
+
const connections: Http1Connection[] = this.connections
|
|
3751
|
+
if (slot >= 0 && slot < toI32(connections.length)) {
|
|
3752
|
+
connections[slot].abort()
|
|
3143
3753
|
}
|
|
3144
3754
|
}
|
|
3145
3755
|
|
|
@@ -3154,21 +3764,23 @@ export class Http1TlsServer {
|
|
|
3154
3764
|
if (now < this.expireAfter) {
|
|
3155
3765
|
return 0
|
|
3156
3766
|
}
|
|
3157
|
-
|
|
3767
|
+
const scans: i32 = this.expireScans
|
|
3768
|
+
this.expireScans = scans < 2147483647 ? scans + 1 : scans
|
|
3158
3769
|
let soonest: i64 = http1ExpireFloor(this.config)
|
|
3159
|
-
|
|
3770
|
+
// In u32, as `Http1Server.expire` counts.
|
|
3771
|
+
let n: u32 = 0
|
|
3160
3772
|
for (let k: i32 = 0; k < this.tls.size() && k < toI32(this.connections.length); k++) {
|
|
3161
|
-
|
|
3162
|
-
|
|
3773
|
+
const conn: Http1Connection | null = this.live(k)
|
|
3774
|
+
if (conn !== null) {
|
|
3163
3775
|
if (conn.expired(now)) {
|
|
3164
3776
|
this.tls.close(k)
|
|
3165
|
-
n = n + 1
|
|
3777
|
+
n = n + toU32(1)
|
|
3166
3778
|
} else if (conn.expiresNoSooner(now) < soonest) {
|
|
3167
3779
|
soonest = conn.expiresNoSooner(now)
|
|
3168
3780
|
}
|
|
3169
3781
|
}
|
|
3170
3782
|
}
|
|
3171
3783
|
this.expireAfter = soonest
|
|
3172
|
-
return n
|
|
3784
|
+
return toI32(n)
|
|
3173
3785
|
}
|
|
3174
3786
|
}
|