@amritk/nish-x86_64-linux 0.36.0 → 0.38.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 +92 -71
- package/bin/nish +0 -0
- package/docs/AI.md +75 -14
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +1045 -512
- package/runtime/nish.h +53 -6
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +740 -0
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-region.c +85 -7
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +65 -9
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +231 -31
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +109 -82
- package/std/array.ts +266 -0
- package/std/bytes.ts +23 -19
- package/std/compress/checksum.ts +1 -1
- package/std/crypto/aes.ts +48 -43
- package/std/crypto/base64url.ts +7 -5
- package/std/crypto/bcrypt-batch.ts +13 -13
- package/std/crypto/bcrypt.ts +33 -27
- package/std/crypto/chacha20poly1305.ts +16 -14
- package/std/crypto/ed25519.ts +19 -16
- package/std/crypto/hmac.ts +7 -6
- package/std/crypto/jwt.ts +23 -20
- package/std/crypto/p256.ts +63 -56
- package/std/crypto/p384.ts +23 -20
- package/std/crypto/pki.ts +18 -15
- package/std/crypto/rsa.ts +23 -19
- package/std/crypto/x25519.ts +11 -10
- package/std/crypto/x509.ts +69 -62
- package/std/host/ring.ts +124 -0
- package/std/host/text.ts +86 -0
- package/std/json-equal.ts +12 -11
- package/std/json-number.ts +12 -12
- package/std/json-tokenizer.ts +26 -22
- package/std/json-typed.ts +92 -78
- package/std/json-value.ts +38 -33
- package/std/json.ts +32 -28
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +1744 -295
- package/std/net/handler.ts +30 -0
- package/std/net/hpack.ts +29 -26
- package/std/net/http-compress.ts +42 -28
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-client-codec.ts +29 -25
- package/std/net/http1-codec.ts +62 -91
- package/std/net/http1-server.ts +79 -57
- package/std/net/http2-frame.ts +17 -16
- package/std/net/http2-tls.ts +17 -15
- package/std/net/http2.ts +67 -54
- package/std/net/http3-frame.ts +11 -10
- package/std/net/http3-server.ts +36 -35
- package/std/net/http3.ts +70 -58
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +185 -164
- package/std/net/prefork.ts +38 -34
- package/std/net/qpack.ts +15 -14
- package/std/net/quic-conn-ack.ts +1 -0
- package/std/net/quic-conn-cid.ts +11 -0
- package/std/net/quic-conn-params.ts +41 -14
- package/std/net/quic-datagram.ts +7 -0
- package/std/net/quic-frame.ts +48 -12
- package/std/net/quic-listener.ts +61 -44
- package/std/net/quic-packet.ts +71 -52
- package/std/net/quic-recovery.ts +59 -21
- package/std/net/quic-stream.ts +83 -55
- package/std/net/quic.ts +204 -152
- package/std/net/serve.ts +763 -0
- package/std/net/tls/client.ts +1 -0
- package/std/net/tls/codec.ts +81 -12
- package/std/net/tls/record-client.ts +30 -18
- package/std/net/tls/record-server.ts +74 -46
- package/std/net/tls/record.ts +33 -24
- package/std/net/tls/ticket.ts +2 -0
- package/std/net/tls/verify.ts +8 -7
- package/std/net/tls-tcp.ts +58 -50
- package/std/net/tls.ts +55 -43
- package/std/net/url.ts +12 -9
- package/std/net/websocket.ts +23 -19
- package/std/net/webtransport.ts +24 -18
- package/std/random.ts +8 -6
- package/std/regex.ts +70 -59
- package/std/sync/ring.ts +24 -21
- package/std/sync/shm.ts +9 -7
- package/std/term-io.ts +17 -14
- package/std/term-probe.ts +11 -10
- package/std/term-rate.ts +71 -61
- package/std/term.ts +73 -59
- package/std/text.ts +242 -15
- package/std/threads.ts +19 -15
- package/std/yaml-json.ts +17 -15
- package/std/yaml-write.ts +21 -14
package/std/net/http1-codec.ts
CHANGED
|
@@ -86,7 +86,6 @@ import {
|
|
|
86
86
|
any,
|
|
87
87
|
bitmask,
|
|
88
88
|
eq,
|
|
89
|
-
extract,
|
|
90
89
|
gt,
|
|
91
90
|
loadU8x16,
|
|
92
91
|
lt,
|
|
@@ -256,7 +255,13 @@ export class Http1Parser {
|
|
|
256
255
|
* @internal Set by `http1TakeLine` and read by the field-line parser in this module.
|
|
257
256
|
*/
|
|
258
257
|
lineControl: boolean = false
|
|
258
|
+
/** Whether the request's body is chunked (`Transfer-Encoding: chunked`), so it has no `contentLength`. */
|
|
259
259
|
chunked: boolean = false
|
|
260
|
+
/**
|
|
261
|
+
* Whether the connection stays open after this request: HTTP/1.1 unless it
|
|
262
|
+
* sent `Connection: close`, HTTP/1.0 only when it sent `keep-alive`. A
|
|
263
|
+
* server closes after its response when this is false.
|
|
264
|
+
*/
|
|
260
265
|
keepAlive: boolean = false
|
|
261
266
|
/** Whether the request asked to switch protocols (`upgrade` is its value when `keepText`). */
|
|
262
267
|
upgrading: boolean = false
|
|
@@ -293,12 +298,17 @@ export class Http1Parser {
|
|
|
293
298
|
left: i32 = 0
|
|
294
299
|
/** The declared body length, or -1 when the request has none or is chunked. */
|
|
295
300
|
contentLength: i32 = -1
|
|
301
|
+
/** Where the bytes the last `HTTP1_BODY` handed over start in `body`. */
|
|
296
302
|
bodyOff: i32 = 0
|
|
303
|
+
/** How many bytes the last `HTTP1_BODY` handed over, from `body[bodyOff]`; 0 before the first. */
|
|
297
304
|
bodyLen: i32 = 0
|
|
298
305
|
/** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
|
|
299
306
|
methodStart: i32 = 0
|
|
307
|
+
/** Where the method ends in `head`, one past its last byte. */
|
|
300
308
|
methodEnd: i32 = 0
|
|
309
|
+
/** Where the request target starts in `head`. */
|
|
301
310
|
targetStart: i32 = 0
|
|
311
|
+
/** Where the request target ends in `head`, one past its last byte. */
|
|
302
312
|
targetEnd: i32 = 0
|
|
303
313
|
|
|
304
314
|
// --- limits, the caller's --------------------------------------------------
|
|
@@ -326,10 +336,13 @@ export class Http1Parser {
|
|
|
326
336
|
maxChunk: i32 = 1073741824
|
|
327
337
|
|
|
328
338
|
// --- what `keepText`, an error or a chunked body reads ----------------------
|
|
339
|
+
/** The request's method as text when `keepText` is on, else empty. */
|
|
329
340
|
method: string = ""
|
|
341
|
+
/** The request target (path and query) as text when `keepText` is on, else empty. */
|
|
330
342
|
target: string = ""
|
|
331
343
|
/** The field names, in lowercase, in the order they arrived; `values` beside them. */
|
|
332
344
|
names: string[]
|
|
345
|
+
/** The field values, in the order they arrived, each at its name's index in `names`; filled only when `keepText`. */
|
|
333
346
|
values: string[]
|
|
334
347
|
/** The `Upgrade` field when `Connection` asked for it, else empty. */
|
|
335
348
|
upgrade: string = ""
|
|
@@ -657,73 +670,6 @@ const http1StartRequest = (p: Http1Parser): void => {
|
|
|
657
670
|
p.headHeld = false
|
|
658
671
|
}
|
|
659
672
|
|
|
660
|
-
/**
|
|
661
|
-
* The bytes of `x` below 32 or equal to 127 — a control byte or DEL, which
|
|
662
|
-
* `http1TakeLine` stops at — as their high bits: zero when there is none,
|
|
663
|
-
* and else the lowest set bit is the first such byte's. Two of the bit
|
|
664
|
-
* tricks of "Bit Twiddling Hacks" (S. E. Anderson, public domain): a byte
|
|
665
|
-
* below `n` leaves its high bit set in `(x - n * ones) & ~x`, for `n` up to
|
|
666
|
-
* 128, and `127` is the byte that is zero in `x ^ 127 * ones`. A byte from
|
|
667
|
-
* 128 up has its high bit set in `x`, so `~x` clears it: obs-text never
|
|
668
|
-
* matches. A borrow only runs up from a byte that matched, so the bits above
|
|
669
|
-
* the first match may be false and the lowest is exact, in each mask and so
|
|
670
|
-
* in their union. Each subtraction is masked to 64 bits because a `u64` is a
|
|
671
|
-
* BigInt under Node, which does not wrap; natively the mask is a no-op.
|
|
672
|
-
*/
|
|
673
|
-
const http1ControlBits = (x: u64): u64 => {
|
|
674
|
-
const ones32: u64 = toU64(16843009)
|
|
675
|
-
const ones: u64 = (ones32 << toU64(32)) | ones32
|
|
676
|
-
const low32: u64 = 0xffffffff
|
|
677
|
-
const all: u64 = (low32 << toU64(32)) | low32
|
|
678
|
-
const high: u64 = ones * toU64(128)
|
|
679
|
-
const below: u64 = (x - ones * toU64(32)) & all & ~x & high
|
|
680
|
-
const y: u64 = x ^ (ones * toU64(127))
|
|
681
|
-
const del: u64 = (y - ones) & all & ~y & high
|
|
682
|
-
return below | del
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
/** Which byte of a word `bits`'s lowest set bit is in, 0 to 7; `bits` is not zero. */
|
|
686
|
-
const http1FirstByte = (bits: u64): i32 => {
|
|
687
|
-
const mask32: u64 = 0xffffffff
|
|
688
|
-
const lo: u32 = toU32(bits & mask32)
|
|
689
|
-
if (lo !== toU32(0)) {
|
|
690
|
-
return (toI32(31) - toI32(Math.clz32(lo & (toU32(0) - lo)))) >> toI32(3)
|
|
691
|
-
}
|
|
692
|
-
const hi: u32 = toU32(bits >> toU64(32))
|
|
693
|
-
return toI32(4) + ((toI32(31) - toI32(Math.clz32(hi & (toU32(0) - hi)))) >> toI32(3))
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
/**
|
|
697
|
-
* Lanes `at` to `at + 7` of `v`, the first the lowest byte: eight bytes of a
|
|
698
|
-
* sixteen-byte load as one word, which `opt` folds to a bitcast of the
|
|
699
|
-
* vector's half. One load is one bounds check for sixteen bytes, where eight
|
|
700
|
-
* reads of `buf[i + k]` would be eight.
|
|
701
|
-
*/
|
|
702
|
-
const http1Lanes = (v: u8x16, at: i32): u64 => {
|
|
703
|
-
if (at === 0) {
|
|
704
|
-
return (
|
|
705
|
-
toU64(extract(v, 0)) |
|
|
706
|
-
(toU64(extract(v, 1)) << toU64(8)) |
|
|
707
|
-
(toU64(extract(v, 2)) << toU64(16)) |
|
|
708
|
-
(toU64(extract(v, 3)) << toU64(24)) |
|
|
709
|
-
(toU64(extract(v, 4)) << toU64(32)) |
|
|
710
|
-
(toU64(extract(v, 5)) << toU64(40)) |
|
|
711
|
-
(toU64(extract(v, 6)) << toU64(48)) |
|
|
712
|
-
(toU64(extract(v, 7)) << toU64(56))
|
|
713
|
-
)
|
|
714
|
-
}
|
|
715
|
-
return (
|
|
716
|
-
toU64(extract(v, 8)) |
|
|
717
|
-
(toU64(extract(v, 9)) << toU64(8)) |
|
|
718
|
-
(toU64(extract(v, 10)) << toU64(16)) |
|
|
719
|
-
(toU64(extract(v, 11)) << toU64(24)) |
|
|
720
|
-
(toU64(extract(v, 12)) << toU64(32)) |
|
|
721
|
-
(toU64(extract(v, 13)) << toU64(40)) |
|
|
722
|
-
(toU64(extract(v, 14)) << toU64(48)) |
|
|
723
|
-
(toU64(extract(v, 15)) << toU64(56))
|
|
724
|
-
)
|
|
725
|
-
}
|
|
726
|
-
|
|
727
673
|
/** `http1TakeLine`'s answer when a whole line is ready; never an event. */
|
|
728
674
|
const HTTP1_LINE: i32 = -1
|
|
729
675
|
|
|
@@ -756,18 +702,12 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
756
702
|
let found: boolean = false
|
|
757
703
|
while (!found && i >= 0 && i <= stop - 16 && i <= toI32(buf.length) - 16) {
|
|
758
704
|
const v: u8x16 = loadU8x16(buf, i)
|
|
759
|
-
const
|
|
760
|
-
if (
|
|
761
|
-
i +=
|
|
705
|
+
const bad: i32 = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
|
|
706
|
+
if (bad !== 0) {
|
|
707
|
+
i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
762
708
|
found = true
|
|
763
709
|
} else {
|
|
764
|
-
|
|
765
|
-
if (high !== toU64(0)) {
|
|
766
|
-
i += 8 + http1FirstByte(high)
|
|
767
|
-
found = true
|
|
768
|
-
} else {
|
|
769
|
-
i += 16
|
|
770
|
-
}
|
|
710
|
+
i += 16
|
|
771
711
|
}
|
|
772
712
|
}
|
|
773
713
|
while (!found && i >= 0 && i < stop && i < toI32(buf.length) && buf[i] >= 32 && buf[i] !== 127) {
|
|
@@ -1186,7 +1126,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1186
1126
|
const methodEnd: i32 = i
|
|
1187
1127
|
i += 1
|
|
1188
1128
|
const targetStart: i32 = i
|
|
1189
|
-
|
|
1129
|
+
// The target is visible ASCII, 33 to 126: sixteen bytes at a time, as far
|
|
1130
|
+
// as the first that is not or the line's end, and a byte at a time for a
|
|
1131
|
+
// tail shorter than a vector before `buf`'s end.
|
|
1132
|
+
let found: boolean = false
|
|
1133
|
+
while (!found && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
|
|
1134
|
+
const bad: i32 = bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93)))
|
|
1135
|
+
if (bad !== 0) {
|
|
1136
|
+
i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
1137
|
+
found = true
|
|
1138
|
+
} else {
|
|
1139
|
+
i += 16
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
if (i > b) {
|
|
1143
|
+
i = b
|
|
1144
|
+
}
|
|
1145
|
+
while (!found && i >= 0 && i < b && i < toI32(buf.length) && buf[i] > 32 && buf[i] < 127) {
|
|
1190
1146
|
i += 1
|
|
1191
1147
|
}
|
|
1192
1148
|
if (i - targetStart > p.maxTarget) {
|
|
@@ -1692,7 +1648,8 @@ export const http1IsFieldText = (text: string): boolean => {
|
|
|
1692
1648
|
* (which would end the line early and let the rest be read as a field or a
|
|
1693
1649
|
* second response); a field name that is not a token; `names` and `values` of
|
|
1694
1650
|
* different lengths; a `Content-Length` or `Transfer-Encoding` among the
|
|
1695
|
-
* caller's fields, since the framing is decided here;
|
|
1651
|
+
* caller's fields, since the framing is decided here; two `Date` fields in
|
|
1652
|
+
* any case, since `Date` is a singleton (RFC 9110 §5.3); a 1xx or 204 response
|
|
1696
1653
|
* with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
|
|
1697
1654
|
* below zero.
|
|
1698
1655
|
*
|
|
@@ -1724,6 +1681,7 @@ export const http1ResponseHead = (
|
|
|
1724
1681
|
return null
|
|
1725
1682
|
}
|
|
1726
1683
|
const parts: string[] = [`HTTP/1.1 ${status} ${reason}\r\n`]
|
|
1684
|
+
let dated: boolean = false
|
|
1727
1685
|
for (let i: i32 = 0; i < count && i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
1728
1686
|
const name: string = names[i]
|
|
1729
1687
|
const value: string = values[i]
|
|
@@ -1737,6 +1695,12 @@ export const http1ResponseHead = (
|
|
|
1737
1695
|
) {
|
|
1738
1696
|
return null
|
|
1739
1697
|
}
|
|
1698
|
+
if (http1SameWord(name, 0, nameLength, "date")) {
|
|
1699
|
+
if (dated) {
|
|
1700
|
+
return null
|
|
1701
|
+
}
|
|
1702
|
+
dated = true
|
|
1703
|
+
}
|
|
1740
1704
|
parts.push(`${name}: ${value}\r\n`)
|
|
1741
1705
|
}
|
|
1742
1706
|
if (bodyLength >= 0) {
|
|
@@ -2041,16 +2005,18 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
|
2041
2005
|
}
|
|
2042
2006
|
|
|
2043
2007
|
/**
|
|
2044
|
-
* `http1ResponseHead`'s status line and header section, written into `out`
|
|
2045
|
-
*
|
|
2046
|
-
* `nish/net/http-fields` holds them), and `connection`, when not empty, is
|
|
2047
|
-
*
|
|
2048
|
-
*
|
|
2049
|
-
*
|
|
2050
|
-
*
|
|
2051
|
-
*
|
|
2008
|
+
* `http1ResponseHead`'s status line and header section, written into `out` at
|
|
2009
|
+
* `at` with nothing allocated: field names and values are octets (as
|
|
2010
|
+
* `nish/net/http-fields` holds them), and `connection`, when not empty, is sent
|
|
2011
|
+
* as a `Connection` field after the caller's. `date`, when not empty, is sent
|
|
2012
|
+
* as a `Date` field between the caller's fields and the framing ones (RFC 9110
|
|
2013
|
+
* §6.6.1), on a final status only, and not when the caller's fields already
|
|
2014
|
+
* hold a `date` in any case: the caller's wins, so a response carries one.
|
|
2015
|
+
*
|
|
2016
|
+
* Answers where the head ends; `HTTP1_REFUSED` for everything
|
|
2052
2017
|
* `http1ResponseHead` refuses, a `Connection` among the caller's fields when
|
|
2053
|
-
* `connection` is given, a `connection` that is not field text,
|
|
2018
|
+
* `connection` is given, a `connection` that is not field text, two `date`
|
|
2019
|
+
* fields among the caller's (a singleton, RFC 9110 §5.3), and a `date`
|
|
2054
2020
|
* that is not field text on a final status, the only one it is sent on;
|
|
2055
2021
|
* `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
|
|
2056
2022
|
* counted when it is sent. Either way `out` is untouched.
|
|
@@ -2094,7 +2060,7 @@ export const http1WriteResponseHead = (
|
|
|
2094
2060
|
// field, the connection field and the empty line: counted before anything
|
|
2095
2061
|
// is written, in 64 bits so that no caller's lengths can wrap the sum.
|
|
2096
2062
|
let need: i64 = toI64(17) + toI64(reason.length)
|
|
2097
|
-
let
|
|
2063
|
+
let dated: boolean = false
|
|
2098
2064
|
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
2099
2065
|
const name: u8[] = names[i]
|
|
2100
2066
|
const value: u8[] = values[i]
|
|
@@ -2108,10 +2074,15 @@ export const http1WriteResponseHead = (
|
|
|
2108
2074
|
return HTTP1_REFUSED
|
|
2109
2075
|
}
|
|
2110
2076
|
if (http1BytesAre(name, "date")) {
|
|
2111
|
-
|
|
2077
|
+
// Date is a singleton (RFC 9110 §5.3): a second one is refused.
|
|
2078
|
+
if (dated) {
|
|
2079
|
+
return HTTP1_REFUSED
|
|
2080
|
+
}
|
|
2081
|
+
dated = true
|
|
2112
2082
|
}
|
|
2113
2083
|
need = need + toI64(name.length) + toI64(value.length) + toI64(4)
|
|
2114
2084
|
}
|
|
2085
|
+
const sendDate: boolean = status >= 200 && toI32(date.length) > 0 && !dated
|
|
2115
2086
|
if (sendDate) {
|
|
2116
2087
|
// "Date: " value CRLF: 37 bytes for an IMF-fixdate.
|
|
2117
2088
|
need = need + toI64(8) + toI64(date.length)
|
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
|
|
@@ -508,6 +511,7 @@ const http1ServerReserved = (name: u8[]): boolean =>
|
|
|
508
511
|
*/
|
|
509
512
|
export class Http1OutputPool {
|
|
510
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. */
|
|
511
515
|
free: u8[][]
|
|
512
516
|
private size: i32 = 0
|
|
513
517
|
/** How many buffers the pool has made. */
|
|
@@ -579,6 +583,7 @@ export class Http1Connection {
|
|
|
579
583
|
pool: Http1OutputPool | null
|
|
580
584
|
/** Where the parser's input and head buffers go while the slot rests, with `Http1Config.idleInput`; null otherwise. */
|
|
581
585
|
inputs: Http1OutputPool | null
|
|
586
|
+
/** Where the parser's head buffer goes while the slot rests, beside `inputs`; null otherwise. */
|
|
582
587
|
heads: Http1OutputPool | null
|
|
583
588
|
/**
|
|
584
589
|
* The `Date` every final response carries. The connection makes its own;
|
|
@@ -606,7 +611,9 @@ export class Http1Connection {
|
|
|
606
611
|
* @internal Read by `std/net/handler.ts` to know how much of a sized body is still to write.
|
|
607
612
|
*/
|
|
608
613
|
responseLeft: i32 = 0
|
|
614
|
+
/** Where the unsent bytes start in `output`; a carrier sends from here and calls `consume` with what it sent. */
|
|
609
615
|
outputStart: i32 = 0
|
|
616
|
+
/** Where the unsent bytes end in `output`, one past the last; equal to `outputStart` when nothing waits. */
|
|
610
617
|
outputEnd: i32 = 0
|
|
611
618
|
/**
|
|
612
619
|
* `output.length`, kept beside the offsets so that `acquire` and `release`
|
|
@@ -634,7 +641,9 @@ export class Http1Connection {
|
|
|
634
641
|
private inputEnded: boolean = false
|
|
635
642
|
/** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
|
|
636
643
|
headTimed: boolean = false
|
|
644
|
+
/** Where the bytes `H1_BODY` or `H1_WS_MESSAGE` handed over start in `data`. */
|
|
637
645
|
dataStart: i32 = 0
|
|
646
|
+
/** How many bytes `H1_BODY` or `H1_WS_MESSAGE` handed over, from `data[dataStart]`. */
|
|
638
647
|
dataLength: i32 = 0
|
|
639
648
|
/** `H1_WS_MESSAGE`'s opcode: text or binary. */
|
|
640
649
|
opcode: i32 = 0
|
|
@@ -878,6 +887,16 @@ export class Http1Connection {
|
|
|
878
887
|
this.inputEnded = true
|
|
879
888
|
}
|
|
880
889
|
|
|
890
|
+
/**
|
|
891
|
+
* Whether the peer's byte stream has ended (`endInput`): a request still
|
|
892
|
+
* held on the connection is one its client hung up on, or half-closed.
|
|
893
|
+
*
|
|
894
|
+
* @returns true once the carrier read the end of the peer's stream
|
|
895
|
+
*/
|
|
896
|
+
ended(): boolean {
|
|
897
|
+
return this.inputEnded
|
|
898
|
+
}
|
|
899
|
+
|
|
881
900
|
/** The socket failed: nothing more is sent or read, and the connection is done. */
|
|
882
901
|
abort(): void {
|
|
883
902
|
this.state = H1_S_FAILED
|
|
@@ -1355,21 +1374,22 @@ export class Http1Connection {
|
|
|
1355
1374
|
}
|
|
1356
1375
|
|
|
1357
1376
|
/**
|
|
1358
|
-
* `write` of `buf[off .. off + len)`, sent from `buf` itself where it can
|
|
1359
|
-
*
|
|
1360
|
-
*
|
|
1361
|
-
*
|
|
1362
|
-
*
|
|
1363
|
-
*
|
|
1364
|
-
*
|
|
1365
|
-
*
|
|
1366
|
-
*
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1369
|
-
* `
|
|
1370
|
-
*
|
|
1371
|
-
*
|
|
1372
|
-
*
|
|
1377
|
+
* `write` of `buf[off .. off + len)`, sent from `buf` itself where it can be:
|
|
1378
|
+
* under a `Content-Length` response on a connection whose carrier named its
|
|
1379
|
+
* socket (`fd`, set by `Http1Server.accept`) and with no write held back, the
|
|
1380
|
+
* queued output and the body leave in one `netWrite2`, and only what the
|
|
1381
|
+
* socket did not take is copied into the output, the way `write` copies it
|
|
1382
|
+
* (what does not fit is answered short, and `H1_WRITE` says when to send the
|
|
1383
|
+
* rest).
|
|
1384
|
+
*
|
|
1385
|
+
* Once it returns the connection holds no reference to `buf`, so the caller
|
|
1386
|
+
* may reuse or give back its bytes at once. A chunked or empty response, a
|
|
1387
|
+
* write held back (`H1_AGAIN` or a short answer not yet followed by
|
|
1388
|
+
* `H1_WRITE`), and a connection with no socket (`Http1TlsServer`, a sans-IO
|
|
1389
|
+
* carrier) copy all of it: it is `write`, with the same bytes on the wire.
|
|
1390
|
+
* Answers what `write` answers, `H1_INVALID` for more than the response still
|
|
1391
|
+
* owes; `H1_CLOSED` also when the socket failed during the send, which aborts
|
|
1392
|
+
* the connection. A window outside `buf` panics.
|
|
1373
1393
|
*
|
|
1374
1394
|
* @param buf the body bytes to send, free for the caller to reuse once this returns
|
|
1375
1395
|
* @param off where in `buf` the window starts
|
|
@@ -1496,8 +1516,8 @@ export class Http1Connection {
|
|
|
1496
1516
|
/**
|
|
1497
1517
|
* The segment `respondEncoded` counts in under `enc.segmentMode`: this
|
|
1498
1518
|
* connection's `mss` when the program set it, else `enc.mss` when the
|
|
1499
|
-
* program set it
|
|
1500
|
-
*
|
|
1519
|
+
* program set it (above 0), else the socket's (`socketMss`), else
|
|
1520
|
+
* `HTTP_DEFAULT_MSS`.
|
|
1501
1521
|
*
|
|
1502
1522
|
* @param enc the compressor whose own `mss` is the fallback after this connection's
|
|
1503
1523
|
* @returns the segment size in bytes that segment counting uses for this connection
|
|
@@ -1506,7 +1526,7 @@ export class Http1Connection {
|
|
|
1506
1526
|
if (this.mss > 0) {
|
|
1507
1527
|
return this.mss
|
|
1508
1528
|
}
|
|
1509
|
-
if (enc.mss > 0
|
|
1529
|
+
if (enc.mss > 0) {
|
|
1510
1530
|
return enc.mss
|
|
1511
1531
|
}
|
|
1512
1532
|
return this.socketMss > 0 ? this.socketMss : HTTP_DEFAULT_MSS
|
|
@@ -1515,24 +1535,24 @@ export class Http1Connection {
|
|
|
1515
1535
|
/**
|
|
1516
1536
|
* Answers the current request with all of `body[off .. off + len)` in one
|
|
1517
1537
|
* call: `respond`, `write` and `end`, the body compressed by `enc` when
|
|
1518
|
-
* `coding` (from `acceptEncoding`) is gzip and `enc.compress` takes it,
|
|
1519
|
-
*
|
|
1520
|
-
*
|
|
1521
|
-
*
|
|
1522
|
-
*
|
|
1523
|
-
*
|
|
1524
|
-
*
|
|
1525
|
-
*
|
|
1526
|
-
* says what was chosen. A
|
|
1527
|
-
*
|
|
1528
|
-
* (`enc.compressAnyway`).
|
|
1529
|
-
*
|
|
1530
|
-
*
|
|
1531
|
-
* it: `H1_AGAIN` when the output has
|
|
1532
|
-
* when to try again, compressing again),
|
|
1533
|
-
* larger than the output could ever hold or one
|
|
1534
|
-
* `H1_CLOSED` with no request to answer; 0 once it is
|
|
1535
|
-
* outside `body` panics.
|
|
1538
|
+
* `coding` (from `acceptEncoding`) is gzip and `enc.compress` takes it, with
|
|
1539
|
+
* `Content-Encoding: gzip` and the compressed `Content-Length`, and otherwise
|
|
1540
|
+
* sent as it is; `Vary: Accept-Encoding` while `enc.vary` is set and the
|
|
1541
|
+
* choice turned on the request (`HttpCompressor.varies`).
|
|
1542
|
+
*
|
|
1543
|
+
* Under `enc.segmentMode` the compressed response goes out only when, head
|
|
1544
|
+
* included, it takes fewer segments of `segmentMss(enc)` bytes than the body
|
|
1545
|
+
* as it is, and a response that fits one segment as it is goes so without
|
|
1546
|
+
* compressing (`enc.oneSegment`); `enc.decision` says what was chosen. A
|
|
1547
|
+
* request that refuses identity (`identityRefused`) is sent gzip whatever the
|
|
1548
|
+
* length, the saving or the segments (`enc.compressAnyway`). The program's
|
|
1549
|
+
* fields may not name `Content-Encoding` or, with `enc.vary`, `Vary`.
|
|
1550
|
+
*
|
|
1551
|
+
* The whole response is written or none of it: `H1_AGAIN` when the output has
|
|
1552
|
+
* no room for it yet (`H1_WRITE` says when to try again, compressing again),
|
|
1553
|
+
* `H1_INVALID` for a response larger than the output could ever hold or one
|
|
1554
|
+
* `respond` refuses, `H1_CLOSED` with no request to answer; 0 once it is
|
|
1555
|
+
* queued. A window outside `body` panics.
|
|
1536
1556
|
*
|
|
1537
1557
|
* @param status the HTTP status code to send, as `respond` takes it
|
|
1538
1558
|
* @param names the program's field names, without `Content-Encoding` (or `Vary` under `enc.vary`)
|
|
@@ -1859,6 +1879,7 @@ export class Http1Server {
|
|
|
1859
1879
|
outputs: Http1OutputPool
|
|
1860
1880
|
/** The input and head buffers resting slots lend, with `Http1Config.idleInput`. */
|
|
1861
1881
|
inputs: Http1OutputPool
|
|
1882
|
+
/** The head buffers resting slots lend, beside `inputs`; `made` counts how many the slots ever needed. */
|
|
1862
1883
|
heads: Http1OutputPool
|
|
1863
1884
|
/**
|
|
1864
1885
|
* The slots closed since they were used, a stack whose top `accept` takes
|
|
@@ -2248,6 +2269,7 @@ export class Http1Server {
|
|
|
2248
2269
|
*/
|
|
2249
2270
|
export class Http1TlsServer {
|
|
2250
2271
|
private config: Http1Config
|
|
2272
|
+
/** The TLS 1.3 pool underneath, one slot beside each connection; read it for a slot's TLS connection. */
|
|
2251
2273
|
tls: TlsTcpServer
|
|
2252
2274
|
/**
|
|
2253
2275
|
* The connections built so far, slot `k`'s at `k`: one per slot ever
|
package/std/net/http2-frame.ts
CHANGED
|
@@ -27,22 +27,23 @@
|
|
|
27
27
|
* const end: i32 = http2WriteData(out, at, streamId, body, 0, n, H2_FLAG_END_STREAM, -1);
|
|
28
28
|
* };
|
|
29
29
|
*
|
|
30
|
-
* **What a parse refuses**, as the error code RFC 9113 names, which closes
|
|
31
|
-
*
|
|
32
|
-
* (FRAME_SIZE_ERROR, §4.2); DATA, HEADERS, PRIORITY, RST_STREAM,
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
30
|
+
* **What a parse refuses**, as the error code RFC 9113 names, which closes the
|
|
31
|
+
* connection: a frame longer than the receiver's SETTINGS_MAX_FRAME_SIZE
|
|
32
|
+
* (FRAME_SIZE_ERROR, §4.2); DATA, HEADERS, PRIORITY, RST_STREAM, PUSH_PROMISE
|
|
33
|
+
* or CONTINUATION on stream 0, and SETTINGS, PING or GOAWAY on any other
|
|
34
|
+
* (PROTOCOL_ERROR); a frame too short for its fixed fields, a RST_STREAM or
|
|
35
|
+
* WINDOW_UPDATE that is not four bytes, a PING that is not eight, a SETTINGS
|
|
36
|
+
* whose length is not a multiple of six or an ACK with a payload, and a GOAWAY
|
|
37
|
+
* under eight (FRAME_SIZE_ERROR); padding as long as what it pads or longer
|
|
38
|
+
* (PROTOCOL_ERROR, §6.1); and a WINDOW_UPDATE of zero on the connection
|
|
39
|
+
* (PROTOCOL_ERROR, §6.9).
|
|
40
|
+
*
|
|
41
|
+
* Two refusals only reset the stream, and come back in `streamError` with a
|
|
42
|
+
* parse that answers 0: a PRIORITY of the wrong length (FRAME_SIZE_ERROR,
|
|
43
|
+
* §6.3), and a WINDOW_UPDATE of zero on a stream (PROTOCOL_ERROR). A HEADERS or
|
|
44
|
+
* PRIORITY that makes a stream depend on itself is the same (PROTOCOL_ERROR,
|
|
45
|
+
* §5.3.1). A frame of a type this module does not know is read and answers 0:
|
|
46
|
+
* §4.1 says it is ignored, and that is the connection's to do.
|
|
46
47
|
*
|
|
47
48
|
* SETTINGS values are checked one at a time by `http2SettingError`, because
|
|
48
49
|
* which value a refusal names is the reader's business (§6.5.2): an
|
package/std/net/http2-tls.ts
CHANGED
|
@@ -56,23 +56,25 @@
|
|
|
56
56
|
* configuration offers `h2` (and may offer `http/1.1` beside it, for a
|
|
57
57
|
* program that serves both and calls `alpn(slot)` itself).
|
|
58
58
|
*
|
|
59
|
-
* **Slots, a cap; buffers, borrowed.** The constructor makes the TLS pool
|
|
60
|
-
*
|
|
59
|
+
* **Slots, a cap; buffers, borrowed.** The constructor makes the TLS pool and
|
|
60
|
+
* slot 0's connection, but no other slot's and no frame buffer: a slot's
|
|
61
61
|
* connection is made by the first `accept` into it and kept, so a pool sized
|
|
62
62
|
* for its peak costs at start-up only what does not depend on the size, and
|
|
63
|
-
* holds at most `size` connections once the peak has come.
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* slot
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* slot count, so a
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
63
|
+
* holds at most `size` connections once the peak has come.
|
|
64
|
+
*
|
|
65
|
+
* A connection borrows its input, output and header-block buffers from the
|
|
66
|
+
* server's `Http2Pools` when it first needs each, and gives each back once it
|
|
67
|
+
* is empty and when the slot closes, so a free or idle slot holds none. A slot
|
|
68
|
+
* whose handshake is not done holds one output buffer, its SETTINGS written and
|
|
69
|
+
* waiting on TLS, until the handshake ends or the slot closes: one client that
|
|
70
|
+
* stalls its handshake keeps one buffer, as it kept a whole slot before
|
|
71
|
+
* (`docs/security/tls.md`, TLS-4). Each pool's ceiling is the slot count, so a
|
|
72
|
+
* borrow never fails.
|
|
73
|
+
*
|
|
74
|
+
* `accept` resets the slot's connection, and once the pools are warm nothing a
|
|
75
|
+
* connection does allocates but a header block's HPACK decoding
|
|
76
|
+
* (`docs/security/http2.md`, H2-1). The caps are the `Http2Config`'s, one for
|
|
77
|
+
* every slot, so a program sets them once.
|
|
76
78
|
*
|
|
77
79
|
* **The Date.** Every final response carries one `date` field (RFC 9110
|
|
78
80
|
* §6.6.1) from the pool's `HttpDate`, which formats the IMF-fixdate at most
|