nish 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/README.md +27 -21
- package/docs/AI.md +237 -160
- package/docs/INSTALL.md +1 -1
- package/package.json +12 -12
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +94 -11
- package/runtime/nish.h +5 -0
- package/runtime/runtime-host.c +21 -5
- package/std/README.md +14 -11
- package/std/base64.ts +74 -35
- package/std/bytes.ts +207 -97
- package/std/cli-args.ts +150 -107
- package/std/compress/checksum.ts +68 -10
- package/std/compress/deflate.ts +213 -115
- package/std/compress/gzip.ts +124 -49
- package/std/compress/zlib.ts +57 -15
- package/std/crypto/aes.ts +51 -9
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/ed25519.ts +12 -10
- package/std/crypto/hkdf.ts +405 -283
- package/std/crypto/hmac.ts +484 -149
- package/std/crypto/jwt.ts +51 -40
- package/std/crypto/p256.ts +14 -19
- package/std/crypto/pki-roots.ts +35 -12
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1635 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +212 -90
- package/std/crypto/sha512.ts +135 -49
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +236 -119
- package/std/json.ts +81 -46
- package/std/net/buffer-pool.ts +25 -14
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +224 -61
- package/std/net/handler.ts +189 -88
- package/std/net/hpack.ts +216 -103
- package/std/net/http-compress.ts +104 -42
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +326 -24
- package/std/net/http1-client-codec.ts +159 -55
- package/std/net/http1-codec.ts +427 -237
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +139 -60
- package/std/net/http3.ts +140 -79
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +97 -64
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +147 -40
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +61 -39
- package/std/net/quic-packet.ts +91 -50
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +36 -27
- package/std/net/quic.ts +84 -54
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +86 -36
- package/std/net/tls/client.ts +81 -34
- package/std/net/tls/codec.ts +3 -4
- package/std/net/tls/record.ts +80 -35
- package/std/net/tls/schedule.ts +339 -149
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +194 -50
- package/std/net/upstream.ts +83 -38
- package/std/net/url.ts +170 -94
- package/std/net/websocket.ts +19 -20
- package/std/net/webtransport.ts +123 -33
- package/std/raster.ts +12 -2
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +176 -24
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +38 -16
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/net/http1-codec.ts
CHANGED
|
@@ -456,9 +456,11 @@ export class Http1Parser {
|
|
|
456
456
|
* @param len how many bytes from `off` to append
|
|
457
457
|
*/
|
|
458
458
|
feed(data: u8[], off: i32, len: i32): void {
|
|
459
|
-
|
|
459
|
+
const size: i32 = toI32(data.length)
|
|
460
|
+
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
460
461
|
panic("Http1Parser: the window is outside the buffer")
|
|
461
462
|
}
|
|
463
|
+
http1Append(this, data, off, len)
|
|
462
464
|
}
|
|
463
465
|
|
|
464
466
|
/**
|
|
@@ -477,23 +479,7 @@ export class Http1Parser {
|
|
|
477
479
|
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
478
480
|
return false
|
|
479
481
|
}
|
|
480
|
-
|
|
481
|
-
return true
|
|
482
|
-
}
|
|
483
|
-
if (!http1Reserve(this, len)) {
|
|
484
|
-
return true
|
|
485
|
-
}
|
|
486
|
-
const buf: u8[] = this.buf
|
|
487
|
-
const at: i32 = this.end
|
|
488
|
-
// One move: http1Reserve made room for `len` bytes at `at`, and the
|
|
489
|
-
// window was checked above, so it always succeeds; were it to fail, the
|
|
490
|
-
// parser refuses rather than drop the bytes.
|
|
491
|
-
if (!tryCopyBytes(buf, at, data, off, len)) {
|
|
492
|
-
http1Fail(this, 413, "the buffer could not take the input")
|
|
493
|
-
return true
|
|
494
|
-
}
|
|
495
|
-
// In range, because the move fit: `at + len` is at most `buf.length`.
|
|
496
|
-
this.end = toI32(toU32(at) + toU32(len))
|
|
482
|
+
http1Append(this, data, off, len)
|
|
497
483
|
return true
|
|
498
484
|
}
|
|
499
485
|
|
|
@@ -617,8 +603,15 @@ export class Http1Parser {
|
|
|
617
603
|
* @returns the count of unread input bytes, at `buf[start ..]`
|
|
618
604
|
*/
|
|
619
605
|
buffered(): i32 {
|
|
620
|
-
// `start` never passes `end`, and both are offsets into `buf
|
|
621
|
-
|
|
606
|
+
// `start` never passes `end`, and both are offsets into `buf`; a parser
|
|
607
|
+
// whose public fields were moved out of that order holds nothing unread.
|
|
608
|
+
const from: i32 = this.start
|
|
609
|
+
const to: i32 = this.end
|
|
610
|
+
let held: i32 = 0
|
|
611
|
+
if (from >= 0 && to > from) {
|
|
612
|
+
held = to - from
|
|
613
|
+
}
|
|
614
|
+
return held
|
|
622
615
|
}
|
|
623
616
|
|
|
624
617
|
/**
|
|
@@ -626,13 +619,17 @@ export class Http1Parser {
|
|
|
626
619
|
* @returns a copy the caller owns of the bytes that belong to the next protocol, empty when none arrived yet
|
|
627
620
|
*/
|
|
628
621
|
upgradeBytes(): u8[] {
|
|
629
|
-
const
|
|
630
|
-
const to: i32 = this.end
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
622
|
+
const from: i32 = this.start
|
|
623
|
+
const to: i32 = this.end < toI32(this.buf.length) ? this.end : toI32(this.buf.length)
|
|
624
|
+
if (from < 0 || to <= from) {
|
|
625
|
+
return []
|
|
626
|
+
}
|
|
627
|
+
const count: i32 = to - from
|
|
628
|
+
if (count <= 0) {
|
|
629
|
+
return []
|
|
635
630
|
}
|
|
631
|
+
const out: u8[] = new Array<u8>(count)
|
|
632
|
+
tryCopyBytes(out, 0, this.buf, from, count)
|
|
636
633
|
return out
|
|
637
634
|
}
|
|
638
635
|
|
|
@@ -676,6 +673,54 @@ const HTTP1_FIRST_SPANS: i32 = 64
|
|
|
676
673
|
/** The largest input buffer a parser grows to: 2^30 bytes. */
|
|
677
674
|
const HTTP1_MOST_INPUT: i32 = 1073741824
|
|
678
675
|
|
|
676
|
+
/**
|
|
677
|
+
* `feed`'s and `tryFeed`'s append, once each has checked that
|
|
678
|
+
* `data[off .. off + len)` is a window of `data`. A closed or refused parser
|
|
679
|
+
* drops the bytes; one that cannot make room refuses with a 413.
|
|
680
|
+
*/
|
|
681
|
+
const http1Append = (p: Http1Parser, data: u8[], off: i32, len: i32): void => {
|
|
682
|
+
if (p.state === HTTP1_S_CLOSED || p.state === HTTP1_S_ERROR) {
|
|
683
|
+
return
|
|
684
|
+
}
|
|
685
|
+
if (!http1Reserve(p, len)) {
|
|
686
|
+
return
|
|
687
|
+
}
|
|
688
|
+
const buf: u8[] = p.buf
|
|
689
|
+
const at: i32 = p.end
|
|
690
|
+
// One move: http1Reserve made room for `len` bytes at `at`, and the caller
|
|
691
|
+
// checked the window, so it always succeeds; were it to fail, the parser
|
|
692
|
+
// refuses rather than drop the bytes.
|
|
693
|
+
if (!tryCopyBytes(buf, at, data, off, len)) {
|
|
694
|
+
http1Fail(p, 413, "the buffer could not take the input")
|
|
695
|
+
return
|
|
696
|
+
}
|
|
697
|
+
// The move fit, so `at` and `len` are non-negative i32s and their u32 sum
|
|
698
|
+
// is exact: at most `buf.length`.
|
|
699
|
+
const end: u32 = toU32(at) + toU32(len)
|
|
700
|
+
p.end = toI32(end)
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
|
|
705
|
+
* step subtracts a non-negative value from one that is no larger than
|
|
706
|
+
* `bytes.length`, so the bounds prover sees no sum that can overflow.
|
|
707
|
+
*/
|
|
708
|
+
const http1WindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
|
|
709
|
+
if (off < 0 || len < 0) {
|
|
710
|
+
return -1
|
|
711
|
+
}
|
|
712
|
+
const size: i32 = toI32(bytes.length)
|
|
713
|
+
const room: i32 = size - off
|
|
714
|
+
if (room < 0) {
|
|
715
|
+
return -1
|
|
716
|
+
}
|
|
717
|
+
const gap: i32 = room - len
|
|
718
|
+
if (gap < 0) {
|
|
719
|
+
return -1
|
|
720
|
+
}
|
|
721
|
+
return size - gap
|
|
722
|
+
}
|
|
723
|
+
|
|
679
724
|
/**
|
|
680
725
|
* Makes room for `len` more bytes at `p.end`, moving the live bytes down
|
|
681
726
|
* first. False, with the parser refused, when that would take a buffer past
|
|
@@ -721,19 +766,23 @@ const http1Reserve = (p: Http1Parser, len: i32): boolean => {
|
|
|
721
766
|
return false
|
|
722
767
|
}
|
|
723
768
|
p.buf = buf
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
769
|
+
// A line already consumed sits before `from` and moves below 0, as it
|
|
770
|
+
// always has; `http1Less` holds a mark moved by hand at the i32 range's end
|
|
771
|
+
// rather than let it overflow.
|
|
772
|
+
p.lineStart = http1Less(p.lineStart, from)
|
|
773
|
+
p.lineEnd = http1Less(p.lineEnd, from)
|
|
774
|
+
p.scan = http1Less(p.scan, from)
|
|
727
775
|
p.start = 0
|
|
728
776
|
p.end = toI32(live)
|
|
729
777
|
return true
|
|
730
778
|
}
|
|
731
779
|
|
|
732
780
|
/**
|
|
733
|
-
* A buffer of `size` bytes whose first `live` are `old[from ..]
|
|
734
|
-
*
|
|
735
|
-
*
|
|
736
|
-
*
|
|
781
|
+
* A buffer of `size` bytes whose first `live` are `old[from ..]`: one zeroed
|
|
782
|
+
* allocation and one bulk copy. The first input buffer, `HTTP1_FIRST_INPUT`
|
|
783
|
+
* bytes, is the common one and copies nothing. Its callers cap `size` below
|
|
784
|
+
* 2^31 (twice `HTTP1_MOST_INPUT`), so clearing its top bit (`<< 1 >>> 1`),
|
|
785
|
+
* which is what proves the allocation's length non-negative, changes nothing.
|
|
737
786
|
*/
|
|
738
787
|
const http1Grown = (old: u8[], from: i32, live: u32, size: u32): u8[] => {
|
|
739
788
|
if (old.length === 0 && size === toU32(HTTP1_FIRST_INPUT)) {
|
|
@@ -742,23 +791,14 @@ const http1Grown = (old: u8[], from: i32, live: u32, size: u32): u8[] => {
|
|
|
742
791
|
if (old.length === 0 && size === toU32(HTTP1_FIRST_HEAD)) {
|
|
743
792
|
return new Array<u8>(HTTP1_FIRST_HEAD)
|
|
744
793
|
}
|
|
745
|
-
const
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
}
|
|
754
|
-
return sized
|
|
755
|
-
}
|
|
756
|
-
const buf: u8[] = []
|
|
757
|
-
for (let k: i32 = from; k >= 0 && k < toI32(old.length) && k < to; k += 1) {
|
|
758
|
-
buf.push(old[k])
|
|
759
|
-
}
|
|
760
|
-
while (toU32(buf.length) < size) {
|
|
761
|
-
buf.push(0)
|
|
794
|
+
const n: i32 = toI32((size << toU32(1)) >>> toU32(1))
|
|
795
|
+
const buf: u8[] = new Array<u8>(n)
|
|
796
|
+
// `live` is the count from `from` to the end of the bytes in `old`, so the
|
|
797
|
+
// window always fits and `to` is its end; -1 copies nothing.
|
|
798
|
+
const to: i32 = http1WindowEnd(old, from, toI32(live))
|
|
799
|
+
if (from >= 0 && to > from) {
|
|
800
|
+
const count: i32 = to - from
|
|
801
|
+
tryCopyBytes(buf, 0, old, from, count < n ? count : n)
|
|
762
802
|
}
|
|
763
803
|
return buf
|
|
764
804
|
}
|
|
@@ -866,7 +906,7 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
866
906
|
const bad: u32 = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
|
|
867
907
|
if (bad !== 0) {
|
|
868
908
|
// The lowest set lane, 0 to 15: sixteen lanes set at most bit 15.
|
|
869
|
-
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (
|
|
909
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
|
|
870
910
|
if (lane >= 0 && lane < 16) {
|
|
871
911
|
i += lane
|
|
872
912
|
}
|
|
@@ -982,10 +1022,10 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
|
982
1022
|
let run: i32 = 16
|
|
983
1023
|
if (bad !== 0) {
|
|
984
1024
|
// The lowest set lane, 0 to 15.
|
|
985
|
-
run = toI32(31) - toI32(Math.clz32(bad & (
|
|
1025
|
+
run = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
|
|
986
1026
|
}
|
|
987
|
-
// `0 <= k < stop`, so the
|
|
988
|
-
const left: i32 =
|
|
1027
|
+
// `0 <= k < stop`, so the difference is the bytes left.
|
|
1028
|
+
const left: i32 = stop - k
|
|
989
1029
|
if (run > left) {
|
|
990
1030
|
run = left
|
|
991
1031
|
}
|
|
@@ -994,8 +1034,13 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
|
994
1034
|
lt(lanes, splatU8x16(toU8(run)))
|
|
995
1035
|
)
|
|
996
1036
|
store(select(upper, or(v, splatU8x16(32)), v), head, k)
|
|
997
|
-
// `run` is
|
|
998
|
-
|
|
1037
|
+
// `run` is 0 to 16 and `k` at most `head.length - 16`, so the sum is
|
|
1038
|
+
// inside `head`; the test spells that out for the bounds proof and is
|
|
1039
|
+
// never taken.
|
|
1040
|
+
if (run < 0 || run > 16 || k > toI32(head.length) - 16) {
|
|
1041
|
+
return k
|
|
1042
|
+
}
|
|
1043
|
+
k = k + run
|
|
999
1044
|
if (run < 16) {
|
|
1000
1045
|
return k
|
|
1001
1046
|
}
|
|
@@ -1055,8 +1100,8 @@ export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
|
|
|
1055
1100
|
*/
|
|
1056
1101
|
export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
|
|
1057
1102
|
const n: i32 = toI32(word.length)
|
|
1058
|
-
// `0 <= a <= b
|
|
1059
|
-
if (a < 0 || b < a || b > toI32(text.length) ||
|
|
1103
|
+
// `0 <= a <= b` by the tests before it, so the difference is the slice's length.
|
|
1104
|
+
if (a < 0 || b < a || b > toI32(text.length) || b - a !== n) {
|
|
1060
1105
|
return false
|
|
1061
1106
|
}
|
|
1062
1107
|
let j: i32 = a
|
|
@@ -1080,8 +1125,8 @@ export const http1SameWord = (text: string, a: i32, b: i32, word: string): boole
|
|
|
1080
1125
|
*/
|
|
1081
1126
|
const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean): boolean => {
|
|
1082
1127
|
const n: i32 = toI32(word.length)
|
|
1083
|
-
// `0 <= a <= b
|
|
1084
|
-
if (a < 0 || b < a || b > toI32(bytes.length) ||
|
|
1128
|
+
// `0 <= a <= b` by the tests before it, so the difference is the slice's length.
|
|
1129
|
+
if (a < 0 || b < a || b > toI32(bytes.length) || b - a !== n) {
|
|
1085
1130
|
return false
|
|
1086
1131
|
}
|
|
1087
1132
|
// `j` walks `bytes[a .. b)` beside `k`: the lengths are equal.
|
|
@@ -1162,8 +1207,8 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
|
1162
1207
|
http1Fail(p, 400, "the request line is outside the buffer")
|
|
1163
1208
|
return -1
|
|
1164
1209
|
}
|
|
1165
|
-
// `0 <= from <= to`, so the
|
|
1166
|
-
const n: i32 =
|
|
1210
|
+
// `0 <= from <= to`, checked above, so the difference is the line's length.
|
|
1211
|
+
const n: i32 = to - from
|
|
1167
1212
|
const head: u8[] = p.head
|
|
1168
1213
|
const buf: u8[] = p.buf
|
|
1169
1214
|
// A line of up to 32 bytes is one or two vector stores; a longer one, or
|
|
@@ -1180,8 +1225,14 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
|
1180
1225
|
http1Fail(p, 400, "the request line does not fit the head")
|
|
1181
1226
|
return -1
|
|
1182
1227
|
}
|
|
1183
|
-
//
|
|
1184
|
-
|
|
1228
|
+
// The line was stored at `head[at ..]`, so the window fits and its end is
|
|
1229
|
+
// the new length; were it not to, the parser is refused.
|
|
1230
|
+
const headEnd: i32 = http1WindowEnd(head, at, n)
|
|
1231
|
+
if (headEnd < 0) {
|
|
1232
|
+
http1Fail(p, 400, "the request line does not fit the head")
|
|
1233
|
+
return -1
|
|
1234
|
+
}
|
|
1235
|
+
p.headLength = headEnd
|
|
1185
1236
|
return at
|
|
1186
1237
|
}
|
|
1187
1238
|
|
|
@@ -1189,15 +1240,22 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
|
1189
1240
|
const http1SpanSet = (p: Http1Parser, at: i32, value: i32): void => {
|
|
1190
1241
|
if (at >= toI32(p.spans.length)) {
|
|
1191
1242
|
const old: i32[] = p.spans
|
|
1192
|
-
//
|
|
1193
|
-
//
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1243
|
+
// `at` counts four spans a field. The head is under `HTTP1_MOST_INPUT`
|
|
1244
|
+
// (2^30) bytes and each field line takes at least four of them, so there
|
|
1245
|
+
// are under 2^28 fields, `at` is under 2^30 and the size under 2^31:
|
|
1246
|
+
// clearing its top bit (`<< 1 >>> 1`), which is what proves the
|
|
1247
|
+
// allocation's length non-negative, changes nothing.
|
|
1248
|
+
// (The caller's own guard is looser: it lets the count reach 2^29 - 1.)
|
|
1249
|
+
const wide: u32 = at < HTTP1_FIRST_SPANS ? toU32(HTTP1_FIRST_SPANS) : toU32(at) * toU32(2) + toU32(4)
|
|
1250
|
+
const size: i32 = toI32((wide << toU32(1)) >>> toU32(1))
|
|
1251
|
+
const grown: i32[] = new Array<i32>(size)
|
|
1252
|
+
// `new Array` is zero-filled already. The fill stays because
|
|
1253
|
+
// `Http1Parser.next` inlines this function, and without the fill it
|
|
1254
|
+
// compiles to 2.2M more instructions on the std-http1 bench row.
|
|
1255
|
+
grown.fill(0)
|
|
1256
|
+
const keep: i32 = toI32(old.length)
|
|
1257
|
+
for (let k: i32 = 0; k < keep && k < toI32(grown.length); k += 1) {
|
|
1258
|
+
grown[k] = old[k]
|
|
1201
1259
|
}
|
|
1202
1260
|
p.spans = grown
|
|
1203
1261
|
}
|
|
@@ -1297,14 +1355,19 @@ const http1HeadIsVector = (
|
|
|
1297
1355
|
return false
|
|
1298
1356
|
}
|
|
1299
1357
|
if (at > toI32(head.length) - 16) {
|
|
1300
|
-
//
|
|
1301
|
-
|
|
1358
|
+
// A window past the head's end matches nothing, as `http1HeadIs` says.
|
|
1359
|
+
const end: i32 = http1WindowEnd(head, at, n)
|
|
1360
|
+
return end >= 0 && http1HeadIs(head, at, end, text, fold)
|
|
1302
1361
|
}
|
|
1303
1362
|
let v: u8x16 = loadU8x16(head, at)
|
|
1304
1363
|
if (fold) {
|
|
1305
1364
|
v = select(lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)), or(v, splatU8x16(32)), v)
|
|
1306
1365
|
}
|
|
1307
|
-
|
|
1366
|
+
// The low `n` lanes: `n` is 0 to 16, so the shift is 0 to 16 places.
|
|
1367
|
+
let want: i32 = 65535
|
|
1368
|
+
if (n < 16) {
|
|
1369
|
+
want = toI32(65535) >> (16 - n)
|
|
1370
|
+
}
|
|
1308
1371
|
return (bitmask(eq(v, word)) & want) === want
|
|
1309
1372
|
}
|
|
1310
1373
|
|
|
@@ -1371,7 +1434,7 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1371
1434
|
const commas: u32 = toU32(bitmask(eq(loadU8x16(head, end), splatU8x16(44))))
|
|
1372
1435
|
if (commas !== 0) {
|
|
1373
1436
|
// The lowest set lane, 0 to 15.
|
|
1374
|
-
const lane: i32 = toI32(31) - toI32(Math.clz32(commas & (
|
|
1437
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(commas & (0 - commas)))
|
|
1375
1438
|
if (lane >= 0 && lane < 16) {
|
|
1376
1439
|
end += lane
|
|
1377
1440
|
}
|
|
@@ -1396,8 +1459,11 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1396
1459
|
while (b > a && b > 0 && b <= toI32(head.length) && http1IsOws(toI32(head[b - 1]))) {
|
|
1397
1460
|
b -= 1
|
|
1398
1461
|
}
|
|
1399
|
-
// `0 <= a <= b`, so the
|
|
1400
|
-
|
|
1462
|
+
// `0 <= a <= b`, so the difference is the element's length.
|
|
1463
|
+
let n: i32 = 0
|
|
1464
|
+
if (a >= 0 && b >= a) {
|
|
1465
|
+
n = b - a
|
|
1466
|
+
}
|
|
1401
1467
|
if (n === 5 && http1HeadIsVector(head, a, n, close, "close", true)) {
|
|
1402
1468
|
found = found | HTTP1_CONNECTION_CLOSE
|
|
1403
1469
|
} else if (n === 10 && http1HeadIsVector(head, a, n, keepAlive, "keep-alive", true)) {
|
|
@@ -1441,8 +1507,10 @@ const http1MethodOverrun = (p: Http1Parser): boolean => {
|
|
|
1441
1507
|
return false
|
|
1442
1508
|
}
|
|
1443
1509
|
}
|
|
1444
|
-
// `start` is within a buffer under 2^30 bytes, so the sum is an i32
|
|
1445
|
-
|
|
1510
|
+
// `start` is within a buffer under 2^30 bytes, so the sum is an i32;
|
|
1511
|
+
// `http1Past` holds a start moved by hand at the i32 range's end rather
|
|
1512
|
+
// than let it overflow.
|
|
1513
|
+
const stop: i32 = http1Past(from, HTTP1_MAX_METHOD)
|
|
1446
1514
|
for (let k: i32 = from; k >= 0 && k < p.end && k < toI32(buf.length); k += 1) {
|
|
1447
1515
|
if (buf[k] === 32 || buf[k] === 13 || buf[k] === 10) {
|
|
1448
1516
|
return false
|
|
@@ -1477,7 +1545,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1477
1545
|
const bad: u32 = toU32(http1NonTokenLanes(loadU8x16(buf, i)))
|
|
1478
1546
|
if (bad !== 0) {
|
|
1479
1547
|
// The lowest set lane, 0 to 15.
|
|
1480
|
-
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (
|
|
1548
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
|
|
1481
1549
|
if (lane >= 0 && lane < 16) {
|
|
1482
1550
|
i += lane
|
|
1483
1551
|
}
|
|
@@ -1514,7 +1582,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1514
1582
|
const bad: u32 = toU32(bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93))))
|
|
1515
1583
|
if (bad !== 0) {
|
|
1516
1584
|
// The lowest set lane, 0 to 15.
|
|
1517
|
-
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (
|
|
1585
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (0 - bad)))
|
|
1518
1586
|
if (lane >= 0 && lane < 16) {
|
|
1519
1587
|
i += lane
|
|
1520
1588
|
}
|
|
@@ -1569,8 +1637,8 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1569
1637
|
return http1Fail(p, 505, "an HTTP major version other than 1")
|
|
1570
1638
|
}
|
|
1571
1639
|
p.minor = buf[i + 7] === 48 ? 0 : 1
|
|
1572
|
-
// `a <= b`, checked above.
|
|
1573
|
-
const lineLength: i32 =
|
|
1640
|
+
// `0 <= a <= b`, checked above.
|
|
1641
|
+
const lineLength: i32 = b - a
|
|
1574
1642
|
if (p.headLength < 0 || toU64(p.headLength) + toU64(lineLength) + toU64(16) > toU64(p.head.length)) {
|
|
1575
1643
|
if (!http1HeadGrow(p, lineLength)) {
|
|
1576
1644
|
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
@@ -1581,10 +1649,15 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1581
1649
|
return HTTP1_ERROR
|
|
1582
1650
|
}
|
|
1583
1651
|
p.methodStart = at
|
|
1584
|
-
// Each mark is within the line, `a` to `b`, and the line
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1652
|
+
// Each mark is within the line, `a` to `b`, and the line now ends the head,
|
|
1653
|
+
// so each offset is non-negative and its u32 sum with `at` is exact: at
|
|
1654
|
+
// most the head's length.
|
|
1655
|
+
const methodAt: u32 = toU32(at) + toU32(methodEnd - a)
|
|
1656
|
+
const targetFrom: u32 = toU32(at) + toU32(targetStart - a)
|
|
1657
|
+
const targetTo: u32 = toU32(at) + toU32(targetEnd - a)
|
|
1658
|
+
p.methodEnd = toI32(methodAt)
|
|
1659
|
+
p.targetStart = toI32(targetFrom)
|
|
1660
|
+
p.targetEnd = toI32(targetTo)
|
|
1588
1661
|
if (p.keepText) {
|
|
1589
1662
|
p.method = http1Text(buf, a, methodEnd, false)
|
|
1590
1663
|
p.target = http1Text(buf, targetStart, targetEnd, false)
|
|
@@ -1599,7 +1672,7 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1599
1672
|
* head, `http1Section`'s copy of it: `buf[x]` is at `head[x + delta]`.
|
|
1600
1673
|
* Answers `HTTP1_NEED_MORE` or the refusal.
|
|
1601
1674
|
*/
|
|
1602
|
-
const http1FieldLine = (p: Http1Parser, keep: boolean,
|
|
1675
|
+
const http1FieldLine = (p: Http1Parser, keep: boolean, shift: integer<0, 2147483646>): i32 => {
|
|
1603
1676
|
const buf: u8[] = p.buf
|
|
1604
1677
|
const n: i32 = toI32(buf.length)
|
|
1605
1678
|
const a: i32 = p.lineStart
|
|
@@ -1610,103 +1683,138 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1610
1683
|
// read outside `buf`.
|
|
1611
1684
|
return http1Fail(p, 400, "a field line out of order")
|
|
1612
1685
|
}
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
// A kept line's name is read in the head's copy, each upper-case letter
|
|
1617
|
-
// lowered as the scan passes it: one walk over the name, not a check of
|
|
1618
|
-
// `buf` and then a second walk of the copy. The copy of a line refused
|
|
1619
|
-
// below is never read, since a refusal ends the parse.
|
|
1620
|
-
let at: i32 = 0
|
|
1621
|
-
let i: i32 = a
|
|
1622
|
-
if (keep) {
|
|
1623
|
-
at = http1Past(a, delta)
|
|
1624
|
-
i = http1Past(a, http1Less(http1LowerName(p.head, at, http1Past(at, http1Less(b, a))), at))
|
|
1625
|
-
} else {
|
|
1686
|
+
if (!keep) {
|
|
1687
|
+
// A trailer is checked and counted, but not kept: nothing here reads one.
|
|
1688
|
+
let i: i32 = a
|
|
1626
1689
|
while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1627
1690
|
i += 1
|
|
1628
1691
|
}
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
// Whitespace is no tchar, so an obs-fold line has an empty name: its
|
|
1632
|
-
// own refusal is told apart here, off the path every field takes.
|
|
1633
|
-
if (i === a && a >= 0 && a < b && a < toI32(buf.length) && (buf[a] === 32 || buf[a] === 9)) {
|
|
1634
|
-
return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
|
|
1692
|
+
if (i === a || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 58) {
|
|
1693
|
+
return http1NameRefused(p, i === a)
|
|
1635
1694
|
}
|
|
1636
|
-
return
|
|
1695
|
+
return http1FieldCounted(p)
|
|
1696
|
+
}
|
|
1697
|
+
// A kept line is read in the head's copy, `head[at .. end)`, its name
|
|
1698
|
+
// lowered as the scan passes it: one walk over the name, not a check of
|
|
1699
|
+
// `buf` and then a second walk of the copy. The copy of a line refused
|
|
1700
|
+
// below is never read, since a refusal ends the parse. The head is at most
|
|
1701
|
+
// 2^30 bytes, so the copy's start and length are too; the test spells that
|
|
1702
|
+
// out for the bounds proof, and refuses a parser moved by hand.
|
|
1703
|
+
if (a > 1073741823) {
|
|
1704
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
1705
|
+
}
|
|
1706
|
+
const at: i32 = a - 1073741823 + shift
|
|
1707
|
+
const length: i32 = b - a
|
|
1708
|
+
if (at < 0 || at > 1073741823 || length > 1073741823) {
|
|
1709
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
1710
|
+
}
|
|
1711
|
+
const end: i32 = at + length
|
|
1712
|
+
const head: u8[] = p.head
|
|
1713
|
+
const nameEnd: i32 = http1LowerName(head, at, end)
|
|
1714
|
+
if (
|
|
1715
|
+
nameEnd === at ||
|
|
1716
|
+
nameEnd >= end ||
|
|
1717
|
+
nameEnd < 0 ||
|
|
1718
|
+
nameEnd >= toI32(head.length) ||
|
|
1719
|
+
head[nameEnd] !== 58
|
|
1720
|
+
) {
|
|
1721
|
+
return http1NameRefused(p, nameEnd === at)
|
|
1722
|
+
}
|
|
1723
|
+
const counted: i32 = http1FieldCounted(p)
|
|
1724
|
+
if (counted !== HTTP1_NEED_MORE) {
|
|
1725
|
+
return counted
|
|
1726
|
+
}
|
|
1727
|
+
// `nameEnd` is inside the head, so the value starts inside it or at its end.
|
|
1728
|
+
let valueStart: i32 = nameEnd + 1
|
|
1729
|
+
let valueEnd: i32 = end
|
|
1730
|
+
while (
|
|
1731
|
+
valueStart >= 0 &&
|
|
1732
|
+
valueStart < valueEnd &&
|
|
1733
|
+
valueStart < toI32(head.length) &&
|
|
1734
|
+
(head[valueStart] === 32 || head[valueStart] === 9)
|
|
1735
|
+
) {
|
|
1736
|
+
valueStart += 1
|
|
1737
|
+
}
|
|
1738
|
+
while (
|
|
1739
|
+
valueEnd >= 1 &&
|
|
1740
|
+
valueEnd > valueStart &&
|
|
1741
|
+
valueEnd <= toI32(head.length) &&
|
|
1742
|
+
(head[valueEnd - 1] === 32 || head[valueEnd - 1] === 9)
|
|
1743
|
+
) {
|
|
1744
|
+
valueEnd -= 1
|
|
1745
|
+
}
|
|
1746
|
+
// The fields that frame the message are counted here, as their names
|
|
1747
|
+
// are read, for `http1EndOfHead`: every kept field asks the same question.
|
|
1748
|
+
// Each count is at most `fieldCount`, and the value bytes at most the
|
|
1749
|
+
// head's.
|
|
1750
|
+
const kind: i32 = http1FramingField(head, at, nameEnd - at)
|
|
1751
|
+
if (kind === HTTP1_FIELD_HOST) {
|
|
1752
|
+
p.hostFields = http1Past(p.hostFields, 1)
|
|
1753
|
+
p.hostField = p.headerCount
|
|
1754
|
+
} else if (kind === HTTP1_FIELD_LENGTH) {
|
|
1755
|
+
p.lengthFields = http1Past(p.lengthFields, 1)
|
|
1756
|
+
p.lengthField = p.headerCount
|
|
1757
|
+
} else if (kind === HTTP1_FIELD_CODING) {
|
|
1758
|
+
p.codingFields = http1Past(p.codingFields, 1)
|
|
1759
|
+
} else if (kind === HTTP1_FIELD_CONNECTION) {
|
|
1760
|
+
p.connectionOptions = p.connectionOptions | http1ConnectionList(head, valueStart, valueEnd)
|
|
1761
|
+
} else if (kind === HTTP1_FIELD_UPGRADE) {
|
|
1762
|
+
p.upgradeFields = http1Past(p.upgradeFields, 1)
|
|
1763
|
+
p.upgradeValueBytes = http1Past(p.upgradeValueBytes, http1Less(valueEnd, valueStart))
|
|
1764
|
+
}
|
|
1765
|
+
// Four spans a field. Each field line is at least four bytes of a head
|
|
1766
|
+
// under 2^30, so the count is below 2^28 and the product fits; the test
|
|
1767
|
+
// spells that out for the bounds proof, and refuses a count moved by hand.
|
|
1768
|
+
const count: i32 = p.headerCount
|
|
1769
|
+
if (count < 0 || count > 536870911) {
|
|
1770
|
+
return http1Fail(p, 431, "more fields than the limit")
|
|
1771
|
+
}
|
|
1772
|
+
const base: i32 = count * 4
|
|
1773
|
+
http1SpanSet(p, base + 3, valueEnd)
|
|
1774
|
+
// The last of the four is set first, so the array has room for them all.
|
|
1775
|
+
const spans: i32[] = p.spans
|
|
1776
|
+
if (base >= 0 && base < toI32(spans.length) - 2) {
|
|
1777
|
+
spans[base] = at
|
|
1778
|
+
spans[base + 1] = nameEnd
|
|
1779
|
+
spans[base + 2] = valueStart
|
|
1780
|
+
}
|
|
1781
|
+
p.headerCount = count + 1
|
|
1782
|
+
if (p.keepText) {
|
|
1783
|
+
// The copy holds the line's bytes, its name already lowered.
|
|
1784
|
+
p.names.push(http1Text(head, at, nameEnd, true))
|
|
1785
|
+
p.values.push(http1Text(head, valueStart, valueEnd, false))
|
|
1786
|
+
}
|
|
1787
|
+
return HTTP1_NEED_MORE
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
/**
|
|
1791
|
+
* The refusal of a field line whose name is not tchars up to a colon:
|
|
1792
|
+
* whitespace is no tchar, so an obs-fold line has an empty name, and its own
|
|
1793
|
+
* refusal is told apart here, off the path every field takes.
|
|
1794
|
+
*/
|
|
1795
|
+
const http1NameRefused = (p: Http1Parser, empty: boolean): i32 => {
|
|
1796
|
+
const buf: u8[] = p.buf
|
|
1797
|
+
const a: i32 = p.lineStart
|
|
1798
|
+
if (empty && a >= 0 && a < p.lineEnd && a < toI32(buf.length) && (buf[a] === 32 || buf[a] === 9)) {
|
|
1799
|
+
return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
|
|
1637
1800
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1801
|
+
return http1Fail(p, 400, "a malformed field name")
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
/**
|
|
1805
|
+
* Counts a field line whose name has been read, or refuses it: a control
|
|
1806
|
+
* byte on the line is one in its value, the name being tchars and the colon
|
|
1807
|
+
* field text, and the count stops at `maxHeaders`.
|
|
1808
|
+
*/
|
|
1809
|
+
const http1FieldCounted = (p: Http1Parser): i32 => {
|
|
1641
1810
|
if (p.lineControl) {
|
|
1642
1811
|
return http1Fail(p, 400, "a control character in a field value")
|
|
1643
1812
|
}
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
if (p.fieldCount > p.maxHeaders) {
|
|
1813
|
+
const count: i32 = p.fieldCount
|
|
1814
|
+
if (count >= p.maxHeaders) {
|
|
1647
1815
|
return http1Fail(p, 431, "more fields than the limit")
|
|
1648
1816
|
}
|
|
1649
|
-
|
|
1650
|
-
if (keep) {
|
|
1651
|
-
let valueStart: i32 = http1Past(nameEnd, 1)
|
|
1652
|
-
let valueEnd: i32 = b
|
|
1653
|
-
while (
|
|
1654
|
-
valueStart >= 0 &&
|
|
1655
|
-
valueStart < valueEnd &&
|
|
1656
|
-
valueStart < toI32(buf.length) &&
|
|
1657
|
-
(buf[valueStart] === 32 || buf[valueStart] === 9)
|
|
1658
|
-
) {
|
|
1659
|
-
valueStart += 1
|
|
1660
|
-
}
|
|
1661
|
-
while (
|
|
1662
|
-
valueEnd >= 1 &&
|
|
1663
|
-
valueEnd > valueStart &&
|
|
1664
|
-
valueEnd <= toI32(buf.length) &&
|
|
1665
|
-
(buf[valueEnd - 1] === 32 || buf[valueEnd - 1] === 9)
|
|
1666
|
-
) {
|
|
1667
|
-
valueEnd -= 1
|
|
1668
|
-
}
|
|
1669
|
-
// The fields that frame the message are counted here, as their names
|
|
1670
|
-
// are read, for `http1EndOfHead`: every kept field asks the same question.
|
|
1671
|
-
// Each count is at most `fieldCount`, and the value bytes at most the
|
|
1672
|
-
// head's.
|
|
1673
|
-
const kind: i32 = http1FramingField(p.head, at, http1Less(nameEnd, a))
|
|
1674
|
-
if (kind === HTTP1_FIELD_HOST) {
|
|
1675
|
-
p.hostFields = http1Past(p.hostFields, 1)
|
|
1676
|
-
p.hostField = p.headerCount
|
|
1677
|
-
} else if (kind === HTTP1_FIELD_LENGTH) {
|
|
1678
|
-
p.lengthFields = http1Past(p.lengthFields, 1)
|
|
1679
|
-
p.lengthField = p.headerCount
|
|
1680
|
-
} else if (kind === HTTP1_FIELD_CODING) {
|
|
1681
|
-
p.codingFields = http1Past(p.codingFields, 1)
|
|
1682
|
-
} else if (kind === HTTP1_FIELD_CONNECTION) {
|
|
1683
|
-
p.connectionOptions =
|
|
1684
|
-
p.connectionOptions |
|
|
1685
|
-
http1ConnectionList(
|
|
1686
|
-
p.head,
|
|
1687
|
-
http1Past(at, http1Less(valueStart, a)),
|
|
1688
|
-
http1Past(at, http1Less(valueEnd, a))
|
|
1689
|
-
)
|
|
1690
|
-
} else if (kind === HTTP1_FIELD_UPGRADE) {
|
|
1691
|
-
p.upgradeFields = http1Past(p.upgradeFields, 1)
|
|
1692
|
-
p.upgradeValueBytes = http1Past(p.upgradeValueBytes, http1Less(valueEnd, valueStart))
|
|
1693
|
-
}
|
|
1694
|
-
// Four spans a field, and fields are at most `maxHeaders`.
|
|
1695
|
-
const base: i32 = toI32(toU32(4) * toU32(p.headerCount))
|
|
1696
|
-
http1SpanSet(p, http1Past(base, 3), http1Past(at, http1Less(valueEnd, a)))
|
|
1697
|
-
// The last of the four is set first, so the array has room for them all.
|
|
1698
|
-
const spans: i32[] = p.spans
|
|
1699
|
-
if (base >= 0 && base < toI32(spans.length) - 2) {
|
|
1700
|
-
spans[base] = at
|
|
1701
|
-
spans[base + 1] = http1Past(at, http1Less(nameEnd, a))
|
|
1702
|
-
spans[base + 2] = http1Past(at, http1Less(valueStart, a))
|
|
1703
|
-
}
|
|
1704
|
-
p.headerCount = http1Past(p.headerCount, 1)
|
|
1705
|
-
if (p.keepText) {
|
|
1706
|
-
p.names.push(http1Text(buf, a, nameEnd, true))
|
|
1707
|
-
p.values.push(http1Text(buf, valueStart, valueEnd, false))
|
|
1708
|
-
}
|
|
1709
|
-
}
|
|
1817
|
+
p.fieldCount = count + 1
|
|
1710
1818
|
return HTTP1_NEED_MORE
|
|
1711
1819
|
}
|
|
1712
1820
|
|
|
@@ -2032,7 +2140,7 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
|
|
|
2032
2140
|
if (from < 0 || to <= from) {
|
|
2033
2141
|
return HTTP1_NEED_MORE
|
|
2034
2142
|
}
|
|
2035
|
-
const ready: i32 =
|
|
2143
|
+
const ready: i32 = to - from
|
|
2036
2144
|
let take: i32 = ready < p.left ? ready : p.left
|
|
2037
2145
|
if (take > p.maxChunk && p.maxChunk > 0) {
|
|
2038
2146
|
take = p.maxChunk
|
|
@@ -2048,10 +2156,10 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
|
|
|
2048
2156
|
p.bodyOff = from
|
|
2049
2157
|
p.bodyLen = take
|
|
2050
2158
|
// `take` is at most `end - start`, and at most `left`, which was counted
|
|
2051
|
-
// within `maxBody`: none of the three
|
|
2159
|
+
// within `maxBody`: none of the three leaves the i32 range.
|
|
2052
2160
|
p.start = http1Past(from, take)
|
|
2053
2161
|
p.scan = p.start
|
|
2054
|
-
p.left =
|
|
2162
|
+
p.left = http1Less(p.left, take)
|
|
2055
2163
|
p.bodyTotal = http1Past(total, take)
|
|
2056
2164
|
if (p.left === 0) {
|
|
2057
2165
|
p.state = after
|
|
@@ -2059,6 +2167,20 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
|
|
|
2059
2167
|
return HTTP1_BODY
|
|
2060
2168
|
}
|
|
2061
2169
|
|
|
2170
|
+
/**
|
|
2171
|
+
* The last `q` whose vector, landing at `q + delta`, still fits in `head`
|
|
2172
|
+
* with sixteen bytes to spare: `head.length - 32 - delta`, held at the i32
|
|
2173
|
+
* range's end, which a head under 2^30 bytes never reaches.
|
|
2174
|
+
*/
|
|
2175
|
+
const http1GrowAt = (head: u8[], delta: i32): i32 => {
|
|
2176
|
+
const room: i64 = toI64(toI32(head.length)) - toI64(32) - toI64(delta)
|
|
2177
|
+
let at: i32 = 2147483647
|
|
2178
|
+
if (room >= toI64(-2147483647) && room < toI64(2147483647)) {
|
|
2179
|
+
at = toI32(room)
|
|
2180
|
+
}
|
|
2181
|
+
return at
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2062
2184
|
/**
|
|
2063
2185
|
* Reads a header or trailer section's lines while whole ones are buffered:
|
|
2064
2186
|
* `http1TakeLine` and `http1FieldLine` in one loop, each line limited by what
|
|
@@ -2093,11 +2215,29 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2093
2215
|
// line), so the copy, and the head, stay within the limit.
|
|
2094
2216
|
//
|
|
2095
2217
|
// Every offset here is a position in `buf` or the head, both under 2^31
|
|
2096
|
-
// bytes, and `delta` moves one to the other, so the sums below
|
|
2097
|
-
//
|
|
2218
|
+
// bytes, and `delta` moves one to the other, so the sums below never leave
|
|
2219
|
+
// the i32 range. A sum a guard bounds is written plainly; the rest are
|
|
2220
|
+
// taken with `http1Past` and `http1Less`, whose proofs need no bound from
|
|
2221
|
+
// the caller.
|
|
2098
2222
|
const copy: boolean = !trailer
|
|
2099
|
-
|
|
2100
|
-
|
|
2223
|
+
// The input and the head are each at most 2^30 bytes, so `delta` is
|
|
2224
|
+
// within 2^30 either way; the test spells that out for the bounds proof,
|
|
2225
|
+
// and refuses a parser moved by hand.
|
|
2226
|
+
const headLength: i32 = p.headLength
|
|
2227
|
+
const from: i32 = p.start
|
|
2228
|
+
if (headLength < 0 || headLength > 1073741823 || from < 0 || from > 1073741823) {
|
|
2229
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
2230
|
+
}
|
|
2231
|
+
const lead: integer<0, 1073741823> = headLength
|
|
2232
|
+
const fed: integer<0, 1073741823> = from
|
|
2233
|
+
const shifted: i32 = lead + (1073741823 - fed)
|
|
2234
|
+
if (shifted < 0 || shifted > 2147483646) {
|
|
2235
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
2236
|
+
}
|
|
2237
|
+
const shift: integer<0, 2147483646> = shifted
|
|
2238
|
+
const delta: i32 = shift - 1073741823
|
|
2239
|
+
const most: i32 = p.maxHeaderBytes
|
|
2240
|
+
const wideStop: i64 = toI64(p.start) + toI64(most) - toI64(p.sectionBytes) + toI64(2)
|
|
2101
2241
|
let copyStop: i32 = 2147483647
|
|
2102
2242
|
if (wideStop >= toI64(-2147483647) && wideStop < toI64(2147483647)) {
|
|
2103
2243
|
copyStop = toI32(wideStop)
|
|
@@ -2106,51 +2246,61 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2106
2246
|
// passes `growAt`, so most vectors pay one compare for each.
|
|
2107
2247
|
const copyEnd: i32 = copy ? copyStop : -2147483647
|
|
2108
2248
|
let head: u8[] = p.head
|
|
2109
|
-
let growAt: i32 =
|
|
2249
|
+
let growAt: i32 = http1GrowAt(head, delta)
|
|
2110
2250
|
// Bit `k` of `bits` is set when `buf[base + k]` is a control byte or DEL
|
|
2111
|
-
// at or past `i`: the vector last loaded, its lanes before `i`
|
|
2112
|
-
|
|
2251
|
+
// at or past `i`: the vector last loaded, at `base`, its lanes before `i`
|
|
2252
|
+
// cleared. `base` is the start of a vector in `buf`, so its range is
|
|
2253
|
+
// declared for a lane to be added to it, and the next vector is at `next`
|
|
2254
|
+
// or `i`.
|
|
2255
|
+
let base: integer<0, 1073741823> = 0
|
|
2256
|
+
let next: i32 = i
|
|
2113
2257
|
let bits: u32 = 0
|
|
2258
|
+
// A vector at or below `lastWhole` holds sixteen bytes before `stop`.
|
|
2259
|
+
const lastWhole: i32 = http1Less(stop, 16)
|
|
2114
2260
|
while (true) {
|
|
2115
2261
|
// The next control byte at or past `i`, or the end of the bytes so far.
|
|
2116
2262
|
let j: i32 = stop
|
|
2117
2263
|
let seeking: boolean = true
|
|
2118
2264
|
while (seeking) {
|
|
2119
2265
|
if (bits !== 0) {
|
|
2120
|
-
// The lowest set bit, 0 to 15
|
|
2121
|
-
|
|
2266
|
+
// The lowest set bit, 0 to 15, a lane of the vector at `base`, which
|
|
2267
|
+
// is at most `buf.length - 16`: the sum is a position in `buf`.
|
|
2268
|
+
const low: u32 = bits & (toU32(0) - bits)
|
|
2269
|
+
j = base + (31 - toI32(Math.clz32(low)))
|
|
2122
2270
|
seeking = false
|
|
2123
2271
|
} else {
|
|
2124
|
-
const next: i32 = http1Past(base, 16)
|
|
2125
2272
|
const q: i32 = next > i ? next : i
|
|
2126
2273
|
if (q >= stop) {
|
|
2127
2274
|
seeking = false
|
|
2128
|
-
} else if (q >= 0 && q
|
|
2275
|
+
} else if (q >= 0 && q < 1073741824 && q < toI32(buf.length) - 15) {
|
|
2129
2276
|
// The vector may run past `stop` into bytes not yet fed: its lanes
|
|
2130
2277
|
// there are dropped from `bits`, and its copy of them is
|
|
2131
2278
|
// overwritten when they arrive.
|
|
2132
2279
|
const v: u8x16 = loadU8x16(buf, q)
|
|
2280
|
+
base = q
|
|
2281
|
+
next = q + 16
|
|
2133
2282
|
if (q < copyEnd) {
|
|
2134
2283
|
if (q > growAt) {
|
|
2135
2284
|
if (!http1HeadGrow(p, http1Less(http1Past(http1Past(q, delta), 16), p.headLength))) {
|
|
2136
2285
|
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
2137
2286
|
}
|
|
2138
2287
|
head = p.head
|
|
2139
|
-
growAt =
|
|
2288
|
+
growAt = http1GrowAt(head, delta)
|
|
2140
2289
|
}
|
|
2141
2290
|
// `q <= growAt` here, so the vector fits in the head.
|
|
2142
|
-
const to: i32 =
|
|
2291
|
+
const to: i32 = q - 1073741823 + shift
|
|
2143
2292
|
if (to >= 0 && to <= toI32(head.length) - 16) {
|
|
2144
2293
|
store(v, head, to)
|
|
2145
2294
|
}
|
|
2146
2295
|
}
|
|
2147
2296
|
bits = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2297
|
+
if (q > lastWhole) {
|
|
2298
|
+
// `q < stop` here: keep the lanes before `stop`, 1 to 15 of them.
|
|
2299
|
+
const keep: i32 = http1Less(stop, q)
|
|
2300
|
+
if (keep < 16) {
|
|
2301
|
+
bits = bits & ((toU32(1) << toU32(keep)) - toU32(1))
|
|
2302
|
+
}
|
|
2152
2303
|
}
|
|
2153
|
-
base = q
|
|
2154
2304
|
} else {
|
|
2155
2305
|
// Fewer than sixteen bytes are left in `buf`: a byte at a time.
|
|
2156
2306
|
let k: i32 = q
|
|
@@ -2175,7 +2325,7 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2175
2325
|
growAt = http1Less(http1Less(toI32(head.length), 32), delta)
|
|
2176
2326
|
}
|
|
2177
2327
|
j = k
|
|
2178
|
-
|
|
2328
|
+
next = http1Past(k, 1)
|
|
2179
2329
|
seeking = false
|
|
2180
2330
|
}
|
|
2181
2331
|
}
|
|
@@ -2193,18 +2343,19 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2193
2343
|
}
|
|
2194
2344
|
if (lf >= 0) {
|
|
2195
2345
|
const start: i32 = p.start
|
|
2196
|
-
if (lf === start || lf < 1 || lf > toI32(buf.length) || buf[lf - 1] !== 13) {
|
|
2346
|
+
if (lf === start || lf < 1 || lf > 1073741823 || lf >= toI32(buf.length) || buf[lf - 1] !== 13) {
|
|
2197
2347
|
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
2198
2348
|
}
|
|
2199
|
-
// The line, `start` to `lf`, is in `buf
|
|
2200
|
-
|
|
2201
|
-
const
|
|
2202
|
-
|
|
2349
|
+
// The line, `start` to `lf`, is in `buf`, so `after` is at most its
|
|
2350
|
+
// length.
|
|
2351
|
+
const after: i32 = lf + 1
|
|
2352
|
+
const sectionBytes: i64 = toI64(p.sectionBytes) + toI64(after) - toI64(start)
|
|
2353
|
+
if (sectionBytes > toI64(most) + toI64(2)) {
|
|
2203
2354
|
return http1Fail(p, 431, "a line over its limit")
|
|
2204
2355
|
}
|
|
2205
2356
|
// The scan stops at the CR, so the LF after it may be in no vector
|
|
2206
2357
|
// or tail copied: store it, and the head holds each line's CRLF.
|
|
2207
|
-
const at: i32 =
|
|
2358
|
+
const at: i32 = lf - 1073741823 + shift
|
|
2208
2359
|
if (copy && at >= 0 && at < toI32(head.length)) {
|
|
2209
2360
|
head[at] = 10
|
|
2210
2361
|
}
|
|
@@ -2219,19 +2370,20 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2219
2370
|
if (c === 13) {
|
|
2220
2371
|
bits = bits & (bits - toU32(1))
|
|
2221
2372
|
}
|
|
2222
|
-
|
|
2223
|
-
if (sectionBytes > p.maxHeaderBytes) {
|
|
2373
|
+
if (sectionBytes > toI64(most)) {
|
|
2224
2374
|
return http1Fail(p, 431, "a header section over the limit")
|
|
2225
2375
|
}
|
|
2376
|
+
// At most `maxHeaderBytes`, checked just above.
|
|
2377
|
+
p.sectionBytes = toI32(sectionBytes)
|
|
2226
2378
|
if (lf - 1 === start) {
|
|
2227
2379
|
if (trailer) {
|
|
2228
2380
|
p.state = HTTP1_S_MESSAGE_END
|
|
2229
2381
|
return HTTP1_LINE
|
|
2230
2382
|
}
|
|
2231
|
-
p.headLength =
|
|
2383
|
+
p.headLength = after - 1073741823 + shift
|
|
2232
2384
|
return http1EndOfHead(p)
|
|
2233
2385
|
}
|
|
2234
|
-
if (http1FieldLine(p, copy,
|
|
2386
|
+
if (http1FieldLine(p, copy, shift) === HTTP1_ERROR) {
|
|
2235
2387
|
return HTTP1_ERROR
|
|
2236
2388
|
}
|
|
2237
2389
|
p.lineControl = false
|
|
@@ -2244,7 +2396,8 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
2244
2396
|
if (c !== 9) {
|
|
2245
2397
|
p.lineControl = true
|
|
2246
2398
|
}
|
|
2247
|
-
|
|
2399
|
+
// `j < buf.length`, checked above.
|
|
2400
|
+
i = j + 1
|
|
2248
2401
|
bits = bits & (bits - toU32(1))
|
|
2249
2402
|
}
|
|
2250
2403
|
}
|
|
@@ -2275,8 +2428,14 @@ const http1Next = (p: Http1Parser): i32 => {
|
|
|
2275
2428
|
}
|
|
2276
2429
|
// The longest well-formed request line: the method, the target, two
|
|
2277
2430
|
// spaces and "HTTP/1.1".
|
|
2278
|
-
// `maxTarget` is clamped to 2^30,
|
|
2279
|
-
|
|
2431
|
+
// `maxTarget` is clamped to 2^30, and one moved past it by hand is held
|
|
2432
|
+
// there, so the sum is an i32.
|
|
2433
|
+
const target: i32 = p.maxTarget
|
|
2434
|
+
const got: i32 = http1TakeLine(
|
|
2435
|
+
p,
|
|
2436
|
+
target < 1073741824 ? target + (HTTP1_MAX_METHOD + 10) : 1073741824,
|
|
2437
|
+
414
|
|
2438
|
+
)
|
|
2280
2439
|
if (got !== HTTP1_LINE) {
|
|
2281
2440
|
return got
|
|
2282
2441
|
}
|
|
@@ -2521,23 +2680,29 @@ export const http1TryWriteChunk = (out: u8[], at: i32, data: readonly u8[], off:
|
|
|
2521
2680
|
if (at < 0 || at > outLength || need > toU64(toU32(outLength) - toU32(at))) {
|
|
2522
2681
|
return HTTP1_NO_ROOM
|
|
2523
2682
|
}
|
|
2524
|
-
// It fits, so none of the offsets below passes `outLength
|
|
2525
|
-
|
|
2526
|
-
const
|
|
2683
|
+
// It fits, so none of the offsets below passes `outLength`: their u32
|
|
2684
|
+
// sums are exact.
|
|
2685
|
+
const end32: u32 = toU32(at) + toU32(digits)
|
|
2686
|
+
const headEnd32: u32 = end32 + toU32(2)
|
|
2687
|
+
const end: i32 = toI32(end32)
|
|
2688
|
+
const headEnd: i32 = toI32(headEnd32)
|
|
2527
2689
|
// The bytes go first, so a copy that fails leaves `out` untouched. It
|
|
2528
2690
|
// cannot: the window is inside `data` and the chunk fits, both checked.
|
|
2529
2691
|
if (!tryCopyBytes(out, headEnd, data, off, len)) {
|
|
2530
2692
|
return HTTP1_NO_ROOM
|
|
2531
2693
|
}
|
|
2532
2694
|
let v: i32 = len
|
|
2533
|
-
// The digits are written last first, `out[
|
|
2534
|
-
|
|
2695
|
+
// The digits are written last first, `out[j]` each time. `end32` is at
|
|
2696
|
+
// least `digits`, which is at least 1, so `last32` is exact.
|
|
2697
|
+
const last32: u32 = end32 - toU32(1)
|
|
2698
|
+
for (let j: i32 = toI32(last32); j >= at && j >= 0 && j < outLength; j -= 1) {
|
|
2535
2699
|
const d: i32 = v & 15
|
|
2536
|
-
out[
|
|
2700
|
+
out[j] = toU8(d < 10 ? 48 + d : 87 + d)
|
|
2537
2701
|
v = v >> 4
|
|
2538
2702
|
}
|
|
2539
2703
|
http1PutCrlf(out, end)
|
|
2540
|
-
|
|
2704
|
+
const tail32: u32 = headEnd32 + toU32(len)
|
|
2705
|
+
return http1PutCrlf(out, toI32(tail32))
|
|
2541
2706
|
}
|
|
2542
2707
|
|
|
2543
2708
|
/**
|
|
@@ -2701,32 +2866,49 @@ export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
|
|
|
2701
2866
|
k = at > -n ? http1Less(0, at) : n
|
|
2702
2867
|
}
|
|
2703
2868
|
let j: i32 = http1Past(at, k)
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
j
|
|
2707
|
-
|
|
2869
|
+
// Both start at 0 or past it and only climb, so the test is made once.
|
|
2870
|
+
if (k >= 0 && j >= 0) {
|
|
2871
|
+
while (k < n && j < toI32(out.length)) {
|
|
2872
|
+
out[j] = toU8(text.charCodeAt(k))
|
|
2873
|
+
j += 1
|
|
2874
|
+
k += 1
|
|
2875
|
+
}
|
|
2708
2876
|
}
|
|
2709
2877
|
return http1Past(at, n)
|
|
2710
2878
|
}
|
|
2711
2879
|
|
|
2712
2880
|
/**
|
|
2713
|
-
* `at + n
|
|
2714
|
-
*
|
|
2715
|
-
* does for them.
|
|
2881
|
+
* `at + n`, held at the i32 range's end where the sum would leave it: the
|
|
2882
|
+
* writers' callers measure the head first and the parser adds only offsets
|
|
2883
|
+
* in buffers under 2^30 bytes, so it never does for them. The sum is taken
|
|
2884
|
+
* in i64, where two i32s cannot overflow, and clamped back: two selects,
|
|
2885
|
+
* where a branch on each sign would cost the scan a compare per offset.
|
|
2716
2886
|
*/
|
|
2717
|
-
const http1Past = (at: i32, n: i32): i32 =>
|
|
2887
|
+
const http1Past = (at: i32, n: i32): i32 => {
|
|
2888
|
+
const s: i64 = toI64(at) + toI64(n)
|
|
2889
|
+
const hi: i64 = s > toI64(2147483647) ? toI64(2147483647) : s
|
|
2890
|
+
const lo: i64 = hi < toI64(-2147483648) ? toI64(-2147483648) : hi
|
|
2891
|
+
return toI32(lo)
|
|
2892
|
+
}
|
|
2718
2893
|
|
|
2719
2894
|
/**
|
|
2720
|
-
* `at - n`,
|
|
2895
|
+
* `at - n`, held at the i32 range's end where the difference would leave it:
|
|
2721
2896
|
* the parser takes it only between offsets in one buffer, so it never does.
|
|
2897
|
+
* Taken in i64 and clamped, as `http1Past` takes a sum.
|
|
2722
2898
|
*/
|
|
2723
|
-
const http1Less = (at: i32, n: i32): i32 =>
|
|
2899
|
+
const http1Less = (at: i32, n: i32): i32 => {
|
|
2900
|
+
const s: i64 = toI64(at) - toI64(n)
|
|
2901
|
+
const hi: i64 = s > toI64(2147483647) ? toI64(2147483647) : s
|
|
2902
|
+
const lo: i64 = hi < toI64(-2147483648) ? toI64(-2147483648) : hi
|
|
2903
|
+
return toI32(lo)
|
|
2904
|
+
}
|
|
2724
2905
|
|
|
2725
2906
|
/** Writes CR LF at `out[at ..)`, likewise: two stores, where `http1PutText` walks a string. */
|
|
2726
2907
|
const http1PutCrlf = (out: u8[], at: i32): i32 => {
|
|
2727
2908
|
if (at >= 0 && at < toI32(out.length) - 1) {
|
|
2728
2909
|
out[at] = 13
|
|
2729
2910
|
out[at + 1] = 10
|
|
2911
|
+
return at + 2
|
|
2730
2912
|
}
|
|
2731
2913
|
return http1Past(at, 2)
|
|
2732
2914
|
}
|
|
@@ -2736,6 +2918,7 @@ const http1PutColonSpace = (out: u8[], at: i32): i32 => {
|
|
|
2736
2918
|
if (at >= 0 && at < toI32(out.length) - 1) {
|
|
2737
2919
|
out[at] = 58
|
|
2738
2920
|
out[at + 1] = 32
|
|
2921
|
+
return at + 2
|
|
2739
2922
|
}
|
|
2740
2923
|
return http1Past(at, 2)
|
|
2741
2924
|
}
|
|
@@ -2743,12 +2926,16 @@ const http1PutColonSpace = (out: u8[], at: i32): i32 => {
|
|
|
2743
2926
|
/** Writes `bytes` at `out[at ..)`, likewise, as one copy when it fits. */
|
|
2744
2927
|
const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
|
|
2745
2928
|
const n: i32 = toI32(bytes.length)
|
|
2746
|
-
if (
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2929
|
+
if (tryCopyBytes(out, at, bytes, 0, n)) {
|
|
2930
|
+
// The copy fit, so `at` and `n` are non-negative i32s whose u32 sum is
|
|
2931
|
+
// exact: at most `out.length`.
|
|
2932
|
+
const end: u32 = toU32(at) + toU32(n)
|
|
2933
|
+
return toI32(end)
|
|
2934
|
+
}
|
|
2935
|
+
let j: i32 = at
|
|
2936
|
+
for (let k: i32 = 0; k < n && k < toI32(bytes.length) && j >= 0 && j < toI32(out.length); k += 1) {
|
|
2937
|
+
out[j] = bytes[k]
|
|
2938
|
+
j += 1
|
|
2752
2939
|
}
|
|
2753
2940
|
return http1Past(at, n)
|
|
2754
2941
|
}
|
|
@@ -2763,7 +2950,10 @@ const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
|
|
|
2763
2950
|
const http1PutFixed = (out: u8[], at: i32, limit: i32, v: u8x16, text: string): i32 => {
|
|
2764
2951
|
if (at >= 0 && limit >= 16 && at <= limit - 16 && at <= toI32(out.length) - 16) {
|
|
2765
2952
|
store(v, out, at)
|
|
2766
|
-
|
|
2953
|
+
// `at` is at most `out.length - 16` and `text` at most sixteen bytes, so
|
|
2954
|
+
// the u32 sum is exact.
|
|
2955
|
+
const end: u32 = toU32(at) + toU32(text.length)
|
|
2956
|
+
return toI32(end)
|
|
2767
2957
|
}
|
|
2768
2958
|
return http1PutText(out, at, text)
|
|
2769
2959
|
}
|
|
@@ -2785,7 +2975,7 @@ const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 =>
|
|
|
2785
2975
|
out,
|
|
2786
2976
|
at
|
|
2787
2977
|
)
|
|
2788
|
-
return
|
|
2978
|
+
return at + 13
|
|
2789
2979
|
}
|
|
2790
2980
|
const end: i32 = http1PutText(out, at, "HTTP/1.1 ")
|
|
2791
2981
|
if (end >= 0 && end < toI32(out.length) - 3) {
|