@amritk/nish-aarch64-linux 0.49.1 → 0.50.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 +233 -159
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +12 -9
- package/std/README.md +8 -5
- package/std/base64.ts +74 -1
- package/std/bytes.ts +201 -93
- package/std/compress/deflate.ts +20 -15
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/hmac.ts +1 -1
- package/std/crypto/jwt.ts +32 -19
- package/std/crypto/p256.ts +9 -12
- package/std/crypto/pki-roots.ts +7 -4
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1632 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +135 -68
- 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 +222 -104
- package/std/json.ts +81 -46
- 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 +120 -38
- package/std/net/handler.ts +178 -66
- package/std/net/hpack.ts +192 -98
- package/std/net/http-compress.ts +83 -30
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +317 -20
- package/std/net/http1-client-codec.ts +137 -40
- package/std/net/http1-codec.ts +391 -202
- 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 +12 -15
- package/std/net/http3.ts +41 -29
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +92 -54
- 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 +66 -25
- 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 +19 -12
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +7 -5
- package/std/net/quic.ts +36 -31
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +81 -33
- package/std/net/tls/record.ts +26 -13
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +152 -38
- package/std/net/upstream.ts +65 -19
- package/std/net/url.ts +97 -78
- package/std/net/websocket.ts +16 -9
- package/std/net/webtransport.ts +28 -12
- 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 +158 -22
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +33 -11
- 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/http-compress.ts
CHANGED
|
@@ -195,12 +195,15 @@ const httpSegmentsOf = (bytes: i32, size: u32): i32 => {
|
|
|
195
195
|
if (bytes <= 0) {
|
|
196
196
|
return 0
|
|
197
197
|
}
|
|
198
|
-
// `size`
|
|
199
|
-
//
|
|
200
|
-
//
|
|
201
|
-
// the
|
|
198
|
+
// Both callers pass a `size` of 1 or more, so the test always holds; it is
|
|
199
|
+
// what shows the division has a divisor. The quotient is then at most
|
|
200
|
+
// `bytes - 1`, so `whole < bytes` always holds too, and it is what proves
|
|
201
|
+
// the count, at most `bytes`, cannot overflow.
|
|
202
202
|
if (size > 0) {
|
|
203
|
-
|
|
203
|
+
const whole: i32 = toI32(toU32(bytes - 1) / size)
|
|
204
|
+
if (whole >= 0 && whole < bytes) {
|
|
205
|
+
return whole + 1
|
|
206
|
+
}
|
|
204
207
|
}
|
|
205
208
|
return 0
|
|
206
209
|
}
|
|
@@ -291,8 +294,12 @@ const httpQValue = (buf: u8[], start: i32, end: i32): i32 => {
|
|
|
291
294
|
return -1
|
|
292
295
|
}
|
|
293
296
|
// At most three digits follow the point, each 9 or less at a scale of 100
|
|
294
|
-
// or less, so `q` stays
|
|
295
|
-
|
|
297
|
+
// or less, so `q` stays at most 1000 (`HTTP_Q_ONE`) and the test always
|
|
298
|
+
// holds; it is what proves the sum cannot overflow.
|
|
299
|
+
if (q < 0 || q > 1000 || scale < 0 || scale > 100) {
|
|
300
|
+
return -1
|
|
301
|
+
}
|
|
302
|
+
q = q + d * scale
|
|
296
303
|
scale = scale / 10
|
|
297
304
|
}
|
|
298
305
|
return q
|
|
@@ -694,9 +701,13 @@ export class HttpCompressor {
|
|
|
694
701
|
return -1
|
|
695
702
|
}
|
|
696
703
|
// The longest output kept; `output` holds `compressBound(maxLength)`, more.
|
|
697
|
-
// `len` is 0 or more and the saving 1 or more, so the
|
|
698
|
-
//
|
|
699
|
-
const
|
|
704
|
+
// `len` is 0 or more and the saving 1 or more, so the test always holds;
|
|
705
|
+
// it is what proves the difference cannot overflow.
|
|
706
|
+
const saving: i32 = this.leastSaving(len)
|
|
707
|
+
let keep: i32 = -1
|
|
708
|
+
if (len >= 0 && saving >= 0) {
|
|
709
|
+
keep = len - saving
|
|
710
|
+
}
|
|
700
711
|
let n: i32 = -1
|
|
701
712
|
if (keep > 0) {
|
|
702
713
|
const fixed: boolean =
|
|
@@ -706,9 +717,10 @@ export class HttpCompressor {
|
|
|
706
717
|
let room: i32 = keep
|
|
707
718
|
if (deflate) {
|
|
708
719
|
// Below `size - HTTP_ZLIB_SHORTER`, `keep` plus that many is below
|
|
709
|
-
// `size
|
|
720
|
+
// `size`; the test, with the constant's 12 spelled for the prover, is
|
|
721
|
+
// what proves the sum cannot overflow.
|
|
710
722
|
const size: i32 = toI32(this.output.length)
|
|
711
|
-
room = keep < size -
|
|
723
|
+
room = keep < size - 12 ? keep + 12 : size
|
|
712
724
|
}
|
|
713
725
|
n = fixed
|
|
714
726
|
? this.fixed.compress(body, off, len, this.output, toI32(0), room)
|
|
@@ -923,18 +935,38 @@ export class HttpCompressor {
|
|
|
923
935
|
* uncompressed
|
|
924
936
|
*/
|
|
925
937
|
http1IdentityHead(encodedHead: i32, encodedLength: i32, length: i32): i32 {
|
|
926
|
-
// `encodedHead` is
|
|
927
|
-
//
|
|
928
|
-
//
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
938
|
+
// `encodedHead` is the length of a head just written, far below 2^30, each
|
|
939
|
+
// field here is a fixed word ("gzip", "deflate", "vary",
|
|
940
|
+
// "accept-encoding") and a count has at most ten digits, so the tests
|
|
941
|
+
// always hold; they are what prove the sums below cannot overflow.
|
|
942
|
+
const gzip: i32 = toI32(this.gzipValue.length)
|
|
943
|
+
const coding: i32 = toI32(this.codingValue.length)
|
|
944
|
+
const name: i32 = toI32(this.varyName.length)
|
|
945
|
+
const value: i32 = toI32(this.acceptEncodingValue.length)
|
|
946
|
+
const before: i32 = httpDecimalDigits(encodedLength)
|
|
947
|
+
const after: i32 = httpDecimalDigits(length)
|
|
948
|
+
if (encodedHead < 0 || encodedHead > 1073741824) {
|
|
949
|
+
return encodedHead
|
|
950
|
+
}
|
|
951
|
+
if (
|
|
952
|
+
gzip < 0 ||
|
|
953
|
+
gzip > 64 ||
|
|
954
|
+
coding < 0 ||
|
|
955
|
+
coding > 64 ||
|
|
956
|
+
name < 0 ||
|
|
957
|
+
name > 64 ||
|
|
958
|
+
value < 0 ||
|
|
959
|
+
value > 64
|
|
960
|
+
) {
|
|
961
|
+
return encodedHead
|
|
962
|
+
}
|
|
963
|
+
if (before < 0 || before > 10 || after < 0 || after > 10) {
|
|
964
|
+
return encodedHead
|
|
965
|
+
}
|
|
934
966
|
if (this.vary) {
|
|
935
|
-
|
|
967
|
+
return encodedHead - HTTP1_GZIP_FIELD_BYTES + gzip - coding - (name + value + 4) - before + after
|
|
936
968
|
}
|
|
937
|
-
return
|
|
969
|
+
return encodedHead - HTTP1_GZIP_FIELD_BYTES + gzip - coding - before + after
|
|
938
970
|
}
|
|
939
971
|
|
|
940
972
|
/**
|
|
@@ -1117,6 +1149,27 @@ export const HTTP_DECODE_BAD_CALL: i32 = -3
|
|
|
1117
1149
|
/** The first output buffer `HttpBodyDecoder` makes, when its limit allows that many bytes. */
|
|
1118
1150
|
const HTTP_DECODE_FIRST: i32 = 4096
|
|
1119
1151
|
|
|
1152
|
+
/**
|
|
1153
|
+
* Why `new HttpBodyDecoder(limit)` would panic: a `limit` under 1. A caller
|
|
1154
|
+
* that checks it first, and leaves on a fault, reaches no panic there.
|
|
1155
|
+
* @param limit the limit the decoder would be made with
|
|
1156
|
+
* @returns the fault, or "" when the limit is at least 1
|
|
1157
|
+
*/
|
|
1158
|
+
export const httpBodyDecoderFault = (limit: i32): string => {
|
|
1159
|
+
if (limit < 1) {
|
|
1160
|
+
return `HttpBodyDecoder: a limit of ${limit} is under 1`
|
|
1161
|
+
}
|
|
1162
|
+
return ""
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/** Panics on the fault `httpBodyDecoderFault` names, the decoder's contract. */
|
|
1166
|
+
const httpCheckBodyDecoder = (limit: i32): void => {
|
|
1167
|
+
const fault: string = httpBodyDecoderFault(limit)
|
|
1168
|
+
if (fault.length > 0) {
|
|
1169
|
+
panic(fault)
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1120
1173
|
/**
|
|
1121
1174
|
* Decodes a request body sent with `Content-Encoding: gzip` or `deflate`
|
|
1122
1175
|
* under a limit on what it decodes to, so a small body that inflates to
|
|
@@ -1142,13 +1195,11 @@ export class HttpBodyDecoder {
|
|
|
1142
1195
|
|
|
1143
1196
|
/**
|
|
1144
1197
|
* A decoder that refuses a body decoding to more than `limit` bytes; a
|
|
1145
|
-
* `limit` under 1 panics.
|
|
1198
|
+
* `limit` under 1 panics, the fault `httpBodyDecoderFault` names first.
|
|
1146
1199
|
* @param limit the most bytes one body may decode to
|
|
1147
1200
|
*/
|
|
1148
1201
|
constructor(limit: i32) {
|
|
1149
|
-
|
|
1150
|
-
panic(`HttpBodyDecoder: a limit of ${limit} is under 1`)
|
|
1151
|
-
}
|
|
1202
|
+
httpCheckBodyDecoder(limit)
|
|
1152
1203
|
this.limit = limit
|
|
1153
1204
|
this.output = []
|
|
1154
1205
|
this.gunzip = new GzipDecompressor()
|
|
@@ -1202,8 +1253,9 @@ export class HttpBodyDecoder {
|
|
|
1202
1253
|
const limit: i32 = this.limit
|
|
1203
1254
|
// A first guess of four times the input, so most bodies decode at once.
|
|
1204
1255
|
// Under the test, `len` is at most a quarter of `limit`, so four times it
|
|
1205
|
-
// is an `i32
|
|
1206
|
-
|
|
1256
|
+
// is an `i32`. The literal is the largest quarter an `i32` limit has, so
|
|
1257
|
+
// its test never decides the answer; it is what proves the product.
|
|
1258
|
+
let size: i32 = len < 0 || len > 536870911 || len > limit >> 2 ? limit : len * 4
|
|
1207
1259
|
if (size < HTTP_DECODE_FIRST) {
|
|
1208
1260
|
size = limit < HTTP_DECODE_FIRST ? limit : HTTP_DECODE_FIRST
|
|
1209
1261
|
}
|
|
@@ -1232,8 +1284,9 @@ export class HttpBodyDecoder {
|
|
|
1232
1284
|
return HTTP_DECODE_TOO_LARGE
|
|
1233
1285
|
}
|
|
1234
1286
|
// Under the test, `have` is at most half of `limit`, so twice it is an
|
|
1235
|
-
// `i32
|
|
1236
|
-
|
|
1287
|
+
// `i32`. The literal is the largest half an `i32` limit has, so its test
|
|
1288
|
+
// never decides the answer; it is what proves the product.
|
|
1289
|
+
size = have < 0 || have > 1073741823 || have > limit >> 1 ? limit : have * 2
|
|
1237
1290
|
}
|
|
1238
1291
|
}
|
|
1239
1292
|
}
|
package/std/net/http-fields.ts
CHANGED
|
@@ -59,6 +59,18 @@
|
|
|
59
59
|
* `Date` field takes. It reads no clock, so a wasm32 build can import it; the
|
|
60
60
|
* caller supplies the second.
|
|
61
61
|
*
|
|
62
|
+
* **Media types.** `mediaTypeParse` reads a `Content-Type` value (RFC 9110
|
|
63
|
+
* §8.3.1): a `type/subtype` pair of tokens, lowercased, then `;`-separated
|
|
64
|
+
* parameters whose names are lowercased tokens and whose values are tokens or
|
|
65
|
+
* quoted strings, a backslash in one escaping the octet after it. A
|
|
66
|
+
* parameter named twice is refused, since two readers could believe
|
|
67
|
+
* different ones, and so is a 17th, which keeps that check linear in the
|
|
68
|
+
* value. `formDataDispositionParse` reads the `Content-Disposition`
|
|
69
|
+
* of a `multipart/form-data` part the same way, except that a backslash in a
|
|
70
|
+
* quoted value is a backslash: the HTML form submission algorithm writes `"`
|
|
71
|
+
* as `%22` and leaves `\` alone, so `filename="C:\x.txt"` names the file a
|
|
72
|
+
* browser sent. `mediaTypeParam` looks a parameter up.
|
|
73
|
+
*
|
|
62
74
|
* Names and values are octets (`u8[]`), as HPACK and QPACK answer them. A
|
|
63
75
|
* section that passes keeps references to the caller's arrays, not copies, so
|
|
64
76
|
* it is valid for as long as they are.
|
|
@@ -66,8 +78,8 @@
|
|
|
66
78
|
* Private helpers share the importing program's flat symbol namespace
|
|
67
79
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
68
80
|
*
|
|
69
|
-
* Written from RFC 9110, RFC 9113, RFC 9114
|
|
70
|
-
* another implementation.
|
|
81
|
+
* Written from RFC 9110, RFC 9113, RFC 9114, RFC 8441 and the HTML form
|
|
82
|
+
* submission algorithm, not ported from another implementation.
|
|
71
83
|
*/
|
|
72
84
|
|
|
73
85
|
/** A field section that has the shape asked for. */
|
|
@@ -355,8 +367,12 @@ const httpFieldsStatusOf = (value: u8[]): i32 => {
|
|
|
355
367
|
if (digit < 0 || digit > 9) {
|
|
356
368
|
return -1
|
|
357
369
|
}
|
|
358
|
-
//
|
|
359
|
-
|
|
370
|
+
// The length test above allows three digits, so `code` is at most 99
|
|
371
|
+
// before the last one; the test is what proves the sum cannot overflow.
|
|
372
|
+
if (code < 0 || code > 99) {
|
|
373
|
+
return -1
|
|
374
|
+
}
|
|
375
|
+
code = code * 10 + digit
|
|
360
376
|
}
|
|
361
377
|
return code >= 100 && code <= 599 ? code : -1
|
|
362
378
|
}
|
|
@@ -367,17 +383,17 @@ const httpFieldsLengthOf = (value: u8[]): i64 => {
|
|
|
367
383
|
if (n === 0 || n > HTTP_FIELDS_MAX_LENGTH_DIGITS) {
|
|
368
384
|
return toI64(-1)
|
|
369
385
|
}
|
|
370
|
-
|
|
386
|
+
// The value stays `u64` throughout: at most eighteen digits stay below
|
|
387
|
+
// 10^18, so the sum never wraps and its value fits an `i64` exactly.
|
|
388
|
+
let length: u64 = 0
|
|
371
389
|
for (const b of value) {
|
|
372
390
|
const digit: i32 = toI32(b) - 48
|
|
373
391
|
if (digit < 0 || digit > 9) {
|
|
374
392
|
return toI64(-1)
|
|
375
393
|
}
|
|
376
|
-
|
|
377
|
-
// leave an `i64`.
|
|
378
|
-
length = toI64(toU64(length) * toU64(10) + toU64(digit))
|
|
394
|
+
length = length * 10 + toU64(digit)
|
|
379
395
|
}
|
|
380
|
-
return length
|
|
396
|
+
return toI64(length)
|
|
381
397
|
}
|
|
382
398
|
|
|
383
399
|
/** Whether `authority` holds an `@`, which only userinfo would put there (RFC 9113 §8.3.1). */
|
|
@@ -714,16 +730,20 @@ const HTTP_FIELDS_DATE_NAMES: string = "SunMonTueWedThuFriSatJanFebMarAprMayJunJ
|
|
|
714
730
|
* Writes `b` at `out[at + k]`. Its one caller, `httpDateWrite`, has checked
|
|
715
731
|
* `0 <= at` and `at + HTTP_DATE_SIZE <= out.length` before the first write,
|
|
716
732
|
* and every `k` it passes is a constant from 0 to 28, under `HTTP_DATE_SIZE`:
|
|
717
|
-
* so `at + k` is inside `out
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
*
|
|
733
|
+
* so `at + k` is inside `out` and the tests always hold. The first test,
|
|
734
|
+
* whose literals are `HTTP_DATE_SIZE - 1` and `2^31 - 1` less that, proves
|
|
735
|
+
* the sum cannot overflow, and the second proves the index; nothing is
|
|
736
|
+
* allocated, and a byte it skipped would show in the `httpDateWrite` round
|
|
737
|
+
* trips `tests/run.js` pins byte for byte (`tests/link/net_http_date`). The
|
|
738
|
+
* skip is unreachable by that room check in `httpDateWrite`, which the prover
|
|
739
|
+
* does not carry across the call, so the tests stay.
|
|
723
740
|
*/
|
|
724
741
|
const httpFieldsPut = (out: u8[], at: i32, k: i32, b: u32): void => {
|
|
725
|
-
|
|
726
|
-
|
|
742
|
+
if (at < 0 || at > 2147483619 || k < 0 || k > 28) {
|
|
743
|
+
return
|
|
744
|
+
}
|
|
745
|
+
const pos: i32 = at + k
|
|
746
|
+
if (pos < toI32(out.length)) {
|
|
727
747
|
out[pos] = toU8(b)
|
|
728
748
|
}
|
|
729
749
|
}
|
|
@@ -733,22 +753,36 @@ const httpFieldsPut = (out: u8[], at: i32, k: i32, b: u32): void => {
|
|
|
733
753
|
* `out[at + k ..)`. `httpDateWrite` passes a weekday, `(days + 4) % 7`, so 0
|
|
734
754
|
* to 6, or `6 + month` with `month` 1 to 12 by the 153-day rule, so 7 to 18:
|
|
735
755
|
* `3 * index + 2` is at most 56, under the table's 57 letters, and the test
|
|
736
|
-
* always holds. It is what proves the index.
|
|
756
|
+
* always holds. It is what proves the index. Every `k` it is passed is 0 or
|
|
757
|
+
* 8, so the test on `k`, whose 26 is `HTTP_DATE_SIZE - 3`, always holds too;
|
|
758
|
+
* it is what proves `k + d` cannot overflow.
|
|
737
759
|
*/
|
|
738
760
|
const httpFieldsPutName = (out: u8[], at: i32, k: i32, index: u32): void => {
|
|
761
|
+
if (k < 0 || k > 26) {
|
|
762
|
+
return
|
|
763
|
+
}
|
|
739
764
|
const names: string = HTTP_FIELDS_DATE_NAMES
|
|
740
|
-
for (let d:
|
|
741
|
-
|
|
765
|
+
for (let d: i32 = 0; d < 3; d++) {
|
|
766
|
+
// The letter's place is worked in `u32`, as `index` is: at most 56.
|
|
767
|
+
const step: u32 = toU32(d)
|
|
768
|
+
const pos: i32 = toI32(index * 3 + step)
|
|
742
769
|
if (pos >= 0 && pos < toI32(names.length)) {
|
|
743
|
-
httpFieldsPut(out, at,
|
|
770
|
+
httpFieldsPut(out, at, k + d, toU32(names.charCodeAt(pos)))
|
|
744
771
|
}
|
|
745
772
|
}
|
|
746
773
|
}
|
|
747
774
|
|
|
748
|
-
/**
|
|
775
|
+
/**
|
|
776
|
+
* Writes `n`, 0 to 99, as two digits at `out[at + k ..)`. Every `k` it is
|
|
777
|
+
* passed is at most 23, so the test, whose 27 is `HTTP_DATE_SIZE - 2`, always
|
|
778
|
+
* holds; it is what proves `k + 1` cannot overflow.
|
|
779
|
+
*/
|
|
749
780
|
const httpFieldsPutTwo = (out: u8[], at: i32, k: i32, n: u32): void => {
|
|
781
|
+
if (k < 0 || k > 27) {
|
|
782
|
+
return
|
|
783
|
+
}
|
|
750
784
|
httpFieldsPut(out, at, k, toU32(48) + n / 10)
|
|
751
|
-
httpFieldsPut(out, at,
|
|
785
|
+
httpFieldsPut(out, at, k + 1, toU32(48) + (n % 10))
|
|
752
786
|
}
|
|
753
787
|
|
|
754
788
|
/**
|
|
@@ -774,12 +808,7 @@ const httpFieldsPutTwo = (out: u8[], at: i32, k: i32, n: u32): void => {
|
|
|
774
808
|
* or -1 when nothing was written
|
|
775
809
|
*/
|
|
776
810
|
export const httpDateWrite = (out: u8[], at: i32, seconds: i64): i32 => {
|
|
777
|
-
if (
|
|
778
|
-
seconds < 0 ||
|
|
779
|
-
seconds > HTTP_DATE_LAST_SECOND ||
|
|
780
|
-
at < 0 ||
|
|
781
|
-
toI64(at) + toI64(HTTP_DATE_SIZE) > toI64(out.length)
|
|
782
|
-
) {
|
|
811
|
+
if (seconds < 0 || seconds > HTTP_DATE_LAST_SECOND || at < 0 || at > toI32(out.length) - 29) {
|
|
783
812
|
return -1
|
|
784
813
|
}
|
|
785
814
|
// The test above keeps `seconds` far below 2^63, so every step is `u64`
|
|
@@ -819,6 +848,273 @@ export const httpDateWrite = (out: u8[], at: i32, seconds: i64): i32 => {
|
|
|
819
848
|
httpFieldsPut(out, at, 26, 71)
|
|
820
849
|
httpFieldsPut(out, at, 27, 77)
|
|
821
850
|
httpFieldsPut(out, at, 28, 84)
|
|
822
|
-
// The
|
|
823
|
-
|
|
851
|
+
// The room test above, whose 29 is `HTTP_DATE_SIZE`, keeps the end at most
|
|
852
|
+
// `out.length`; it is what proves the sum cannot overflow.
|
|
853
|
+
return at + 29
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* A media type or a disposition as `mediaTypeParse` and
|
|
858
|
+
* `formDataDispositionParse` read it: the type and subtype in lowercase, and
|
|
859
|
+
* the parameters in the order they came, each name in lowercase beside its
|
|
860
|
+
* value with any quoting removed.
|
|
861
|
+
*/
|
|
862
|
+
export class MediaType {
|
|
863
|
+
/** The type, `text` in `text/html`, or a disposition's type, in lowercase. */
|
|
864
|
+
type: string = ""
|
|
865
|
+
/** The subtype, `html` in `text/html`, in lowercase; empty for a disposition. */
|
|
866
|
+
subtype: string = ""
|
|
867
|
+
/** The parameter names, in lowercase, in the order they came. */
|
|
868
|
+
names: string[] = []
|
|
869
|
+
/** The parameter values, `values[k]` beside `names[k]`, quotes removed. */
|
|
870
|
+
values: string[] = []
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/** The most parameters a media type or disposition may carry; one more is refused. */
|
|
874
|
+
const HTTP_FIELDS_MAX_PARAMS: i32 = 16
|
|
875
|
+
|
|
876
|
+
/** Whether `c` is a `tchar` (RFC 9110 §5.6.2), in either case. */
|
|
877
|
+
const httpFieldsTchar = (c: i32): boolean => httpFieldsLowerTchar(c) || (c >= 65 && c <= 90)
|
|
878
|
+
|
|
879
|
+
/** Whether `c` is optional whitespace (RFC 9110 §5.6.3): a space or a tab. */
|
|
880
|
+
const httpFieldsOws = (c: i32): boolean => c === 32 || c === 9
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Whether `c` may stand inside a quoted string as itself (RFC 9110 §5.6.4's
|
|
884
|
+
* `qdtext`): a tab, a space, any visible character but `"` and `\`, or
|
|
885
|
+
* obs-text.
|
|
886
|
+
*/
|
|
887
|
+
const httpFieldsQdtext = (c: i32): boolean =>
|
|
888
|
+
c === 9 || c === 32 || c === 33 || (c >= 35 && c <= 91) || (c >= 93 && c <= 126) || (c >= 128 && c <= 255)
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* The octets in `bytes[from .. to)` as a string, letters in lowercase when
|
|
892
|
+
* `lower`; empty when the window is not inside `bytes`. It goes a byte at a
|
|
893
|
+
* time because `String.fromBytes` keeps a range check no proof removes; each
|
|
894
|
+
* `String.fromCharCode` is the one octet, so the string's bytes are the
|
|
895
|
+
* window's.
|
|
896
|
+
*/
|
|
897
|
+
const httpFieldsString = (bytes: readonly u8[], from: i32, to: i32, lower: boolean): string => {
|
|
898
|
+
const parts: string[] = []
|
|
899
|
+
let i: i32 = from
|
|
900
|
+
while (i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
901
|
+
const c: i32 = toI32(bytes[i])
|
|
902
|
+
parts.push(String.fromCharCode(lower && c >= 65 && c <= 90 ? c | 32 : c))
|
|
903
|
+
i = i + 1
|
|
904
|
+
}
|
|
905
|
+
return parts.join("")
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/** The end of the run of `tchar` that starts at `from`, at most `to`. */
|
|
909
|
+
const httpFieldsTokenEnd = (bytes: readonly u8[], from: i32, to: i32): i32 => {
|
|
910
|
+
const n: i32 = toI32(bytes.length)
|
|
911
|
+
let i: i32 = from
|
|
912
|
+
while (i >= 0 && i < to && i < n && httpFieldsTchar(toI32(bytes[i]))) {
|
|
913
|
+
i = i + 1
|
|
914
|
+
}
|
|
915
|
+
return i
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/** The first index from `from` that is not optional whitespace, at most `to`. */
|
|
919
|
+
const httpFieldsOwsEnd = (bytes: readonly u8[], from: i32, to: i32): i32 => {
|
|
920
|
+
const n: i32 = toI32(bytes.length)
|
|
921
|
+
let i: i32 = from
|
|
922
|
+
while (i >= 0 && i < to && i < n && httpFieldsOws(toI32(bytes[i]))) {
|
|
923
|
+
i = i + 1
|
|
924
|
+
}
|
|
925
|
+
return i
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Reads the parameters in `bytes[from .. to)` into `into`: each is optional
|
|
930
|
+
* whitespace, `;`, optional whitespace and then, unless the list ends or
|
|
931
|
+
* another `;` follows, a token name, `=` and a value that is a token or a
|
|
932
|
+
* quoted string (RFC 9110 §5.6.6). With `escapes`, a backslash inside the
|
|
933
|
+
* quotes takes the next octet as itself (§5.6.4); without, it is an octet
|
|
934
|
+
* like any other, as HTML form submission writes a name (it escapes `"` as
|
|
935
|
+
* `%22` instead). Answers false for any other shape, for a name given
|
|
936
|
+
* twice, letters compared without case, and for a 17th parameter.
|
|
937
|
+
*/
|
|
938
|
+
const httpFieldsParams = (
|
|
939
|
+
bytes: readonly u8[],
|
|
940
|
+
from: i32,
|
|
941
|
+
to: i32,
|
|
942
|
+
escapes: boolean,
|
|
943
|
+
into: MediaType
|
|
944
|
+
): boolean => {
|
|
945
|
+
let i: i32 = from
|
|
946
|
+
while (i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
947
|
+
i = httpFieldsOwsEnd(bytes, i, to)
|
|
948
|
+
if (i === to) {
|
|
949
|
+
return true
|
|
950
|
+
}
|
|
951
|
+
if (i < 0 || i >= toI32(bytes.length) || toI32(bytes[i]) !== 59) {
|
|
952
|
+
return false
|
|
953
|
+
}
|
|
954
|
+
i = httpFieldsOwsEnd(bytes, i + 1, to)
|
|
955
|
+
if (i === to) {
|
|
956
|
+
return true
|
|
957
|
+
}
|
|
958
|
+
if (i < 0 || i >= toI32(bytes.length)) {
|
|
959
|
+
return false
|
|
960
|
+
}
|
|
961
|
+
if (toI32(bytes[i]) === 59) {
|
|
962
|
+
continue
|
|
963
|
+
}
|
|
964
|
+
const nameEnd: i32 = httpFieldsTokenEnd(bytes, i, to)
|
|
965
|
+
if (
|
|
966
|
+
nameEnd === i ||
|
|
967
|
+
nameEnd < 0 ||
|
|
968
|
+
nameEnd >= to ||
|
|
969
|
+
nameEnd >= toI32(bytes.length) ||
|
|
970
|
+
toI32(bytes[nameEnd]) !== 61
|
|
971
|
+
) {
|
|
972
|
+
return false
|
|
973
|
+
}
|
|
974
|
+
// A cap on the count keeps the repeat check below from going quadratic.
|
|
975
|
+
if (toI32(into.names.length) >= HTTP_FIELDS_MAX_PARAMS) {
|
|
976
|
+
return false
|
|
977
|
+
}
|
|
978
|
+
const name: string = httpFieldsString(bytes, i, nameEnd, true)
|
|
979
|
+
for (const seen of into.names) {
|
|
980
|
+
if (seen === name) {
|
|
981
|
+
return false
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
i = nameEnd + 1
|
|
985
|
+
if (i < to && i < toI32(bytes.length) && toI32(bytes[i]) === 34) {
|
|
986
|
+
const out: string[] = []
|
|
987
|
+
i = i + 1
|
|
988
|
+
let closed: boolean = false
|
|
989
|
+
while (!closed && i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
990
|
+
const c: i32 = toI32(bytes[i])
|
|
991
|
+
if (c === 34) {
|
|
992
|
+
closed = true
|
|
993
|
+
} else if (c === 92 && escapes) {
|
|
994
|
+
// A quoted pair: the octet after the backslash is a tab, a space,
|
|
995
|
+
// a visible character or obs-text, and stands for itself.
|
|
996
|
+
i = i + 1
|
|
997
|
+
if (i >= to || i >= toI32(bytes.length)) {
|
|
998
|
+
return false
|
|
999
|
+
}
|
|
1000
|
+
const e: i32 = toI32(bytes[i])
|
|
1001
|
+
if (!(e === 9 || (e >= 32 && e <= 126) || (e >= 128 && e <= 255))) {
|
|
1002
|
+
return false
|
|
1003
|
+
}
|
|
1004
|
+
out.push(String.fromCharCode(e))
|
|
1005
|
+
} else if (httpFieldsQdtext(c) || c === 92) {
|
|
1006
|
+
out.push(String.fromCharCode(c))
|
|
1007
|
+
} else {
|
|
1008
|
+
return false
|
|
1009
|
+
}
|
|
1010
|
+
i = i + 1
|
|
1011
|
+
}
|
|
1012
|
+
if (!closed) {
|
|
1013
|
+
return false
|
|
1014
|
+
}
|
|
1015
|
+
into.names.push(name)
|
|
1016
|
+
into.values.push(out.join(""))
|
|
1017
|
+
} else {
|
|
1018
|
+
const valueEnd: i32 = httpFieldsTokenEnd(bytes, i, to)
|
|
1019
|
+
if (valueEnd === i) {
|
|
1020
|
+
return false
|
|
1021
|
+
}
|
|
1022
|
+
into.names.push(name)
|
|
1023
|
+
into.values.push(httpFieldsString(bytes, i, valueEnd, false))
|
|
1024
|
+
i = valueEnd
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
return i === to
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/**
|
|
1031
|
+
* Reads a `Content-Type` value (RFC 9110 §8.3.1): a `type/subtype` pair of
|
|
1032
|
+
* tokens, then parameters, each `; name=value` with optional whitespace
|
|
1033
|
+
* around the `;`, the value a token or a quoted string whose backslash takes
|
|
1034
|
+
* the next octet as itself. Whitespace may also open and close the value.
|
|
1035
|
+
* Type, subtype and parameter names are answered in lowercase; values as
|
|
1036
|
+
* given, less their quoting.
|
|
1037
|
+
*
|
|
1038
|
+
* const m: MediaType | null = mediaTypeParse(value, 0, toI32(value.length));
|
|
1039
|
+
* // m.type === "multipart", mediaTypeParam(m, "boundary") === "xyz"
|
|
1040
|
+
*
|
|
1041
|
+
* @param value the octets that hold the field value
|
|
1042
|
+
* @param from where the value starts in `value`
|
|
1043
|
+
* @param to where it ends, one past its last octet
|
|
1044
|
+
* @returns the media type, or null when the window is not inside `value`, a
|
|
1045
|
+
* token is empty or holds a non-`tchar`, a quoted string is not closed or
|
|
1046
|
+
* holds a control character, a parameter has no `=`, a parameter name
|
|
1047
|
+
* comes twice, or more than 16 parameters come
|
|
1048
|
+
*/
|
|
1049
|
+
export const mediaTypeParse = (value: readonly u8[], from: i32, to: i32): MediaType | null => {
|
|
1050
|
+
const n: i32 = toI32(value.length)
|
|
1051
|
+
if (from < 0 || to < from || to > n) {
|
|
1052
|
+
return null
|
|
1053
|
+
}
|
|
1054
|
+
const start: i32 = httpFieldsOwsEnd(value, from, to)
|
|
1055
|
+
const typeEnd: i32 = httpFieldsTokenEnd(value, start, to)
|
|
1056
|
+
if (typeEnd === start || typeEnd < 0 || typeEnd >= to || typeEnd >= n || toI32(value[typeEnd]) !== 47) {
|
|
1057
|
+
return null
|
|
1058
|
+
}
|
|
1059
|
+
const subStart: i32 = typeEnd + 1
|
|
1060
|
+
const subEnd: i32 = httpFieldsTokenEnd(value, subStart, to)
|
|
1061
|
+
if (subEnd === subStart) {
|
|
1062
|
+
return null
|
|
1063
|
+
}
|
|
1064
|
+
const m: MediaType = new MediaType()
|
|
1065
|
+
m.type = httpFieldsString(value, start, typeEnd, true)
|
|
1066
|
+
m.subtype = httpFieldsString(value, subStart, subEnd, true)
|
|
1067
|
+
return httpFieldsParams(value, subEnd, to, true, m) ? m : null
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Reads a part's `Content-Disposition` value the way a browser writes one in
|
|
1072
|
+
* a `multipart/form-data` body (RFC 7578 §4.2, and HTML's form submission):
|
|
1073
|
+
* a disposition type token, then parameters as `mediaTypeParse` reads them,
|
|
1074
|
+
* except that a backslash inside quotes is an octet like any other. A sender
|
|
1075
|
+
* writes a `"`, CR or LF in a name as `%22`, `%0D` or `%0A`, which are kept
|
|
1076
|
+
* as given; Windows paths keep their backslashes.
|
|
1077
|
+
*
|
|
1078
|
+
* @param value the octets that hold the field value
|
|
1079
|
+
* @param from where the value starts in `value`
|
|
1080
|
+
* @param to where it ends, one past its last octet
|
|
1081
|
+
* @returns the disposition, its type in `type` and `subtype` empty, or null
|
|
1082
|
+
* on the shapes `mediaTypeParse` refuses past the type
|
|
1083
|
+
*/
|
|
1084
|
+
export const formDataDispositionParse = (value: readonly u8[], from: i32, to: i32): MediaType | null => {
|
|
1085
|
+
const n: i32 = toI32(value.length)
|
|
1086
|
+
if (from < 0 || to < from || to > n) {
|
|
1087
|
+
return null
|
|
1088
|
+
}
|
|
1089
|
+
const start: i32 = httpFieldsOwsEnd(value, from, to)
|
|
1090
|
+
const typeEnd: i32 = httpFieldsTokenEnd(value, start, to)
|
|
1091
|
+
if (typeEnd === start) {
|
|
1092
|
+
return null
|
|
1093
|
+
}
|
|
1094
|
+
const m: MediaType = new MediaType()
|
|
1095
|
+
m.type = httpFieldsString(value, start, typeEnd, true)
|
|
1096
|
+
return httpFieldsParams(value, typeEnd, to, false, m) ? m : null
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* The value of parameter `name` of `m`, its letters compared without case.
|
|
1101
|
+
*
|
|
1102
|
+
* @param m a media type or disposition `mediaTypeParse` or
|
|
1103
|
+
* `formDataDispositionParse` answered
|
|
1104
|
+
* @param name the parameter's name, in any case
|
|
1105
|
+
* @returns its value, quotes removed, or null when `m` has no such parameter
|
|
1106
|
+
*/
|
|
1107
|
+
export const mediaTypeParam = (m: MediaType, name: string): string | null => {
|
|
1108
|
+
const raw: u8[] = httpFieldBytes(name)
|
|
1109
|
+
const key: string = httpFieldsString(raw, 0, toI32(raw.length), true)
|
|
1110
|
+
const count: i32 =
|
|
1111
|
+
toI32(m.names.length) < toI32(m.values.length) ? toI32(m.names.length) : toI32(m.values.length)
|
|
1112
|
+
let k: i32 = 0
|
|
1113
|
+
while (k >= 0 && k < count && k < toI32(m.names.length) && k < toI32(m.values.length)) {
|
|
1114
|
+
if (m.names[k] === key) {
|
|
1115
|
+
return m.values[k]
|
|
1116
|
+
}
|
|
1117
|
+
k = k + 1
|
|
1118
|
+
}
|
|
1119
|
+
return null
|
|
824
1120
|
}
|