@amritk/nish-x86_64-linux 0.49.1 → 0.51.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 +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +237 -160
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/net/http1-server.ts
CHANGED
|
@@ -458,18 +458,40 @@ const http1ServerConfigFault = (config: Http1Config): string => {
|
|
|
458
458
|
const one: i32 = 1
|
|
459
459
|
const smallest: i32 = 64
|
|
460
460
|
const output: i32 = 1024
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
http1ServerCapFault("
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
http1ServerCapFault("
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
http1ServerCapFault("
|
|
472
|
-
|
|
461
|
+
// One cap at a time, the first fault answered: no list of the twelve
|
|
462
|
+
// answers is built, so a check that passes costs comparisons alone.
|
|
463
|
+
let fault: string = http1ServerCapFault("maxTarget", config.maxTarget, one, big)
|
|
464
|
+
if (fault.length === 0) {
|
|
465
|
+
fault = http1ServerCapFault("maxHeaderBytes", config.maxHeaderBytes, smallest, big)
|
|
466
|
+
}
|
|
467
|
+
if (fault.length === 0) {
|
|
468
|
+
fault = http1ServerCapFault("maxHeaders", config.maxHeaders, one, big)
|
|
469
|
+
}
|
|
470
|
+
if (fault.length === 0) {
|
|
471
|
+
fault = http1ServerCapFault("maxBody", config.maxBody, H1_ZERO, big)
|
|
472
|
+
}
|
|
473
|
+
if (fault.length === 0) {
|
|
474
|
+
fault = http1ServerCapFault("chunkSize", config.chunkSize, one, big)
|
|
475
|
+
}
|
|
476
|
+
if (fault.length === 0) {
|
|
477
|
+
fault = http1ServerCapFault("outputSize", config.outputSize, output, big)
|
|
478
|
+
}
|
|
479
|
+
if (fault.length === 0) {
|
|
480
|
+
fault = http1ServerCapFault("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER)
|
|
481
|
+
}
|
|
482
|
+
if (fault.length === 0) {
|
|
483
|
+
fault = http1ServerCapFault("idleTimeout", config.idleTimeout, one, big)
|
|
484
|
+
}
|
|
485
|
+
if (fault.length === 0) {
|
|
486
|
+
fault = http1ServerCapFault("headTimeout", config.headTimeout, one, big)
|
|
487
|
+
}
|
|
488
|
+
if (fault.length === 0) {
|
|
489
|
+
fault = http1ServerCapFault("lingerTimeout", config.lingerTimeout, one, big)
|
|
490
|
+
}
|
|
491
|
+
if (fault.length === 0) {
|
|
492
|
+
fault = http1ServerCapFault("lingerBytes", config.lingerBytes, H1_ZERO, big)
|
|
493
|
+
}
|
|
494
|
+
if (fault.length === 0) {
|
|
473
495
|
// The buffers the peer's bytes fill grow by doubling and stop at 2^30
|
|
474
496
|
// (`Http1Parser`'s input and head, `WsDecoder`'s input), so a cap whose
|
|
475
497
|
// buffer could need more is refused here rather than reached by a peer:
|
|
@@ -477,19 +499,14 @@ const http1ServerConfigFault = (config: Http1Config): string => {
|
|
|
477
499
|
// the input never holds more than the larger of the two lines or a chunk.
|
|
478
500
|
// Each is at most 2^30 here, so the sum wraps only at 2^31, to a
|
|
479
501
|
// negative the check refuses as it would the sum itself.
|
|
480
|
-
http1ServerCapFault(
|
|
502
|
+
fault = http1ServerCapFault(
|
|
481
503
|
"maxTarget + maxHeaderBytes",
|
|
482
504
|
toI32(toI64(config.maxTarget) + toI64(config.maxHeaderBytes)),
|
|
483
505
|
one,
|
|
484
506
|
big - H1_LINE_OVERHEAD
|
|
485
|
-
)
|
|
486
|
-
]
|
|
487
|
-
for (const fault of caps) {
|
|
488
|
-
if (fault.length > 0) {
|
|
489
|
-
return fault
|
|
490
|
-
}
|
|
507
|
+
)
|
|
491
508
|
}
|
|
492
|
-
return
|
|
509
|
+
return fault
|
|
493
510
|
}
|
|
494
511
|
|
|
495
512
|
/** Panics unless every cap of `config` is one a connection can work with: a cap outside is the program's mistake. */
|
|
@@ -550,11 +567,49 @@ const http1ServerPast = (now: i64, since: i64, ms: i32): boolean =>
|
|
|
550
567
|
now > since && toU64(now) - toU64(since) > toU64(ms) * toU64(1000000)
|
|
551
568
|
|
|
552
569
|
/**
|
|
553
|
-
* The `monotonicNanos` instant `ms` milliseconds after `at`.
|
|
554
|
-
*
|
|
555
|
-
*
|
|
570
|
+
* The `monotonicNanos` instant `ms` milliseconds after `at`. An instant is
|
|
571
|
+
* centuries short of 2^63 and the timeout a checked cap, so the sum is exact;
|
|
572
|
+
* `http1ServerSum64` holds it at the `i64` range's ends only so that the
|
|
573
|
+
* bounds prover sees no sum that can overflow.
|
|
556
574
|
*/
|
|
557
|
-
const http1ServerAfter = (at: i64, ms: i32): i64 =>
|
|
575
|
+
const http1ServerAfter = (at: i64, ms: i32): i64 => http1ServerSum64(at, toI64(ms) * 1000000)
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* `a + b`, held at the `i64` range's ends where the sum would leave it. Two
|
|
579
|
+
* values of opposite signs add in `i64`, as they cannot leave the range. Two
|
|
580
|
+
* of one sign add as magnitudes in `u64`, where they are below 2^64 and so
|
|
581
|
+
* exact: a sum of values at least 0 is the `u64` sum, and a sum of values
|
|
582
|
+
* below 0 is `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is held below
|
|
583
|
+
* 2^63 before it is read back as an `i64`, so the read is exact too.
|
|
584
|
+
*/
|
|
585
|
+
const http1ServerSum64 = (a: i64, b: i64): i64 => {
|
|
586
|
+
if (a >= 0) {
|
|
587
|
+
if (b < 0) {
|
|
588
|
+
return a + b
|
|
589
|
+
}
|
|
590
|
+
const sum: u64 = toU64(a) + toU64(b)
|
|
591
|
+
return toI64(http1ServerBelow2p63(sum))
|
|
592
|
+
}
|
|
593
|
+
if (b >= 0) {
|
|
594
|
+
return a + b
|
|
595
|
+
}
|
|
596
|
+
const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
|
|
597
|
+
const k: i64 = toI64(http1ServerBelow2p63(past))
|
|
598
|
+
if (k < 0) {
|
|
599
|
+
// Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
|
|
600
|
+
return -1
|
|
601
|
+
}
|
|
602
|
+
return -1 - k
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
|
|
606
|
+
const http1ServerBelow2p63 = (x: u64): u64 => {
|
|
607
|
+
const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
|
|
608
|
+
if (x > most) {
|
|
609
|
+
return most
|
|
610
|
+
}
|
|
611
|
+
return x
|
|
612
|
+
}
|
|
558
613
|
|
|
559
614
|
/** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
|
|
560
615
|
const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
|
|
@@ -776,6 +831,71 @@ export const http1ConfigChecked = (config: Http1Config): Result<Http1Config, str
|
|
|
776
831
|
return fault.length > 0 ? Err(fault) : Ok(config)
|
|
777
832
|
}
|
|
778
833
|
|
|
834
|
+
/** A copy of `config`, field for field: a snapshot whoever holds `config` cannot change. */
|
|
835
|
+
const http1ConfigCopy = (config: Http1Config): Http1Config => {
|
|
836
|
+
const kept: Http1Config = new Http1Config()
|
|
837
|
+
kept.maxTarget = config.maxTarget
|
|
838
|
+
kept.maxHeaderBytes = config.maxHeaderBytes
|
|
839
|
+
kept.maxHeaders = config.maxHeaders
|
|
840
|
+
kept.maxBody = config.maxBody
|
|
841
|
+
kept.chunkSize = config.chunkSize
|
|
842
|
+
kept.outputSize = config.outputSize
|
|
843
|
+
kept.maxMessage = config.maxMessage
|
|
844
|
+
kept.idleTimeout = config.idleTimeout
|
|
845
|
+
kept.headTimeout = config.headTimeout
|
|
846
|
+
kept.lingerTimeout = config.lingerTimeout
|
|
847
|
+
kept.lingerBytes = config.lingerBytes
|
|
848
|
+
kept.idleInput = config.idleInput
|
|
849
|
+
return kept
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* A connection under a copy of `config`, checked once, the twin of
|
|
854
|
+
* `Http1Connection`'s constructor that refuses a cap outside what the
|
|
855
|
+
* connection can work with as an `Err` rather than panicking. The connection
|
|
856
|
+
* keeps the copy, so a later change to `config` does not reach it.
|
|
857
|
+
*
|
|
858
|
+
* @param config the caps the connection is held to, copied and checked now
|
|
859
|
+
* @param date the `Date` its responses carry, shared with other connections; null makes it one of its own
|
|
860
|
+
* @returns the connection, or the first cap outside its range, named with the range it must lie in
|
|
861
|
+
*/
|
|
862
|
+
export const http1ConnectionChecked = (
|
|
863
|
+
config: Http1Config,
|
|
864
|
+
date: HttpDate | null = null
|
|
865
|
+
): Result<Http1Connection, string> => {
|
|
866
|
+
const kept: Http1Config = http1ConfigCopy(config)
|
|
867
|
+
const fault: string = http1ServerConfigFault(kept)
|
|
868
|
+
if (fault.length > 0) {
|
|
869
|
+
return Err(fault)
|
|
870
|
+
}
|
|
871
|
+
return Ok(new Http1Connection(kept, date))
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* A server pool under a copy of `config`, checked once, the twin of
|
|
876
|
+
* `Http1Server`'s constructor: a cap outside what a connection can work
|
|
877
|
+
* with is an `Err` here, where the constructor's pool would panic building
|
|
878
|
+
* its first connection. The pool keeps the copy, so a later change to
|
|
879
|
+
* `config` reaches none of its connections.
|
|
880
|
+
*
|
|
881
|
+
* @param config the caps every slot's connection is held to, copied and checked now
|
|
882
|
+
* @param listener the listening socket from `tcpListen`, which stays the program's to close
|
|
883
|
+
* @param size how many connections the pool serves at once; less than 1 counts as 1
|
|
884
|
+
* @returns the pool, or the first cap outside its range, named with the range it must lie in
|
|
885
|
+
*/
|
|
886
|
+
export const http1ServerChecked = (
|
|
887
|
+
config: Http1Config,
|
|
888
|
+
listener: i32,
|
|
889
|
+
size: i32
|
|
890
|
+
): Result<Http1Server, string> => {
|
|
891
|
+
const kept: Http1Config = http1ConfigCopy(config)
|
|
892
|
+
const fault: string = http1ServerConfigFault(kept)
|
|
893
|
+
if (fault.length > 0) {
|
|
894
|
+
return Err(fault)
|
|
895
|
+
}
|
|
896
|
+
return Ok(new Http1Server(kept, listener, size))
|
|
897
|
+
}
|
|
898
|
+
|
|
779
899
|
/** The reason phrase a status is sent with; RFC 9112 §4 lets a client ignore it. */
|
|
780
900
|
const http1ServerReason = (status: i32): string => {
|
|
781
901
|
switch (status) {
|
|
@@ -1352,10 +1472,23 @@ export class Http1Connection {
|
|
|
1352
1472
|
return 0
|
|
1353
1473
|
}
|
|
1354
1474
|
if (this.state === H1_S_WEBSOCKET) {
|
|
1355
|
-
|
|
1475
|
+
// `[off, off + n)` lies inside the window checked above, as `n <= len`;
|
|
1476
|
+
// spelled as `end - off` behind its test so the decoder's check is proven.
|
|
1477
|
+
const ws: WsDecoder = this.decoder()
|
|
1478
|
+
const end: i32 = toI32(toI64(off) + toI64(n))
|
|
1479
|
+
if (off < 0 || off > end || end > toI32(buf.length)) {
|
|
1480
|
+
this.abort()
|
|
1481
|
+
return 0
|
|
1482
|
+
}
|
|
1483
|
+
ws.feed(buf, off, end - off)
|
|
1356
1484
|
} else {
|
|
1357
1485
|
this.unpark()
|
|
1358
|
-
|
|
1486
|
+
// In the window checked above, so `tryFeed` takes it; false would be a
|
|
1487
|
+
// window it refused, and the connection could not go on.
|
|
1488
|
+
if (!this.parser.tryFeed(buf, off, n)) {
|
|
1489
|
+
this.abort()
|
|
1490
|
+
return 0
|
|
1491
|
+
}
|
|
1359
1492
|
}
|
|
1360
1493
|
return n
|
|
1361
1494
|
}
|
|
@@ -1601,7 +1734,17 @@ export class Http1Connection {
|
|
|
1601
1734
|
const p: Http1Parser = this.parser
|
|
1602
1735
|
const ws: WsDecoder = this.decoder()
|
|
1603
1736
|
ws.reset()
|
|
1604
|
-
|
|
1737
|
+
// What the parser holds past the head: its own buffer's window,
|
|
1738
|
+
// tested so the decoder's window check is proven. A parser that
|
|
1739
|
+
// broke it is a connection that cannot go on.
|
|
1740
|
+
const held: u8[] = p.buf
|
|
1741
|
+
const from: i32 = p.start
|
|
1742
|
+
const end: i32 = toI32(toI64(from) + toI64(p.buffered()))
|
|
1743
|
+
if (from < 0 || from > end || end > toI32(held.length)) {
|
|
1744
|
+
this.abort()
|
|
1745
|
+
return H1_ERROR
|
|
1746
|
+
}
|
|
1747
|
+
ws.feed(held, from, end - from)
|
|
1605
1748
|
this.state = H1_S_WEBSOCKET
|
|
1606
1749
|
return this.nextFrame()
|
|
1607
1750
|
}
|
|
@@ -2790,7 +2933,9 @@ export class Http1Server {
|
|
|
2790
2933
|
* @param size how many connections the pool serves at once; less than 1 counts as 1
|
|
2791
2934
|
*/
|
|
2792
2935
|
constructor(config: Http1Config, listener: i32, size: i32) {
|
|
2793
|
-
|
|
2936
|
+
// A copy: the caps each connection is built under are the ones given
|
|
2937
|
+
// here, whatever the caller sets on `config` later.
|
|
2938
|
+
this.config = http1ConfigCopy(config)
|
|
2794
2939
|
this.listener = listener
|
|
2795
2940
|
this.connections = []
|
|
2796
2941
|
this.at = []
|
|
@@ -2810,11 +2955,21 @@ export class Http1Server {
|
|
|
2810
2955
|
this.peer = new Array<u8>(H1_ADDRESS_SIZE)
|
|
2811
2956
|
}
|
|
2812
2957
|
|
|
2813
|
-
/**
|
|
2814
|
-
|
|
2815
|
-
|
|
2958
|
+
/**
|
|
2959
|
+
* A new connection on this pool's outputs and `Date`, appended to
|
|
2960
|
+
* `connections`; null, building none, when the pool's caps are ones a
|
|
2961
|
+
* connection cannot work with.
|
|
2962
|
+
*/
|
|
2963
|
+
private build(): Http1Connection | null {
|
|
2964
|
+
const config: Http1Config = this.config
|
|
2965
|
+
const date: HttpDate = this.date
|
|
2966
|
+
const fault: string = http1ServerConfigFault(config)
|
|
2967
|
+
if (fault.length > 0) {
|
|
2968
|
+
return null
|
|
2969
|
+
}
|
|
2970
|
+
const conn: Http1Connection = new Http1Connection(config, date)
|
|
2816
2971
|
conn.pool = this.outputs
|
|
2817
|
-
if (
|
|
2972
|
+
if (config.idleInput) {
|
|
2818
2973
|
conn.inputs = this.inputs
|
|
2819
2974
|
conn.heads = this.heads
|
|
2820
2975
|
}
|
|
@@ -2885,6 +3040,23 @@ export class Http1Server {
|
|
|
2885
3040
|
* @returns the slot's connection, which the pool keeps; read its events and write its response through it
|
|
2886
3041
|
*/
|
|
2887
3042
|
connection(slot: i32): Http1Connection {
|
|
3043
|
+
const conn: Http1Connection | null = this.tryConnection(slot)
|
|
3044
|
+
// Null only for a free slot when no connection could be built: the
|
|
3045
|
+
// pool's caps are bad, which this constructor panics on.
|
|
3046
|
+
return conn !== null ? conn : new Http1Connection(this.config, this.date)
|
|
3047
|
+
}
|
|
3048
|
+
|
|
3049
|
+
/**
|
|
3050
|
+
* The connection `slot` holds, as `connection` answers it, or null where
|
|
3051
|
+
* `connection` would panic: the twin for a pool made by `new Http1Server`
|
|
3052
|
+
* whose caps no connection can work with (`http1ServerChecked` makes a
|
|
3053
|
+
* pool that never answers null). A free slot, or one out of range, names
|
|
3054
|
+
* the first connection built, as `connection`'s does.
|
|
3055
|
+
*
|
|
3056
|
+
* @param slot the slot `accept` answered
|
|
3057
|
+
* @returns the slot's connection, which the pool keeps; null when none could be built
|
|
3058
|
+
*/
|
|
3059
|
+
tryConnection(slot: i32): Http1Connection | null {
|
|
2888
3060
|
const conn: Http1Connection | null = this.attach(slot)
|
|
2889
3061
|
if (conn !== null) {
|
|
2890
3062
|
return conn
|
|
@@ -2972,7 +3144,11 @@ export class Http1Server {
|
|
|
2972
3144
|
}
|
|
2973
3145
|
const taken: Http1Connection | null = k >= 0 && k < toI32(connections.length) ? connections[k] : null
|
|
2974
3146
|
// No spare: a new one, at the end of `connections`.
|
|
2975
|
-
const conn: Http1Connection = taken !== null ? taken : this.build()
|
|
3147
|
+
const conn: Http1Connection | null = taken !== null ? taken : this.build()
|
|
3148
|
+
if (conn === null) {
|
|
3149
|
+
// Bad caps: no connection, so the slot reads as closed.
|
|
3150
|
+
return null
|
|
3151
|
+
}
|
|
2976
3152
|
if (taken === null) {
|
|
2977
3153
|
k = toI32(this.connections.length) - 1
|
|
2978
3154
|
}
|
|
@@ -3072,9 +3248,9 @@ export class Http1Server {
|
|
|
3072
3248
|
this.at[k] = -1
|
|
3073
3249
|
this.rested[k] = this.clock()
|
|
3074
3250
|
this.owner[k] = this.accepts
|
|
3075
|
-
// A serial only keys a kept head to its peer
|
|
3076
|
-
//
|
|
3077
|
-
this.accepts =
|
|
3251
|
+
// A serial only keys a kept head to its peer. It is held at 2^63 - 1
|
|
3252
|
+
// rather than overflowing, after more accepts than a server ever sees.
|
|
3253
|
+
this.accepts = http1ServerSum64(this.accepts, 1)
|
|
3078
3254
|
// Once a connection, not a request, so a keep-alive request costs no
|
|
3079
3255
|
// syscall: a path whose MSS moves after the handshake is not followed.
|
|
3080
3256
|
const mss: i32 = netMss(fd)
|
|
@@ -3136,8 +3312,14 @@ export class Http1Server {
|
|
|
3136
3312
|
}
|
|
3137
3313
|
break
|
|
3138
3314
|
}
|
|
3315
|
+
// `netRead` answers at most `want`; a count past the scratch is a
|
|
3316
|
+
// broken read, and the connection is aborted rather than fed it.
|
|
3317
|
+
if (n > toI32(scratch.length)) {
|
|
3318
|
+
conn.abort()
|
|
3319
|
+
break
|
|
3320
|
+
}
|
|
3139
3321
|
moved = true
|
|
3140
|
-
conn.feed(scratch,
|
|
3322
|
+
conn.feed(scratch, toI32(0), n)
|
|
3141
3323
|
// A short read emptied the socket. The loop's readiness is
|
|
3142
3324
|
// level-triggered, so more input, or the peer's close, wakes it again;
|
|
3143
3325
|
// reading once more here would only answer -11, at the price of a
|
|
@@ -3699,7 +3881,7 @@ export class Http1TlsServer {
|
|
|
3699
3881
|
* Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does. Answers the interest.
|
|
3700
3882
|
*
|
|
3701
3883
|
* @param slot the slot whose handshake waits for its signature
|
|
3702
|
-
* @param key the certificate's P-256
|
|
3884
|
+
* @param key the certificate's private key, P-256 or RSA as `signatureScheme` says, which stays the caller's to wipe
|
|
3703
3885
|
* @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
|
|
3704
3886
|
*/
|
|
3705
3887
|
signP256(slot: i32, key: Secret<u8[]>): i32 {
|
|
@@ -421,8 +421,14 @@ export class Http2CleartextServer {
|
|
|
421
421
|
this.close(slot)
|
|
422
422
|
return false
|
|
423
423
|
}
|
|
424
|
-
// `netRead` answers at most the 24 - `have` bytes it was asked for, so the
|
|
425
|
-
|
|
424
|
+
// `netRead` answers at most the 24 - `have` bytes it was asked for, so the sum is at most 24. The test
|
|
425
|
+
// restates that with literals (`H2C_PREFACE_SIZE` is 24) for the bounds proof; a read that broke it
|
|
426
|
+
// would be a fault, and ends the connection as a failed read does.
|
|
427
|
+
if (have > 24 || n > 24) {
|
|
428
|
+
this.close(slot)
|
|
429
|
+
return false
|
|
430
|
+
}
|
|
431
|
+
const end: i32 = have + n
|
|
426
432
|
for (let k: i32 = have; k < end; k++) {
|
|
427
433
|
if (k < 0 || k >= toI32(preface.length) || k >= toI32(want.length)) {
|
|
428
434
|
break
|
|
@@ -542,8 +548,8 @@ export class Http2CleartextServer {
|
|
|
542
548
|
if (start < 0 || end < start) {
|
|
543
549
|
break
|
|
544
550
|
}
|
|
545
|
-
// Both ends checked just above, so the
|
|
546
|
-
const n: i32 = netWrite(s.fd, conn.output, start,
|
|
551
|
+
// Both ends checked just above, so the difference is the span's length and cannot overflow.
|
|
552
|
+
const n: i32 = netWrite(s.fd, conn.output, start, end - start)
|
|
547
553
|
if (n === H2C_AGAIN) {
|
|
548
554
|
break
|
|
549
555
|
}
|
package/std/net/http2-frame.ts
CHANGED
|
@@ -395,8 +395,8 @@ const http2FramePriority = (frame: Http2Frame, buf: u8[], at: i32): i32 => {
|
|
|
395
395
|
(toI32(buf[at + 1]) << 16) |
|
|
396
396
|
(toI32(buf[at + 2]) << 8) |
|
|
397
397
|
toI32(buf[at + 3])
|
|
398
|
-
// A byte is 0 to 255, so the weight is 1 to 256.
|
|
399
|
-
frame.weight = toI32(
|
|
398
|
+
// A byte is 0 to 255, so the weight is 1 to 256 and the sum cannot overflow.
|
|
399
|
+
frame.weight = toI32(buf[at + 4]) + 1
|
|
400
400
|
if (frame.dependency === frame.streamId) {
|
|
401
401
|
frame.streamError = H2_PROTOCOL_ERROR
|
|
402
402
|
}
|
package/std/net/http2-tls.ts
CHANGED
|
@@ -304,7 +304,7 @@ export class Http2TlsServer {
|
|
|
304
304
|
* Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does.
|
|
305
305
|
*
|
|
306
306
|
* @param slot The slot whose interest says `TLS_RECORD_SIGN`.
|
|
307
|
-
* @param key The certificate's P-256
|
|
307
|
+
* @param key The certificate's private key, P-256 or RSA as `signatureScheme` says; borrowed for the call, still the caller's to wipe.
|
|
308
308
|
* @returns The slot's interest, as `flush` answers it; `TLS_RECORD_DONE` for a free slot.
|
|
309
309
|
*/
|
|
310
310
|
signP256(slot: i32, key: Secret<u8[]>): i32 {
|
|
@@ -399,8 +399,8 @@ export class Http2TlsServer {
|
|
|
399
399
|
if (start < 0 || end < start) {
|
|
400
400
|
break
|
|
401
401
|
}
|
|
402
|
-
// `start` is 0 or more and `end` at least `start`, so the
|
|
403
|
-
const n: i32 = this.tls.write(slot, conn.output, start,
|
|
402
|
+
// `start` is 0 or more and `end` at least `start`, so the difference is the held byte count and fits.
|
|
403
|
+
const n: i32 = this.tls.write(slot, conn.output, start, end - start)
|
|
404
404
|
if (n <= 0) {
|
|
405
405
|
break
|
|
406
406
|
}
|
package/std/net/http2.ts
CHANGED
|
@@ -457,27 +457,81 @@ const http2CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void =>
|
|
|
457
457
|
}
|
|
458
458
|
|
|
459
459
|
/**
|
|
460
|
-
* How long before `now` the instant `since` was, both `monotonicNanos` readings.
|
|
461
|
-
*
|
|
460
|
+
* How long before `now` the instant `since` was, both `monotonicNanos` readings. Two readings of one
|
|
461
|
+
* monotonic clock are under 2^63 apart, so `http2Minus` gives the exact difference.
|
|
462
462
|
*/
|
|
463
|
-
const http2Since = (now: i64, since: i64): i64 =>
|
|
463
|
+
const http2Since = (now: i64, since: i64): i64 => http2Minus(now, since)
|
|
464
464
|
|
|
465
465
|
/**
|
|
466
|
-
* The `monotonicNanos` instant `ms` milliseconds after `since`. The product is under 2^52 for an
|
|
467
|
-
* `ms
|
|
466
|
+
* The `monotonicNanos` instant `ms` milliseconds after `since`. The product is under 2^52 in size for an
|
|
467
|
+
* `i32` `ms`, and a reading plus it stays far below 2^63, so `http2Plus` gives the exact sum.
|
|
468
468
|
*/
|
|
469
|
-
const http2After = (since: i64, ms: i32): i64 =>
|
|
469
|
+
const http2After = (since: i64, ms: i32): i64 => http2Plus(since, toI64(ms) * 1000000)
|
|
470
470
|
|
|
471
471
|
/**
|
|
472
|
-
* `a + b` for flow-control figures
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
472
|
+
* `a + b` for flow-control figures, clock readings and byte counts, held at the `i64` range's ends where
|
|
473
|
+
* the sum would leave it. None of them does: a flow-control figure (a window, the bytes owed to one, or
|
|
474
|
+
* the change a SETTINGS makes to every window) stays within 2^33 either side of 0, since a window that
|
|
475
|
+
* passes 2^31 - 1 fails its stream or the connection and no frame moves one by more than 2^31 - 1; a
|
|
476
|
+
* clock is centuries short of 2^63; and 2^63 bytes cannot arrive. Two values of opposite signs add in
|
|
477
|
+
* `i64`, as they cannot leave the range. Two of one sign add as magnitudes in `u64`, where they are below
|
|
478
|
+
* 2^64 and so exact: a sum of values at least 0 is the `u64` sum, and a sum of values below 0 is
|
|
479
|
+
* `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is held below 2^63 before it is read back as an
|
|
480
|
+
* `i64`, so the read is exact too, and the bounds prover sees no sum that can overflow.
|
|
476
481
|
*/
|
|
477
|
-
const http2Plus = (a: i64, b: i64): i64 =>
|
|
482
|
+
const http2Plus = (a: i64, b: i64): i64 => {
|
|
483
|
+
if (a >= 0) {
|
|
484
|
+
if (b < 0) {
|
|
485
|
+
return a + b
|
|
486
|
+
}
|
|
487
|
+
const sum: u64 = toU64(a) + toU64(b)
|
|
488
|
+
return toI64(http2Below2p63(sum))
|
|
489
|
+
}
|
|
490
|
+
if (b >= 0) {
|
|
491
|
+
return a + b
|
|
492
|
+
}
|
|
493
|
+
const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
|
|
494
|
+
const k: i64 = toI64(http2Below2p63(past))
|
|
495
|
+
if (k < 0) {
|
|
496
|
+
// Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
|
|
497
|
+
return -1
|
|
498
|
+
}
|
|
499
|
+
return -1 - k
|
|
500
|
+
}
|
|
478
501
|
|
|
479
|
-
/**
|
|
480
|
-
|
|
502
|
+
/**
|
|
503
|
+
* `a - b` for the figures `http2Plus` takes, held at the range's ends the same way. Two values of one
|
|
504
|
+
* sign subtract in `i64`. Otherwise the difference is a sum of magnitudes: `a + (-1 - b) + 1` for `a` at
|
|
505
|
+
* least 0, and `-1 - ((-1 - a) + b)` for `a` below 0.
|
|
506
|
+
*/
|
|
507
|
+
const http2Minus = (a: i64, b: i64): i64 => {
|
|
508
|
+
if (a >= 0) {
|
|
509
|
+
if (b >= 0) {
|
|
510
|
+
return a - b
|
|
511
|
+
}
|
|
512
|
+
const sum: u64 = toU64(a) + toU64(-1 - b) + toU64(1)
|
|
513
|
+
return toI64(http2Below2p63(sum))
|
|
514
|
+
}
|
|
515
|
+
if (b < 0) {
|
|
516
|
+
return a - b
|
|
517
|
+
}
|
|
518
|
+
const past: u64 = toU64(-1 - a) + toU64(b)
|
|
519
|
+
const k: i64 = toI64(http2Below2p63(past))
|
|
520
|
+
if (k < 0) {
|
|
521
|
+
// Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
|
|
522
|
+
return -1
|
|
523
|
+
}
|
|
524
|
+
return -1 - k
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
|
|
528
|
+
const http2Below2p63 = (x: u64): u64 => {
|
|
529
|
+
const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
|
|
530
|
+
if (x > most) {
|
|
531
|
+
return most
|
|
532
|
+
}
|
|
533
|
+
return x
|
|
534
|
+
}
|
|
481
535
|
|
|
482
536
|
/** Whether one of `names` is `date` in any ASCII case (RFC 9110 §5.1). */
|
|
483
537
|
const http2NamesHoldDate = (names: u8[][]): boolean => {
|
|
@@ -1486,8 +1540,8 @@ export class Http2Connection {
|
|
|
1486
1540
|
return this.discarded(id) ? H2_NEED_MORE : this.refuseStream(id, H2_STREAM_CLOSED)
|
|
1487
1541
|
}
|
|
1488
1542
|
const end: boolean = f.has(H2_FLAG_END_STREAM)
|
|
1489
|
-
// A count of body bytes: 2^63 of them cannot arrive, so
|
|
1490
|
-
const received: i64 =
|
|
1543
|
+
// A count of body bytes: 2^63 of them cannot arrive, so `http2Plus` gives the exact sum.
|
|
1544
|
+
const received: i64 = http2Plus(s.received, toI64(f.contentLength))
|
|
1491
1545
|
const declared: boolean = s.contentLength >= toI64(0)
|
|
1492
1546
|
let error: i32 = H2_NO_ERROR
|
|
1493
1547
|
if (s.remoteEnded) {
|
package/std/net/http3-frame.ts
CHANGED
|
@@ -347,26 +347,20 @@ export class Http3Settings {
|
|
|
347
347
|
|
|
348
348
|
/**
|
|
349
349
|
* Panics unless `[off, off + len)` lies inside `buf`: a window outside its buffer is the program's mistake.
|
|
350
|
+
* A window guard (`docs/LANGUAGE.md`, `window`), so a caller that proves the window in range removes the
|
|
351
|
+
* panic site, and a function whose first statement hands it its own window passes that proof to its callers.
|
|
350
352
|
* @param what The caller's name, which starts the panic message.
|
|
351
353
|
* @param buf The buffer the window is meant to lie in.
|
|
352
354
|
* @param off The window's first byte offset; negative panics.
|
|
353
355
|
* @param len The window's length in bytes; negative panics, and so does an end past `buf.length`.
|
|
354
356
|
*/
|
|
355
357
|
export const h3CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
+
const size: i32 = toI32(buf.length)
|
|
359
|
+
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
360
|
+
panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${size} bytes`)
|
|
358
361
|
}
|
|
359
362
|
}
|
|
360
363
|
|
|
361
|
-
/**
|
|
362
|
-
* `off + len`, after `h3CheckWindow` has panicked unless the window lies inside `buf`: the sum is then at
|
|
363
|
-
* most `buf.length`, so the `i64` it is taken in always narrows back exactly.
|
|
364
|
-
*/
|
|
365
|
-
const h3WindowEnd = (what: string, buf: u8[], off: i32, len: i32): i32 => {
|
|
366
|
-
h3CheckWindow(what, buf, off, len)
|
|
367
|
-
return toI32(toI64(off) + toI64(len))
|
|
368
|
-
}
|
|
369
|
-
|
|
370
364
|
/**
|
|
371
365
|
* The offset just past the varint at `buf[at]`, or -1 when it does not end at or before `end` (or `at` is
|
|
372
366
|
* outside `buf`). Taken in `i64`, so it narrows back exactly whenever it is at most `end`.
|
|
@@ -452,7 +446,8 @@ export const h3FrameHeaderSize = (type: i64, length: i64): i32 => {
|
|
|
452
446
|
* @returns The header's size, 2 to 16 bytes, or 0 when the window ends before both varints are whole.
|
|
453
447
|
*/
|
|
454
448
|
export const h3ReadFrameHeader = (h: Http3FrameHeader, buf: u8[], off: i32, len: i32): i32 => {
|
|
455
|
-
|
|
449
|
+
h3CheckWindow("h3ReadFrameHeader", buf, off, len)
|
|
450
|
+
const end: i32 = toI32(toI64(off) + toI64(len))
|
|
456
451
|
const type: i64 = quicVarintRead(buf, off, end)
|
|
457
452
|
const at: i32 = h3VarintEnd(buf, off, end)
|
|
458
453
|
if (type < 0 || at < 0) {
|
|
@@ -519,7 +514,8 @@ export const h3Greased = (value: i64): boolean => value >= 0x21 && value % H3_GR
|
|
|
519
514
|
* error.
|
|
520
515
|
*/
|
|
521
516
|
export const h3ReadSettings = (s: Http3Settings, buf: u8[], off: i32, len: i32): i64 => {
|
|
522
|
-
|
|
517
|
+
h3CheckWindow("h3ReadSettings", buf, off, len)
|
|
518
|
+
const end: i32 = toI32(toI64(off) + toI64(len))
|
|
523
519
|
s.reset()
|
|
524
520
|
let at: i32 = off
|
|
525
521
|
while (at < end) {
|
|
@@ -624,7 +620,8 @@ export const h3PutIdFrame = (buf: u8[], at: i32, end: i32, type: i64, id: i64):
|
|
|
624
620
|
* bytes after it.
|
|
625
621
|
*/
|
|
626
622
|
export const h3ReadIdPayload = (buf: u8[], off: i32, len: i32): i64 => {
|
|
627
|
-
|
|
623
|
+
h3CheckWindow("h3ReadIdPayload", buf, off, len)
|
|
624
|
+
const end: i32 = toI32(toI64(off) + toI64(len))
|
|
628
625
|
const id: i64 = quicVarintRead(buf, off, end)
|
|
629
626
|
return id >= 0 && h3VarintEnd(buf, off, end) === end ? id : H3_FRAME_NONE
|
|
630
627
|
}
|