@amritk/nish-aarch64-linux 0.35.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/net/http1-server.ts
CHANGED
|
@@ -45,15 +45,16 @@
|
|
|
45
45
|
*
|
|
46
46
|
* **Streaming both ways.** A request body arrives a chunk at a time, at most
|
|
47
47
|
* `chunkSize` bytes in each `H1_BODY`, whether it was sent with
|
|
48
|
-
* `Content-Length` or chunked. A response is written with a length or
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* a
|
|
56
|
-
*
|
|
48
|
+
* `Content-Length` or chunked. A response is written with a length or chunked,
|
|
49
|
+
* a `write` at a time into the output buffer; a `write` takes what fits and
|
|
50
|
+
* answers how much, and when it could not take everything the connection
|
|
51
|
+
* answers `H1_NEED_MORE` until the socket has drained half the output, then
|
|
52
|
+
* `H1_WRITE`. Until then it reads nothing, so the window the program is writing
|
|
53
|
+
* from stays valid: back-pressure on the way out.
|
|
54
|
+
*
|
|
55
|
+
* Nothing buffers a whole body. On an `Http1Server` slot `sendBody` sends a
|
|
56
|
+
* body with a length from the program's own array, the queued output with it,
|
|
57
|
+
* and copies only what the socket did not take, under the same rules.
|
|
57
58
|
*
|
|
58
59
|
* **What the server decides.** The framing field (`Content-Length` or
|
|
59
60
|
* `Transfer-Encoding: chunked`) and the `Connection` field are the server's;
|
|
@@ -92,18 +93,20 @@
|
|
|
92
93
|
* instead of requests, the bytes that followed the request included. A ping
|
|
93
94
|
* is answered with a pong and a close with a close, by the connection.
|
|
94
95
|
*
|
|
95
|
-
* **Caps are start-up numbers**, all in `Http1Config`: the request target,
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
96
|
+
* **Caps are start-up numbers**, all in `Http1Config`: the request target, the
|
|
97
|
+
* header section and its field count, a request body, the chunk handed over,
|
|
98
|
+
* the output buffer, a WebSocket message, the idle timeout and the deadline for
|
|
99
|
+
* a whole request head (`headTimeout`, against slowloris); the slots are the
|
|
100
|
+
* pool's size.
|
|
101
|
+
*
|
|
102
|
+
* `Http1Server` builds a slot's connection on the first accept into it and
|
|
103
|
+
* keeps it, so a pool sized for its peak costs at start-up only what does not
|
|
104
|
+
* depend on the size, and holds at most `size` connections once the peak has
|
|
105
|
+
* come; `Http1TlsServer` does the same for a slot's connection and, through
|
|
106
|
+
* `TlsTcpServer`, its TLS state. Every buffer is allocated with its slot or
|
|
107
|
+
* grown to its cap on first use and kept, so a warmed slot serves request after
|
|
108
|
+
* request with `Arena.used()` flat. Accepting a WebSocket allocates (the accept
|
|
109
|
+
* key), once a connection.
|
|
107
110
|
*
|
|
108
111
|
* **Output is borrowed.** The pools' slots share an `Http1OutputPool`: a
|
|
109
112
|
* connection takes an `outputSize` buffer when it first needs room to write
|
|
@@ -197,6 +200,7 @@ export const H1_POOL_FULL: i32 = -105
|
|
|
197
200
|
|
|
198
201
|
/** Interest bits, the same as `nish/net/tls/record-server`'s: the low two are `pollModify`'s events. */
|
|
199
202
|
export const H1_WANT_READ: i32 = 1
|
|
203
|
+
/** The slot has output waiting for the socket: ask the readiness loop for writable. */
|
|
200
204
|
export const H1_WANT_WRITE: i32 = 2
|
|
201
205
|
/** The slot's connection is over and every byte is sent: `close` it. */
|
|
202
206
|
export const H1_DONE: i32 = 16
|
|
@@ -286,19 +290,19 @@ const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i3
|
|
|
286
290
|
*/
|
|
287
291
|
export class HttpDate {
|
|
288
292
|
/** The IMF-fixdate of `second`: what `current` answers, made once. */
|
|
289
|
-
bytes: u8[]
|
|
293
|
+
private bytes: u8[]
|
|
290
294
|
/** The second `bytes` holds, since 1970; -1 before the first `current`. */
|
|
291
|
-
second: i64 = -1
|
|
295
|
+
private second: i64 = -1
|
|
292
296
|
/** The instant `pin` fixed, in milliseconds since 1970, while `pinned`. */
|
|
293
|
-
pinnedMs: i64 = 0
|
|
297
|
+
private pinnedMs: i64 = 0
|
|
294
298
|
/** The clock reading `hold` took, in milliseconds since 1970, while `held`. */
|
|
295
|
-
heldMs: i64 = 0
|
|
299
|
+
private heldMs: i64 = 0
|
|
296
300
|
/** How many times `bytes` has been rewritten. */
|
|
297
|
-
rewrites: i64 = 0
|
|
301
|
+
private rewrites: i64 = 0
|
|
298
302
|
/** How many times the wall clock has been read. */
|
|
299
|
-
reads: i64 = 0
|
|
300
|
-
pinned: boolean = false
|
|
301
|
-
held: boolean = false
|
|
303
|
+
private reads: i64 = 0
|
|
304
|
+
private pinned: boolean = false
|
|
305
|
+
private held: boolean = false
|
|
302
306
|
|
|
303
307
|
/** A date on the wall clock, with nothing formatted yet. */
|
|
304
308
|
constructor() {
|
|
@@ -311,6 +315,8 @@ export class HttpDate {
|
|
|
311
315
|
* steps back. It allocates nothing, and the array is the same one each time,
|
|
312
316
|
* so a caller copies it out rather than keeping it. A wall clock before 1970
|
|
313
317
|
* or past 9999 answers the nearer of the two ends.
|
|
318
|
+
*
|
|
319
|
+
* @returns the object's own 29-byte IMF-fixdate of the current second, to copy before the next call rewrites it
|
|
314
320
|
*/
|
|
315
321
|
current(): u8[] {
|
|
316
322
|
let ms: i64 = this.pinnedMs
|
|
@@ -335,6 +341,9 @@ export class HttpDate {
|
|
|
335
341
|
* Makes `current` answer `unixMs`, milliseconds since 1970, until `unpin`.
|
|
336
342
|
* False, and nothing changed, for an instant before 1970-01-01T00:00:00Z or
|
|
337
343
|
* after 9999-12-31T23:59:59.999Z, which an IMF-fixdate cannot name.
|
|
344
|
+
*
|
|
345
|
+
* @param unixMs the instant every later `current` names, in milliseconds since 1970
|
|
346
|
+
* @returns true once the date is pinned; false when the instant is out of the IMF-fixdate's range
|
|
338
347
|
*/
|
|
339
348
|
pin(unixMs: i64): boolean {
|
|
340
349
|
if (unixMs < 0 || unixMs / 1000 > HTTP_DATE_LAST_SECOND) {
|
|
@@ -368,17 +377,25 @@ export class HttpDate {
|
|
|
368
377
|
}
|
|
369
378
|
|
|
370
379
|
/** The wall clock, in milliseconds since 1970. */
|
|
371
|
-
now(): i64 {
|
|
380
|
+
private now(): i64 {
|
|
372
381
|
this.reads += 1
|
|
373
382
|
return toI64(Date.now())
|
|
374
383
|
}
|
|
375
384
|
|
|
376
|
-
/**
|
|
385
|
+
/**
|
|
386
|
+
* How many times the wall clock has been read: once per `hold`, and once per `current` outside one.
|
|
387
|
+
*
|
|
388
|
+
* @returns the count of `Date.now()` reads since the date was made, for a test that checks a serve call reads once
|
|
389
|
+
*/
|
|
377
390
|
clockReads(): i64 {
|
|
378
391
|
return this.reads
|
|
379
392
|
}
|
|
380
393
|
|
|
381
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* How many times the bytes have been rewritten: one per second that `current` was asked for, at most.
|
|
396
|
+
*
|
|
397
|
+
* @returns the count of IMF-fixdate formats since the date was made, for a test that checks the once-a-second promise
|
|
398
|
+
*/
|
|
382
399
|
formats(): i64 {
|
|
383
400
|
return this.rewrites
|
|
384
401
|
}
|
|
@@ -494,17 +511,27 @@ const http1ServerReserved = (name: u8[]): boolean =>
|
|
|
494
511
|
*/
|
|
495
512
|
export class Http1OutputPool {
|
|
496
513
|
// Widest first, so the layout packs without padding.
|
|
514
|
+
/** The buffers given back and not yet taken again; `made - free.length` is how many are out. */
|
|
497
515
|
free: u8[][]
|
|
498
|
-
size: i32 = 0
|
|
516
|
+
private size: i32 = 0
|
|
499
517
|
/** How many buffers the pool has made. */
|
|
500
518
|
made: i32 = 0
|
|
501
519
|
|
|
520
|
+
/**
|
|
521
|
+
* An empty pool: no buffer is made until the first `take`.
|
|
522
|
+
*
|
|
523
|
+
* @param size the length in bytes of every buffer the pool makes
|
|
524
|
+
*/
|
|
502
525
|
constructor(size: i32) {
|
|
503
526
|
this.size = size
|
|
504
527
|
this.free = []
|
|
505
528
|
}
|
|
506
529
|
|
|
507
|
-
/**
|
|
530
|
+
/**
|
|
531
|
+
* A buffer of `size` bytes: the one given back last, or a new one.
|
|
532
|
+
*
|
|
533
|
+
* @internal Called by `Http1Connection` in this module, when it first needs room to write and when it unparks its input and head buffers.
|
|
534
|
+
*/
|
|
508
535
|
take(): u8[] {
|
|
509
536
|
if (this.free.length > 0) {
|
|
510
537
|
const buf: u8[] = this.free[this.free.length - 1]
|
|
@@ -515,7 +542,11 @@ export class Http1OutputPool {
|
|
|
515
542
|
return new Array<u8>(this.size)
|
|
516
543
|
}
|
|
517
544
|
|
|
518
|
-
/**
|
|
545
|
+
/**
|
|
546
|
+
* Takes back a buffer `take` gave.
|
|
547
|
+
*
|
|
548
|
+
* @internal Called by `Http1Connection` in this module, from `release` and `park`, once a borrowed buffer holds nothing it still needs.
|
|
549
|
+
*/
|
|
519
550
|
give(buf: u8[]): void {
|
|
520
551
|
this.free.push(buf)
|
|
521
552
|
}
|
|
@@ -552,6 +583,7 @@ export class Http1Connection {
|
|
|
552
583
|
pool: Http1OutputPool | null
|
|
553
584
|
/** Where the parser's input and head buffers go while the slot rests, with `Http1Config.idleInput`; null otherwise. */
|
|
554
585
|
inputs: Http1OutputPool | null
|
|
586
|
+
/** Where the parser's head buffer goes while the slot rests, beside `inputs`; null otherwise. */
|
|
555
587
|
heads: Http1OutputPool | null
|
|
556
588
|
/**
|
|
557
589
|
* The `Date` every final response carries. The connection makes its own;
|
|
@@ -564,16 +596,24 @@ export class Http1Connection {
|
|
|
564
596
|
lastActive: i64 = 0
|
|
565
597
|
/** When the head being read began to arrive, in `monotonicNanos`; meaningful while `headTimed`. */
|
|
566
598
|
headSince: i64 = 0
|
|
567
|
-
state: i32 = 0
|
|
568
|
-
response: i32 = 0
|
|
599
|
+
private state: i32 = 0
|
|
600
|
+
private response: i32 = 0
|
|
569
601
|
/**
|
|
570
602
|
* How the response's body is framed: a length (0 or more), `HTTP1_CHUNKED`,
|
|
571
603
|
* or `HTTP1_NO_BODY` for one that ends with the connection.
|
|
604
|
+
*
|
|
605
|
+
* @internal Read by `std/net/handler.ts` to tell a sized response from a chunked one when it checks a write against what is owed.
|
|
572
606
|
*/
|
|
573
607
|
responseFraming: i32 = 0
|
|
574
|
-
/**
|
|
608
|
+
/**
|
|
609
|
+
* A response with a length: the bytes it still owes.
|
|
610
|
+
*
|
|
611
|
+
* @internal Read by `std/net/handler.ts` to know how much of a sized body is still to write.
|
|
612
|
+
*/
|
|
575
613
|
responseLeft: i32 = 0
|
|
614
|
+
/** Where the unsent bytes start in `output`; a carrier sends from here and calls `consume` with what it sent. */
|
|
576
615
|
outputStart: i32 = 0
|
|
616
|
+
/** Where the unsent bytes end in `output`, one past the last; equal to `outputStart` when nothing waits. */
|
|
577
617
|
outputEnd: i32 = 0
|
|
578
618
|
/**
|
|
579
619
|
* `output.length`, kept beside the offsets so that `acquire` and `release`
|
|
@@ -581,33 +621,35 @@ export class Http1Connection {
|
|
|
581
621
|
* the empty one is out of cache by the time a resting connection's next
|
|
582
622
|
* request comes. Only the connection assigns `output`, and with it this.
|
|
583
623
|
*/
|
|
584
|
-
outputCap: i32 = 0
|
|
624
|
+
private outputCap: i32 = 0
|
|
585
625
|
/** The most input the connection buffers, unread: the cap `inputRoom` holds it to. */
|
|
586
626
|
bufferLimit: i32 = 0
|
|
587
627
|
/** A request's head has been read and its exchange is not over. */
|
|
588
|
-
requestOpen: boolean = false
|
|
628
|
+
private requestOpen: boolean = false
|
|
589
629
|
/** The request's body has been read to its end. */
|
|
590
|
-
requestEnded: boolean = false
|
|
630
|
+
private requestEnded: boolean = false
|
|
591
631
|
/** The response carries no body (HEAD, 204, 304): what is written is dropped. */
|
|
592
632
|
responseEmpty: boolean = false
|
|
593
633
|
/** The connection closes once this exchange is over. */
|
|
594
634
|
closeAfter: boolean = false
|
|
595
635
|
/** The program accepted the request's WebSocket handshake. */
|
|
596
|
-
upgraded: boolean = false
|
|
636
|
+
private upgraded: boolean = false
|
|
597
637
|
/** A write was held back; `next` answers nothing but `H1_WRITE` until there is room. */
|
|
598
638
|
writeHeld: boolean = false
|
|
599
|
-
pongPending: boolean = false
|
|
639
|
+
private pongPending: boolean = false
|
|
600
640
|
/** The peer's byte stream has ended. */
|
|
601
|
-
inputEnded: boolean = false
|
|
641
|
+
private inputEnded: boolean = false
|
|
602
642
|
/** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
|
|
603
643
|
headTimed: boolean = false
|
|
644
|
+
/** Where the bytes `H1_BODY` or `H1_WS_MESSAGE` handed over start in `data`. */
|
|
604
645
|
dataStart: i32 = 0
|
|
646
|
+
/** How many bytes `H1_BODY` or `H1_WS_MESSAGE` handed over, from `data[dataStart]`. */
|
|
605
647
|
dataLength: i32 = 0
|
|
606
648
|
/** `H1_WS_MESSAGE`'s opcode: text or binary. */
|
|
607
649
|
opcode: i32 = 0
|
|
608
650
|
/** `H1_ERROR`'s HTTP status or WebSocket close code, or `H1_WS_CLOSE`'s close code. */
|
|
609
651
|
status: i32 = 0
|
|
610
|
-
pongLength: i32 = 0
|
|
652
|
+
private pongLength: i32 = 0
|
|
611
653
|
/**
|
|
612
654
|
* The TCP maximum segment size the program sets for this connection, for
|
|
613
655
|
* `respondEncoded` under `HttpCompressor.segmentMode`; 0, the default,
|
|
@@ -625,7 +667,7 @@ export class Http1Connection {
|
|
|
625
667
|
* as `write` does.
|
|
626
668
|
*/
|
|
627
669
|
fd: i32 = -1
|
|
628
|
-
config: Http1Config
|
|
670
|
+
private config: Http1Config
|
|
629
671
|
/**
|
|
630
672
|
* The frame decoder: made when the slot first switches to WebSocket, so a
|
|
631
673
|
* slot that only ever speaks HTTP never holds its buffers, then kept for
|
|
@@ -637,9 +679,13 @@ export class Http1Connection {
|
|
|
637
679
|
/** A ping's payload, kept until its pong fits: empty until the first ping. */
|
|
638
680
|
pong: u8[]
|
|
639
681
|
/** No fields, for the responses the connection writes itself: made once. */
|
|
640
|
-
noFields: u8[][]
|
|
682
|
+
private noFields: u8[][]
|
|
641
683
|
|
|
642
|
-
/**
|
|
684
|
+
/**
|
|
685
|
+
* A connection under `config`. A cap outside what the connection can work with panics.
|
|
686
|
+
*
|
|
687
|
+
* @param config the caps the connection is held to, read now and kept
|
|
688
|
+
*/
|
|
643
689
|
constructor(config: Http1Config) {
|
|
644
690
|
const big: i32 = 1073741824
|
|
645
691
|
const one: i32 = 1
|
|
@@ -780,12 +826,20 @@ export class Http1Connection {
|
|
|
780
826
|
|
|
781
827
|
// ---- Bytes in and out ---------------------------------------------------------
|
|
782
828
|
|
|
783
|
-
/**
|
|
829
|
+
/**
|
|
830
|
+
* The most input the connection holds unread: what `inputRoom` counts against.
|
|
831
|
+
*
|
|
832
|
+
* @returns the cap in bytes on unread input: the buffer limit for HTTP, the message cap plus a frame header for WebSocket
|
|
833
|
+
*/
|
|
784
834
|
inputLimit(): i32 {
|
|
785
835
|
return this.state === H1_S_WEBSOCKET ? this.config.maxMessage + 14 : this.bufferLimit
|
|
786
836
|
}
|
|
787
837
|
|
|
788
|
-
/**
|
|
838
|
+
/**
|
|
839
|
+
* How many bytes `feed` would take now: none while a write is held back or once the connection is closing.
|
|
840
|
+
*
|
|
841
|
+
* @returns how many more input bytes the connection takes now; 0 asks the carrier to stop reading
|
|
842
|
+
*/
|
|
789
843
|
inputRoom(): i32 {
|
|
790
844
|
if (this.writeHeld || this.inputEnded || this.state === H1_S_CLOSING || this.state === H1_S_FAILED) {
|
|
791
845
|
return 0
|
|
@@ -806,6 +860,11 @@ export class Http1Connection {
|
|
|
806
860
|
* Takes as much of `buf[off .. off + len)` as `inputRoom` allows and
|
|
807
861
|
* answers how much; the rest is the caller's to feed again later. A window
|
|
808
862
|
* outside `buf` panics.
|
|
863
|
+
*
|
|
864
|
+
* @param buf the bytes the carrier read from the peer
|
|
865
|
+
* @param off where in `buf` the new bytes start
|
|
866
|
+
* @param len how many new bytes there are
|
|
867
|
+
* @returns how many of them the connection took, from `off`; the rest is the caller's to feed again
|
|
809
868
|
*/
|
|
810
869
|
feed(buf: u8[], off: i32, len: i32): i32 {
|
|
811
870
|
http1ServerCheckWindow("Http1Connection.feed", buf, off, len)
|
|
@@ -845,7 +904,7 @@ export class Http1Connection {
|
|
|
845
904
|
* is written into it, which is why `room` calls it. It allocates only when
|
|
846
905
|
* the pool has no free buffer, or for a connection's own, once.
|
|
847
906
|
*/
|
|
848
|
-
acquire(): void {
|
|
907
|
+
private acquire(): void {
|
|
849
908
|
if (this.outputCap > 0) {
|
|
850
909
|
return
|
|
851
910
|
}
|
|
@@ -874,7 +933,11 @@ export class Http1Connection {
|
|
|
874
933
|
this.outputEnd = 0
|
|
875
934
|
}
|
|
876
935
|
|
|
877
|
-
/**
|
|
936
|
+
/**
|
|
937
|
+
* Marks the first `n` bytes of the output sent.
|
|
938
|
+
*
|
|
939
|
+
* @param n how many bytes from `outputStart` the carrier sent; more than are waiting counts as all of them
|
|
940
|
+
*/
|
|
878
941
|
consume(n: i32): void {
|
|
879
942
|
const pending: i32 = this.outputEnd - this.outputStart
|
|
880
943
|
this.outputStart = this.outputStart + (n < pending ? n : pending)
|
|
@@ -884,12 +947,20 @@ export class Http1Connection {
|
|
|
884
947
|
}
|
|
885
948
|
}
|
|
886
949
|
|
|
887
|
-
/**
|
|
950
|
+
/**
|
|
951
|
+
* Whether the output holds bytes to send.
|
|
952
|
+
*
|
|
953
|
+
* @returns true while `output[outputStart .. outputEnd)` is not empty, so the carrier should wait for writable
|
|
954
|
+
*/
|
|
888
955
|
wantsWrite(): boolean {
|
|
889
956
|
return this.outputEnd > this.outputStart
|
|
890
957
|
}
|
|
891
958
|
|
|
892
|
-
/**
|
|
959
|
+
/**
|
|
960
|
+
* Whether the connection is over and everything is sent: the carrier closes it.
|
|
961
|
+
*
|
|
962
|
+
* @returns true once the connection is closing or failed and no byte or pong is left to send
|
|
963
|
+
*/
|
|
893
964
|
isDone(): boolean {
|
|
894
965
|
return (
|
|
895
966
|
(this.state === H1_S_CLOSING || this.state === H1_S_FAILED) && !this.wantsWrite() && !this.pongPending
|
|
@@ -897,7 +968,7 @@ export class Http1Connection {
|
|
|
897
968
|
}
|
|
898
969
|
|
|
899
970
|
/** The room at the end of the output, once what was sent is moved out of the way. */
|
|
900
|
-
room(): i32 {
|
|
971
|
+
private room(): i32 {
|
|
901
972
|
this.acquire()
|
|
902
973
|
const output: u8[] = this.output
|
|
903
974
|
const live: i32 = this.outputEnd - this.outputStart
|
|
@@ -912,14 +983,18 @@ export class Http1Connection {
|
|
|
912
983
|
}
|
|
913
984
|
|
|
914
985
|
/** The room a response or a frame may use: all but the control reserve. */
|
|
915
|
-
bodyRoom(): i32 {
|
|
986
|
+
private bodyRoom(): i32 {
|
|
916
987
|
const r: i32 = this.room() - H1_CONTROL_RESERVE
|
|
917
988
|
return r > 0 ? r : 0
|
|
918
989
|
}
|
|
919
990
|
|
|
920
991
|
// ---- Events -------------------------------------------------------------------
|
|
921
992
|
|
|
922
|
-
/**
|
|
993
|
+
/**
|
|
994
|
+
* The next event; see the module comment.
|
|
995
|
+
*
|
|
996
|
+
* @returns one of the `H1_` events: `H1_NEED_MORE` when there is nothing to hand over yet, `H1_ERROR` once the connection has failed
|
|
997
|
+
*/
|
|
923
998
|
next(): i32 {
|
|
924
999
|
if (this.state === H1_S_FAILED) {
|
|
925
1000
|
return H1_ERROR
|
|
@@ -943,7 +1018,7 @@ export class Http1Connection {
|
|
|
943
1018
|
}
|
|
944
1019
|
|
|
945
1020
|
/** `next` while the connection speaks HTTP. */
|
|
946
|
-
nextRequest(): i32 {
|
|
1021
|
+
private nextRequest(): i32 {
|
|
947
1022
|
while (true) {
|
|
948
1023
|
if (this.requestEnded) {
|
|
949
1024
|
if (this.response !== H1_R_DONE) {
|
|
@@ -1014,7 +1089,7 @@ export class Http1Connection {
|
|
|
1014
1089
|
* Refuses the request with `status`: the response that says so, when none
|
|
1015
1090
|
* has started, and the close after it. Answers `H1_ERROR`.
|
|
1016
1091
|
*/
|
|
1017
|
-
fail(status: i32): i32 {
|
|
1092
|
+
private fail(status: i32): i32 {
|
|
1018
1093
|
this.status = status
|
|
1019
1094
|
if (this.response === H1_R_NONE) {
|
|
1020
1095
|
// The control reserve keeps room for this whatever the output holds.
|
|
@@ -1040,7 +1115,7 @@ export class Http1Connection {
|
|
|
1040
1115
|
}
|
|
1041
1116
|
|
|
1042
1117
|
/** The frame decoder, made on the slot's first switch to WebSocket. */
|
|
1043
|
-
decoder(): WsDecoder {
|
|
1118
|
+
private decoder(): WsDecoder {
|
|
1044
1119
|
const ws: WsDecoder | null = this.ws
|
|
1045
1120
|
if (ws !== null) {
|
|
1046
1121
|
return ws
|
|
@@ -1051,7 +1126,7 @@ export class Http1Connection {
|
|
|
1051
1126
|
}
|
|
1052
1127
|
|
|
1053
1128
|
/** `next` once the slot has switched to WebSocket. */
|
|
1054
|
-
nextFrame(): i32 {
|
|
1129
|
+
private nextFrame(): i32 {
|
|
1055
1130
|
while (true) {
|
|
1056
1131
|
if (this.pongPending && !this.sendPong()) {
|
|
1057
1132
|
return H1_NEED_MORE
|
|
@@ -1096,7 +1171,7 @@ export class Http1Connection {
|
|
|
1096
1171
|
}
|
|
1097
1172
|
|
|
1098
1173
|
/** Writes the pending pong if it fits, from the control reserve; answers whether it went. */
|
|
1099
|
-
sendPong(): boolean {
|
|
1174
|
+
private sendPong(): boolean {
|
|
1100
1175
|
this.room()
|
|
1101
1176
|
const end: i32 = websocketWriteFrame(
|
|
1102
1177
|
this.output,
|
|
@@ -1117,7 +1192,7 @@ export class Http1Connection {
|
|
|
1117
1192
|
}
|
|
1118
1193
|
|
|
1119
1194
|
/** Writes a close frame with `code` (none for 1005), from the control reserve. */
|
|
1120
|
-
sendClose(code: i32): void {
|
|
1195
|
+
private sendClose(code: i32): void {
|
|
1121
1196
|
this.room()
|
|
1122
1197
|
const payload: u8[] | null = websocketClosePayload(code, "")
|
|
1123
1198
|
// A code that may not be sent (1005, for a close that had none) is
|
|
@@ -1141,7 +1216,11 @@ export class Http1Connection {
|
|
|
1141
1216
|
|
|
1142
1217
|
// ---- The response -------------------------------------------------------------
|
|
1143
1218
|
|
|
1144
|
-
/**
|
|
1219
|
+
/**
|
|
1220
|
+
* Whether the program may answer the current request now.
|
|
1221
|
+
*
|
|
1222
|
+
* @returns true while a request's head has been read and no response to it has been started
|
|
1223
|
+
*/
|
|
1145
1224
|
answerable(): boolean {
|
|
1146
1225
|
return this.state === H1_S_HTTP && this.requestOpen && this.response === H1_R_NONE
|
|
1147
1226
|
}
|
|
@@ -1155,6 +1234,13 @@ export class Http1Connection {
|
|
|
1155
1234
|
* name or value `http1WriteResponseHead` refuses, or a head larger than the
|
|
1156
1235
|
* output; `H1_AGAIN` when the output has no room for it yet (`H1_WRITE`
|
|
1157
1236
|
* says when to try again); `H1_CLOSED` with no request to answer.
|
|
1237
|
+
*
|
|
1238
|
+
* @param status the HTTP status code to send, 100 to 599 but not 101
|
|
1239
|
+
* @param names the program's field names, one per field; nothing checks their
|
|
1240
|
+
* case, and the reserved names are refused in any ASCII case
|
|
1241
|
+
* @param values each field's value, at the same index as its name
|
|
1242
|
+
* @param bodyLength the body's length in bytes, `HTTP1_CHUNKED`, or `HTTP1_NO_BODY`
|
|
1243
|
+
* @returns 0 once the head is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
|
|
1158
1244
|
*/
|
|
1159
1245
|
respond(status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 {
|
|
1160
1246
|
if (!this.answerable()) {
|
|
@@ -1232,6 +1318,11 @@ export class Http1Connection {
|
|
|
1232
1318
|
* when nothing was. A response with no body takes everything and sends
|
|
1233
1319
|
* nothing. `H1_CLOSED` with no response under way. A window outside `buf`
|
|
1234
1320
|
* panics.
|
|
1321
|
+
*
|
|
1322
|
+
* @param buf the body bytes to send, which stay the caller's
|
|
1323
|
+
* @param off where in `buf` the window starts
|
|
1324
|
+
* @param len how many bytes the window holds
|
|
1325
|
+
* @returns how many bytes from `off` were taken, or `H1_AGAIN`, `H1_INVALID` or `H1_CLOSED` as above
|
|
1235
1326
|
*/
|
|
1236
1327
|
write(buf: readonly u8[], off: i32, len: i32): i32 {
|
|
1237
1328
|
http1ServerCheckWindow("Http1Connection.write", buf, off, len)
|
|
@@ -1273,21 +1364,27 @@ export class Http1Connection {
|
|
|
1273
1364
|
}
|
|
1274
1365
|
|
|
1275
1366
|
/**
|
|
1276
|
-
* `write` of `buf[off .. off + len)`, sent from `buf` itself where it can
|
|
1277
|
-
*
|
|
1278
|
-
*
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
*
|
|
1284
|
-
*
|
|
1285
|
-
*
|
|
1286
|
-
*
|
|
1287
|
-
* `
|
|
1288
|
-
*
|
|
1289
|
-
*
|
|
1290
|
-
*
|
|
1367
|
+
* `write` of `buf[off .. off + len)`, sent from `buf` itself where it can be:
|
|
1368
|
+
* under a `Content-Length` response on a connection whose carrier named its
|
|
1369
|
+
* socket (`fd`, set by `Http1Server.accept`) and with no write held back, the
|
|
1370
|
+
* queued output and the body leave in one `netWrite2`, and only what the
|
|
1371
|
+
* socket did not take is copied into the output, the way `write` copies it
|
|
1372
|
+
* (what does not fit is answered short, and `H1_WRITE` says when to send the
|
|
1373
|
+
* rest).
|
|
1374
|
+
*
|
|
1375
|
+
* Once it returns the connection holds no reference to `buf`, so the caller
|
|
1376
|
+
* may reuse or give back its bytes at once. A chunked or empty response, a
|
|
1377
|
+
* write held back (`H1_AGAIN` or a short answer not yet followed by
|
|
1378
|
+
* `H1_WRITE`), and a connection with no socket (`Http1TlsServer`, a sans-IO
|
|
1379
|
+
* carrier) copy all of it: it is `write`, with the same bytes on the wire.
|
|
1380
|
+
* Answers what `write` answers, `H1_INVALID` for more than the response still
|
|
1381
|
+
* owes; `H1_CLOSED` also when the socket failed during the send, which aborts
|
|
1382
|
+
* the connection. A window outside `buf` panics.
|
|
1383
|
+
*
|
|
1384
|
+
* @param buf the body bytes to send, free for the caller to reuse once this returns
|
|
1385
|
+
* @param off where in `buf` the window starts
|
|
1386
|
+
* @param len how many bytes the window holds
|
|
1387
|
+
* @returns how many bytes from `off` were sent or queued, or the error `write` would answer
|
|
1291
1388
|
*/
|
|
1292
1389
|
sendBody(buf: readonly u8[], off: i32, len: i32): i32 {
|
|
1293
1390
|
http1ServerCheckWindow("Http1Connection.sendBody", buf, off, len)
|
|
@@ -1334,6 +1431,8 @@ export class Http1Connection {
|
|
|
1334
1431
|
* `H1_INVALID` for a response with a length that still owes bytes;
|
|
1335
1432
|
* `H1_AGAIN` when the last chunk does not fit yet; `H1_CLOSED` with no
|
|
1336
1433
|
* response under way.
|
|
1434
|
+
*
|
|
1435
|
+
* @returns 0 once the response is complete, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
|
|
1337
1436
|
*/
|
|
1338
1437
|
end(): i32 {
|
|
1339
1438
|
if (this.response !== H1_R_BODY || this.state !== H1_S_HTTP) {
|
|
@@ -1371,6 +1470,8 @@ export class Http1Connection {
|
|
|
1371
1470
|
* `HTTP_CODING_NONE` when the client refuses identity and does not take
|
|
1372
1471
|
* gzip. Allocates nothing. Read at `H1_REQUEST`, `H1_BODY` or `H1_END`
|
|
1373
1472
|
* alike: the head stays the request's until its response is done.
|
|
1473
|
+
*
|
|
1474
|
+
* @returns the `HTTP_CODING_` value the response should use, ready to pass to `respondEncoded`
|
|
1374
1475
|
*/
|
|
1375
1476
|
acceptEncoding(): i32 {
|
|
1376
1477
|
return this.acceptEncodingOf(HTTP_OFFER_GZIP)
|
|
@@ -1379,13 +1480,15 @@ export class Http1Connection {
|
|
|
1379
1480
|
/**
|
|
1380
1481
|
* Whether the current request's `Accept-Encoding` refuses the body as it
|
|
1381
1482
|
* is (`httpIdentityRefused`); `respondEncoded` then gzips any body it can.
|
|
1483
|
+
*
|
|
1484
|
+
* @returns true when the client's `Accept-Encoding` rules out the body as it is, so only a compressed one is acceptable
|
|
1382
1485
|
*/
|
|
1383
1486
|
identityRefused(): boolean {
|
|
1384
1487
|
return this.acceptEncodingOf(toI32(0)) === HTTP_CODING_NONE
|
|
1385
1488
|
}
|
|
1386
1489
|
|
|
1387
1490
|
/** `httpAcceptEncoding` of the current request's `Accept-Encoding`, with `offered`. */
|
|
1388
|
-
acceptEncodingOf(offered: i32): i32 {
|
|
1491
|
+
private acceptEncodingOf(offered: i32): i32 {
|
|
1389
1492
|
const parser: Http1Parser = this.parser
|
|
1390
1493
|
const i: i32 = parser.headerIndex("accept-encoding")
|
|
1391
1494
|
const spans: i32[] = parser.spans
|
|
@@ -1405,6 +1508,9 @@ export class Http1Connection {
|
|
|
1405
1508
|
* connection's `mss` when the program set it, else `enc.mss` when the
|
|
1406
1509
|
* program set it to other than `HTTP_DEFAULT_MSS`, else the socket's
|
|
1407
1510
|
* (`socketMss`), else `HTTP_DEFAULT_MSS`.
|
|
1511
|
+
*
|
|
1512
|
+
* @param enc the compressor whose own `mss` is the fallback after this connection's
|
|
1513
|
+
* @returns the segment size in bytes that segment counting uses for this connection
|
|
1408
1514
|
*/
|
|
1409
1515
|
segmentMss(enc: HttpCompressor): i32 {
|
|
1410
1516
|
if (this.mss > 0) {
|
|
@@ -1419,24 +1525,34 @@ export class Http1Connection {
|
|
|
1419
1525
|
/**
|
|
1420
1526
|
* Answers the current request with all of `body[off .. off + len)` in one
|
|
1421
1527
|
* call: `respond`, `write` and `end`, the body compressed by `enc` when
|
|
1422
|
-
* `coding` (from `acceptEncoding`) is gzip and `enc.compress` takes it,
|
|
1423
|
-
*
|
|
1424
|
-
*
|
|
1425
|
-
*
|
|
1426
|
-
*
|
|
1427
|
-
*
|
|
1428
|
-
*
|
|
1429
|
-
*
|
|
1430
|
-
* says what was chosen. A
|
|
1431
|
-
*
|
|
1432
|
-
* (`enc.compressAnyway`).
|
|
1433
|
-
*
|
|
1434
|
-
*
|
|
1435
|
-
* it: `H1_AGAIN` when the output has
|
|
1436
|
-
* when to try again, compressing again),
|
|
1437
|
-
* larger than the output could ever hold or one
|
|
1438
|
-
* `H1_CLOSED` with no request to answer; 0 once it is
|
|
1439
|
-
* outside `body` panics.
|
|
1528
|
+
* `coding` (from `acceptEncoding`) is gzip and `enc.compress` takes it, with
|
|
1529
|
+
* `Content-Encoding: gzip` and the compressed `Content-Length`, and otherwise
|
|
1530
|
+
* sent as it is; `Vary: Accept-Encoding` while `enc.vary` is set and the
|
|
1531
|
+
* choice turned on the request (`HttpCompressor.varies`).
|
|
1532
|
+
*
|
|
1533
|
+
* Under `enc.segmentMode` the compressed response goes out only when, head
|
|
1534
|
+
* included, it takes fewer segments of `segmentMss(enc)` bytes than the body
|
|
1535
|
+
* as it is, and a response that fits one segment as it is goes so without
|
|
1536
|
+
* compressing (`enc.oneSegment`); `enc.decision` says what was chosen. A
|
|
1537
|
+
* request that refuses identity (`identityRefused`) is sent gzip whatever the
|
|
1538
|
+
* length, the saving or the segments (`enc.compressAnyway`). The program's
|
|
1539
|
+
* fields may not name `Content-Encoding` or, with `enc.vary`, `Vary`.
|
|
1540
|
+
*
|
|
1541
|
+
* The whole response is written or none of it: `H1_AGAIN` when the output has
|
|
1542
|
+
* no room for it yet (`H1_WRITE` says when to try again, compressing again),
|
|
1543
|
+
* `H1_INVALID` for a response larger than the output could ever hold or one
|
|
1544
|
+
* `respond` refuses, `H1_CLOSED` with no request to answer; 0 once it is
|
|
1545
|
+
* queued. A window outside `body` panics.
|
|
1546
|
+
*
|
|
1547
|
+
* @param status the HTTP status code to send, as `respond` takes it
|
|
1548
|
+
* @param names the program's field names, without `Content-Encoding` (or `Vary` under `enc.vary`)
|
|
1549
|
+
* @param values each field's value, at the same index as its name
|
|
1550
|
+
* @param body the program's bytes the body is in, read but not kept
|
|
1551
|
+
* @param off where in `body` the response body starts
|
|
1552
|
+
* @param len how many bytes the response body holds as it is
|
|
1553
|
+
* @param enc the compressor that gzips the body and adds the coding fields; its scratch output is reused per call
|
|
1554
|
+
* @param coding the coding chosen for this request, normally what `acceptEncoding` answered
|
|
1555
|
+
* @returns 0 once the whole response is queued, else `H1_AGAIN`, `H1_INVALID` or `H1_CLOSED` as above
|
|
1440
1556
|
*/
|
|
1441
1557
|
respondEncoded(
|
|
1442
1558
|
status: i32,
|
|
@@ -1524,7 +1640,7 @@ export class Http1Connection {
|
|
|
1524
1640
|
}
|
|
1525
1641
|
|
|
1526
1642
|
/** Takes back a head `respond` just queued: `outputEnd` and `closeAfter` as they were before it. */
|
|
1527
|
-
unrespond(outputEnd: i32, closeAfter: boolean): void {
|
|
1643
|
+
private unrespond(outputEnd: i32, closeAfter: boolean): void {
|
|
1528
1644
|
this.outputEnd = outputEnd
|
|
1529
1645
|
this.closeAfter = closeAfter
|
|
1530
1646
|
this.response = H1_R_NONE
|
|
@@ -1542,6 +1658,9 @@ export class Http1Connection {
|
|
|
1542
1658
|
* request is not a valid opening handshake (the program answers 400, or 426
|
|
1543
1659
|
* for the wrong version) or `protocol` is not a token; `H1_AGAIN` when the
|
|
1544
1660
|
* output has no room yet; `H1_CLOSED` with no request to answer.
|
|
1661
|
+
*
|
|
1662
|
+
* @param protocol the subprotocol to name in `Sec-WebSocket-Protocol`, one the client offered, or "" for none
|
|
1663
|
+
* @returns 0 once the 101 is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
|
|
1545
1664
|
*/
|
|
1546
1665
|
acceptWebSocket(protocol: string): i32 {
|
|
1547
1666
|
if (!this.answerable()) {
|
|
@@ -1574,6 +1693,13 @@ export class Http1Connection {
|
|
|
1574
1693
|
* `websocketWriteFrame` refuses, a close (that is `closeWebSocket`), or one
|
|
1575
1694
|
* larger than the output; `H1_AGAIN` when it does not fit yet; `H1_CLOSED`
|
|
1576
1695
|
* when the slot is not an open WebSocket. A window outside `buf` panics.
|
|
1696
|
+
*
|
|
1697
|
+
* @param fin true when this frame ends its message; false for a fragment that continuations follow
|
|
1698
|
+
* @param opcode the frame's `WS_OP_` opcode: text, binary, continuation, ping or pong, never close
|
|
1699
|
+
* @param buf the payload bytes, copied into the output
|
|
1700
|
+
* @param off where in `buf` the window starts
|
|
1701
|
+
* @param len how many bytes the window holds
|
|
1702
|
+
* @returns 0 once the frame is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
|
|
1577
1703
|
*/
|
|
1578
1704
|
sendFrame(fin: boolean, opcode: i32, buf: u8[], off: i32, len: i32): i32 {
|
|
1579
1705
|
http1ServerCheckWindow("Http1Connection.sendFrame", buf, off, len)
|
|
@@ -1600,6 +1726,9 @@ export class Http1Connection {
|
|
|
1600
1726
|
* then the connection is done once it is sent. Answers 0, `H1_INVALID` for a
|
|
1601
1727
|
* code that may not be sent, or `H1_CLOSED` when the slot is not an open
|
|
1602
1728
|
* WebSocket.
|
|
1729
|
+
*
|
|
1730
|
+
* @param code the close status code to send (RFC 6455 §7.4), such as 1000 for a normal close
|
|
1731
|
+
* @returns 0 once the close frame is queued, else `H1_INVALID` or `H1_CLOSED` as above
|
|
1603
1732
|
*/
|
|
1604
1733
|
closeWebSocket(code: i32): i32 {
|
|
1605
1734
|
if (this.state !== H1_S_WEBSOCKET) {
|
|
@@ -1613,13 +1742,17 @@ export class Http1Connection {
|
|
|
1613
1742
|
return 0
|
|
1614
1743
|
}
|
|
1615
1744
|
|
|
1616
|
-
/**
|
|
1745
|
+
/**
|
|
1746
|
+
* Whether the slot has switched to WebSocket and is open.
|
|
1747
|
+
*
|
|
1748
|
+
* @returns true while `next` reads frames rather than requests and `sendFrame` may be called
|
|
1749
|
+
*/
|
|
1617
1750
|
isWebSocket(): boolean {
|
|
1618
1751
|
return this.state === H1_S_WEBSOCKET
|
|
1619
1752
|
}
|
|
1620
1753
|
|
|
1621
1754
|
/** Whether nothing has moved for longer than the idle timeout, at `now` (`monotonicNanos`). */
|
|
1622
|
-
idle(now: i64): boolean {
|
|
1755
|
+
private idle(now: i64): boolean {
|
|
1623
1756
|
return now - this.lastActive > toI64(this.config.idleTimeout) * toI64(1000000)
|
|
1624
1757
|
}
|
|
1625
1758
|
|
|
@@ -1629,6 +1762,8 @@ export class Http1Connection {
|
|
|
1629
1762
|
* call it after each `feed`; a program that feeds a connection itself calls
|
|
1630
1763
|
* it the same way. Bytes that wait while the previous exchange is still
|
|
1631
1764
|
* open start no clock: they count from when it is over (`nextRequest`).
|
|
1765
|
+
*
|
|
1766
|
+
* @param now the current time in `monotonicNanos`, which the deadline counts from
|
|
1632
1767
|
*/
|
|
1633
1768
|
headArrived(now: i64): void {
|
|
1634
1769
|
if (this.parser.buffered() > 0) {
|
|
@@ -1643,6 +1778,8 @@ export class Http1Connection {
|
|
|
1643
1778
|
* the handshake: a record trickled a byte at a time hands no plaintext to
|
|
1644
1779
|
* `headArrived` until it is whole, so its bytes would otherwise only keep
|
|
1645
1780
|
* the slot from going idle.
|
|
1781
|
+
*
|
|
1782
|
+
* @internal Called by `headArrived` and by `Http1TlsServer` in this module to start the slowloris clock on ciphertext.
|
|
1646
1783
|
*/
|
|
1647
1784
|
headAwaited(now: i64): void {
|
|
1648
1785
|
if (!this.headTimed && this.state === H1_S_HTTP && !this.requestOpen) {
|
|
@@ -1652,11 +1789,15 @@ export class Http1Connection {
|
|
|
1652
1789
|
}
|
|
1653
1790
|
|
|
1654
1791
|
/** Whether a head has been arriving for longer than `headTimeout` at `now` (`monotonicNanos`). */
|
|
1655
|
-
headLate(now: i64): boolean {
|
|
1792
|
+
private headLate(now: i64): boolean {
|
|
1656
1793
|
return this.headTimed && now - this.headSince > toI64(this.config.headTimeout) * toI64(1000000)
|
|
1657
1794
|
}
|
|
1658
1795
|
|
|
1659
|
-
/**
|
|
1796
|
+
/**
|
|
1797
|
+
* Whether `expire` closes the slot at `now`: idle, or a head past its deadline.
|
|
1798
|
+
*
|
|
1799
|
+
* @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to pick the slots to close.
|
|
1800
|
+
*/
|
|
1660
1801
|
expired(now: i64): boolean {
|
|
1661
1802
|
return this.idle(now) || this.headLate(now)
|
|
1662
1803
|
}
|
|
@@ -1669,6 +1810,10 @@ export class Http1Connection {
|
|
|
1669
1810
|
* `lastActive` when it ends. Counting a quiet keep-alive's clock from
|
|
1670
1811
|
* `lastActive` put it in the past once it was quiet for longer than
|
|
1671
1812
|
* `headTimeout`, so each `expire` walked every slot.
|
|
1813
|
+
*
|
|
1814
|
+
* @param now the current time in `monotonicNanos`
|
|
1815
|
+
* @returns the earliest `monotonicNanos` instant at which `expired` could answer true, so `expire` can skip the slot until then
|
|
1816
|
+
* @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to skip the slots that cannot expire yet.
|
|
1672
1817
|
*/
|
|
1673
1818
|
expiresNoSooner(now: i64): i64 {
|
|
1674
1819
|
const idle: i64 = this.lastActive + toI64(this.config.idleTimeout) * toI64(1000000)
|
|
@@ -1700,7 +1845,7 @@ const http1ExpireFloor = (config: Http1Config): i64 => {
|
|
|
1700
1845
|
* `accept` until it calls `close`, and its descriptor is `fd(slot)`.
|
|
1701
1846
|
*/
|
|
1702
1847
|
export class Http1Server {
|
|
1703
|
-
config: Http1Config
|
|
1848
|
+
private config: Http1Config
|
|
1704
1849
|
/**
|
|
1705
1850
|
* The connections built so far, slot `k`'s at `k`: one per slot ever
|
|
1706
1851
|
* accepted into, and slot 0's once `connection` has asked for it.
|
|
@@ -1719,11 +1864,12 @@ export class Http1Server {
|
|
|
1719
1864
|
*/
|
|
1720
1865
|
fds: i32[]
|
|
1721
1866
|
/** Each used slot's interest as the loop last armed it, for `rearm`. */
|
|
1722
|
-
armed: i32[]
|
|
1867
|
+
private armed: i32[]
|
|
1723
1868
|
/** The output buffers the slots borrow while they write. */
|
|
1724
1869
|
outputs: Http1OutputPool
|
|
1725
1870
|
/** The input and head buffers resting slots lend, with `Http1Config.idleInput`. */
|
|
1726
1871
|
inputs: Http1OutputPool
|
|
1872
|
+
/** The head buffers resting slots lend, beside `inputs`; `made` counts how many the slots ever needed. */
|
|
1727
1873
|
heads: Http1OutputPool
|
|
1728
1874
|
/**
|
|
1729
1875
|
* The slots closed since they were used, a stack whose top `accept` takes
|
|
@@ -1733,19 +1879,19 @@ export class Http1Server {
|
|
|
1733
1879
|
*/
|
|
1734
1880
|
free: i32[]
|
|
1735
1881
|
/** Where the socket is read into before the connection takes the bytes. */
|
|
1736
|
-
scratch: u8[]
|
|
1882
|
+
private scratch: u8[]
|
|
1737
1883
|
/** The last accepted peer's address, in `nish:net`'s 18-byte form. */
|
|
1738
|
-
peer: u8[]
|
|
1884
|
+
private peer: u8[]
|
|
1739
1885
|
/** How many slots the pool may hold, which `size` answers. */
|
|
1740
|
-
slots: i32 = 1
|
|
1886
|
+
private slots: i32 = 1
|
|
1741
1887
|
/** The listening socket, which the program made with `tcpListen` and still owns. */
|
|
1742
|
-
listener: i32 = -1
|
|
1888
|
+
private listener: i32 = -1
|
|
1743
1889
|
/**
|
|
1744
1890
|
* No slot can expire before this (`monotonicNanos`), so `expire` called
|
|
1745
1891
|
* sooner walks none: it walks every slot only about once per the shorter
|
|
1746
1892
|
* timeout, however many there are.
|
|
1747
1893
|
*/
|
|
1748
|
-
expireAfter: i64 = 0
|
|
1894
|
+
private expireAfter: i64 = 0
|
|
1749
1895
|
/** How many calls of `expire` walked every slot, for a program measuring its cost. */
|
|
1750
1896
|
expireScans: i32 = 0
|
|
1751
1897
|
|
|
@@ -1755,6 +1901,10 @@ export class Http1Server {
|
|
|
1755
1901
|
* allocated by the first `accept` into it and kept, so a pool sized for a
|
|
1756
1902
|
* peak costs at start-up only the read scratch, the shared `Date` and the
|
|
1757
1903
|
* output pool, and at most `size` connections once the peak has come.
|
|
1904
|
+
*
|
|
1905
|
+
* @param config the caps every slot's connection is held to
|
|
1906
|
+
* @param listener the listening socket from `tcpListen`, which stays the program's to close
|
|
1907
|
+
* @param size how many connections the pool serves at once; less than 1 counts as 1
|
|
1758
1908
|
*/
|
|
1759
1909
|
constructor(config: Http1Config, listener: i32, size: i32) {
|
|
1760
1910
|
this.config = config
|
|
@@ -1773,7 +1923,7 @@ export class Http1Server {
|
|
|
1773
1923
|
}
|
|
1774
1924
|
|
|
1775
1925
|
/** A new connection on this pool's outputs and `Date`, for the next slot built. */
|
|
1776
|
-
build(): Http1Connection {
|
|
1926
|
+
private build(): Http1Connection {
|
|
1777
1927
|
const conn: Http1Connection = new Http1Connection(this.config)
|
|
1778
1928
|
conn.pool = this.outputs
|
|
1779
1929
|
conn.date = this.date
|
|
@@ -1785,12 +1935,20 @@ export class Http1Server {
|
|
|
1785
1935
|
return conn
|
|
1786
1936
|
}
|
|
1787
1937
|
|
|
1788
|
-
/**
|
|
1938
|
+
/**
|
|
1939
|
+
* How many slots the pool has, built or not.
|
|
1940
|
+
*
|
|
1941
|
+
* @returns the pool's capacity in connections, as the constructor fixed it
|
|
1942
|
+
*/
|
|
1789
1943
|
size(): i32 {
|
|
1790
1944
|
return this.slots
|
|
1791
1945
|
}
|
|
1792
1946
|
|
|
1793
|
-
/**
|
|
1947
|
+
/**
|
|
1948
|
+
* How many slots hold a connection.
|
|
1949
|
+
*
|
|
1950
|
+
* @returns the count of slots accepted into and not yet closed
|
|
1951
|
+
*/
|
|
1794
1952
|
busy(): i32 {
|
|
1795
1953
|
let n: i32 = 0
|
|
1796
1954
|
for (const fd of this.fds) {
|
|
@@ -1801,12 +1959,22 @@ export class Http1Server {
|
|
|
1801
1959
|
return n
|
|
1802
1960
|
}
|
|
1803
1961
|
|
|
1804
|
-
/**
|
|
1962
|
+
/**
|
|
1963
|
+
* Whether `slot` is a slot of this pool that holds a connection.
|
|
1964
|
+
*
|
|
1965
|
+
* @param slot the slot index to ask about, any value
|
|
1966
|
+
* @returns true when the slot is in range and accepted into, not closed
|
|
1967
|
+
*/
|
|
1805
1968
|
holds(slot: i32): boolean {
|
|
1806
1969
|
return slot >= 0 && slot < toI32(this.fds.length) && this.fds[slot] >= 0
|
|
1807
1970
|
}
|
|
1808
1971
|
|
|
1809
|
-
/**
|
|
1972
|
+
/**
|
|
1973
|
+
* The descriptor of `slot`, for `pollAdd`; -1 for a free or unknown one.
|
|
1974
|
+
*
|
|
1975
|
+
* @param slot the slot `accept` answered
|
|
1976
|
+
* @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
|
|
1977
|
+
*/
|
|
1810
1978
|
fd(slot: i32): i32 {
|
|
1811
1979
|
return this.holds(slot) ? this.fds[slot] : -1
|
|
1812
1980
|
}
|
|
@@ -1815,6 +1983,9 @@ export class Http1Server {
|
|
|
1815
1983
|
* The connection of `slot`. A slot never accepted into, or out of range,
|
|
1816
1984
|
* names slot 0's connection, which this builds when no slot has been yet
|
|
1817
1985
|
* (the first `accept` then takes it, restarted).
|
|
1986
|
+
*
|
|
1987
|
+
* @param slot the slot `accept` answered
|
|
1988
|
+
* @returns the slot's connection, which the pool keeps; read its events and write its response through it
|
|
1818
1989
|
*/
|
|
1819
1990
|
connection(slot: i32): Http1Connection {
|
|
1820
1991
|
if (slot >= 0 && slot < toI32(this.connections.length)) {
|
|
@@ -1828,6 +1999,8 @@ export class Http1Server {
|
|
|
1828
1999
|
* slot, which the program adds to its loop with `H1_WANT_READ`; -11 when
|
|
1829
2000
|
* none is waiting, `H1_POOL_FULL` when every slot is busy (the connection
|
|
1830
2001
|
* is closed), or `tcpAccept`'s own failure.
|
|
2002
|
+
*
|
|
2003
|
+
* @returns the new connection's slot, or a negative code as above
|
|
1831
2004
|
*/
|
|
1832
2005
|
accept(): i32 {
|
|
1833
2006
|
const fd: i32 = tcpAccept(this.listener, this.peer)
|
|
@@ -1870,6 +2043,9 @@ export class Http1Server {
|
|
|
1870
2043
|
* and the socket has more, then sends. Answers the interest. It begins a
|
|
1871
2044
|
* serve call, which the program's `flush` of the slot ends: the responses
|
|
1872
2045
|
* written in between carry one clock reading as their `Date`.
|
|
2046
|
+
*
|
|
2047
|
+
* @param slot the slot `accept` answered
|
|
2048
|
+
* @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
|
|
1873
2049
|
*/
|
|
1874
2050
|
readable(slot: i32): i32 {
|
|
1875
2051
|
if (!this.holds(slot)) {
|
|
@@ -1912,7 +2088,12 @@ export class Http1Server {
|
|
|
1912
2088
|
return this.settle(slot)
|
|
1913
2089
|
}
|
|
1914
2090
|
|
|
1915
|
-
/**
|
|
2091
|
+
/**
|
|
2092
|
+
* The socket of `slot` is writable: sends, and begins a serve call as `readable` does. Answers the interest.
|
|
2093
|
+
*
|
|
2094
|
+
* @param slot the slot `accept` answered
|
|
2095
|
+
* @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
|
|
2096
|
+
*/
|
|
1916
2097
|
writable(slot: i32): i32 {
|
|
1917
2098
|
if (!this.holds(slot)) {
|
|
1918
2099
|
return H1_DONE
|
|
@@ -1927,6 +2108,9 @@ export class Http1Server {
|
|
|
1927
2108
|
* for is made here and `H1_WRITE` comes in the same pass; otherwise what
|
|
1928
2109
|
* the program writes in answer to one event and the next goes out together
|
|
1929
2110
|
* at `flush`, as one segment rather than several.
|
|
2111
|
+
*
|
|
2112
|
+
* @param slot the slot `accept` answered
|
|
2113
|
+
* @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
|
|
1930
2114
|
*/
|
|
1931
2115
|
next(slot: i32): i32 {
|
|
1932
2116
|
if (!this.holds(slot)) {
|
|
@@ -1944,6 +2128,9 @@ export class Http1Server {
|
|
|
1944
2128
|
* answers the interest: `H1_DONE` once the connection is over and sent,
|
|
1945
2129
|
* otherwise read while it has room and write while it holds bytes. It ends
|
|
1946
2130
|
* the serve call, so a response written after it reads the clock anew.
|
|
2131
|
+
*
|
|
2132
|
+
* @param slot the slot `accept` answered
|
|
2133
|
+
* @returns the `H1_WANT_` bits to arm the slot with, or `H1_DONE` when the program should `close` it
|
|
1947
2134
|
*/
|
|
1948
2135
|
flush(slot: i32): i32 {
|
|
1949
2136
|
this.date.unhold()
|
|
@@ -1951,7 +2138,7 @@ export class Http1Server {
|
|
|
1951
2138
|
}
|
|
1952
2139
|
|
|
1953
2140
|
/** `flush` within a serve call: sends, answers the interest, and keeps the clock reading. */
|
|
1954
|
-
settle(slot: i32): i32 {
|
|
2141
|
+
private settle(slot: i32): i32 {
|
|
1955
2142
|
if (!this.holds(slot)) {
|
|
1956
2143
|
return H1_DONE
|
|
1957
2144
|
}
|
|
@@ -1972,7 +2159,7 @@ export class Http1Server {
|
|
|
1972
2159
|
}
|
|
1973
2160
|
|
|
1974
2161
|
/** Writes what `slot`'s connection holds, which it holds, until the socket would block. */
|
|
1975
|
-
send(slot: i32): void {
|
|
2162
|
+
private send(slot: i32): void {
|
|
1976
2163
|
const conn: Http1Connection = this.connections[slot]
|
|
1977
2164
|
const fd: i32 = this.fds[slot]
|
|
1978
2165
|
const pending: i32 = conn.outputEnd - conn.outputStart
|
|
@@ -1998,6 +2185,12 @@ export class Http1Server {
|
|
|
1998
2185
|
* armed when it must. A keep-alive connection answers the same interest
|
|
1999
2186
|
* request after request, and each `pollModify` is a system call
|
|
2000
2187
|
* (`epoll_ctl`) that changes nothing.
|
|
2188
|
+
*
|
|
2189
|
+
* @param slot the slot `accept` answered
|
|
2190
|
+
* @param wants the interest bits a serve call answered; only `H1_WANT_READ` and `H1_WANT_WRITE` count
|
|
2191
|
+
* @returns true when the interest changed and the program should call
|
|
2192
|
+
* `pollModify`; false when `slot` is already armed for it, or is free or
|
|
2193
|
+
* out of range
|
|
2001
2194
|
*/
|
|
2002
2195
|
rearm(slot: i32, wants: i32): boolean {
|
|
2003
2196
|
const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
|
|
@@ -2008,7 +2201,11 @@ export class Http1Server {
|
|
|
2008
2201
|
return true
|
|
2009
2202
|
}
|
|
2010
2203
|
|
|
2011
|
-
/**
|
|
2204
|
+
/**
|
|
2205
|
+
* Closes `slot`'s socket, whatever is left unsent, and frees the slot.
|
|
2206
|
+
*
|
|
2207
|
+
* @param slot the slot to close; a free or unknown one is ignored
|
|
2208
|
+
*/
|
|
2012
2209
|
close(slot: i32): void {
|
|
2013
2210
|
if (!this.holds(slot)) {
|
|
2014
2211
|
return
|
|
@@ -2024,6 +2221,9 @@ export class Http1Server {
|
|
|
2024
2221
|
/**
|
|
2025
2222
|
* Closes every slot that has been idle past `idleTimeout`, or reading one
|
|
2026
2223
|
* head for longer than `headTimeout`, at `now` (`monotonicNanos`); answers how many.
|
|
2224
|
+
*
|
|
2225
|
+
* @param now the current time in `monotonicNanos`, which every timeout is measured against
|
|
2226
|
+
* @returns how many slots this call closed
|
|
2027
2227
|
*/
|
|
2028
2228
|
expire(now: i64): i32 {
|
|
2029
2229
|
if (now < this.expireAfter) {
|
|
@@ -2058,7 +2258,8 @@ export class Http1Server {
|
|
|
2058
2258
|
* `Http1Server`'s is.
|
|
2059
2259
|
*/
|
|
2060
2260
|
export class Http1TlsServer {
|
|
2061
|
-
config: Http1Config
|
|
2261
|
+
private config: Http1Config
|
|
2262
|
+
/** The TLS 1.3 pool underneath, one slot beside each connection; read it for a slot's TLS connection. */
|
|
2062
2263
|
tls: TlsTcpServer
|
|
2063
2264
|
/**
|
|
2064
2265
|
* The connections built so far, slot `k`'s at `k`: one per slot ever
|
|
@@ -2072,18 +2273,18 @@ export class Http1TlsServer {
|
|
|
2072
2273
|
* another slot's live request. Its first read allocates it, once per
|
|
2073
2274
|
* server, and a caller must not change it.
|
|
2074
2275
|
*/
|
|
2075
|
-
vacant: Http1Connection | null = null
|
|
2276
|
+
private vacant: Http1Connection | null = null
|
|
2076
2277
|
/** The `Date` every slot's responses carry, as `Http1Server.date` is. */
|
|
2077
2278
|
date: HttpDate
|
|
2078
2279
|
/** Where TLS decrypts into before the connection takes the bytes. */
|
|
2079
|
-
scratch: u8[]
|
|
2280
|
+
private scratch: u8[]
|
|
2080
2281
|
/** The output buffers the slots borrow while they write. */
|
|
2081
2282
|
outputs: Http1OutputPool
|
|
2082
2283
|
/** The input and head buffers resting slots lend, with `Http1Config.idleInput`. */
|
|
2083
|
-
inputs: Http1OutputPool
|
|
2084
|
-
heads: Http1OutputPool
|
|
2284
|
+
private inputs: Http1OutputPool
|
|
2285
|
+
private heads: Http1OutputPool
|
|
2085
2286
|
/** No slot can expire before this (`monotonicNanos`): `Http1Server.expireAfter`. */
|
|
2086
|
-
expireAfter: i64 = 0
|
|
2287
|
+
private expireAfter: i64 = 0
|
|
2087
2288
|
/** How many calls of `expire` walked every slot. */
|
|
2088
2289
|
expireScans: i32 = 0
|
|
2089
2290
|
|
|
@@ -2093,6 +2294,11 @@ export class Http1TlsServer {
|
|
|
2093
2294
|
* a slot builds its connection and, in `tls`, its handshake state, and
|
|
2094
2295
|
* keeps them, so a pool sized for a peak costs at start-up only the read
|
|
2095
2296
|
* scratches, the shared `Date` and the empty pools.
|
|
2297
|
+
*
|
|
2298
|
+
* @param tlsConfig the certificate chain and TLS settings every handshake uses
|
|
2299
|
+
* @param config the caps every slot's connection is held to
|
|
2300
|
+
* @param listener the listening socket from `tcpListen`, which stays the program's to close
|
|
2301
|
+
* @param size how many connections the pool serves at once
|
|
2096
2302
|
*/
|
|
2097
2303
|
constructor(tlsConfig: TlsServerConfig, config: Http1Config, listener: i32, size: i32) {
|
|
2098
2304
|
this.config = config
|
|
@@ -2110,14 +2316,14 @@ export class Http1TlsServer {
|
|
|
2110
2316
|
* built. Called by `accept` alone, as `tls` builds slots: slot `k`'s
|
|
2111
2317
|
* connection must sit at index `k` of `connections`.
|
|
2112
2318
|
*/
|
|
2113
|
-
build(): Http1Connection {
|
|
2319
|
+
private build(): Http1Connection {
|
|
2114
2320
|
const conn: Http1Connection = this.make()
|
|
2115
2321
|
this.connections.push(conn)
|
|
2116
2322
|
return conn
|
|
2117
2323
|
}
|
|
2118
2324
|
|
|
2119
2325
|
/** A connection on this pool's outputs and `Date` that no slot holds yet. */
|
|
2120
|
-
make(): Http1Connection {
|
|
2326
|
+
private make(): Http1Connection {
|
|
2121
2327
|
const conn: Http1Connection = new Http1Connection(this.config)
|
|
2122
2328
|
conn.pool = this.outputs
|
|
2123
2329
|
conn.date = this.date
|
|
@@ -2128,22 +2334,40 @@ export class Http1TlsServer {
|
|
|
2128
2334
|
return conn
|
|
2129
2335
|
}
|
|
2130
2336
|
|
|
2131
|
-
/**
|
|
2337
|
+
/**
|
|
2338
|
+
* How many slots the pool has, built or not.
|
|
2339
|
+
*
|
|
2340
|
+
* @returns the pool's capacity in connections, as `tls` holds it
|
|
2341
|
+
*/
|
|
2132
2342
|
size(): i32 {
|
|
2133
2343
|
return this.tls.size()
|
|
2134
2344
|
}
|
|
2135
2345
|
|
|
2136
|
-
/**
|
|
2346
|
+
/**
|
|
2347
|
+
* How many slots hold a connection.
|
|
2348
|
+
*
|
|
2349
|
+
* @returns the count of slots accepted into and not yet closed, handshakes included
|
|
2350
|
+
*/
|
|
2137
2351
|
busy(): i32 {
|
|
2138
2352
|
return this.tls.busy()
|
|
2139
2353
|
}
|
|
2140
2354
|
|
|
2141
|
-
/**
|
|
2355
|
+
/**
|
|
2356
|
+
* Whether `slot` holds a connection.
|
|
2357
|
+
*
|
|
2358
|
+
* @param slot the slot index to ask about, any value
|
|
2359
|
+
* @returns true when the slot is in range and accepted into, not closed
|
|
2360
|
+
*/
|
|
2142
2361
|
holds(slot: i32): boolean {
|
|
2143
2362
|
return this.tls.holds(slot)
|
|
2144
2363
|
}
|
|
2145
2364
|
|
|
2146
|
-
/**
|
|
2365
|
+
/**
|
|
2366
|
+
* The descriptor of `slot`, for `pollAdd`; -1 for a free one.
|
|
2367
|
+
*
|
|
2368
|
+
* @param slot the slot `accept` answered
|
|
2369
|
+
* @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
|
|
2370
|
+
*/
|
|
2147
2371
|
fd(slot: i32): i32 {
|
|
2148
2372
|
return this.tls.fd(slot)
|
|
2149
2373
|
}
|
|
@@ -2153,7 +2377,7 @@ export class Http1TlsServer {
|
|
|
2153
2377
|
* as `Http2TlsServer.live` asks: a slot accepted on `tls` directly holds a
|
|
2154
2378
|
* TLS session and no `Http1Connection`, and reads as free here.
|
|
2155
2379
|
*/
|
|
2156
|
-
live(slot: i32): boolean {
|
|
2380
|
+
private live(slot: i32): boolean {
|
|
2157
2381
|
return this.tls.holds(slot) && slot < toI32(this.connections.length)
|
|
2158
2382
|
}
|
|
2159
2383
|
|
|
@@ -2161,6 +2385,9 @@ export class Http1TlsServer {
|
|
|
2161
2385
|
* The HTTP/1.1 connection of `slot`. A slot never accepted into answers
|
|
2162
2386
|
* `vacant`, as a free slot answers a connection that holds nothing, and
|
|
2163
2387
|
* builds no slot; a slot out of range names slot 0's.
|
|
2388
|
+
*
|
|
2389
|
+
* @param slot the slot `accept` answered
|
|
2390
|
+
* @returns the slot's connection, which the pool keeps; the shared `vacant` one, not to be changed, for a slot never built
|
|
2164
2391
|
*/
|
|
2165
2392
|
connection(slot: i32): Http1Connection {
|
|
2166
2393
|
const index: i32 = slot >= 0 && slot < this.tls.size() ? slot : 0
|
|
@@ -2176,12 +2403,22 @@ export class Http1TlsServer {
|
|
|
2176
2403
|
return made
|
|
2177
2404
|
}
|
|
2178
2405
|
|
|
2179
|
-
/**
|
|
2406
|
+
/**
|
|
2407
|
+
* The ALPN protocol `slot`'s handshake chose, or empty.
|
|
2408
|
+
*
|
|
2409
|
+
* @param slot the slot `accept` answered
|
|
2410
|
+
* @returns the protocol identifier the handshake agreed on, such as "http/1.1", or "" when none was chosen yet or at all
|
|
2411
|
+
*/
|
|
2180
2412
|
alpn(slot: i32): string {
|
|
2181
2413
|
return this.tls.connection(slot).tls.alpn
|
|
2182
2414
|
}
|
|
2183
2415
|
|
|
2184
|
-
/**
|
|
2416
|
+
/**
|
|
2417
|
+
* Whether `slot`'s handshake is done and chose a protocol other than `http/1.1`.
|
|
2418
|
+
*
|
|
2419
|
+
* @param slot the slot `accept` answered
|
|
2420
|
+
* @returns true when the slot will be shut down rather than served, because its client wants another protocol
|
|
2421
|
+
*/
|
|
2185
2422
|
refused(slot: i32): boolean {
|
|
2186
2423
|
const chosen: string = this.alpn(slot)
|
|
2187
2424
|
return this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN && chosen !== "" && chosen !== H1_ALPN
|
|
@@ -2192,6 +2429,10 @@ export class Http1TlsServer {
|
|
|
2192
2429
|
* the caller's 32-byte server random and x25519 key, and gives it a fresh
|
|
2193
2430
|
* connection, building the slot's on its first accept. Answers the slot, or
|
|
2194
2431
|
* what `TlsTcpServer.accept` answers.
|
|
2432
|
+
*
|
|
2433
|
+
* @param serverRandom 32 fresh random bytes for the ServerHello
|
|
2434
|
+
* @param ephemeralPrivate a fresh x25519 private key for this handshake alone
|
|
2435
|
+
* @returns the new connection's slot, or the negative code `TlsTcpServer.accept` answers
|
|
2195
2436
|
*/
|
|
2196
2437
|
accept(serverRandom: u8[], ephemeralPrivate: u8[]): i32 {
|
|
2197
2438
|
const slot: i32 = this.tls.accept(serverRandom, ephemeralPrivate)
|
|
@@ -2208,7 +2449,12 @@ export class Http1TlsServer {
|
|
|
2208
2449
|
return slot
|
|
2209
2450
|
}
|
|
2210
2451
|
|
|
2211
|
-
/**
|
|
2452
|
+
/**
|
|
2453
|
+
* The socket of `slot` is readable: TLS reads it, and what the connection has to send goes after. Answers the interest.
|
|
2454
|
+
*
|
|
2455
|
+
* @param slot the slot `accept` answered
|
|
2456
|
+
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
2457
|
+
*/
|
|
2212
2458
|
readable(slot: i32): i32 {
|
|
2213
2459
|
const live: boolean = this.live(slot)
|
|
2214
2460
|
const now: i64 = monotonicNanos()
|
|
@@ -2228,13 +2474,24 @@ export class Http1TlsServer {
|
|
|
2228
2474
|
return this.flush(slot)
|
|
2229
2475
|
}
|
|
2230
2476
|
|
|
2231
|
-
/**
|
|
2477
|
+
/**
|
|
2478
|
+
* The socket of `slot` is writable. Answers the interest.
|
|
2479
|
+
*
|
|
2480
|
+
* @param slot the slot `accept` answered
|
|
2481
|
+
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
2482
|
+
*/
|
|
2232
2483
|
writable(slot: i32): i32 {
|
|
2233
2484
|
this.tls.writable(slot)
|
|
2234
2485
|
return this.flush(slot)
|
|
2235
2486
|
}
|
|
2236
2487
|
|
|
2237
|
-
/**
|
|
2488
|
+
/**
|
|
2489
|
+
* Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does. Answers the interest.
|
|
2490
|
+
*
|
|
2491
|
+
* @param slot the slot whose handshake waits for its signature
|
|
2492
|
+
* @param key the certificate's P-256 private key, which stays the caller's to wipe
|
|
2493
|
+
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
2494
|
+
*/
|
|
2238
2495
|
signP256(slot: i32, key: Secret<u8[]>): i32 {
|
|
2239
2496
|
this.tls.signP256(slot, key)
|
|
2240
2497
|
return this.flush(slot)
|
|
@@ -2244,6 +2501,9 @@ export class Http1TlsServer {
|
|
|
2244
2501
|
* Moves what TLS has decrypted for `slot` into its connection, as far as
|
|
2245
2502
|
* the connection has room, and answers the connection's next event. A free
|
|
2246
2503
|
* slot, or one whose ALPN was another protocol, answers `H1_ERROR`.
|
|
2504
|
+
*
|
|
2505
|
+
* @param slot the slot `accept` answered
|
|
2506
|
+
* @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
|
|
2247
2507
|
*/
|
|
2248
2508
|
next(slot: i32): i32 {
|
|
2249
2509
|
if (!this.live(slot) || this.refused(slot)) {
|
|
@@ -2277,6 +2537,9 @@ export class Http1TlsServer {
|
|
|
2277
2537
|
* and sends it; shuts TLS down once the connection is over or ALPN chose
|
|
2278
2538
|
* another protocol. Answers TLS's interest, with the write bit while the
|
|
2279
2539
|
* connection still holds bytes.
|
|
2540
|
+
*
|
|
2541
|
+
* @param slot the slot `accept` answered
|
|
2542
|
+
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
2280
2543
|
*/
|
|
2281
2544
|
flush(slot: i32): i32 {
|
|
2282
2545
|
if (!this.live(slot)) {
|
|
@@ -2299,7 +2562,7 @@ export class Http1TlsServer {
|
|
|
2299
2562
|
* Writes what `slot`'s connection holds into TLS, as far as TLS takes it;
|
|
2300
2563
|
* a slot never built has nothing to write.
|
|
2301
2564
|
*/
|
|
2302
|
-
send(slot: i32): void {
|
|
2565
|
+
private send(slot: i32): void {
|
|
2303
2566
|
if (slot < 0 || slot >= toI32(this.connections.length)) {
|
|
2304
2567
|
return
|
|
2305
2568
|
}
|
|
@@ -2318,7 +2581,11 @@ export class Http1TlsServer {
|
|
|
2318
2581
|
conn.release()
|
|
2319
2582
|
}
|
|
2320
2583
|
|
|
2321
|
-
/**
|
|
2584
|
+
/**
|
|
2585
|
+
* Ends `slot`'s connection and frees the slot at once, as `TlsTcpServer.close` does.
|
|
2586
|
+
*
|
|
2587
|
+
* @param slot the slot to close
|
|
2588
|
+
*/
|
|
2322
2589
|
close(slot: i32): void {
|
|
2323
2590
|
this.tls.close(slot)
|
|
2324
2591
|
if (slot >= 0 && slot < toI32(this.connections.length)) {
|
|
@@ -2329,6 +2596,9 @@ export class Http1TlsServer {
|
|
|
2329
2596
|
/**
|
|
2330
2597
|
* Closes every slot that has been idle past `idleTimeout`, or reading one
|
|
2331
2598
|
* head for longer than `headTimeout`, at `now` (`monotonicNanos`); answers how many.
|
|
2599
|
+
*
|
|
2600
|
+
* @param now the current time in `monotonicNanos`, which every timeout is measured against
|
|
2601
|
+
* @returns how many slots this call closed
|
|
2332
2602
|
*/
|
|
2333
2603
|
expire(now: i64): i32 {
|
|
2334
2604
|
if (now < this.expireAfter) {
|