@amritk/nish-x86_64-linux 0.42.0 → 0.45.1
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/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
*
|
|
78
78
|
* Written from RFC 9112 and RFC 9110, not ported from another implementation.
|
|
79
79
|
*/
|
|
80
|
-
import {
|
|
80
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
81
81
|
import {
|
|
82
82
|
HTTP1_BODY,
|
|
83
83
|
HTTP1_CLOSED,
|
|
@@ -145,29 +145,39 @@ export const HTTP1_E_TRAILER_LIMIT: i32 = 15
|
|
|
145
145
|
export const HTTP1_E_CHUNK_LINE_LIMIT: i32 = 16
|
|
146
146
|
/** `error`: more than `HTTP1_MAX_INFORMATIONAL` interim responses. */
|
|
147
147
|
export const HTTP1_E_INFORMATIONAL_LIMIT: i32 = 17
|
|
148
|
+
/**
|
|
149
|
+
* `error`: the parser would hold more than 2^30 bytes, the most one of its
|
|
150
|
+
* arrays holds: input fed and not yet read, or a response head's kept reason
|
|
151
|
+
* phrase and fields.
|
|
152
|
+
*/
|
|
153
|
+
export const HTTP1_E_MEMORY_LIMIT: i32 = 18
|
|
148
154
|
|
|
149
155
|
// --- The writer -------------------------------------------------------------
|
|
150
156
|
|
|
151
157
|
/** How many decimal digits `n`, which is not negative, takes. */
|
|
152
158
|
const http1ClientDigits = (n: i64): i32 => {
|
|
153
|
-
|
|
159
|
+
// Counted unsigned: an i64 has at most 19 digits, so the count cannot wrap.
|
|
160
|
+
let digits: u32 = 1
|
|
154
161
|
let v: i64 = n
|
|
155
162
|
while (v >= toI64(10)) {
|
|
156
163
|
v = v / toI64(10)
|
|
157
|
-
digits
|
|
164
|
+
digits = digits + toU32(1)
|
|
158
165
|
}
|
|
159
|
-
return digits
|
|
166
|
+
return toI32(digits)
|
|
160
167
|
}
|
|
161
168
|
|
|
162
169
|
/** Writes the decimal digits of `n`, which is not negative, at `out[at ..)`, which holds them, and answers where they end. */
|
|
163
170
|
const http1ClientPutDecimal = (out: u8[], at: i32, n: i64): i32 => {
|
|
164
|
-
|
|
171
|
+
// Unsigned sums: the caller counted room for the digits at `at`, so `end`
|
|
172
|
+
// is at most `out.length`, and `end - 1` is at least `at - 1`.
|
|
173
|
+
const end: i32 = toI32(toU32(at) + toU32(http1ClientDigits(n)))
|
|
165
174
|
let v: i64 = n
|
|
166
|
-
for (let k: i32 =
|
|
167
|
-
|
|
175
|
+
for (let k: i32 = toI32(toU32(end) - toU32(1)); k >= at && k >= 0 && k < toI32(out.length); k -= 1) {
|
|
176
|
+
// `v % 10` is a digit, 0 to 9, so adding '0' cannot wrap.
|
|
177
|
+
out[k] = toU8(toU32(toI32(v % toI64(10))) + toU32(48))
|
|
168
178
|
v = v / toI64(10)
|
|
169
179
|
}
|
|
170
|
-
return
|
|
180
|
+
return end
|
|
171
181
|
}
|
|
172
182
|
|
|
173
183
|
/** Whether `c` may follow the first letter of a URI scheme (RFC 3986 §3.1). */
|
|
@@ -207,7 +217,7 @@ const http1ClientTargetOk = (method: string, target: string): boolean => {
|
|
|
207
217
|
colon = k
|
|
208
218
|
}
|
|
209
219
|
}
|
|
210
|
-
if (colon < 1 || n - colon - 1 < 1 || n - colon - 1 > 5) {
|
|
220
|
+
if (colon < 1 || colon >= n || n - colon - 1 < 1 || n - colon - 1 > 5) {
|
|
211
221
|
return false
|
|
212
222
|
}
|
|
213
223
|
for (let k: i32 = colon + 1; k < n; k += 1) {
|
|
@@ -218,6 +228,11 @@ const http1ClientTargetOk = (method: string, target: string): boolean => {
|
|
|
218
228
|
}
|
|
219
229
|
return true
|
|
220
230
|
}
|
|
231
|
+
// `n` is at least 1, but the calls above leave the prover nothing to know
|
|
232
|
+
// `target`'s length by, so it is asked again.
|
|
233
|
+
if (toI32(target.length) < 1) {
|
|
234
|
+
return false
|
|
235
|
+
}
|
|
221
236
|
const first: i32 = toI32(target.charCodeAt(0))
|
|
222
237
|
if (first === 42) {
|
|
223
238
|
return n === 1 && method === "OPTIONS"
|
|
@@ -274,17 +289,23 @@ const http1ClientTrimEnd = (value: string, a: i32, b: i32): i32 => {
|
|
|
274
289
|
const http1ClientListHas = (value: string, word: string): boolean => {
|
|
275
290
|
const n: i32 = toI32(value.length)
|
|
276
291
|
let start: i32 = 0
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
if (
|
|
292
|
+
// Each comma ends an element, and the end of the value ends the last one,
|
|
293
|
+
// which is tested after the walk so that no index steps past `n`.
|
|
294
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
295
|
+
if (http1ClientIsComma(value, k)) {
|
|
296
|
+
if (http1ClientListElementIs(value, start, k, word)) {
|
|
282
297
|
return true
|
|
283
298
|
}
|
|
284
299
|
start = k + 1
|
|
285
300
|
}
|
|
286
301
|
}
|
|
287
|
-
return
|
|
302
|
+
return http1ClientListElementIs(value, start, n, word)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Whether the list element `value[a .. b)`, trimmed of OWS, is `word` in any case. */
|
|
306
|
+
const http1ClientListElementIs = (value: string, a: i32, b: i32, word: string): boolean => {
|
|
307
|
+
const from: i32 = http1ClientTrimStart(value, a, b)
|
|
308
|
+
return http1SameWord(value, from, http1ClientTrimEnd(value, from, b), word)
|
|
288
309
|
}
|
|
289
310
|
|
|
290
311
|
/**
|
|
@@ -316,8 +337,10 @@ const http1ClientRequestNeed = (
|
|
|
316
337
|
}
|
|
317
338
|
// method SP target SP "HTTP/1.1" CRLF, the fields, the framing field and
|
|
318
339
|
// the empty line.
|
|
319
|
-
|
|
320
|
-
|
|
340
|
+
// Unsigned 64-bit sums: every term is a length below 2^31, and there are
|
|
341
|
+
// fewer than 2^31 of them, so the total stays below 2^63.
|
|
342
|
+
let need: u64 = toU64(method.length) + toU64(target.length) + toU64(14)
|
|
343
|
+
let hosts: u32 = 0
|
|
321
344
|
for (let i: i32 = 0; i < count && i < toI32(values.length); i += 1) {
|
|
322
345
|
const name: string = names[i]
|
|
323
346
|
const value: string = values[i]
|
|
@@ -341,23 +364,23 @@ const http1ClientRequestNeed = (
|
|
|
341
364
|
return toI64(-1)
|
|
342
365
|
}
|
|
343
366
|
if (http1SameWord(name, 0, nameLength, "host")) {
|
|
344
|
-
hosts
|
|
367
|
+
hosts = hosts + toU32(1)
|
|
345
368
|
}
|
|
346
|
-
need = need +
|
|
369
|
+
need = need + toU64(nameLength) + toU64(value.length) + toU64(4)
|
|
347
370
|
}
|
|
348
|
-
if (hosts !== 1) {
|
|
371
|
+
if (hosts !== toU32(1)) {
|
|
349
372
|
return toI64(-1)
|
|
350
373
|
}
|
|
351
374
|
if (bodyLength >= toI64(0)) {
|
|
352
|
-
need = need +
|
|
375
|
+
need = need + toU64(18) + toU64(http1ClientDigits(bodyLength))
|
|
353
376
|
} else if (bodyLength === toI64(-1)) {
|
|
354
|
-
need = need +
|
|
377
|
+
need = need + toU64(28)
|
|
355
378
|
}
|
|
356
379
|
if (upgrading) {
|
|
357
380
|
// "Upgrade: " value CRLF "Connection: Upgrade" CRLF
|
|
358
|
-
need = need +
|
|
381
|
+
need = need + toU64(upgrade.length) + toU64(32)
|
|
359
382
|
}
|
|
360
|
-
return need
|
|
383
|
+
return toI64(need)
|
|
361
384
|
}
|
|
362
385
|
|
|
363
386
|
/** Writes field `i` at `out[at ..)`, which holds it, and answers where it ends. */
|
|
@@ -523,11 +546,23 @@ const http1ClientHeadArray = (
|
|
|
523
546
|
if (need < toI64(0) || need > toI64(1073741824)) {
|
|
524
547
|
return null
|
|
525
548
|
}
|
|
526
|
-
const out: u8[] =
|
|
549
|
+
const out: u8[] = http1ClientZeros(toI32(need))
|
|
527
550
|
http1ClientPutHead(out, 0, method, target, names, values, bodyLength, upgrade)
|
|
528
551
|
return out
|
|
529
552
|
}
|
|
530
553
|
|
|
554
|
+
/**
|
|
555
|
+
* `n` zero bytes, appended one at a time: the prover reads only a constant
|
|
556
|
+
* length of `new Array` as in range, and these lengths are computed.
|
|
557
|
+
*/
|
|
558
|
+
const http1ClientZeros = (n: i32): u8[] => {
|
|
559
|
+
const out: u8[] = []
|
|
560
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
561
|
+
out.push(toU8(0))
|
|
562
|
+
}
|
|
563
|
+
return out
|
|
564
|
+
}
|
|
565
|
+
|
|
531
566
|
// --- The parser -------------------------------------------------------------
|
|
532
567
|
|
|
533
568
|
// The parser's states, one per kind of thing it is waiting for.
|
|
@@ -766,7 +801,9 @@ export class Http1ResponseParser {
|
|
|
766
801
|
/**
|
|
767
802
|
* Appends `data[off .. off + len)` to the input. A window outside `data`
|
|
768
803
|
* panics. After `finish()`, or once the parser has closed or refused, the
|
|
769
|
-
* input is dropped; in a tunnel it is kept for `tunnelBytes()`.
|
|
804
|
+
* input is dropped; in a tunnel it is kept for `tunnelBytes()`. Input that
|
|
805
|
+
* would leave more than 2^30 bytes fed and not read is refused with
|
|
806
|
+
* `HTTP1_E_MEMORY_LIMIT`.
|
|
770
807
|
* @param data The bytes read from the connection.
|
|
771
808
|
* @param off Where the new bytes start in `data`.
|
|
772
809
|
* @param len How many bytes to append.
|
|
@@ -779,10 +816,12 @@ export class Http1ResponseParser {
|
|
|
779
816
|
if (this.closed || this.state === HTTP1_CLIENT_S_CLOSED || this.state === HTTP1_CLIENT_S_ERROR) {
|
|
780
817
|
return
|
|
781
818
|
}
|
|
782
|
-
http1ClientReserve(this, len)
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
819
|
+
if (!http1ClientReserve(this, len) || !tryCopyBytes(this.buf, this.end, data, off, len)) {
|
|
820
|
+
http1ClientFail(this, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes fed and not read")
|
|
821
|
+
return
|
|
822
|
+
}
|
|
823
|
+
// Unsigned: the copy above fit `len` bytes after `end` inside `buf`.
|
|
824
|
+
this.end = toI32(toU32(this.end) + toU32(len))
|
|
786
825
|
}
|
|
787
826
|
|
|
788
827
|
/**
|
|
@@ -830,10 +869,16 @@ export class Http1ResponseParser {
|
|
|
830
869
|
* @returns The field's value, OWS trimmed, or `null` when the response has no such field.
|
|
831
870
|
*/
|
|
832
871
|
header(name: string): string | null {
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
872
|
+
const spans: i32[] = this.spans
|
|
873
|
+
// `j` is `i * 4`, stepped beside `i` and asked against `spans` each pass.
|
|
874
|
+
let j: i32 = 0
|
|
875
|
+
for (let i: i32 = 0; i < this.spanCount && j >= 0 && j <= toI32(spans.length) - 4; i += 1) {
|
|
876
|
+
const valueStart: i32 = spans[j + 2]
|
|
877
|
+
const valueEnd: i32 = spans[j + 3]
|
|
878
|
+
if (http1ClientHeadSameWord(this.head, spans[j], spans[j + 1], name)) {
|
|
879
|
+
return http1Text(this.head, valueStart, valueEnd, false)
|
|
836
880
|
}
|
|
881
|
+
j += 4
|
|
837
882
|
}
|
|
838
883
|
return null
|
|
839
884
|
}
|
|
@@ -852,10 +897,11 @@ export class Http1ResponseParser {
|
|
|
852
897
|
* @returns The name, or "" past the last field.
|
|
853
898
|
*/
|
|
854
899
|
fieldName(i: i32): string {
|
|
855
|
-
|
|
900
|
+
const j: i32 = http1ClientSpanAt(this, i)
|
|
901
|
+
if (j < 0 || j > toI32(this.spans.length) - 4) {
|
|
856
902
|
return ""
|
|
857
903
|
}
|
|
858
|
-
return http1Text(this.head, this.spans[
|
|
904
|
+
return http1Text(this.head, this.spans[j], this.spans[j + 1], false)
|
|
859
905
|
}
|
|
860
906
|
|
|
861
907
|
/**
|
|
@@ -864,10 +910,11 @@ export class Http1ResponseParser {
|
|
|
864
910
|
* @returns The value, or "" past the last field.
|
|
865
911
|
*/
|
|
866
912
|
fieldValue(i: i32): string {
|
|
867
|
-
|
|
913
|
+
const j: i32 = http1ClientSpanAt(this, i)
|
|
914
|
+
if (j < 0 || j > toI32(this.spans.length) - 4) {
|
|
868
915
|
return ""
|
|
869
916
|
}
|
|
870
|
-
return http1Text(this.head, this.spans[
|
|
917
|
+
return http1Text(this.head, this.spans[j + 2], this.spans[j + 3], false)
|
|
871
918
|
}
|
|
872
919
|
|
|
873
920
|
/**
|
|
@@ -886,7 +933,8 @@ export class Http1ResponseParser {
|
|
|
886
933
|
* @returns The count of unread input bytes.
|
|
887
934
|
*/
|
|
888
935
|
buffered(): i32 {
|
|
889
|
-
|
|
936
|
+
// Unsigned: the parser keeps `start <= end`, so the difference cannot wrap.
|
|
937
|
+
return toI32(toU32(this.end) - toU32(this.start))
|
|
890
938
|
}
|
|
891
939
|
|
|
892
940
|
/**
|
|
@@ -894,44 +942,74 @@ export class Http1ResponseParser {
|
|
|
894
942
|
* @returns The bytes, or an empty array outside a tunnel.
|
|
895
943
|
*/
|
|
896
944
|
tunnelBytes(): u8[] {
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
945
|
+
const from: i32 = this.start
|
|
946
|
+
const to: i32 = this.end
|
|
947
|
+
// The parser keeps `0 <= start <= end <= buf.length`, so this is every unread byte.
|
|
948
|
+
if (this.state !== HTTP1_CLIENT_S_TUNNEL || from < 0 || to <= from || to > toI32(this.buf.length)) {
|
|
949
|
+
return []
|
|
900
950
|
}
|
|
901
|
-
const
|
|
902
|
-
|
|
903
|
-
|
|
951
|
+
const buf: u8[] = this.buf
|
|
952
|
+
const out: u8[] = []
|
|
953
|
+
for (let k: i32 = from; k >= 0 && k < to && k < toI32(buf.length); k += 1) {
|
|
954
|
+
out.push(buf[k])
|
|
904
955
|
}
|
|
905
956
|
return out
|
|
906
957
|
}
|
|
907
958
|
}
|
|
908
959
|
|
|
909
|
-
/**
|
|
910
|
-
|
|
960
|
+
/**
|
|
961
|
+
* Makes room for `len` more bytes at `p.end`, moving the live bytes down
|
|
962
|
+
* first. Answers false when that would take an array past 2^30 bytes.
|
|
963
|
+
*/
|
|
964
|
+
const http1ClientReserve = (p: Http1ResponseParser, len: i32): boolean => {
|
|
911
965
|
const old: u8[] = p.buf
|
|
912
966
|
const capacity: i32 = toI32(old.length)
|
|
913
|
-
|
|
914
|
-
|
|
967
|
+
const from: i32 = p.start
|
|
968
|
+
const end: i32 = p.end
|
|
969
|
+
// The parser keeps `0 <= start <= end <= buf.length`; asked here so the
|
|
970
|
+
// differences below are known not to wrap.
|
|
971
|
+
if (len < 0 || from < 0 || end < from || end > capacity) {
|
|
972
|
+
return false
|
|
973
|
+
}
|
|
974
|
+
// Unsigned, as are the sizes below: `from <= end <= capacity` was just
|
|
975
|
+
// asked, and `size` starts at `capacity` or more, so no difference wraps.
|
|
976
|
+
if (toU32(len) <= toU32(capacity) - toU32(end)) {
|
|
977
|
+
return true
|
|
915
978
|
}
|
|
916
|
-
const live:
|
|
917
|
-
let size:
|
|
918
|
-
while (size - live < len) {
|
|
919
|
-
if (size > 1073741823) {
|
|
920
|
-
|
|
979
|
+
const live: u32 = toU32(end) - toU32(from)
|
|
980
|
+
let size: u32 = toU32(capacity < HTTP1_FIRST_INPUT ? HTTP1_FIRST_INPUT : capacity)
|
|
981
|
+
while (size - live < toU32(len)) {
|
|
982
|
+
if (size > toU32(1073741823)) {
|
|
983
|
+
return false
|
|
921
984
|
}
|
|
922
|
-
size = size * 2
|
|
985
|
+
size = size * toU32(2)
|
|
923
986
|
}
|
|
924
987
|
// A fresh array when it has to grow, and the same one otherwise: either way
|
|
925
988
|
// the live bytes move to the front, and every index into them moves too.
|
|
926
|
-
const buf: u8[] = size === capacity ? old :
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
copyBytes(buf, 0, old, from, live)
|
|
989
|
+
const buf: u8[] = size === toU32(capacity) ? old : http1ClientZeros(toI32(size))
|
|
990
|
+
if (live > toU32(0) && !tryCopyBytes(buf, 0, old, from, toI32(live))) {
|
|
991
|
+
return false
|
|
930
992
|
}
|
|
931
993
|
p.buf = buf
|
|
932
|
-
|
|
994
|
+
// Unsigned: `scan` and `start` move down together, and `scan` is past `start`
|
|
995
|
+
// whenever it is read.
|
|
996
|
+
p.scan = toI32(toU32(p.scan) - toU32(from))
|
|
933
997
|
p.start = 0
|
|
934
|
-
p.end = live
|
|
998
|
+
p.end = toI32(live)
|
|
999
|
+
return true
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* Where field `i`'s four offsets start in `p.spans`, or -1 when `i` is not
|
|
1004
|
+
* below `fields()`. Each field takes 4 slots, and `spans` holds
|
|
1005
|
+
* `spanCount * 4` of them.
|
|
1006
|
+
*/
|
|
1007
|
+
const http1ClientSpanAt = (p: Http1ResponseParser, i: i32): i32 => {
|
|
1008
|
+
// `i` below 2^29 keeps `i * 4` inside an `i32`; `spans` cannot hold more.
|
|
1009
|
+
if (i < 0 || i >= p.spanCount || i > 536870911) {
|
|
1010
|
+
return -1
|
|
1011
|
+
}
|
|
1012
|
+
return i * 4
|
|
935
1013
|
}
|
|
936
1014
|
|
|
937
1015
|
/**
|
|
@@ -981,13 +1059,19 @@ const http1ClientStartHead = (p: Http1ResponseParser): void => {
|
|
|
981
1059
|
const http1ClientTakeLine = (p: Http1ResponseParser, limit: i32, overCode: i32, overText: string): i32 => {
|
|
982
1060
|
const buf: u8[] = p.buf
|
|
983
1061
|
const end: i32 = p.end
|
|
984
|
-
|
|
1062
|
+
const start: i32 = p.start
|
|
1063
|
+
// In 64 bits, where a difference of two `i32`s cannot wrap.
|
|
1064
|
+
const most: i64 = toI64(limit) + toI64(1)
|
|
1065
|
+
let i: i32 = p.scan > start ? p.scan : start
|
|
985
1066
|
while (i >= 0 && i < end && i < toI32(buf.length)) {
|
|
986
1067
|
if (buf[i] === 10) {
|
|
987
|
-
|
|
1068
|
+
// `i` is at least `start`, so `i < 1` is `i === start` at 0. Read
|
|
1069
|
+
// before the call below, which drops what is known of `buf`.
|
|
1070
|
+
const bare: boolean = i < 1 || i === start || buf[i - 1] !== 13
|
|
1071
|
+
if (toI64(i) - toI64(start) > most) {
|
|
988
1072
|
return http1ClientFail(p, overCode, overText)
|
|
989
1073
|
}
|
|
990
|
-
if (
|
|
1074
|
+
if (bare) {
|
|
991
1075
|
return http1ClientFail(p, HTTP1_E_BARE_LF, "a line ended in a bare LF")
|
|
992
1076
|
}
|
|
993
1077
|
p.lineStart = p.start
|
|
@@ -999,15 +1083,14 @@ const http1ClientTakeLine = (p: Http1ResponseParser, limit: i32, overCode: i32,
|
|
|
999
1083
|
i += 1
|
|
1000
1084
|
}
|
|
1001
1085
|
p.scan = end
|
|
1002
|
-
if (end -
|
|
1086
|
+
if (toI64(end) - toI64(start) > most) {
|
|
1003
1087
|
return http1ClientFail(p, overCode, overText)
|
|
1004
1088
|
}
|
|
1005
1089
|
return HTTP1_NEED_MORE
|
|
1006
1090
|
}
|
|
1007
1091
|
|
|
1008
|
-
/** Whether `
|
|
1009
|
-
const
|
|
1010
|
-
i >= 0 && i < toI32(buf.length) && buf[i] >= 48 && buf[i] <= 57
|
|
1092
|
+
/** Whether byte `c` is an ASCII digit. */
|
|
1093
|
+
const http1ClientIsDigit = (c: i32): boolean => c >= 48 && c <= 57
|
|
1011
1094
|
|
|
1012
1095
|
/**
|
|
1013
1096
|
* Reads the status line in `lineStart .. lineEnd` (RFC 9112 §4):
|
|
@@ -1018,45 +1101,68 @@ const http1ClientStatusLine = (p: Http1ResponseParser): i32 => {
|
|
|
1018
1101
|
const buf: u8[] = p.buf
|
|
1019
1102
|
const a: i32 = p.lineStart
|
|
1020
1103
|
const b: i32 = p.lineEnd < toI32(buf.length) ? p.lineEnd : toI32(buf.length)
|
|
1104
|
+
// In 64 bits, where a difference of two `i32`s cannot wrap.
|
|
1105
|
+
const width: i64 = toI64(b) - toI64(a)
|
|
1106
|
+
// `b` is at most `buf.length`, so a line of 12 bytes or more has
|
|
1107
|
+
// `a <= buf.length - 12`; asked so the reads below are known in range.
|
|
1108
|
+
if (width < toI64(12) || a < 0 || a > toI32(buf.length) - 12) {
|
|
1109
|
+
return http1ClientFail(p, HTTP1_E_STATUS_LINE, "a malformed status line")
|
|
1110
|
+
}
|
|
1111
|
+
// Every byte is read before any call, which would drop what is known of `buf`.
|
|
1112
|
+
const major: i32 = toI32(buf[a + 5])
|
|
1113
|
+
const minor: i32 = toI32(buf[a + 7])
|
|
1114
|
+
const hundreds: i32 = toI32(buf[a + 9])
|
|
1115
|
+
const tens: i32 = toI32(buf[a + 10])
|
|
1116
|
+
const ones: i32 = toI32(buf[a + 11])
|
|
1117
|
+
// Past 12 bytes, the space before the reason; `a <= buf.length - 13` then
|
|
1118
|
+
// holds as well, and is asked so the read is known in range.
|
|
1119
|
+
let reasonFrom: i32 = b
|
|
1120
|
+
if (width > toI64(12)) {
|
|
1121
|
+
if (a > toI32(buf.length) - 13 || buf[a + 12] !== 32) {
|
|
1122
|
+
return http1ClientFail(p, HTTP1_E_STATUS_LINE, "a malformed status line")
|
|
1123
|
+
}
|
|
1124
|
+
reasonFrom = a + 13
|
|
1125
|
+
}
|
|
1021
1126
|
if (
|
|
1022
|
-
b - a < 12 ||
|
|
1023
|
-
a < 0 ||
|
|
1024
1127
|
buf[a] !== 72 ||
|
|
1025
1128
|
buf[a + 1] !== 84 ||
|
|
1026
1129
|
buf[a + 2] !== 84 ||
|
|
1027
1130
|
buf[a + 3] !== 80 ||
|
|
1028
1131
|
buf[a + 4] !== 47 ||
|
|
1029
|
-
!http1ClientDigitAt(buf, a + 5) ||
|
|
1030
1132
|
buf[a + 6] !== 46 ||
|
|
1031
|
-
!http1ClientDigitAt(buf, a + 7) ||
|
|
1032
1133
|
buf[a + 8] !== 32 ||
|
|
1033
|
-
!
|
|
1034
|
-
!
|
|
1035
|
-
!
|
|
1036
|
-
(
|
|
1134
|
+
!http1ClientIsDigit(major) ||
|
|
1135
|
+
!http1ClientIsDigit(minor) ||
|
|
1136
|
+
!http1ClientIsDigit(hundreds) ||
|
|
1137
|
+
!http1ClientIsDigit(tens) ||
|
|
1138
|
+
!http1ClientIsDigit(ones)
|
|
1037
1139
|
) {
|
|
1038
1140
|
return http1ClientFail(p, HTTP1_E_STATUS_LINE, "a malformed status line")
|
|
1039
1141
|
}
|
|
1040
|
-
if (
|
|
1142
|
+
if (major !== 49) {
|
|
1041
1143
|
return http1ClientFail(p, HTTP1_E_VERSION, "an HTTP major version other than 1")
|
|
1042
1144
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1145
|
+
// Each is an ASCII digit, so its low four bits are its value.
|
|
1146
|
+
const status: i32 = (hundreds & 15) * 100 + (tens & 15) * 10 + (ones & 15)
|
|
1045
1147
|
if (status < 100 || status > 599) {
|
|
1046
1148
|
return http1ClientFail(p, HTTP1_E_STATUS_LINE, "a status code outside 100 to 599")
|
|
1047
1149
|
}
|
|
1048
|
-
for (let k: i32 =
|
|
1150
|
+
for (let k: i32 = reasonFrom; k >= 0 && k < b && k < toI32(buf.length); k += 1) {
|
|
1049
1151
|
if (!http1IsValueByte(toI32(buf[k]))) {
|
|
1050
1152
|
return http1ClientFail(p, HTTP1_E_STATUS_LINE, "a control character in the reason phrase")
|
|
1051
1153
|
}
|
|
1052
1154
|
}
|
|
1053
|
-
p.minor =
|
|
1155
|
+
p.minor = minor === 48 ? 0 : 1
|
|
1054
1156
|
p.status = status
|
|
1055
|
-
if (
|
|
1056
|
-
|
|
1157
|
+
if (reasonFrom < b) {
|
|
1158
|
+
const reason: i32 = http1ClientKeep(p, buf, reasonFrom, b, false)
|
|
1159
|
+
if (reason < 0) {
|
|
1160
|
+
return http1ClientFail(p, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes of head kept")
|
|
1161
|
+
}
|
|
1162
|
+
p.reasonStart = reason
|
|
1057
1163
|
p.reasonEnd = p.headUsed
|
|
1058
1164
|
if (p.keepStrings) {
|
|
1059
|
-
p.reason = http1Text(buf,
|
|
1165
|
+
p.reason = http1Text(buf, reasonFrom, b, false)
|
|
1060
1166
|
}
|
|
1061
1167
|
}
|
|
1062
1168
|
p.state = HTTP1_CLIENT_S_FIELDS
|
|
@@ -1072,14 +1178,15 @@ const http1ClientFieldLine = (p: Http1ResponseParser, keep: boolean): i32 => {
|
|
|
1072
1178
|
const buf: u8[] = p.buf
|
|
1073
1179
|
const a: i32 = p.lineStart
|
|
1074
1180
|
const b: i32 = p.lineEnd < toI32(buf.length) ? p.lineEnd : toI32(buf.length)
|
|
1075
|
-
if (a >= 0 && a < b && http1IsOws(toI32(buf[a]))) {
|
|
1181
|
+
if (a >= 0 && a < b && a < toI32(buf.length) && http1IsOws(toI32(buf[a]))) {
|
|
1076
1182
|
return http1ClientFail(p, HTTP1_E_FIELD, "a field line that starts with whitespace (obs-fold)")
|
|
1077
1183
|
}
|
|
1078
1184
|
let i: i32 = a
|
|
1079
1185
|
while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1080
1186
|
i += 1
|
|
1081
1187
|
}
|
|
1082
|
-
|
|
1188
|
+
// `b` is at most `buf.length`; the bounds are asked so the read is known in range.
|
|
1189
|
+
if (i === a || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 58) {
|
|
1083
1190
|
return http1ClientFail(p, HTTP1_E_FIELD, "a malformed field name")
|
|
1084
1191
|
}
|
|
1085
1192
|
const nameEnd: i32 = i
|
|
@@ -1088,7 +1195,8 @@ const http1ClientFieldLine = (p: Http1ResponseParser, keep: boolean): i32 => {
|
|
|
1088
1195
|
return http1ClientFail(p, HTTP1_E_FIELD, "a control character in a field value")
|
|
1089
1196
|
}
|
|
1090
1197
|
}
|
|
1091
|
-
|
|
1198
|
+
// Counting stops at the limit (at most 2^31 - 1), so the sum does not wrap.
|
|
1199
|
+
p.fieldCount = toI32(toU32(p.fieldCount) + toU32(1))
|
|
1092
1200
|
if (p.fieldCount > p.maxHeaders) {
|
|
1093
1201
|
return http1ClientFail(p, HTTP1_E_COUNT_LIMIT, "more fields than the limit")
|
|
1094
1202
|
}
|
|
@@ -1106,14 +1214,18 @@ const http1ClientFieldLine = (p: Http1ResponseParser, keep: boolean): i32 => {
|
|
|
1106
1214
|
}
|
|
1107
1215
|
while (
|
|
1108
1216
|
valueEnd > valueStart &&
|
|
1109
|
-
valueEnd
|
|
1217
|
+
valueEnd >= 1 &&
|
|
1218
|
+
valueEnd <= toI32(buf.length) &&
|
|
1110
1219
|
http1IsOws(toI32(buf[valueEnd - 1]))
|
|
1111
1220
|
) {
|
|
1112
1221
|
valueEnd -= 1
|
|
1113
1222
|
}
|
|
1114
1223
|
const name: i32 = http1ClientKeep(p, buf, a, nameEnd, true)
|
|
1115
|
-
const value: i32 = http1ClientKeep(p, buf, valueStart, valueEnd, false)
|
|
1116
|
-
|
|
1224
|
+
const value: i32 = name < 0 ? -1 : http1ClientKeep(p, buf, valueStart, valueEnd, false)
|
|
1225
|
+
if (value < 0) {
|
|
1226
|
+
return http1ClientFail(p, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes of head kept")
|
|
1227
|
+
}
|
|
1228
|
+
http1ClientSpan(p, name, value, p.headUsed)
|
|
1117
1229
|
if (p.keepStrings) {
|
|
1118
1230
|
p.names.push(http1Text(buf, a, nameEnd, true))
|
|
1119
1231
|
p.values.push(http1Text(buf, valueStart, valueEnd, false))
|
|
@@ -1135,8 +1247,11 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
|
|
|
1135
1247
|
const n: i32 = to < toI32(head.length) ? to : toI32(head.length)
|
|
1136
1248
|
let found: i64 = seen
|
|
1137
1249
|
let start: i32 = from
|
|
1138
|
-
|
|
1139
|
-
|
|
1250
|
+
// The list's end ends its last element, as a comma does; the walk stops
|
|
1251
|
+
// there, before any step past `n`.
|
|
1252
|
+
let k: i32 = from
|
|
1253
|
+
while (k <= n) {
|
|
1254
|
+
if (k >= n || (k >= 0 && k < toI32(head.length) && head[k] === 44)) {
|
|
1140
1255
|
const a: i32 = http1ClientHeadTrimStart(head, start, k)
|
|
1141
1256
|
const b: i32 = http1ClientHeadTrimEnd(head, a, k)
|
|
1142
1257
|
if (a === b) {
|
|
@@ -1149,7 +1264,8 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
|
|
|
1149
1264
|
return toI64(-1)
|
|
1150
1265
|
}
|
|
1151
1266
|
if (v <= (HTTP1_CLIENT_MAX_BODY - toI64(c - 48)) / toI64(10)) {
|
|
1152
|
-
|
|
1267
|
+
// Unsigned: `v` starts at 0 and the test keeps it at most 2^53.
|
|
1268
|
+
v = toI64(toU64(v) * toU64(10) + toU64(c - 48))
|
|
1153
1269
|
} else {
|
|
1154
1270
|
v = HTTP1_CLIENT_MAX_BODY + toI64(1)
|
|
1155
1271
|
}
|
|
@@ -1158,8 +1274,15 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
|
|
|
1158
1274
|
return toI64(-2)
|
|
1159
1275
|
}
|
|
1160
1276
|
found = v
|
|
1277
|
+
if (k >= n) {
|
|
1278
|
+
break
|
|
1279
|
+
}
|
|
1161
1280
|
start = k + 1
|
|
1162
1281
|
}
|
|
1282
|
+
if (k >= n) {
|
|
1283
|
+
break
|
|
1284
|
+
}
|
|
1285
|
+
k += 1
|
|
1163
1286
|
}
|
|
1164
1287
|
return found
|
|
1165
1288
|
}
|
|
@@ -1167,111 +1290,160 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
|
|
|
1167
1290
|
/**
|
|
1168
1291
|
* The fields of `name` together as one comma-separated list (RFC 9110 §5.3),
|
|
1169
1292
|
* tallied for `word`: answers the element count times 4, plus 2 when `word`
|
|
1170
|
-
* appears more than once and 1 when it is the last element.
|
|
1293
|
+
* appears more than once and 1 when it is the last element. Both counts stop
|
|
1294
|
+
* at 2, since the callers only ask whether each is more than 1.
|
|
1171
1295
|
*/
|
|
1172
1296
|
const http1ClientTally = (p: Http1ResponseParser, name: string, word: string): i32 => {
|
|
1173
1297
|
let elements: i32 = 0
|
|
1174
1298
|
let matches: i32 = 0
|
|
1175
1299
|
let last: boolean = false
|
|
1176
1300
|
const head: u8[] = p.head
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1301
|
+
const spans: i32[] = p.spans
|
|
1302
|
+
// `j` is `i * 4`, stepped beside `i` and asked against `spans` each pass.
|
|
1303
|
+
let j: i32 = 0
|
|
1304
|
+
for (let i: i32 = 0; i < p.spanCount && j >= 0 && j <= toI32(spans.length) - 4; i += 1) {
|
|
1305
|
+
const from: i32 = spans[j + 2]
|
|
1306
|
+
const to: i32 = spans[j + 3]
|
|
1307
|
+
if (http1ClientHeadIs(head, spans[j], spans[j + 1], name)) {
|
|
1308
|
+
const n: i32 = to < toI32(head.length) ? to : toI32(head.length)
|
|
1181
1309
|
let start: i32 = from
|
|
1182
|
-
|
|
1183
|
-
|
|
1310
|
+
// The list's end ends its last element, as a comma does; the walk
|
|
1311
|
+
// stops there, before any step past `n`.
|
|
1312
|
+
let k: i32 = from
|
|
1313
|
+
while (k <= n) {
|
|
1314
|
+
if (k >= n || (k >= 0 && k < toI32(head.length) && head[k] === 44)) {
|
|
1184
1315
|
const a: i32 = http1ClientHeadTrimStart(head, start, k)
|
|
1185
1316
|
const b: i32 = http1ClientHeadTrimEnd(head, a, k)
|
|
1186
1317
|
if (b > a) {
|
|
1187
|
-
elements
|
|
1318
|
+
if (elements < 2) {
|
|
1319
|
+
elements += 1
|
|
1320
|
+
}
|
|
1188
1321
|
last = http1ClientHeadSameWord(head, a, b, word)
|
|
1189
|
-
if (last) {
|
|
1322
|
+
if (last && matches < 2) {
|
|
1190
1323
|
matches += 1
|
|
1191
1324
|
}
|
|
1192
1325
|
}
|
|
1326
|
+
if (k >= n) {
|
|
1327
|
+
break
|
|
1328
|
+
}
|
|
1193
1329
|
start = k + 1
|
|
1194
1330
|
}
|
|
1331
|
+
if (k >= n) {
|
|
1332
|
+
break
|
|
1333
|
+
}
|
|
1334
|
+
k += 1
|
|
1195
1335
|
}
|
|
1196
1336
|
}
|
|
1337
|
+
j += 4
|
|
1197
1338
|
}
|
|
1198
|
-
|
|
1339
|
+
// `elements` stops at 2, so the mask keeps it as it is.
|
|
1340
|
+
const repeated: i32 = matches > 1 ? 2 : 0
|
|
1341
|
+
const ends: i32 = last ? 1 : 0
|
|
1342
|
+
return ((elements & 3) << 2) | repeated | ends
|
|
1199
1343
|
}
|
|
1200
1344
|
|
|
1201
1345
|
/** Whether a `Connection` field of the response lists `word`. */
|
|
1202
1346
|
const http1ClientConnectionHas = (p: Http1ResponseParser, word: string): boolean => {
|
|
1203
|
-
|
|
1347
|
+
const spans: i32[] = p.spans
|
|
1348
|
+
// `j` is `i * 4`, stepped beside `i` and asked against `spans` each pass.
|
|
1349
|
+
let j: i32 = 0
|
|
1350
|
+
for (let i: i32 = 0; i < p.spanCount && j >= 0 && j <= toI32(spans.length) - 4; i += 1) {
|
|
1351
|
+
const from: i32 = spans[j + 2]
|
|
1352
|
+
const to: i32 = spans[j + 3]
|
|
1204
1353
|
if (
|
|
1205
|
-
http1ClientHeadIs(p.head,
|
|
1206
|
-
http1ClientHeadListHas(p.head,
|
|
1354
|
+
http1ClientHeadIs(p.head, spans[j], spans[j + 1], "connection") &&
|
|
1355
|
+
http1ClientHeadListHas(p.head, from, to, word)
|
|
1207
1356
|
) {
|
|
1208
1357
|
return true
|
|
1209
1358
|
}
|
|
1359
|
+
j += 4
|
|
1210
1360
|
}
|
|
1211
1361
|
return false
|
|
1212
1362
|
}
|
|
1213
1363
|
|
|
1214
1364
|
/**
|
|
1215
1365
|
* Copies `buf[a .. b)` to the end of `p.head`, lowercased when `lower`, and
|
|
1216
|
-
* answers where the copy starts
|
|
1217
|
-
*
|
|
1366
|
+
* answers where the copy starts, or -1 when `head` would pass 2^30 bytes.
|
|
1367
|
+
* `head` grows by doubling and is kept, so a parser reused for many
|
|
1368
|
+
* responses stops growing at the largest head.
|
|
1218
1369
|
*/
|
|
1219
1370
|
const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lower: boolean): i32 => {
|
|
1220
|
-
const n: i32 = b - a
|
|
1221
1371
|
const at: i32 = p.headUsed
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1372
|
+
const used: i32 = toI32(p.head.length)
|
|
1373
|
+
// The callers pass a window inside `buf`, and `headUsed` stays inside
|
|
1374
|
+
// `head`; asked here so the differences below are known not to wrap.
|
|
1375
|
+
if (a < 0 || b < a || b > toI32(buf.length) || at < 0 || at > used) {
|
|
1376
|
+
return -1
|
|
1377
|
+
}
|
|
1378
|
+
// Unsigned, as are the sizes below: `a <= b` and `at <= used` were just
|
|
1379
|
+
// asked, and `size` starts at `used` or more, so no difference wraps.
|
|
1380
|
+
const n: i32 = toI32(toU32(b) - toU32(a))
|
|
1381
|
+
if (toU32(n) > toU32(used) - toU32(at)) {
|
|
1382
|
+
let size: u32 = used < 256 ? toU32(256) : toU32(used)
|
|
1383
|
+
while (size - toU32(at) < toU32(n)) {
|
|
1384
|
+
if (size > toU32(1073741823)) {
|
|
1385
|
+
return -1
|
|
1227
1386
|
}
|
|
1228
|
-
size = size * 2
|
|
1387
|
+
size = size * toU32(2)
|
|
1388
|
+
}
|
|
1389
|
+
const grown: u8[] = http1ClientZeros(toI32(size))
|
|
1390
|
+
if (!tryCopyBytes(grown, 0, p.head, 0, at)) {
|
|
1391
|
+
return -1
|
|
1229
1392
|
}
|
|
1230
|
-
const grown: u8[] = new Array<u8>(size)
|
|
1231
|
-
copyBytes(grown, 0, p.head, 0, at)
|
|
1232
1393
|
p.head = grown
|
|
1233
1394
|
}
|
|
1234
|
-
|
|
1395
|
+
if (!tryCopyBytes(p.head, at, buf, a, n)) {
|
|
1396
|
+
return -1
|
|
1397
|
+
}
|
|
1398
|
+
// Unsigned: the copy above fit `n` bytes after `at` inside `head`.
|
|
1399
|
+
const stop: i32 = toI32(toU32(at) + toU32(n))
|
|
1235
1400
|
if (lower) {
|
|
1236
1401
|
const head: u8[] = p.head
|
|
1237
|
-
for (let k: i32 = at; k >= 0 && k <
|
|
1402
|
+
for (let k: i32 = at; k >= 0 && k < stop && k < toI32(head.length); k += 1) {
|
|
1238
1403
|
const c: i32 = toI32(head[k])
|
|
1239
1404
|
if (c >= 65 && c <= 90) {
|
|
1240
1405
|
head[k] = toU8(c + 32)
|
|
1241
1406
|
}
|
|
1242
1407
|
}
|
|
1243
1408
|
}
|
|
1244
|
-
p.headUsed =
|
|
1409
|
+
p.headUsed = stop
|
|
1245
1410
|
return at
|
|
1246
1411
|
}
|
|
1247
1412
|
|
|
1248
1413
|
/** Records the field whose name starts at `name` and whose value is `head[value .. valueEnd)`. */
|
|
1249
1414
|
const http1ClientSpan = (p: Http1ResponseParser, name: i32, value: i32, valueEnd: i32): void => {
|
|
1250
|
-
const
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1415
|
+
const spans: i32[] = p.spans
|
|
1416
|
+
// Unsigned: `spans` holds 4 entries a field, so `spanCount * 4` fits an
|
|
1417
|
+
// `i32` length and neither the product nor the count wraps.
|
|
1418
|
+
const at: i32 = toI32(toU32(p.spanCount) * toU32(4))
|
|
1419
|
+
if (at >= 0 && at <= toI32(spans.length) - 4) {
|
|
1420
|
+
spans[at] = name
|
|
1421
|
+
spans[at + 1] = value
|
|
1422
|
+
spans[at + 2] = value
|
|
1423
|
+
spans[at + 3] = valueEnd
|
|
1256
1424
|
} else {
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1425
|
+
spans.push(name)
|
|
1426
|
+
spans.push(value)
|
|
1427
|
+
spans.push(value)
|
|
1428
|
+
spans.push(valueEnd)
|
|
1261
1429
|
}
|
|
1262
|
-
p.spanCount
|
|
1430
|
+
p.spanCount = toI32(toU32(p.spanCount) + toU32(1))
|
|
1263
1431
|
}
|
|
1264
1432
|
|
|
1265
1433
|
/** Whether `head[a .. b)` is `word`, byte for byte. */
|
|
1266
1434
|
const http1ClientHeadIs = (head: u8[], a: i32, b: i32, word: string): boolean => {
|
|
1267
1435
|
const n: i32 = toI32(word.length)
|
|
1268
|
-
|
|
1436
|
+
// In 64 bits, where a difference of two `i32`s cannot wrap.
|
|
1437
|
+
if (toI64(b) - toI64(a) !== toI64(n) || a < 0 || b > toI32(head.length)) {
|
|
1269
1438
|
return false
|
|
1270
1439
|
}
|
|
1271
|
-
|
|
1272
|
-
|
|
1440
|
+
// `i` is `a + k`, stepped beside `k` and asked against `head` each pass.
|
|
1441
|
+
let i: i32 = a
|
|
1442
|
+
for (let k: i32 = 0; k < n && i >= 0 && i < toI32(head.length); k += 1) {
|
|
1443
|
+
if (toI32(head[i]) !== toI32(word.charCodeAt(k))) {
|
|
1273
1444
|
return false
|
|
1274
1445
|
}
|
|
1446
|
+
i += 1
|
|
1275
1447
|
}
|
|
1276
1448
|
return true
|
|
1277
1449
|
}
|
|
@@ -1279,11 +1451,15 @@ const http1ClientHeadIs = (head: u8[], a: i32, b: i32, word: string): boolean =>
|
|
|
1279
1451
|
/** Whether `head[a .. b)` is `word` in any case, ASCII letters folded. */
|
|
1280
1452
|
const http1ClientHeadSameWord = (head: u8[], a: i32, b: i32, word: string): boolean => {
|
|
1281
1453
|
const n: i32 = toI32(word.length)
|
|
1282
|
-
|
|
1454
|
+
// In 64 bits, where a difference of two `i32`s cannot wrap.
|
|
1455
|
+
if (toI64(b) - toI64(a) !== toI64(n) || a < 0 || b > toI32(head.length)) {
|
|
1283
1456
|
return false
|
|
1284
1457
|
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1458
|
+
// `i` is `a + k`, stepped beside `k` and asked against `head` each pass.
|
|
1459
|
+
let i: i32 = a
|
|
1460
|
+
for (let k: i32 = 0; k < n && i >= 0 && i < toI32(head.length); k += 1) {
|
|
1461
|
+
let x: i32 = toI32(head[i])
|
|
1462
|
+
i += 1
|
|
1287
1463
|
let y: i32 = toI32(word.charCodeAt(k))
|
|
1288
1464
|
if (x >= 65 && x <= 90) {
|
|
1289
1465
|
x += 32
|
|
@@ -1320,15 +1496,25 @@ const http1ClientHeadTrimEnd = (head: u8[], a: i32, b: i32): i32 => {
|
|
|
1320
1496
|
const http1ClientHeadListHas = (head: u8[], from: i32, to: i32, word: string): boolean => {
|
|
1321
1497
|
const n: i32 = to < toI32(head.length) ? to : toI32(head.length)
|
|
1322
1498
|
let start: i32 = from
|
|
1323
|
-
|
|
1324
|
-
|
|
1499
|
+
// The list's end ends its last element, as a comma does; the walk stops
|
|
1500
|
+
// there, before any step past `n`.
|
|
1501
|
+
let k: i32 = from
|
|
1502
|
+
while (k <= n) {
|
|
1503
|
+
if (k >= n || (k >= 0 && k < toI32(head.length) && head[k] === 44)) {
|
|
1325
1504
|
const a: i32 = http1ClientHeadTrimStart(head, start, k)
|
|
1326
1505
|
const b: i32 = http1ClientHeadTrimEnd(head, a, k)
|
|
1327
1506
|
if (http1ClientHeadSameWord(head, a, b, word)) {
|
|
1328
1507
|
return true
|
|
1329
1508
|
}
|
|
1509
|
+
if (k >= n) {
|
|
1510
|
+
break
|
|
1511
|
+
}
|
|
1330
1512
|
start = k + 1
|
|
1331
1513
|
}
|
|
1514
|
+
if (k >= n) {
|
|
1515
|
+
break
|
|
1516
|
+
}
|
|
1517
|
+
k += 1
|
|
1332
1518
|
}
|
|
1333
1519
|
return false
|
|
1334
1520
|
}
|
|
@@ -1350,11 +1536,12 @@ const http1ClientEndOfHead = (p: Http1ResponseParser): i32 => {
|
|
|
1350
1536
|
return HTTP1_HEAD
|
|
1351
1537
|
}
|
|
1352
1538
|
if (status < 200) {
|
|
1353
|
-
|
|
1539
|
+
const interim: i32 = p.informational
|
|
1540
|
+
if (interim >= HTTP1_MAX_INFORMATIONAL) {
|
|
1354
1541
|
return http1ClientFail(p, HTTP1_E_INFORMATIONAL_LIMIT, "more interim responses than the limit")
|
|
1355
1542
|
}
|
|
1356
1543
|
// The 1xx's status and fields stay readable until the next status line.
|
|
1357
|
-
p.informational
|
|
1544
|
+
p.informational = interim + 1
|
|
1358
1545
|
p.state = HTTP1_CLIENT_S_STATUS_LINE
|
|
1359
1546
|
return HTTP1_INFORMATIONAL
|
|
1360
1547
|
}
|
|
@@ -1366,13 +1553,19 @@ const http1ClientEndOfHead = (p: Http1ResponseParser): i32 => {
|
|
|
1366
1553
|
}
|
|
1367
1554
|
let codings: boolean = false
|
|
1368
1555
|
let length: i64 = toI64(-1)
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1556
|
+
const spans: i32[] = p.spans
|
|
1557
|
+
// `j` is `i * 4`, stepped beside `i` and asked against `spans` each pass.
|
|
1558
|
+
let j: i32 = 0
|
|
1559
|
+
for (let i: i32 = 0; i < p.spanCount && j >= 0 && j <= toI32(spans.length) - 4; i += 1) {
|
|
1560
|
+
const a: i32 = spans[j]
|
|
1561
|
+
const b: i32 = spans[j + 1]
|
|
1562
|
+
const from: i32 = spans[j + 2]
|
|
1563
|
+
const to: i32 = spans[j + 3]
|
|
1564
|
+
j += 4
|
|
1372
1565
|
if (http1ClientHeadIs(p.head, a, b, "transfer-encoding")) {
|
|
1373
1566
|
codings = true
|
|
1374
1567
|
} else if (http1ClientHeadIs(p.head, a, b, "content-length")) {
|
|
1375
|
-
length = http1ClientLengthList(p.head,
|
|
1568
|
+
length = http1ClientLengthList(p.head, from, to, length)
|
|
1376
1569
|
if (length === toI64(-2)) {
|
|
1377
1570
|
return http1ClientFail(
|
|
1378
1571
|
p,
|
|
@@ -1437,7 +1630,7 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
|
|
|
1437
1630
|
const buf: u8[] = p.buf
|
|
1438
1631
|
const a: i32 = p.lineStart
|
|
1439
1632
|
const b: i32 = p.lineEnd < toI32(buf.length) ? p.lineEnd : toI32(buf.length)
|
|
1440
|
-
const room: i64 = p
|
|
1633
|
+
const room: i64 = http1ClientRoom(p)
|
|
1441
1634
|
let size: i64 = toI64(0)
|
|
1442
1635
|
let over: boolean = false
|
|
1443
1636
|
let i: i32 = a
|
|
@@ -1447,10 +1640,12 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
|
|
|
1447
1640
|
break
|
|
1448
1641
|
}
|
|
1449
1642
|
if (!over) {
|
|
1450
|
-
|
|
1643
|
+
// Unsigned: the first test leaves `digit` at most `room`.
|
|
1644
|
+
if (toI64(digit) > room || size > toI64(toU64(room) - toU64(toI64(digit))) / toI64(16)) {
|
|
1451
1645
|
over = true
|
|
1452
1646
|
} else {
|
|
1453
|
-
|
|
1647
|
+
// Unsigned: `size` starts at 0 and the test keeps it at most `room`.
|
|
1648
|
+
size = toI64(toU64(size) * toU64(16) + toU64(digit))
|
|
1454
1649
|
}
|
|
1455
1650
|
}
|
|
1456
1651
|
i += 1
|
|
@@ -1467,7 +1662,7 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
|
|
|
1467
1662
|
if (i >= b && i > digitsEnd) {
|
|
1468
1663
|
return http1ClientFail(p, HTTP1_E_CHUNK, "a malformed chunk size")
|
|
1469
1664
|
}
|
|
1470
|
-
if (i >= 0 && i < b) {
|
|
1665
|
+
if (i >= 0 && i < b && i < toI32(buf.length)) {
|
|
1471
1666
|
if (buf[i] !== 59) {
|
|
1472
1667
|
return http1ClientFail(p, HTTP1_E_CHUNK, "a malformed chunk size")
|
|
1473
1668
|
}
|
|
@@ -1494,31 +1689,43 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
|
|
|
1494
1689
|
* `after` once the last of them is handed out.
|
|
1495
1690
|
*/
|
|
1496
1691
|
const http1ClientBodyWindow = (p: Http1ResponseParser, after: i32): i32 => {
|
|
1497
|
-
const
|
|
1498
|
-
|
|
1692
|
+
const start: i32 = p.start
|
|
1693
|
+
const end: i32 = p.end
|
|
1694
|
+
// Both are offsets into `buf`, so neither is below 0; asked so the
|
|
1695
|
+
// difference is known not to wrap.
|
|
1696
|
+
if (start < 0 || end < 0 || end <= start) {
|
|
1499
1697
|
return HTTP1_NEED_MORE
|
|
1500
1698
|
}
|
|
1501
|
-
const
|
|
1699
|
+
const ready: i32 = end - start
|
|
1700
|
+
const left: i64 = p.left
|
|
1701
|
+
const take: i32 = toI64(ready) < left ? ready : toI32(left)
|
|
1502
1702
|
p.body = p.buf
|
|
1503
|
-
p.bodyOff =
|
|
1703
|
+
p.bodyOff = start
|
|
1504
1704
|
p.bodyLen = take
|
|
1505
|
-
|
|
1705
|
+
// Unsigned: `take` is at most `ready`, `left`, and the room under
|
|
1706
|
+
// `maxBody` (at most 2^53), so none of these wraps.
|
|
1707
|
+
p.start = toI32(toU32(start) + toU32(take))
|
|
1506
1708
|
p.scan = p.start
|
|
1507
|
-
p.left =
|
|
1508
|
-
p.bodyTotal = p.bodyTotal + toI64(take)
|
|
1709
|
+
p.left = toI64(toU64(left) - toU64(toI64(take)))
|
|
1710
|
+
p.bodyTotal = toI64(toU64(p.bodyTotal) + toU64(toI64(take)))
|
|
1509
1711
|
if (p.left === toI64(0)) {
|
|
1510
1712
|
p.state = after
|
|
1511
1713
|
}
|
|
1512
1714
|
return HTTP1_BODY
|
|
1513
1715
|
}
|
|
1514
1716
|
|
|
1717
|
+
/**
|
|
1718
|
+
* The body bytes `maxBody` still allows: `maxBody - bodyTotal`, negative
|
|
1719
|
+
* when a caller lowered `maxBody` under it (unsigned, then read signed).
|
|
1720
|
+
*/
|
|
1721
|
+
const http1ClientRoom = (p: Http1ResponseParser): i64 => toI64(toU64(p.maxBody) - toU64(p.bodyTotal))
|
|
1722
|
+
|
|
1515
1723
|
/** A body read until the server closes: every byte fed, up to `maxBody`. */
|
|
1516
1724
|
const http1ClientCloseBody = (p: Http1ResponseParser): i32 => {
|
|
1517
|
-
|
|
1518
|
-
if (ready <= 0) {
|
|
1725
|
+
if (p.end <= p.start) {
|
|
1519
1726
|
return HTTP1_NEED_MORE
|
|
1520
1727
|
}
|
|
1521
|
-
const room: i64 = p
|
|
1728
|
+
const room: i64 = http1ClientRoom(p)
|
|
1522
1729
|
if (room <= toI64(0)) {
|
|
1523
1730
|
return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a body read until close over the limit")
|
|
1524
1731
|
}
|
|
@@ -1553,14 +1760,18 @@ const http1ClientNext = (p: Http1ResponseParser): i32 => {
|
|
|
1553
1760
|
const limit: i32 = trailer ? p.maxTrailerBytes : p.maxHeaderBytes
|
|
1554
1761
|
const code: i32 = trailer ? HTTP1_E_TRAILER_LIMIT : HTTP1_E_HEADER_LIMIT
|
|
1555
1762
|
const text: string = trailer ? "a trailer section over the limit" : "a header section over the limit"
|
|
1556
|
-
const
|
|
1763
|
+
const section: i32 = p.sectionBytes
|
|
1764
|
+
// In 64 bits, where sums and differences of `i32`s cannot wrap; the
|
|
1765
|
+
// section is at most its limit, so what is left fits an `i32`.
|
|
1766
|
+
const got: i32 = http1ClientTakeLine(p, toI32(toI64(limit) - toI64(section)), code, text)
|
|
1557
1767
|
if (got !== HTTP1_CLIENT_LINE) {
|
|
1558
1768
|
return got
|
|
1559
1769
|
}
|
|
1560
|
-
|
|
1561
|
-
if (
|
|
1770
|
+
const used: i64 = toI64(section) + (toI64(p.lineEnd) - toI64(p.lineStart)) + toI64(2)
|
|
1771
|
+
if (used > toI64(limit)) {
|
|
1562
1772
|
return http1ClientFail(p, code, text)
|
|
1563
1773
|
}
|
|
1774
|
+
p.sectionBytes = toI32(used)
|
|
1564
1775
|
if (p.lineEnd === p.lineStart) {
|
|
1565
1776
|
if (trailer) {
|
|
1566
1777
|
p.state = HTTP1_CLIENT_S_MESSAGE_END
|