@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/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
|
}
|