@amritk/nish-aarch64-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
package/std/net/http1-codec.ts
CHANGED
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
*
|
|
82
82
|
* Written from RFC 9112 and RFC 9110, not ported from another implementation.
|
|
83
83
|
*/
|
|
84
|
-
import {
|
|
84
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
85
85
|
import { urlIsHostPort } from "nish/net/url"
|
|
86
86
|
import {
|
|
87
87
|
and,
|
|
@@ -456,20 +456,45 @@ 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
|
+
if (!this.tryFeed(data, off, len)) {
|
|
460
|
+
panic("Http1Parser: the window is outside the buffer")
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* `feed` that answers false, taking nothing and leaving the parser as it
|
|
466
|
+
* was, where `feed` panics: a window outside `data`. A caller a strict
|
|
467
|
+
* module reaches uses this one.
|
|
468
|
+
*
|
|
469
|
+
* @param data the bytes the socket gave, which the parser copies and does not keep
|
|
470
|
+
* @param off where in `data` the new bytes start
|
|
471
|
+
* @param len how many bytes from `off` to append
|
|
472
|
+
* @returns false when the window lies outside `data`; true otherwise, the
|
|
473
|
+
* bytes taken, dropped (a closed or refused parser) or refused with a 413
|
|
474
|
+
*/
|
|
475
|
+
tryFeed(data: u8[], off: i32, len: i32): boolean {
|
|
459
476
|
const size: i32 = toI32(data.length)
|
|
460
477
|
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
461
|
-
|
|
478
|
+
return false
|
|
462
479
|
}
|
|
463
480
|
if (this.state === HTTP1_S_CLOSED || this.state === HTTP1_S_ERROR) {
|
|
464
|
-
return
|
|
481
|
+
return true
|
|
482
|
+
}
|
|
483
|
+
if (!http1Reserve(this, len)) {
|
|
484
|
+
return true
|
|
465
485
|
}
|
|
466
|
-
http1Reserve(this, len)
|
|
467
486
|
const buf: u8[] = this.buf
|
|
468
487
|
const at: i32 = this.end
|
|
469
488
|
// One move: http1Reserve made room for `len` bytes at `at`, and the
|
|
470
|
-
// window was checked above
|
|
471
|
-
|
|
472
|
-
|
|
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))
|
|
497
|
+
return true
|
|
473
498
|
}
|
|
474
499
|
|
|
475
500
|
/**
|
|
@@ -489,19 +514,16 @@ export class Http1Parser {
|
|
|
489
514
|
* @returns the first such field's value with its surrounding whitespace trimmed, or `null` when the request has none
|
|
490
515
|
*/
|
|
491
516
|
header(name: string): string | null {
|
|
492
|
-
const
|
|
493
|
-
|
|
517
|
+
const spans: i32[] = this.spans
|
|
518
|
+
const k: i32 = http1FieldSpans(this, name)
|
|
519
|
+
if (k < 0 || k >= toI32(spans.length) - 3) {
|
|
494
520
|
return null
|
|
495
521
|
}
|
|
522
|
+
const i: i32 = k >>> 2
|
|
496
523
|
if (this.keepText && i < toI32(this.values.length)) {
|
|
497
524
|
return this.values[i]
|
|
498
525
|
}
|
|
499
|
-
|
|
500
|
-
const at: i32 = 4 * i
|
|
501
|
-
if (at + 3 >= toI32(spans.length)) {
|
|
502
|
-
return null
|
|
503
|
-
}
|
|
504
|
-
return http1Text(this.head, spans[at + 2], spans[at + 3], false)
|
|
526
|
+
return http1Text(this.head, spans[k + 2], spans[k + 3], false)
|
|
505
527
|
}
|
|
506
528
|
|
|
507
529
|
/**
|
|
@@ -510,14 +532,8 @@ export class Http1Parser {
|
|
|
510
532
|
* @returns which field it is, so its spans are `spans[4 * i ..]`, or -1 when the request has none
|
|
511
533
|
*/
|
|
512
534
|
headerIndex(name: string): i32 {
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
for (let i: i32 = 0; i < count && 4 * i + 1 < toI32(spans.length); i += 1) {
|
|
516
|
-
if (http1HeadIs(this.head, spans[4 * i], spans[4 * i + 1], name, true)) {
|
|
517
|
-
return i
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
return -1
|
|
535
|
+
const k: i32 = http1FieldSpans(this, name)
|
|
536
|
+
return k < 0 ? -1 : k >>> 2
|
|
521
537
|
}
|
|
522
538
|
|
|
523
539
|
/**
|
|
@@ -527,12 +543,12 @@ export class Http1Parser {
|
|
|
527
543
|
* @returns true when that field exists and its trimmed value is `value`, with nothing allocated
|
|
528
544
|
*/
|
|
529
545
|
headerIs(name: string, value: string): boolean {
|
|
530
|
-
const i: i32 = this.headerIndex(name)
|
|
531
546
|
const spans: i32[] = this.spans
|
|
532
|
-
|
|
547
|
+
const k: i32 = http1FieldSpans(this, name)
|
|
548
|
+
if (k < 0 || k >= toI32(spans.length) - 3) {
|
|
533
549
|
return false
|
|
534
550
|
}
|
|
535
|
-
return http1HeadIs(this.head, spans[
|
|
551
|
+
return http1HeadIs(this.head, spans[k + 2], spans[k + 3], value, false)
|
|
536
552
|
}
|
|
537
553
|
|
|
538
554
|
/**
|
|
@@ -565,19 +581,23 @@ export class Http1Parser {
|
|
|
565
581
|
return false
|
|
566
582
|
}
|
|
567
583
|
const spans: i32[] = this.spans
|
|
584
|
+
const n: i32 = toI32(spans.length)
|
|
568
585
|
let found: i32 = -1
|
|
569
|
-
|
|
570
|
-
|
|
586
|
+
// Field `i` has its spans at `spans[k ..]`, `k` four times `i`.
|
|
587
|
+
let k: i32 = 0
|
|
588
|
+
for (let i: i32 = 0; i < this.headerCount && k < n - 3; i += 1) {
|
|
589
|
+
if (http1HeadIs(this.head, spans[k], spans[k + 1], "upgrade", false)) {
|
|
571
590
|
if (found >= 0) {
|
|
572
591
|
return false
|
|
573
592
|
}
|
|
574
|
-
found =
|
|
593
|
+
found = k
|
|
575
594
|
}
|
|
595
|
+
k += 4
|
|
576
596
|
}
|
|
577
|
-
if (found < 0 ||
|
|
597
|
+
if (found < 0 || found >= n - 3) {
|
|
578
598
|
return false
|
|
579
599
|
}
|
|
580
|
-
return http1HeadIs(this.head, spans[
|
|
600
|
+
return http1HeadIs(this.head, spans[found + 2], spans[found + 3], protocol, true)
|
|
581
601
|
}
|
|
582
602
|
|
|
583
603
|
/**
|
|
@@ -597,7 +617,8 @@ export class Http1Parser {
|
|
|
597
617
|
* @returns the count of unread input bytes, at `buf[start ..]`
|
|
598
618
|
*/
|
|
599
619
|
buffered(): i32 {
|
|
600
|
-
|
|
620
|
+
// `start` never passes `end`, and both are offsets into `buf`.
|
|
621
|
+
return toI32(toU32(this.end) - toU32(this.start))
|
|
601
622
|
}
|
|
602
623
|
|
|
603
624
|
/**
|
|
@@ -605,16 +626,12 @@ export class Http1Parser {
|
|
|
605
626
|
* @returns a copy the caller owns of the bytes that belong to the next protocol, empty when none arrived yet
|
|
606
627
|
*/
|
|
607
628
|
upgradeBytes(): u8[] {
|
|
608
|
-
let n: i32 = this.end - this.start
|
|
609
|
-
if (n < 0) {
|
|
610
|
-
n = 0
|
|
611
|
-
}
|
|
612
|
-
const out: u8[] = new Array<u8>(n)
|
|
613
|
-
const outLength: i32 = toI32(out.length)
|
|
614
629
|
const buf: u8[] = this.buf
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
630
|
+
const to: i32 = this.end
|
|
631
|
+
// Built by `push`, so no length is computed for `new Array`.
|
|
632
|
+
const out: u8[] = []
|
|
633
|
+
for (let k: i32 = this.start; k >= 0 && k < toI32(buf.length) && k < to; k += 1) {
|
|
634
|
+
out.push(buf[k])
|
|
618
635
|
}
|
|
619
636
|
return out
|
|
620
637
|
}
|
|
@@ -656,35 +673,94 @@ export const HTTP1_FIRST_HEAD: i32 = 256
|
|
|
656
673
|
/** The spans a parser makes at its first field line: four per field, room for sixteen. */
|
|
657
674
|
const HTTP1_FIRST_SPANS: i32 = 64
|
|
658
675
|
|
|
659
|
-
/**
|
|
660
|
-
const
|
|
676
|
+
/** The largest input buffer a parser grows to: 2^30 bytes. */
|
|
677
|
+
const HTTP1_MOST_INPUT: i32 = 1073741824
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Makes room for `len` more bytes at `p.end`, moving the live bytes down
|
|
681
|
+
* first. False, with the parser refused, when that would take a buffer past
|
|
682
|
+
* `HTTP1_MOST_INPUT` (a 413) or the live bytes are not a window of `buf` (a
|
|
683
|
+
* 400).
|
|
684
|
+
*/
|
|
685
|
+
const http1Reserve = (p: Http1Parser, len: i32): boolean => {
|
|
661
686
|
const old: u8[] = p.buf
|
|
662
|
-
const capacity:
|
|
663
|
-
|
|
664
|
-
|
|
687
|
+
const capacity: u32 = toU32(old.length)
|
|
688
|
+
const from: i32 = p.start
|
|
689
|
+
if (len < 0 || from < 0 || p.end < from || p.end > toI32(capacity)) {
|
|
690
|
+
// The parser keeps `0 <= start <= end <= buf.length`. The fields are
|
|
691
|
+
// public, so a caller may have moved them: such a parser is refused with
|
|
692
|
+
// a 400 rather than read outside `buf`.
|
|
693
|
+
http1Fail(p, 400, "the input window is out of order")
|
|
694
|
+
return false
|
|
665
695
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
696
|
+
// `len` is non-negative and `start <= end <= buf.length`, checked above, so
|
|
697
|
+
// these unsigned counts are the real ones and add up without wrapping in u32.
|
|
698
|
+
const want: u32 = toU32(len)
|
|
699
|
+
const end: u32 = toU32(p.end)
|
|
700
|
+
if (want <= capacity - end) {
|
|
701
|
+
return true
|
|
702
|
+
}
|
|
703
|
+
const live: u32 = end - toU32(from)
|
|
704
|
+
if (want > toU32(HTTP1_MOST_INPUT) - live) {
|
|
705
|
+
http1Fail(p, 413, "more than 2^30 bytes buffered")
|
|
706
|
+
return false
|
|
707
|
+
}
|
|
708
|
+
let size: u32 = capacity < toU32(HTTP1_FIRST_INPUT) ? toU32(HTTP1_FIRST_INPUT) : capacity
|
|
709
|
+
while (size - live < want) {
|
|
672
710
|
size = size * 2
|
|
673
711
|
}
|
|
674
712
|
// A fresh array when it has to grow, and the same one otherwise: either way
|
|
675
713
|
// the live bytes move to the front, and every index into them moves too.
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
714
|
+
// One move, overlapping when the buffer is the same one; the offsets keep it
|
|
715
|
+
// in range.
|
|
716
|
+
const buf: u8[] = size === capacity ? old : http1Grown(old, from, live, size)
|
|
717
|
+
if (size === capacity && !tryCopyBytes(buf, 0, old, from, toI32(live))) {
|
|
718
|
+
// Never false: the window was checked above. Were it, the parser is
|
|
719
|
+
// refused rather than left with bytes it did not move.
|
|
720
|
+
http1Fail(p, 400, "the input window is out of order")
|
|
721
|
+
return false
|
|
681
722
|
}
|
|
682
723
|
p.buf = buf
|
|
683
|
-
p.lineStart
|
|
684
|
-
p.lineEnd
|
|
685
|
-
p.scan
|
|
724
|
+
p.lineStart = toI32(toU32(p.lineStart) - toU32(from))
|
|
725
|
+
p.lineEnd = toI32(toU32(p.lineEnd) - toU32(from))
|
|
726
|
+
p.scan = toI32(toU32(p.scan) - toU32(from))
|
|
686
727
|
p.start = 0
|
|
687
|
-
p.end = live
|
|
728
|
+
p.end = toI32(live)
|
|
729
|
+
return true
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* A buffer of `size` bytes whose first `live` are `old[from ..]`. A size past
|
|
734
|
+
* a `u16` is built by `push`, so no unchecked length reaches `new Array`; the
|
|
735
|
+
* first input buffer, `HTTP1_FIRST_INPUT` bytes, is the common one and copies
|
|
736
|
+
* nothing.
|
|
737
|
+
*/
|
|
738
|
+
const http1Grown = (old: u8[], from: i32, live: u32, size: u32): u8[] => {
|
|
739
|
+
if (old.length === 0 && size === toU32(HTTP1_FIRST_INPUT)) {
|
|
740
|
+
return new Array<u8>(HTTP1_FIRST_INPUT)
|
|
741
|
+
}
|
|
742
|
+
if (old.length === 0 && size === toU32(HTTP1_FIRST_HEAD)) {
|
|
743
|
+
return new Array<u8>(HTTP1_FIRST_HEAD)
|
|
744
|
+
}
|
|
745
|
+
const to: i32 = toI32(toU32(from) + live)
|
|
746
|
+
if (size <= toU32(65535)) {
|
|
747
|
+
// A size that fits a `u16` is one allocation whose length needs no check.
|
|
748
|
+
const sized: u8[] = new Array<u8>(toU16(toI32(size)))
|
|
749
|
+
let at: i32 = 0
|
|
750
|
+
for (let k: i32 = from; k >= 0 && k < toI32(old.length) && k < to && at < toI32(sized.length); k += 1) {
|
|
751
|
+
sized[at] = old[k]
|
|
752
|
+
at += 1
|
|
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)
|
|
762
|
+
}
|
|
763
|
+
return buf
|
|
688
764
|
}
|
|
689
765
|
|
|
690
766
|
/** Refuses the input with `status`. Answers `HTTP1_ERROR`, so a caller can `return` it. */
|
|
@@ -696,6 +772,25 @@ const http1Fail = (p: Http1Parser, status: i32, reason: string): i32 => {
|
|
|
696
772
|
return HTTP1_ERROR
|
|
697
773
|
}
|
|
698
774
|
|
|
775
|
+
/**
|
|
776
|
+
* Where the first field named `name` (any case) has its spans: `k` such that
|
|
777
|
+
* the field is `spans[k .. k + 4)`, four times its index, or -1 when the
|
|
778
|
+
* request has none.
|
|
779
|
+
*/
|
|
780
|
+
const http1FieldSpans = (p: Http1Parser, name: string): i32 => {
|
|
781
|
+
const spans: i32[] = p.spans
|
|
782
|
+
const count: i32 = p.headerCount
|
|
783
|
+
const n: i32 = toI32(spans.length)
|
|
784
|
+
let k: i32 = 0
|
|
785
|
+
for (let i: i32 = 0; i < count && k < n - 3; i += 1) {
|
|
786
|
+
if (http1HeadIs(p.head, spans[k], spans[k + 1], name, true)) {
|
|
787
|
+
return k
|
|
788
|
+
}
|
|
789
|
+
k += 4
|
|
790
|
+
}
|
|
791
|
+
return -1
|
|
792
|
+
}
|
|
793
|
+
|
|
699
794
|
/** Clears the per-request fields and waits for a request line. */
|
|
700
795
|
const http1StartRequest = (p: Http1Parser): void => {
|
|
701
796
|
p.state = HTTP1_S_REQUEST_LINE
|
|
@@ -746,27 +841,35 @@ const HTTP1_LINE: i32 = -1
|
|
|
746
841
|
const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
747
842
|
const buf: u8[] = p.buf
|
|
748
843
|
const end: i32 = p.end
|
|
749
|
-
|
|
750
|
-
if (
|
|
844
|
+
const from: i32 = p.start
|
|
845
|
+
if (from < 0 || end < from) {
|
|
846
|
+
// The parser keeps `0 <= start <= end`. The fields are public, so a caller may
|
|
847
|
+
// have moved them: such a parser is refused with a 400 rather than read
|
|
848
|
+
// outside `buf`.
|
|
849
|
+
return http1Fail(p, 400, "the input window is out of order")
|
|
850
|
+
}
|
|
851
|
+
let i: i32 = from
|
|
852
|
+
if (p.scan > from) {
|
|
751
853
|
i = p.scan
|
|
752
854
|
} else {
|
|
753
855
|
p.lineControl = false
|
|
754
856
|
}
|
|
755
857
|
const stop: i32 = end < toI32(buf.length) ? end : toI32(buf.length)
|
|
756
|
-
|
|
757
|
-
i = 0
|
|
758
|
-
}
|
|
759
|
-
while (i < stop) {
|
|
858
|
+
while (i >= 0 && i < stop && i < toI32(buf.length)) {
|
|
760
859
|
// Most bytes are printable: sixteen at a time while a whole vector is,
|
|
761
860
|
// and the vector that is not answers where its first control byte is.
|
|
762
861
|
// The bytes after a control byte, and a tail shorter than a vector, go
|
|
763
862
|
// one at a time.
|
|
764
863
|
let found: boolean = false
|
|
765
|
-
while (!found && i >= 0 && i <=
|
|
864
|
+
while (!found && i >= 0 && i <= toI32(buf.length) - 16 && i + 16 <= stop) {
|
|
766
865
|
const v: u8x16 = loadU8x16(buf, i)
|
|
767
|
-
const bad:
|
|
866
|
+
const bad: u32 = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
|
|
768
867
|
if (bad !== 0) {
|
|
769
|
-
|
|
868
|
+
// The lowest set lane, 0 to 15: sixteen lanes set at most bit 15.
|
|
869
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
|
|
870
|
+
if (lane >= 0 && lane < 16) {
|
|
871
|
+
i += lane
|
|
872
|
+
}
|
|
770
873
|
found = true
|
|
771
874
|
} else {
|
|
772
875
|
i += 16
|
|
@@ -778,38 +881,39 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
778
881
|
if (i < 0 || i >= stop || i >= toI32(buf.length)) {
|
|
779
882
|
break
|
|
780
883
|
}
|
|
781
|
-
|
|
782
|
-
if (c === 13 && i + 1 < stop && i
|
|
884
|
+
const c: i32 = toI32(buf[i])
|
|
885
|
+
if (c === 13 && i + 1 < stop && i < toI32(buf.length) - 1 && buf[i + 1] === 10) {
|
|
783
886
|
// CRLF, the end of almost every line: its LF is the next byte, so it
|
|
784
887
|
// is taken here rather than by another trip through the scan.
|
|
785
|
-
i
|
|
786
|
-
c = 10
|
|
787
|
-
}
|
|
788
|
-
if (c === 10) {
|
|
789
|
-
if (i === p.start || buf[i - 1] !== 13) {
|
|
790
|
-
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
791
|
-
}
|
|
792
|
-
if (i - 1 - p.start > limit) {
|
|
888
|
+
if (i - from > limit) {
|
|
793
889
|
return http1Fail(p, overStatus, "a line over its limit")
|
|
794
890
|
}
|
|
795
|
-
p.lineStart =
|
|
796
|
-
p.lineEnd = i
|
|
797
|
-
p.start = i +
|
|
798
|
-
p.scan = i +
|
|
891
|
+
p.lineStart = from
|
|
892
|
+
p.lineEnd = i
|
|
893
|
+
p.start = i + 2
|
|
894
|
+
p.scan = i + 2
|
|
799
895
|
return HTTP1_LINE
|
|
800
896
|
}
|
|
897
|
+
if (c === 10) {
|
|
898
|
+
// A CR before it would have ended the line above, or waited at the end
|
|
899
|
+
// of the bytes for this one.
|
|
900
|
+
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
901
|
+
}
|
|
801
902
|
if (c === 13 && i + 1 >= stop) {
|
|
802
903
|
// A CR at the end of the bytes so far: whether it ends the line is
|
|
803
904
|
// the next byte's to say, so the next call looks at it again.
|
|
804
905
|
break
|
|
805
906
|
}
|
|
806
|
-
if (c !== 9
|
|
907
|
+
if (c !== 9) {
|
|
908
|
+
// A control byte, or a CR with no LF after it.
|
|
807
909
|
p.lineControl = true
|
|
808
910
|
}
|
|
809
911
|
i += 1
|
|
810
912
|
}
|
|
811
913
|
p.scan = i
|
|
812
|
-
|
|
914
|
+
// `0 <= from <= end`, checked above, so the u32 difference is the count
|
|
915
|
+
// held; the limits are small constants.
|
|
916
|
+
if (toU32(end) - toU32(from) > toU32(limit) + toU32(1)) {
|
|
813
917
|
return http1Fail(p, overStatus, "a line over its limit")
|
|
814
918
|
}
|
|
815
919
|
return HTTP1_NEED_MORE
|
|
@@ -874,20 +978,24 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
|
874
978
|
let k: i32 = k0
|
|
875
979
|
while (k >= 0 && k < stop && k <= toI32(head.length) - 16) {
|
|
876
980
|
const v: u8x16 = loadU8x16(head, k)
|
|
877
|
-
const bad:
|
|
981
|
+
const bad: u32 = toU32(http1NonTokenLanes(v))
|
|
878
982
|
let run: i32 = 16
|
|
879
983
|
if (bad !== 0) {
|
|
880
|
-
|
|
984
|
+
// The lowest set lane, 0 to 15.
|
|
985
|
+
run = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
|
|
881
986
|
}
|
|
882
|
-
|
|
883
|
-
|
|
987
|
+
// `0 <= k < stop`, so the u32 difference is the bytes left.
|
|
988
|
+
const left: i32 = toI32(toU32(stop) - toU32(k))
|
|
989
|
+
if (run > left) {
|
|
990
|
+
run = left
|
|
884
991
|
}
|
|
885
992
|
const upper: mask8x16 = and(
|
|
886
993
|
lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)),
|
|
887
994
|
lt(lanes, splatU8x16(toU8(run)))
|
|
888
995
|
)
|
|
889
996
|
store(select(upper, or(v, splatU8x16(32)), v), head, k)
|
|
890
|
-
|
|
997
|
+
// `run` is at most the bytes left before `stop`, so the sum fits.
|
|
998
|
+
k = toI32(toU32(k) + toU32(run))
|
|
891
999
|
if (run < 16) {
|
|
892
1000
|
return k
|
|
893
1001
|
}
|
|
@@ -947,11 +1055,14 @@ export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
|
|
|
947
1055
|
*/
|
|
948
1056
|
export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
|
|
949
1057
|
const n: i32 = toI32(word.length)
|
|
950
|
-
|
|
1058
|
+
// `0 <= a <= b`, so the u32 difference is the slice's length.
|
|
1059
|
+
if (a < 0 || b < a || b > toI32(text.length) || toI32(toU32(b) - toU32(a)) !== n) {
|
|
951
1060
|
return false
|
|
952
1061
|
}
|
|
953
|
-
|
|
954
|
-
|
|
1062
|
+
let j: i32 = a
|
|
1063
|
+
for (let k: i32 = 0; k < n && j >= 0 && j < b; k += 1) {
|
|
1064
|
+
let c: i32 = toI32(text.charCodeAt(j))
|
|
1065
|
+
j += 1
|
|
955
1066
|
if (c >= 65 && c <= 90) {
|
|
956
1067
|
c += 32
|
|
957
1068
|
}
|
|
@@ -969,20 +1080,25 @@ export const http1SameWord = (text: string, a: i32, b: i32, word: string): boole
|
|
|
969
1080
|
*/
|
|
970
1081
|
const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean): boolean => {
|
|
971
1082
|
const n: i32 = toI32(word.length)
|
|
972
|
-
|
|
1083
|
+
// `0 <= a <= b`, so the u32 difference is the slice's length.
|
|
1084
|
+
if (a < 0 || b < a || b > toI32(bytes.length) || toI32(toU32(b) - toU32(a)) !== n) {
|
|
973
1085
|
return false
|
|
974
1086
|
}
|
|
1087
|
+
// `j` walks `bytes[a .. b)` beside `k`: the lengths are equal.
|
|
1088
|
+
let j: i32 = a
|
|
975
1089
|
if (!fold) {
|
|
976
|
-
for (let k: i32 = 0; k < n; k += 1) {
|
|
977
|
-
if (toI32(word.charCodeAt(k)) !== toI32(bytes[
|
|
1090
|
+
for (let k: i32 = 0; k < n && j >= 0 && j < toI32(bytes.length); k += 1) {
|
|
1091
|
+
if (toI32(word.charCodeAt(k)) !== toI32(bytes[j])) {
|
|
978
1092
|
return false
|
|
979
1093
|
}
|
|
1094
|
+
j += 1
|
|
980
1095
|
}
|
|
981
1096
|
return true
|
|
982
1097
|
}
|
|
983
|
-
for (let k: i32 = 0; k < n; k += 1) {
|
|
1098
|
+
for (let k: i32 = 0; k < n && j >= 0 && j < toI32(bytes.length); k += 1) {
|
|
984
1099
|
let c: i32 = toI32(word.charCodeAt(k))
|
|
985
|
-
let d: i32 = toI32(bytes[
|
|
1100
|
+
let d: i32 = toI32(bytes[j])
|
|
1101
|
+
j += 1
|
|
986
1102
|
if (c >= 65 && c <= 90) {
|
|
987
1103
|
c += 32
|
|
988
1104
|
}
|
|
@@ -1001,27 +1117,28 @@ const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean):
|
|
|
1001
1117
|
* `p.headLength`, keeping every byte already there, those `http1Section` has
|
|
1002
1118
|
* copied past `headLength` for a line not yet whole included: the rare path
|
|
1003
1119
|
* of `http1HeadAppend` and of the section scan's copy, apart so that the
|
|
1004
|
-
* common one stays small enough to inline.
|
|
1120
|
+
* common one stays small enough to inline. It answers false, and grows
|
|
1121
|
+
* nothing, when the head would pass 2^30 bytes: the caller refuses it.
|
|
1005
1122
|
*/
|
|
1006
|
-
const http1HeadGrow = (p: Http1Parser, n: i32):
|
|
1007
|
-
const
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1123
|
+
const http1HeadGrow = (p: Http1Parser, n: i32): boolean => {
|
|
1124
|
+
const old: u8[] = p.head
|
|
1125
|
+
const capacity: u32 = toU32(old.length)
|
|
1126
|
+
// u32 throughout: each term is at most 2^30 once checked, so no sum wraps.
|
|
1127
|
+
const at: u32 = toU32(p.headLength)
|
|
1128
|
+
const more: u32 = n < 0 ? toU32(0) : toU32(n)
|
|
1129
|
+
const most: u32 = toU32(HTTP1_MOST_INPUT) - toU32(16)
|
|
1130
|
+
if (at > most || more > most - at) {
|
|
1131
|
+
return false
|
|
1011
1132
|
}
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
}
|
|
1133
|
+
const want: u32 = at + more + toU32(16)
|
|
1134
|
+
let size: u32 = capacity < toU32(HTTP1_FIRST_HEAD) ? toU32(HTTP1_FIRST_HEAD) : capacity
|
|
1135
|
+
while (size < want) {
|
|
1016
1136
|
size = size * 2
|
|
1017
1137
|
}
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
const kept: i32 = toI32(old.length) < size ? toI32(old.length) : size
|
|
1021
|
-
if (kept > 0) {
|
|
1022
|
-
copyBytes(grown, 0, old, 0, kept)
|
|
1138
|
+
if (size !== capacity) {
|
|
1139
|
+
p.head = http1Grown(old, 0, capacity, size)
|
|
1023
1140
|
}
|
|
1024
|
-
|
|
1141
|
+
return true
|
|
1025
1142
|
}
|
|
1026
1143
|
|
|
1027
1144
|
/**
|
|
@@ -1033,26 +1150,38 @@ const http1HeadGrow = (p: Http1Parser, n: i32): void => {
|
|
|
1033
1150
|
* last line, so a short line is copied as whole vectors and a vector load at
|
|
1034
1151
|
* any byte of the head is in bounds: the name and value compares read one. A
|
|
1035
1152
|
* vector's lanes past the line write bytes past `headLength`, which nothing
|
|
1036
|
-
* reads until the section scan overwrites them.
|
|
1153
|
+
* reads until the section scan overwrites them. It answers -1, with the
|
|
1154
|
+
* parser refused, when the line or the head's length is out of order or the
|
|
1155
|
+
* line does not fit: the caller returns `HTTP1_ERROR` then.
|
|
1037
1156
|
*/
|
|
1038
1157
|
const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
1039
|
-
const n: i32 = to - from
|
|
1040
1158
|
const at: i32 = p.headLength
|
|
1159
|
+
if (from < 0 || to < from || at < 0) {
|
|
1160
|
+
// `http1RequestLine` checks the line and grows the head first, so this
|
|
1161
|
+
// refuses only a parser whose public fields were moved out of order.
|
|
1162
|
+
http1Fail(p, 400, "the request line is outside the buffer")
|
|
1163
|
+
return -1
|
|
1164
|
+
}
|
|
1165
|
+
// `0 <= from <= to`, so the u32 difference is the line's length.
|
|
1166
|
+
const n: i32 = toI32(toU32(to) - toU32(from))
|
|
1041
1167
|
const head: u8[] = p.head
|
|
1042
1168
|
const buf: u8[] = p.buf
|
|
1043
1169
|
// A line of up to 32 bytes is one or two vector stores; a longer one, or
|
|
1044
1170
|
// one that ends within a vector of `buf`'s end, is one `copyBytes`, which
|
|
1045
1171
|
// outruns a loop of stores at a Cookie or User-Agent line's length.
|
|
1046
|
-
|
|
1047
|
-
if (n <= 32 && from >= 0 && from <= toI32(buf.length) - room && at <= toI32(head.length) - room) {
|
|
1172
|
+
if (n <= 16 && from <= toI32(buf.length) - 16 && at <= toI32(head.length) - 16) {
|
|
1048
1173
|
store(loadU8x16(buf, from), head, at)
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
} else if (
|
|
1053
|
-
|
|
1174
|
+
} else if (n <= 32 && from <= toI32(buf.length) - 32 && at <= toI32(head.length) - 32) {
|
|
1175
|
+
store(loadU8x16(buf, from), head, at)
|
|
1176
|
+
store(loadU8x16(buf, from + 16), head, at + 16)
|
|
1177
|
+
} else if (!tryCopyBytes(head, at, buf, from, n)) {
|
|
1178
|
+
// `http1HeadGrow` left room for the line, so this refuses only a head
|
|
1179
|
+
// moved by hand.
|
|
1180
|
+
http1Fail(p, 400, "the request line does not fit the head")
|
|
1181
|
+
return -1
|
|
1054
1182
|
}
|
|
1055
|
-
|
|
1183
|
+
// Both are at most 2^30, so the u32 sum fits.
|
|
1184
|
+
p.headLength = toI32(toU32(at) + toU32(n))
|
|
1056
1185
|
return at
|
|
1057
1186
|
}
|
|
1058
1187
|
|
|
@@ -1060,9 +1189,15 @@ const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
|
1060
1189
|
const http1SpanSet = (p: Http1Parser, at: i32, value: i32): void => {
|
|
1061
1190
|
if (at >= toI32(p.spans.length)) {
|
|
1062
1191
|
const old: i32[] = p.spans
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1192
|
+
// Built by `push`, so no length is computed for `new Array`; `at` counts
|
|
1193
|
+
// four spans a field, far below 2^31, so the u32 size does not wrap.
|
|
1194
|
+
const size: u32 = at < HTTP1_FIRST_SPANS ? toU32(HTTP1_FIRST_SPANS) : toU32(at) * toU32(2) + toU32(4)
|
|
1195
|
+
const grown: i32[] = []
|
|
1196
|
+
for (let k: i32 = 0; k < toI32(old.length); k += 1) {
|
|
1197
|
+
grown.push(old[k])
|
|
1198
|
+
}
|
|
1199
|
+
while (toU32(grown.length) < size) {
|
|
1200
|
+
grown.push(0)
|
|
1066
1201
|
}
|
|
1067
1202
|
p.spans = grown
|
|
1068
1203
|
}
|
|
@@ -1080,26 +1215,35 @@ const http1SpanSet = (p: Http1Parser, at: i32, value: i32): void => {
|
|
|
1080
1215
|
*/
|
|
1081
1216
|
const http1TallyRun = (tally: Http1ListTally, head: u8[], from: i32, to: i32, word: string): void => {
|
|
1082
1217
|
let start: i32 = from
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1218
|
+
while (start >= 0 && start <= to && start <= toI32(head.length)) {
|
|
1219
|
+
// The element runs to the next comma, or to the value's end.
|
|
1220
|
+
let end: i32 = start
|
|
1221
|
+
while (end >= 0 && end < to && end < toI32(head.length) && head[end] !== 44) {
|
|
1222
|
+
end += 1
|
|
1223
|
+
}
|
|
1224
|
+
let a: i32 = start
|
|
1225
|
+
let b: i32 = end
|
|
1226
|
+
while (a >= 0 && a < b && a < toI32(head.length) && http1IsOws(toI32(head[a]))) {
|
|
1227
|
+
a += 1
|
|
1228
|
+
}
|
|
1229
|
+
while (b > a && b > 0 && b <= toI32(head.length) && http1IsOws(toI32(head[b - 1]))) {
|
|
1230
|
+
b -= 1
|
|
1231
|
+
}
|
|
1232
|
+
// Each element holds a byte of a head under 2^30 bytes, so the counts
|
|
1233
|
+
// stay below it.
|
|
1234
|
+
const elements: i32 = tally.elements
|
|
1235
|
+
if (b > a && elements < HTTP1_MOST_INPUT) {
|
|
1236
|
+
tally.elements = elements + 1
|
|
1237
|
+
tally.lastMatches = http1HeadIs(head, a, b, word, true)
|
|
1238
|
+
const matches: i32 = tally.matches
|
|
1239
|
+
if (tally.lastMatches && matches < HTTP1_MOST_INPUT) {
|
|
1240
|
+
tally.matches = matches + 1
|
|
1100
1241
|
}
|
|
1101
|
-
start = k + 1
|
|
1102
1242
|
}
|
|
1243
|
+
if (end < 0 || end >= to || end >= toI32(head.length)) {
|
|
1244
|
+
break
|
|
1245
|
+
}
|
|
1246
|
+
start = end + 1
|
|
1103
1247
|
}
|
|
1104
1248
|
}
|
|
1105
1249
|
|
|
@@ -1113,10 +1257,13 @@ const http1Tally = (p: Http1Parser, name: string, word: string): Http1ListTally
|
|
|
1113
1257
|
tally.matches = 0
|
|
1114
1258
|
tally.lastMatches = false
|
|
1115
1259
|
const spans: i32[] = p.spans
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1260
|
+
const n: i32 = toI32(spans.length)
|
|
1261
|
+
let k: i32 = 0
|
|
1262
|
+
for (let i: i32 = 0; i < p.headerCount && k < n - 3; i += 1) {
|
|
1263
|
+
if (http1HeadIs(p.head, spans[k], spans[k + 1], name, false)) {
|
|
1264
|
+
http1TallyRun(tally, p.head, spans[k + 2], spans[k + 3], word)
|
|
1119
1265
|
}
|
|
1266
|
+
k += 4
|
|
1120
1267
|
}
|
|
1121
1268
|
return tally
|
|
1122
1269
|
}
|
|
@@ -1145,14 +1292,19 @@ const http1HeadIsVector = (
|
|
|
1145
1292
|
text: string,
|
|
1146
1293
|
fold: boolean
|
|
1147
1294
|
): boolean => {
|
|
1148
|
-
if (at < 0 ||
|
|
1149
|
-
|
|
1295
|
+
if (at < 0 || n < 0 || n > 16) {
|
|
1296
|
+
// No word compared here is longer than a vector, so nothing matches.
|
|
1297
|
+
return false
|
|
1298
|
+
}
|
|
1299
|
+
if (at > toI32(head.length) - 16) {
|
|
1300
|
+
// An offset into the head and a length of at most 16: the u32 sum fits.
|
|
1301
|
+
return http1HeadIs(head, at, toI32(toU32(at) + toU32(n)), text, fold)
|
|
1150
1302
|
}
|
|
1151
1303
|
let v: u8x16 = loadU8x16(head, at)
|
|
1152
1304
|
if (fold) {
|
|
1153
1305
|
v = select(lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)), or(v, splatU8x16(32)), v)
|
|
1154
1306
|
}
|
|
1155
|
-
const want: i32 = n >= 16 ? 65535 : (1 << n) - 1
|
|
1307
|
+
const want: i32 = n >= 16 ? 65535 : toI32((toU32(1) << toU32(n)) - toU32(1))
|
|
1156
1308
|
return (bitmask(eq(v, word)) & want) === want
|
|
1157
1309
|
}
|
|
1158
1310
|
|
|
@@ -1183,7 +1335,7 @@ const http1FramingField = (head: u8[], at: i32, n: i32): i32 => {
|
|
|
1183
1335
|
if (n === 17) {
|
|
1184
1336
|
// "transfer-encodin" in the vector, and the "g" after it.
|
|
1185
1337
|
const coding: u8x16 = u8x16(116, 114, 97, 110, 115, 102, 101, 114, 45, 101, 110, 99, 111, 100, 105, 110)
|
|
1186
|
-
const g: boolean = at >= 0 && at
|
|
1338
|
+
const g: boolean = at >= 0 && at < toI32(head.length) - 16 && head[at + 16] === 103
|
|
1187
1339
|
return g && http1HeadIsVector(head, at, 16, coding, "transfer-encodin", false) ? HTTP1_FIELD_CODING : 0
|
|
1188
1340
|
}
|
|
1189
1341
|
return 0
|
|
@@ -1210,20 +1362,24 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1210
1362
|
const length: i32 = toI32(head.length)
|
|
1211
1363
|
let found: i32 = 0
|
|
1212
1364
|
let start: i32 = from
|
|
1213
|
-
while (start >= 0 && start <= to) {
|
|
1365
|
+
while (start >= 0 && start <= to && start <= length) {
|
|
1214
1366
|
// The element runs to the next comma, or to the value's end.
|
|
1215
1367
|
let end: i32 = start
|
|
1216
1368
|
let seeking: boolean = true
|
|
1217
|
-
while (seeking && end < to) {
|
|
1369
|
+
while (seeking && end >= 0 && end < to) {
|
|
1218
1370
|
if (end <= length - 16) {
|
|
1219
|
-
const commas:
|
|
1371
|
+
const commas: u32 = toU32(bitmask(eq(loadU8x16(head, end), splatU8x16(44))))
|
|
1220
1372
|
if (commas !== 0) {
|
|
1221
|
-
|
|
1373
|
+
// The lowest set lane, 0 to 15.
|
|
1374
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(commas & (toU32(0) - commas)))
|
|
1375
|
+
if (lane >= 0 && lane < 16) {
|
|
1376
|
+
end += lane
|
|
1377
|
+
}
|
|
1222
1378
|
seeking = false
|
|
1223
1379
|
} else {
|
|
1224
1380
|
end += 16
|
|
1225
1381
|
}
|
|
1226
|
-
} else if (end
|
|
1382
|
+
} else if (end < length && head[end] !== 44) {
|
|
1227
1383
|
end += 1
|
|
1228
1384
|
} else {
|
|
1229
1385
|
seeking = false
|
|
@@ -1234,13 +1390,14 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1234
1390
|
}
|
|
1235
1391
|
let a: i32 = start
|
|
1236
1392
|
let b: i32 = end
|
|
1237
|
-
while (a < b && a >= 0 && a <
|
|
1393
|
+
while (a < b && a >= 0 && a < length && http1IsOws(toI32(head[a]))) {
|
|
1238
1394
|
a += 1
|
|
1239
1395
|
}
|
|
1240
|
-
while (b > a && b
|
|
1396
|
+
while (b > a && b > 0 && b <= toI32(head.length) && http1IsOws(toI32(head[b - 1]))) {
|
|
1241
1397
|
b -= 1
|
|
1242
1398
|
}
|
|
1243
|
-
|
|
1399
|
+
// `0 <= a <= b`, so the u32 difference is the element's length.
|
|
1400
|
+
const n: i32 = a >= 0 && b >= a ? toI32(toU32(b) - toU32(a)) : 0
|
|
1244
1401
|
if (n === 5 && http1HeadIsVector(head, a, n, close, "close", true)) {
|
|
1245
1402
|
found = found | HTTP1_CONNECTION_CLOSE
|
|
1246
1403
|
} else if (n === 10 && http1HeadIsVector(head, a, n, keepAlive, "keep-alive", true)) {
|
|
@@ -1248,6 +1405,9 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1248
1405
|
} else if (n === 7 && http1HeadIsVector(head, a, n, upgrade, "upgrade", true)) {
|
|
1249
1406
|
found = found | HTTP1_CONNECTION_UPGRADE
|
|
1250
1407
|
}
|
|
1408
|
+
if (end < 0 || end >= to || end >= length) {
|
|
1409
|
+
break
|
|
1410
|
+
}
|
|
1251
1411
|
start = end + 1
|
|
1252
1412
|
}
|
|
1253
1413
|
return found
|
|
@@ -1260,7 +1420,14 @@ const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
|
1260
1420
|
const http1MethodOverrun = (p: Http1Parser): boolean => {
|
|
1261
1421
|
// Bytes up to `scan` were seen by an earlier call, which found a space
|
|
1262
1422
|
// in time if there were more than `HTTP1_MAX_METHOD` of them.
|
|
1263
|
-
|
|
1423
|
+
const from: i32 = p.start
|
|
1424
|
+
if (from < 0 || p.scan < from) {
|
|
1425
|
+
// The parser keeps `0 <= start <= scan`. The fields are public, so a
|
|
1426
|
+
// caller may have moved them: such a window says nothing about the
|
|
1427
|
+
// method, and `http1TakeLine` refuses the parser with a 400.
|
|
1428
|
+
return false
|
|
1429
|
+
}
|
|
1430
|
+
if (toU32(p.scan) - toU32(from) > toU32(HTTP1_MAX_METHOD)) {
|
|
1264
1431
|
return false
|
|
1265
1432
|
}
|
|
1266
1433
|
const buf: u8[] = p.buf
|
|
@@ -1268,14 +1435,15 @@ const http1MethodOverrun = (p: Http1Parser): boolean => {
|
|
|
1268
1435
|
// A space, CR or LF in the first sixteen bytes, buffered or not, is
|
|
1269
1436
|
// before `HTTP1_MAX_METHOD`: the loop below would answer false at it, or
|
|
1270
1437
|
// at `end` before it.
|
|
1271
|
-
if (
|
|
1272
|
-
const v: u8x16 = loadU8x16(buf,
|
|
1438
|
+
if (from <= n - 16) {
|
|
1439
|
+
const v: u8x16 = loadU8x16(buf, from)
|
|
1273
1440
|
if (any(or(or(eq(v, splatU8x16(32)), eq(v, splatU8x16(13))), eq(v, splatU8x16(10))))) {
|
|
1274
1441
|
return false
|
|
1275
1442
|
}
|
|
1276
1443
|
}
|
|
1277
|
-
|
|
1278
|
-
|
|
1444
|
+
// `start` is within a buffer under 2^30 bytes, so the sum is an i32.
|
|
1445
|
+
const stop: i32 = toI32(toU32(from) + toU32(HTTP1_MAX_METHOD))
|
|
1446
|
+
for (let k: i32 = from; k >= 0 && k < p.end && k < toI32(buf.length); k += 1) {
|
|
1279
1447
|
if (buf[k] === 32 || buf[k] === 13 || buf[k] === 10) {
|
|
1280
1448
|
return false
|
|
1281
1449
|
}
|
|
@@ -1295,14 +1463,24 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1295
1463
|
const n: i32 = toI32(buf.length)
|
|
1296
1464
|
const a: i32 = p.lineStart
|
|
1297
1465
|
const b: i32 = p.lineEnd < n ? p.lineEnd : n
|
|
1466
|
+
if (a < 0 || b < a) {
|
|
1467
|
+
// `http1TakeLine` hands out a line inside `buf`. The fields are public, so a
|
|
1468
|
+
// caller may have moved them: such a parser is refused with a 400 rather than
|
|
1469
|
+
// read outside `buf`.
|
|
1470
|
+
return http1Fail(p, 400, "the request line is out of order")
|
|
1471
|
+
}
|
|
1298
1472
|
// The method is tchars, sixteen at a time as a field name is read; the
|
|
1299
1473
|
// line's CR, a control byte, ends the run if no space or other byte does.
|
|
1300
1474
|
let i: i32 = a
|
|
1301
1475
|
let ended: boolean = false
|
|
1302
1476
|
while (!ended && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
|
|
1303
|
-
const bad:
|
|
1477
|
+
const bad: u32 = toU32(http1NonTokenLanes(loadU8x16(buf, i)))
|
|
1304
1478
|
if (bad !== 0) {
|
|
1305
|
-
|
|
1479
|
+
// The lowest set lane, 0 to 15.
|
|
1480
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
|
|
1481
|
+
if (lane >= 0 && lane < 16) {
|
|
1482
|
+
i += lane
|
|
1483
|
+
}
|
|
1306
1484
|
ended = true
|
|
1307
1485
|
} else {
|
|
1308
1486
|
i += 16
|
|
@@ -1314,7 +1492,15 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1314
1492
|
while (!ended && i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1315
1493
|
i += 1
|
|
1316
1494
|
}
|
|
1317
|
-
|
|
1495
|
+
// `a <= i`, so the u32 difference is the method's length.
|
|
1496
|
+
if (
|
|
1497
|
+
i === a ||
|
|
1498
|
+
toU32(i) - toU32(a) > toU32(HTTP1_MAX_METHOD) ||
|
|
1499
|
+
i >= b ||
|
|
1500
|
+
i < 0 ||
|
|
1501
|
+
i >= toI32(buf.length) ||
|
|
1502
|
+
buf[i] !== 32
|
|
1503
|
+
) {
|
|
1318
1504
|
return http1Fail(p, 400, "a malformed method")
|
|
1319
1505
|
}
|
|
1320
1506
|
const methodEnd: i32 = i
|
|
@@ -1325,9 +1511,13 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1325
1511
|
// tail shorter than a vector before `buf`'s end.
|
|
1326
1512
|
let found: boolean = false
|
|
1327
1513
|
while (!found && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
|
|
1328
|
-
const bad:
|
|
1514
|
+
const bad: u32 = toU32(bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93))))
|
|
1329
1515
|
if (bad !== 0) {
|
|
1330
|
-
|
|
1516
|
+
// The lowest set lane, 0 to 15.
|
|
1517
|
+
const lane: i32 = toI32(31) - toI32(Math.clz32(bad & (toU32(0) - bad)))
|
|
1518
|
+
if (lane >= 0 && lane < 16) {
|
|
1519
|
+
i += lane
|
|
1520
|
+
}
|
|
1331
1521
|
found = true
|
|
1332
1522
|
} else {
|
|
1333
1523
|
i += 16
|
|
@@ -1339,10 +1529,11 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1339
1529
|
while (!found && i >= 0 && i < b && i < toI32(buf.length) && buf[i] > 32 && buf[i] < 127) {
|
|
1340
1530
|
i += 1
|
|
1341
1531
|
}
|
|
1342
|
-
|
|
1532
|
+
// `targetStart <= i`, so the u32 difference is the target's length.
|
|
1533
|
+
if (toU32(i) - toU32(targetStart) > toU32(p.maxTarget)) {
|
|
1343
1534
|
return http1Fail(p, 414, "the request target is over the limit")
|
|
1344
1535
|
}
|
|
1345
|
-
if (i === targetStart || i >= b || buf[i] !== 32) {
|
|
1536
|
+
if (i === targetStart || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 32) {
|
|
1346
1537
|
return http1Fail(p, 400, "a malformed request target")
|
|
1347
1538
|
}
|
|
1348
1539
|
const targetEnd: i32 = i
|
|
@@ -1351,12 +1542,14 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1351
1542
|
// with sixteen bytes to load, one compare for the five letters and the dot
|
|
1352
1543
|
// and one range test for the two digits, else a byte at a time.
|
|
1353
1544
|
let version: boolean = false
|
|
1354
|
-
|
|
1545
|
+
// `i < b` here, so the u32 difference is what is left of the line.
|
|
1546
|
+
const eight: boolean = i >= 0 && i <= b && toU32(b) - toU32(i) === toU32(8)
|
|
1547
|
+
if (eight && i <= toI32(buf.length) - 16) {
|
|
1355
1548
|
const v: u8x16 = loadU8x16(buf, i)
|
|
1356
1549
|
const fixed: i32 = bitmask(eq(v, u8x16(72, 84, 84, 80, 47, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0)))
|
|
1357
1550
|
const digits: i32 = bitmask(lt(subWrapping(v, splatU8x16(48)), splatU8x16(10)))
|
|
1358
1551
|
version = (fixed & 95) === 95 && (digits & 160) === 160
|
|
1359
|
-
} else if (
|
|
1552
|
+
} else if (eight && i <= toI32(buf.length) - 8) {
|
|
1360
1553
|
version =
|
|
1361
1554
|
buf[i] === 72 &&
|
|
1362
1555
|
buf[i + 1] === 84 &&
|
|
@@ -1369,21 +1562,29 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1369
1562
|
buf[i + 7] >= 48 &&
|
|
1370
1563
|
buf[i + 7] <= 57
|
|
1371
1564
|
}
|
|
1372
|
-
if (!version) {
|
|
1565
|
+
if (!version || i < 0 || i > toI32(buf.length) - 8) {
|
|
1373
1566
|
return http1Fail(p, 400, "a malformed HTTP version")
|
|
1374
1567
|
}
|
|
1375
1568
|
if (buf[i + 5] !== 49) {
|
|
1376
1569
|
return http1Fail(p, 505, "an HTTP major version other than 1")
|
|
1377
1570
|
}
|
|
1378
1571
|
p.minor = buf[i + 7] === 48 ? 0 : 1
|
|
1379
|
-
|
|
1380
|
-
|
|
1572
|
+
// `a <= b`, checked above.
|
|
1573
|
+
const lineLength: i32 = toI32(toU32(b) - toU32(a))
|
|
1574
|
+
if (p.headLength < 0 || toU64(p.headLength) + toU64(lineLength) + toU64(16) > toU64(p.head.length)) {
|
|
1575
|
+
if (!http1HeadGrow(p, lineLength)) {
|
|
1576
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
1577
|
+
}
|
|
1381
1578
|
}
|
|
1382
1579
|
const at: i32 = http1HeadAppend(p, a, b)
|
|
1580
|
+
if (at < 0) {
|
|
1581
|
+
return HTTP1_ERROR
|
|
1582
|
+
}
|
|
1383
1583
|
p.methodStart = at
|
|
1384
|
-
|
|
1385
|
-
p.
|
|
1386
|
-
p.
|
|
1584
|
+
// Each mark is within the line, `a` to `b`, and the line is in the head.
|
|
1585
|
+
p.methodEnd = http1Past(at, toI32(toU32(methodEnd) - toU32(a)))
|
|
1586
|
+
p.targetStart = http1Past(at, toI32(toU32(targetStart) - toU32(a)))
|
|
1587
|
+
p.targetEnd = http1Past(at, toI32(toU32(targetEnd) - toU32(a)))
|
|
1387
1588
|
if (p.keepText) {
|
|
1388
1589
|
p.method = http1Text(buf, a, methodEnd, false)
|
|
1389
1590
|
p.target = http1Text(buf, targetStart, targetEnd, false)
|
|
@@ -1403,6 +1604,15 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1403
1604
|
const n: i32 = toI32(buf.length)
|
|
1404
1605
|
const a: i32 = p.lineStart
|
|
1405
1606
|
const b: i32 = p.lineEnd < n ? p.lineEnd : n
|
|
1607
|
+
if (a < 0 || b < a) {
|
|
1608
|
+
// `http1Section` hands out a line inside `buf`. The fields are public, so a
|
|
1609
|
+
// caller may have moved them: such a parser is refused with a 400 rather than
|
|
1610
|
+
// read outside `buf`.
|
|
1611
|
+
return http1Fail(p, 400, "a field line out of order")
|
|
1612
|
+
}
|
|
1613
|
+
// Every offset below is in the line, `a` to `b`, or its copy in the head,
|
|
1614
|
+
// both under 2^31 bytes, so they are added with `http1Past` and
|
|
1615
|
+
// subtracted with `http1Less`, which do not check what cannot wrap.
|
|
1406
1616
|
// A kept line's name is read in the head's copy, each upper-case letter
|
|
1407
1617
|
// lowered as the scan passes it: one walk over the name, not a check of
|
|
1408
1618
|
// `buf` and then a second walk of the copy. The copy of a line refused
|
|
@@ -1410,17 +1620,17 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1410
1620
|
let at: i32 = 0
|
|
1411
1621
|
let i: i32 = a
|
|
1412
1622
|
if (keep) {
|
|
1413
|
-
at = a
|
|
1414
|
-
i = a
|
|
1623
|
+
at = http1Past(a, delta)
|
|
1624
|
+
i = http1Past(a, http1Less(http1LowerName(p.head, at, http1Past(at, http1Less(b, a))), at))
|
|
1415
1625
|
} else {
|
|
1416
1626
|
while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1417
1627
|
i += 1
|
|
1418
1628
|
}
|
|
1419
1629
|
}
|
|
1420
|
-
if (i === a || i >= b || buf[i] !== 58) {
|
|
1630
|
+
if (i === a || i >= b || i < 0 || i >= toI32(buf.length) || buf[i] !== 58) {
|
|
1421
1631
|
// Whitespace is no tchar, so an obs-fold line has an empty name: its
|
|
1422
1632
|
// own refusal is told apart here, off the path every field takes.
|
|
1423
|
-
if (i === a && a >= 0 && a < b && (buf[a] === 32 || buf[a] === 9)) {
|
|
1633
|
+
if (i === a && a >= 0 && a < b && a < toI32(buf.length) && (buf[a] === 32 || buf[a] === 9)) {
|
|
1424
1634
|
return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
|
|
1425
1635
|
}
|
|
1426
1636
|
return http1Fail(p, 400, "a malformed field name")
|
|
@@ -1431,13 +1641,14 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1431
1641
|
if (p.lineControl) {
|
|
1432
1642
|
return http1Fail(p, 400, "a control character in a field value")
|
|
1433
1643
|
}
|
|
1434
|
-
|
|
1644
|
+
// The count is at most `maxHeaders` before it, checked below each time.
|
|
1645
|
+
p.fieldCount = http1Past(p.fieldCount, 1)
|
|
1435
1646
|
if (p.fieldCount > p.maxHeaders) {
|
|
1436
1647
|
return http1Fail(p, 431, "more fields than the limit")
|
|
1437
1648
|
}
|
|
1438
1649
|
// A trailer is checked and counted, but not kept: nothing here reads one.
|
|
1439
1650
|
if (keep) {
|
|
1440
|
-
let valueStart: i32 = nameEnd
|
|
1651
|
+
let valueStart: i32 = http1Past(nameEnd, 1)
|
|
1441
1652
|
let valueEnd: i32 = b
|
|
1442
1653
|
while (
|
|
1443
1654
|
valueStart >= 0 &&
|
|
@@ -1447,37 +1658,50 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1447
1658
|
) {
|
|
1448
1659
|
valueStart += 1
|
|
1449
1660
|
}
|
|
1450
|
-
while (
|
|
1661
|
+
while (
|
|
1662
|
+
valueEnd >= 1 &&
|
|
1663
|
+
valueEnd > valueStart &&
|
|
1664
|
+
valueEnd <= toI32(buf.length) &&
|
|
1665
|
+
(buf[valueEnd - 1] === 32 || buf[valueEnd - 1] === 9)
|
|
1666
|
+
) {
|
|
1451
1667
|
valueEnd -= 1
|
|
1452
1668
|
}
|
|
1453
1669
|
// The fields that frame the message are counted here, as their names
|
|
1454
1670
|
// are read, for `http1EndOfHead`: every kept field asks the same question.
|
|
1455
|
-
|
|
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))
|
|
1456
1674
|
if (kind === HTTP1_FIELD_HOST) {
|
|
1457
|
-
p.hostFields
|
|
1675
|
+
p.hostFields = http1Past(p.hostFields, 1)
|
|
1458
1676
|
p.hostField = p.headerCount
|
|
1459
1677
|
} else if (kind === HTTP1_FIELD_LENGTH) {
|
|
1460
|
-
p.lengthFields
|
|
1678
|
+
p.lengthFields = http1Past(p.lengthFields, 1)
|
|
1461
1679
|
p.lengthField = p.headerCount
|
|
1462
1680
|
} else if (kind === HTTP1_FIELD_CODING) {
|
|
1463
|
-
p.codingFields
|
|
1681
|
+
p.codingFields = http1Past(p.codingFields, 1)
|
|
1464
1682
|
} else if (kind === HTTP1_FIELD_CONNECTION) {
|
|
1465
1683
|
p.connectionOptions =
|
|
1466
|
-
p.connectionOptions |
|
|
1684
|
+
p.connectionOptions |
|
|
1685
|
+
http1ConnectionList(
|
|
1686
|
+
p.head,
|
|
1687
|
+
http1Past(at, http1Less(valueStart, a)),
|
|
1688
|
+
http1Past(at, http1Less(valueEnd, a))
|
|
1689
|
+
)
|
|
1467
1690
|
} else if (kind === HTTP1_FIELD_UPGRADE) {
|
|
1468
|
-
p.upgradeFields
|
|
1469
|
-
p.upgradeValueBytes
|
|
1691
|
+
p.upgradeFields = http1Past(p.upgradeFields, 1)
|
|
1692
|
+
p.upgradeValueBytes = http1Past(p.upgradeValueBytes, http1Less(valueEnd, valueStart))
|
|
1470
1693
|
}
|
|
1471
|
-
|
|
1472
|
-
|
|
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)))
|
|
1473
1697
|
// The last of the four is set first, so the array has room for them all.
|
|
1474
1698
|
const spans: i32[] = p.spans
|
|
1475
|
-
if (base >= 0 && base
|
|
1699
|
+
if (base >= 0 && base < toI32(spans.length) - 2) {
|
|
1476
1700
|
spans[base] = at
|
|
1477
|
-
spans[base + 1] = at
|
|
1478
|
-
spans[base + 2] = at
|
|
1701
|
+
spans[base + 1] = http1Past(at, http1Less(nameEnd, a))
|
|
1702
|
+
spans[base + 2] = http1Past(at, http1Less(valueStart, a))
|
|
1479
1703
|
}
|
|
1480
|
-
p.headerCount
|
|
1704
|
+
p.headerCount = http1Past(p.headerCount, 1)
|
|
1481
1705
|
if (p.keepText) {
|
|
1482
1706
|
p.names.push(http1Text(buf, a, nameEnd, true))
|
|
1483
1707
|
p.values.push(http1Text(buf, valueStart, valueEnd, false))
|
|
@@ -1486,6 +1710,19 @@ const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
|
1486
1710
|
return HTTP1_NEED_MORE
|
|
1487
1711
|
}
|
|
1488
1712
|
|
|
1713
|
+
/**
|
|
1714
|
+
* Where field `field`'s four spans start in `spans`: `4 * field`, or -1 when
|
|
1715
|
+
* no such field is recorded.
|
|
1716
|
+
*/
|
|
1717
|
+
const http1FieldBase = (spans: i32[], field: i32): i32 => {
|
|
1718
|
+
if (field < 0 || field > 536870911) {
|
|
1719
|
+
return -1
|
|
1720
|
+
}
|
|
1721
|
+
// `field < 2^29`, so the shifted index is below 2^31.
|
|
1722
|
+
const k: i32 = toI32(toU32(field) << toU32(2))
|
|
1723
|
+
return k < toI32(spans.length) - 3 ? k : -1
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1489
1726
|
/**
|
|
1490
1727
|
* The header section has ended: decide the framing and the connection's fate
|
|
1491
1728
|
* (§6.3, §9.3) and answer `HTTP1_HEAD`, or the refusal. It reads the counts
|
|
@@ -1497,19 +1734,19 @@ const http1EndOfHead = (p: Http1Parser): i32 => {
|
|
|
1497
1734
|
const head: u8[] = p.head
|
|
1498
1735
|
const hosts: i32 = p.hostFields
|
|
1499
1736
|
const lengths: i32 = p.lengthFields
|
|
1500
|
-
const
|
|
1737
|
+
const lengthAt: i32 = http1FieldBase(spans, p.lengthField)
|
|
1501
1738
|
if ((p.minor === 1 && hosts !== 1) || hosts > 1) {
|
|
1502
1739
|
return http1Fail(p, 400, "a request without exactly one Host")
|
|
1503
1740
|
}
|
|
1504
1741
|
// The one Host must be a host and an optional port (§3.2): a value such as
|
|
1505
1742
|
// `a@b`, `a/b` or `a, b` is read one way by this program and another by a
|
|
1506
1743
|
// proxy or a virtual-host table in front of it. An empty Host is allowed.
|
|
1507
|
-
const
|
|
1744
|
+
const hostAt: i32 = http1FieldBase(spans, p.hostField)
|
|
1508
1745
|
if (
|
|
1509
1746
|
hosts === 1 &&
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
!urlIsHostPort(head, spans[
|
|
1747
|
+
hostAt >= 0 &&
|
|
1748
|
+
hostAt < toI32(spans.length) - 3 &&
|
|
1749
|
+
!urlIsHostPort(head, spans[hostAt + 2], spans[hostAt + 3])
|
|
1513
1750
|
) {
|
|
1514
1751
|
return http1Fail(p, 400, "a Host that is not a host and port")
|
|
1515
1752
|
}
|
|
@@ -1533,13 +1770,8 @@ const http1EndOfHead = (p: Http1Parser): i32 => {
|
|
|
1533
1770
|
p.chunked = true
|
|
1534
1771
|
} else if (lengths > 1) {
|
|
1535
1772
|
return http1Fail(p, 400, "Content-Length given twice")
|
|
1536
|
-
} else if (lengths === 1 &&
|
|
1537
|
-
const size: i32 = http1ContentLength(
|
|
1538
|
-
head,
|
|
1539
|
-
spans[4 * lengthField + 2],
|
|
1540
|
-
spans[4 * lengthField + 3],
|
|
1541
|
-
p.maxBody
|
|
1542
|
-
)
|
|
1773
|
+
} else if (lengths === 1 && lengthAt >= 0 && lengthAt < toI32(spans.length) - 3) {
|
|
1774
|
+
const size: i32 = http1ContentLength(head, spans[lengthAt + 2], spans[lengthAt + 3], p.maxBody)
|
|
1543
1775
|
if (size === -1) {
|
|
1544
1776
|
return http1Fail(p, 400, "a Content-Length that is not a run of digits")
|
|
1545
1777
|
}
|
|
@@ -1587,24 +1819,27 @@ const http1ContentLength = (bytes: u8[], from: i32, to: i32, limit: i32): i32 =>
|
|
|
1587
1819
|
if (to <= from) {
|
|
1588
1820
|
return -1
|
|
1589
1821
|
}
|
|
1590
|
-
|
|
1591
|
-
|
|
1822
|
+
// In 32 unsigned bits: `value` never passes `limit`, an i32, so it
|
|
1823
|
+
// neither wraps nor fails to fit back.
|
|
1824
|
+
let value: u32 = 0
|
|
1825
|
+
let over: boolean = limit < 0
|
|
1826
|
+
const most: u32 = toU32(limit)
|
|
1592
1827
|
for (let k: i32 = from; k < to && k >= 0 && k < toI32(bytes.length); k += 1) {
|
|
1593
1828
|
const c: i32 = toI32(bytes[k])
|
|
1594
1829
|
if (c < 48 || c > 57) {
|
|
1595
1830
|
return -1
|
|
1596
1831
|
}
|
|
1597
1832
|
if (!over) {
|
|
1598
|
-
//
|
|
1599
|
-
|
|
1600
|
-
if (
|
|
1833
|
+
// The digit is compared on its own first, so `most - digit` does not wrap.
|
|
1834
|
+
const digit: u32 = toU32(c) - toU32(48)
|
|
1835
|
+
if (digit > most || value > (most - digit) / toU32(10)) {
|
|
1601
1836
|
over = true
|
|
1602
1837
|
} else {
|
|
1603
|
-
value = value * 10 +
|
|
1838
|
+
value = value * toU32(10) + digit
|
|
1604
1839
|
}
|
|
1605
1840
|
}
|
|
1606
1841
|
}
|
|
1607
|
-
return over ? -2 : value
|
|
1842
|
+
return over ? -2 : toI32(value)
|
|
1608
1843
|
}
|
|
1609
1844
|
|
|
1610
1845
|
/**
|
|
@@ -1731,20 +1966,24 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
|
1731
1966
|
const n: i32 = toI32(buf.length)
|
|
1732
1967
|
const a: i32 = p.lineStart
|
|
1733
1968
|
const b: i32 = p.lineEnd < n ? p.lineEnd : n
|
|
1734
|
-
|
|
1735
|
-
|
|
1969
|
+
// Never more than `maxBody` is counted, so `room` is not negative.
|
|
1970
|
+
const total: i32 = p.bodyTotal
|
|
1971
|
+
const room: u32 = total >= 0 && total <= p.maxBody ? toU32(p.maxBody) - toU32(total) : toU32(0)
|
|
1972
|
+
// In 32 unsigned bits: `size` never passes `room`, an i32.
|
|
1973
|
+
let size: u32 = 0
|
|
1736
1974
|
let over: boolean = false
|
|
1737
1975
|
let i: i32 = a
|
|
1738
1976
|
while (i >= 0 && i < b && i < toI32(buf.length)) {
|
|
1739
|
-
const
|
|
1740
|
-
if (
|
|
1977
|
+
const hex: i32 = http1HexValue(toI32(buf[i]))
|
|
1978
|
+
if (hex < 0) {
|
|
1741
1979
|
break
|
|
1742
1980
|
}
|
|
1743
1981
|
if (!over) {
|
|
1744
|
-
|
|
1982
|
+
const digit: u32 = toU32(hex)
|
|
1983
|
+
if (digit > room || size > (room - digit) / toU32(16)) {
|
|
1745
1984
|
over = true
|
|
1746
1985
|
} else {
|
|
1747
|
-
size = size * 16 + digit
|
|
1986
|
+
size = size * toU32(16) + digit
|
|
1748
1987
|
}
|
|
1749
1988
|
}
|
|
1750
1989
|
i += 1
|
|
@@ -1761,7 +2000,7 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
|
1761
2000
|
if (i >= b && i > digitsEnd) {
|
|
1762
2001
|
return http1Fail(p, 400, "a malformed chunk size")
|
|
1763
2002
|
}
|
|
1764
|
-
if (i >= 0 && i < b) {
|
|
2003
|
+
if (i >= 0 && i < b && i < toI32(buf.length)) {
|
|
1765
2004
|
if (buf[i] !== 59) {
|
|
1766
2005
|
return http1Fail(p, 400, "a malformed chunk size")
|
|
1767
2006
|
}
|
|
@@ -1777,7 +2016,7 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
|
1777
2016
|
if (over) {
|
|
1778
2017
|
return http1Fail(p, 413, "a chunked body over the limit")
|
|
1779
2018
|
}
|
|
1780
|
-
p.left = size
|
|
2019
|
+
p.left = toI32(size)
|
|
1781
2020
|
p.state = size === 0 ? HTTP1_S_TRAILERS : HTTP1_S_CHUNK_DATA
|
|
1782
2021
|
p.sectionBytes = 0
|
|
1783
2022
|
return HTTP1_NEED_MORE
|
|
@@ -1788,21 +2027,32 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
|
1788
2027
|
* `after` once the last of them is handed out.
|
|
1789
2028
|
*/
|
|
1790
2029
|
const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
|
|
1791
|
-
const
|
|
1792
|
-
|
|
2030
|
+
const from: i32 = p.start
|
|
2031
|
+
const to: i32 = p.end
|
|
2032
|
+
if (from < 0 || to <= from) {
|
|
1793
2033
|
return HTTP1_NEED_MORE
|
|
1794
2034
|
}
|
|
2035
|
+
const ready: i32 = toI32(toU32(to) - toU32(from))
|
|
1795
2036
|
let take: i32 = ready < p.left ? ready : p.left
|
|
1796
2037
|
if (take > p.maxChunk && p.maxChunk > 0) {
|
|
1797
2038
|
take = p.maxChunk
|
|
1798
2039
|
}
|
|
2040
|
+
const total: i32 = p.bodyTotal
|
|
2041
|
+
if (take < 0 || total < 0) {
|
|
2042
|
+
// The parser keeps `left` and the body's total 0 or more. The fields are
|
|
2043
|
+
// public, so a caller may have moved them: such a parser is refused with a 400
|
|
2044
|
+
// rather than read outside `buf`.
|
|
2045
|
+
return http1Fail(p, 400, "the body window is out of order")
|
|
2046
|
+
}
|
|
1799
2047
|
p.body = p.buf
|
|
1800
|
-
p.bodyOff =
|
|
2048
|
+
p.bodyOff = from
|
|
1801
2049
|
p.bodyLen = take
|
|
1802
|
-
|
|
2050
|
+
// `take` is at most `end - start`, and at most `left`, which was counted
|
|
2051
|
+
// within `maxBody`: none of the three wraps.
|
|
2052
|
+
p.start = http1Past(from, take)
|
|
1803
2053
|
p.scan = p.start
|
|
1804
|
-
p.left
|
|
1805
|
-
p.bodyTotal
|
|
2054
|
+
p.left = toI32(toU32(p.left) - toU32(take))
|
|
2055
|
+
p.bodyTotal = http1Past(total, take)
|
|
1806
2056
|
if (p.left === 0) {
|
|
1807
2057
|
p.state = after
|
|
1808
2058
|
}
|
|
@@ -1841,47 +2091,64 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
1841
2091
|
// Nothing past `copyStop` is copied: a line reaching it is over its limit
|
|
1842
2092
|
// (see the 431 below; what is left of `maxHeaderBytes` is the same at every
|
|
1843
2093
|
// line), so the copy, and the head, stay within the limit.
|
|
2094
|
+
//
|
|
2095
|
+
// 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 are taken
|
|
2097
|
+
// with `http1Past` and `http1Less`, which do not check what cannot pass.
|
|
1844
2098
|
const copy: boolean = !trailer
|
|
1845
|
-
const delta: i32 = p.headLength
|
|
1846
|
-
const
|
|
1847
|
-
|
|
2099
|
+
const delta: i32 = http1Less(p.headLength, p.start)
|
|
2100
|
+
const wideStop: i64 = toI64(p.start) + toI64(p.maxHeaderBytes) - toI64(p.sectionBytes) + toI64(2)
|
|
2101
|
+
let copyStop: i32 = 2147483647
|
|
2102
|
+
if (wideStop >= toI64(-2147483647) && wideStop < toI64(2147483647)) {
|
|
2103
|
+
copyStop = toI32(wideStop)
|
|
2104
|
+
}
|
|
1848
2105
|
// A vector at `q` is copied when `q < copyEnd`; the head is grown when `q`
|
|
1849
2106
|
// passes `growAt`, so most vectors pay one compare for each.
|
|
1850
2107
|
const copyEnd: i32 = copy ? copyStop : -2147483647
|
|
1851
2108
|
let head: u8[] = p.head
|
|
1852
|
-
let growAt: i32 = toI32(head.length)
|
|
2109
|
+
let growAt: i32 = http1Less(http1Less(toI32(head.length), 32), delta)
|
|
1853
2110
|
// Bit `k` of `bits` is set when `buf[base + k]` is a control byte or DEL
|
|
1854
2111
|
// at or past `i`: the vector last loaded, its lanes before `i` cleared.
|
|
1855
|
-
let base: i32 = i
|
|
1856
|
-
let bits:
|
|
2112
|
+
let base: i32 = http1Less(i, 16)
|
|
2113
|
+
let bits: u32 = 0
|
|
1857
2114
|
while (true) {
|
|
1858
2115
|
// The next control byte at or past `i`, or the end of the bytes so far.
|
|
1859
2116
|
let j: i32 = stop
|
|
1860
2117
|
let seeking: boolean = true
|
|
1861
2118
|
while (seeking) {
|
|
1862
2119
|
if (bits !== 0) {
|
|
1863
|
-
|
|
2120
|
+
// The lowest set bit, 0 to 15.
|
|
2121
|
+
j = http1Past(base, toI32(31) - toI32(Math.clz32(bits & (toU32(0) - bits))))
|
|
1864
2122
|
seeking = false
|
|
1865
2123
|
} else {
|
|
1866
|
-
const
|
|
2124
|
+
const next: i32 = http1Past(base, 16)
|
|
2125
|
+
const q: i32 = next > i ? next : i
|
|
1867
2126
|
if (q >= stop) {
|
|
1868
2127
|
seeking = false
|
|
1869
|
-
} else if (q >= 0 && q <= length - 16) {
|
|
2128
|
+
} else if (q >= 0 && q <= toI32(buf.length) - 16) {
|
|
1870
2129
|
// The vector may run past `stop` into bytes not yet fed: its lanes
|
|
1871
2130
|
// there are dropped from `bits`, and its copy of them is
|
|
1872
2131
|
// overwritten when they arrive.
|
|
1873
2132
|
const v: u8x16 = loadU8x16(buf, q)
|
|
1874
2133
|
if (q < copyEnd) {
|
|
1875
2134
|
if (q > growAt) {
|
|
1876
|
-
http1HeadGrow(p, q
|
|
2135
|
+
if (!http1HeadGrow(p, http1Less(http1Past(http1Past(q, delta), 16), p.headLength))) {
|
|
2136
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
2137
|
+
}
|
|
1877
2138
|
head = p.head
|
|
1878
|
-
growAt = toI32(head.length)
|
|
2139
|
+
growAt = http1Less(http1Less(toI32(head.length), 32), delta)
|
|
2140
|
+
}
|
|
2141
|
+
// `q <= growAt` here, so the vector fits in the head.
|
|
2142
|
+
const to: i32 = http1Past(q, delta)
|
|
2143
|
+
if (to >= 0 && to <= toI32(head.length) - 16) {
|
|
2144
|
+
store(v, head, to)
|
|
1879
2145
|
}
|
|
1880
|
-
store(v, head, q + delta)
|
|
1881
2146
|
}
|
|
1882
|
-
bits = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
|
|
1883
|
-
|
|
1884
|
-
|
|
2147
|
+
bits = toU32(bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127)))))
|
|
2148
|
+
// `q < stop` here: keep the lanes before `stop`, 1 to 15 of them.
|
|
2149
|
+
const keep: i32 = http1Less(stop, q)
|
|
2150
|
+
if (keep < 16) {
|
|
2151
|
+
bits = bits & ((toU32(1) << toU32(keep)) - toU32(1))
|
|
1885
2152
|
}
|
|
1886
2153
|
base = q
|
|
1887
2154
|
} else {
|
|
@@ -1890,18 +2157,25 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
1890
2157
|
while (k >= 0 && k < stop && k < toI32(buf.length) && buf[k] >= 32 && buf[k] !== 127) {
|
|
1891
2158
|
k += 1
|
|
1892
2159
|
}
|
|
1893
|
-
const last: i32 = k < stop ? k
|
|
2160
|
+
const last: i32 = k < stop ? http1Past(k, 1) : stop
|
|
1894
2161
|
const end: i32 = last < copyStop ? last : copyStop
|
|
1895
2162
|
if (copy && q < end) {
|
|
1896
|
-
|
|
1897
|
-
|
|
2163
|
+
const endAt: i32 = http1Past(end, delta)
|
|
2164
|
+
if (endAt > toI32(p.head.length) - 16) {
|
|
2165
|
+
if (!http1HeadGrow(p, http1Less(endAt, p.headLength))) {
|
|
2166
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
if (!tryCopyBytes(p.head, http1Past(q, delta), buf, q, http1Less(end, q))) {
|
|
2170
|
+
// The head was grown past `endAt` just above, so this refuses
|
|
2171
|
+
// only a head moved by hand.
|
|
2172
|
+
return http1Fail(p, 431, "a head of more than 2^30 bytes")
|
|
1898
2173
|
}
|
|
1899
|
-
copyBytes(p.head, q + delta, buf, q, end - q)
|
|
1900
2174
|
head = p.head
|
|
1901
|
-
growAt = toI32(head.length)
|
|
2175
|
+
growAt = http1Less(http1Less(toI32(head.length), 32), delta)
|
|
1902
2176
|
}
|
|
1903
2177
|
j = k
|
|
1904
|
-
base = k
|
|
2178
|
+
base = http1Less(k, 15)
|
|
1905
2179
|
seeking = false
|
|
1906
2180
|
}
|
|
1907
2181
|
}
|
|
@@ -1919,29 +2193,31 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
1919
2193
|
}
|
|
1920
2194
|
if (lf >= 0) {
|
|
1921
2195
|
const start: i32 = p.start
|
|
1922
|
-
if (lf === start || buf[lf - 1] !== 13) {
|
|
2196
|
+
if (lf === start || lf < 1 || lf > toI32(buf.length) || buf[lf - 1] !== 13) {
|
|
1923
2197
|
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
1924
2198
|
}
|
|
1925
|
-
|
|
1926
|
-
|
|
2199
|
+
// The line, `start` to `lf`, is in `buf`.
|
|
2200
|
+
const after: i32 = http1Past(lf, 1)
|
|
2201
|
+
const sectionBytes: i32 = http1Past(p.sectionBytes, http1Less(after, start))
|
|
2202
|
+
if (toI64(sectionBytes) - toI64(2) > toI64(p.maxHeaderBytes)) {
|
|
1927
2203
|
return http1Fail(p, 431, "a line over its limit")
|
|
1928
2204
|
}
|
|
1929
2205
|
// The scan stops at the CR, so the LF after it may be in no vector
|
|
1930
2206
|
// or tail copied: store it, and the head holds each line's CRLF.
|
|
1931
|
-
const at: i32 = lf
|
|
2207
|
+
const at: i32 = http1Past(lf, delta)
|
|
1932
2208
|
if (copy && at >= 0 && at < toI32(head.length)) {
|
|
1933
2209
|
head[at] = 10
|
|
1934
2210
|
}
|
|
1935
2211
|
p.lineStart = start
|
|
1936
2212
|
p.lineEnd = lf - 1
|
|
1937
|
-
p.start =
|
|
1938
|
-
p.scan =
|
|
1939
|
-
i =
|
|
2213
|
+
p.start = after
|
|
2214
|
+
p.scan = after
|
|
2215
|
+
i = after
|
|
1940
2216
|
// The lowest bit is the line's end at `j`, and a CR's LF is the next
|
|
1941
2217
|
// one when it is in this vector, as a control byte: drop them both.
|
|
1942
|
-
bits = bits & (bits - 1)
|
|
2218
|
+
bits = bits & (bits - toU32(1))
|
|
1943
2219
|
if (c === 13) {
|
|
1944
|
-
bits = bits & (bits - 1)
|
|
2220
|
+
bits = bits & (bits - toU32(1))
|
|
1945
2221
|
}
|
|
1946
2222
|
p.sectionBytes = sectionBytes
|
|
1947
2223
|
if (sectionBytes > p.maxHeaderBytes) {
|
|
@@ -1952,7 +2228,7 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
1952
2228
|
p.state = HTTP1_S_MESSAGE_END
|
|
1953
2229
|
return HTTP1_LINE
|
|
1954
2230
|
}
|
|
1955
|
-
p.headLength = p.start
|
|
2231
|
+
p.headLength = http1Past(p.start, delta)
|
|
1956
2232
|
return http1EndOfHead(p)
|
|
1957
2233
|
}
|
|
1958
2234
|
if (http1FieldLine(p, copy, delta) === HTTP1_ERROR) {
|
|
@@ -1968,15 +2244,15 @@ const http1Section = (p: Http1Parser): i32 => {
|
|
|
1968
2244
|
if (c !== 9) {
|
|
1969
2245
|
p.lineControl = true
|
|
1970
2246
|
}
|
|
1971
|
-
i = j
|
|
1972
|
-
bits = bits & (bits - 1)
|
|
2247
|
+
i = http1Past(j, 1)
|
|
2248
|
+
bits = bits & (bits - toU32(1))
|
|
1973
2249
|
}
|
|
1974
2250
|
}
|
|
1975
2251
|
p.scan = i
|
|
1976
2252
|
if (copy) {
|
|
1977
|
-
p.headLength = p.start
|
|
2253
|
+
p.headLength = http1Past(p.start, delta)
|
|
1978
2254
|
}
|
|
1979
|
-
if (p.end - p.start > p.maxHeaderBytes - p.sectionBytes + 1) {
|
|
2255
|
+
if (toI64(p.end) - toI64(p.start) > toI64(p.maxHeaderBytes) - toI64(p.sectionBytes) + toI64(1)) {
|
|
1980
2256
|
return http1Fail(p, 431, "a line over its limit")
|
|
1981
2257
|
}
|
|
1982
2258
|
return HTTP1_NEED_MORE
|
|
@@ -1999,7 +2275,8 @@ const http1Next = (p: Http1Parser): i32 => {
|
|
|
1999
2275
|
}
|
|
2000
2276
|
// The longest well-formed request line: the method, the target, two
|
|
2001
2277
|
// spaces and "HTTP/1.1".
|
|
2002
|
-
|
|
2278
|
+
// `maxTarget` is clamped to 2^30, so the sum is an i32.
|
|
2279
|
+
const got: i32 = http1TakeLine(p, http1Past(p.maxTarget, HTTP1_MAX_METHOD + 10), 414)
|
|
2003
2280
|
if (got !== HTTP1_LINE) {
|
|
2004
2281
|
return got
|
|
2005
2282
|
}
|
|
@@ -2070,10 +2347,9 @@ const http1Next = (p: Http1Parser): i32 => {
|
|
|
2070
2347
|
/** The bytes of `text`, as the language holds a string. */
|
|
2071
2348
|
const http1BytesOf = (text: string): u8[] => {
|
|
2072
2349
|
const n: i32 = toI32(text.length)
|
|
2073
|
-
const out: u8[] =
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
out[k] = toU8(text.charCodeAt(k))
|
|
2350
|
+
const out: u8[] = []
|
|
2351
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
2352
|
+
out.push(toU8(text.charCodeAt(k)))
|
|
2077
2353
|
}
|
|
2078
2354
|
return out
|
|
2079
2355
|
}
|
|
@@ -2210,33 +2486,58 @@ const http1HexDigits = (len: i32): i32 => {
|
|
|
2210
2486
|
* @returns the offset in `out` just past the chunk, or `HTTP1_NO_ROOM` when it does not fit
|
|
2211
2487
|
*/
|
|
2212
2488
|
export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
|
|
2213
|
-
const
|
|
2214
|
-
if (
|
|
2489
|
+
const end: i32 = http1TryWriteChunk(out, at, data, off, len)
|
|
2490
|
+
if (end === HTTP1_BAD_WINDOW) {
|
|
2215
2491
|
panic("http1WriteChunk: the window is outside the buffer")
|
|
2216
2492
|
}
|
|
2493
|
+
return end
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
/**
|
|
2497
|
+
* `http1WriteChunk` that answers `HTTP1_BAD_WINDOW`, with `out` untouched,
|
|
2498
|
+
* where `http1WriteChunk` panics: a window outside `data`. A caller a strict
|
|
2499
|
+
* module reaches uses this one.
|
|
2500
|
+
*
|
|
2501
|
+
* @param out the caller's buffer the chunk is written into
|
|
2502
|
+
* @param at where in `out` the chunk starts
|
|
2503
|
+
* @param data the body bytes to send
|
|
2504
|
+
* @param off where in `data` the chunk's bytes start
|
|
2505
|
+
* @param len how many bytes of `data` the chunk carries
|
|
2506
|
+
* @returns the offset in `out` just past the chunk, `HTTP1_NO_ROOM` when it
|
|
2507
|
+
* does not fit, or `HTTP1_BAD_WINDOW` when the window lies outside `data`
|
|
2508
|
+
*/
|
|
2509
|
+
export const http1TryWriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
|
|
2510
|
+
const size: i32 = toI32(data.length)
|
|
2511
|
+
if (off < 0 || len < 0 || off > size || toU32(len) > toU32(size) - toU32(off)) {
|
|
2512
|
+
return HTTP1_BAD_WINDOW
|
|
2513
|
+
}
|
|
2217
2514
|
if (len === 0) {
|
|
2218
2515
|
return at
|
|
2219
2516
|
}
|
|
2220
2517
|
const digits: i32 = http1HexDigits(len)
|
|
2221
2518
|
const outLength: i32 = toI32(out.length)
|
|
2222
|
-
|
|
2519
|
+
// The chunk's size, in 64 bits so that it cannot wrap.
|
|
2520
|
+
const need: u64 = toU64(digits) + toU64(len) + toU64(4)
|
|
2521
|
+
if (at < 0 || at > outLength || need > toU64(toU32(outLength) - toU32(at))) {
|
|
2522
|
+
return HTTP1_NO_ROOM
|
|
2523
|
+
}
|
|
2524
|
+
// It fits, so none of the offsets below passes `outLength`.
|
|
2525
|
+
const end: i32 = http1Past(at, digits)
|
|
2526
|
+
const headEnd: i32 = http1Past(end, 2)
|
|
2527
|
+
// The bytes go first, so a copy that fails leaves `out` untouched. It
|
|
2528
|
+
// cannot: the window is inside `data` and the chunk fits, both checked.
|
|
2529
|
+
if (!tryCopyBytes(out, headEnd, data, off, len)) {
|
|
2223
2530
|
return HTTP1_NO_ROOM
|
|
2224
2531
|
}
|
|
2225
2532
|
let v: i32 = len
|
|
2226
|
-
|
|
2533
|
+
// The digits are written last first, `out[k - 1]` each time.
|
|
2534
|
+
for (let k: i32 = end; k > 0 && k > at && k <= toI32(out.length); k -= 1) {
|
|
2227
2535
|
const d: i32 = v & 15
|
|
2228
|
-
out[k] = toU8(d < 10 ? 48 + d : 87 + d)
|
|
2536
|
+
out[k - 1] = toU8(d < 10 ? 48 + d : 87 + d)
|
|
2229
2537
|
v = v >> 4
|
|
2230
2538
|
}
|
|
2231
|
-
|
|
2232
|
-
out
|
|
2233
|
-
out[headEnd - 1] = 10
|
|
2234
|
-
for (let k: i32 = 0; k < len && headEnd + k < outLength; k += 1) {
|
|
2235
|
-
out[headEnd + k] = data[off + k]
|
|
2236
|
-
}
|
|
2237
|
-
out[headEnd + len] = 13
|
|
2238
|
-
out[headEnd + len + 1] = 10
|
|
2239
|
-
return headEnd + len + 2
|
|
2539
|
+
http1PutCrlf(out, end)
|
|
2540
|
+
return http1PutCrlf(out, http1Past(headEnd, len))
|
|
2240
2541
|
}
|
|
2241
2542
|
|
|
2242
2543
|
/**
|
|
@@ -2253,14 +2554,27 @@ export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i3
|
|
|
2253
2554
|
*/
|
|
2254
2555
|
export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
|
|
2255
2556
|
const size: i32 = toI32(data.length)
|
|
2256
|
-
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
2557
|
+
if (off < 0 || len < 0 || off > size || toU32(len) > toU32(size) - toU32(off)) {
|
|
2257
2558
|
panic("http1Chunk: the window is outside the buffer")
|
|
2258
2559
|
}
|
|
2259
2560
|
if (len === 0) {
|
|
2260
2561
|
return []
|
|
2261
2562
|
}
|
|
2262
|
-
|
|
2263
|
-
|
|
2563
|
+
// Built by `push`, as `http1WriteChunk` would write it.
|
|
2564
|
+
const out: u8[] = []
|
|
2565
|
+
for (let d: i32 = http1HexDigits(len); d > 0; d -= 1) {
|
|
2566
|
+
const nibble: u32 = (toU32(len) >> (toU32(d - 1) * toU32(4))) & toU32(15)
|
|
2567
|
+
out.push(toU8(nibble < 10 ? toU32(48) + nibble : toU32(87) + nibble))
|
|
2568
|
+
}
|
|
2569
|
+
out.push(13)
|
|
2570
|
+
out.push(10)
|
|
2571
|
+
// The window is inside `data`, checked above, so the sum is an i32.
|
|
2572
|
+
const stop: i32 = http1Past(off, len)
|
|
2573
|
+
for (let k: i32 = off; k >= 0 && k < toI32(data.length) && k < stop; k += 1) {
|
|
2574
|
+
out.push(data[k])
|
|
2575
|
+
}
|
|
2576
|
+
out.push(13)
|
|
2577
|
+
out.push(10)
|
|
2264
2578
|
return out
|
|
2265
2579
|
}
|
|
2266
2580
|
|
|
@@ -2289,6 +2603,8 @@ export const http1WriteLastChunk = (out: u8[], at: i32): i32 => {
|
|
|
2289
2603
|
export const HTTP1_NO_ROOM: i32 = -1
|
|
2290
2604
|
/** `http1WriteResponseHead`: a head the writer refuses to write. */
|
|
2291
2605
|
export const HTTP1_REFUSED: i32 = -2
|
|
2606
|
+
/** `http1TryWriteChunk`: the window lies outside the body bytes; nothing is written. */
|
|
2607
|
+
export const HTTP1_BAD_WINDOW: i32 = -3
|
|
2292
2608
|
|
|
2293
2609
|
/** Whether `bytes` is a non-empty run of tchars. */
|
|
2294
2610
|
const http1BytesAreToken = (bytes: u8[]): boolean => {
|
|
@@ -2332,7 +2648,11 @@ const http1BytesAreFieldText = (bytes: u8[]): boolean => {
|
|
|
2332
2648
|
}
|
|
2333
2649
|
k += 16
|
|
2334
2650
|
}
|
|
2335
|
-
|
|
2651
|
+
const last: i32 = toI32(bytes.length) - 16
|
|
2652
|
+
return (
|
|
2653
|
+
k === n ||
|
|
2654
|
+
(last >= 0 && last <= toI32(bytes.length) - 16 && http1LanesAreFieldText(loadU8x16(bytes, last)))
|
|
2655
|
+
)
|
|
2336
2656
|
}
|
|
2337
2657
|
|
|
2338
2658
|
/**
|
|
@@ -2357,7 +2677,8 @@ const http1DecimalDigits = (n: i32): i32 => {
|
|
|
2357
2677
|
}
|
|
2358
2678
|
let digits: i32 = 4
|
|
2359
2679
|
let v: i32 = n / 10000
|
|
2360
|
-
|
|
2680
|
+
// An i32 has at most ten digits, so the second test always holds.
|
|
2681
|
+
while (v > 0 && digits < 10) {
|
|
2361
2682
|
v = v / 10
|
|
2362
2683
|
digits += 1
|
|
2363
2684
|
}
|
|
@@ -2373,29 +2694,41 @@ const http1DecimalDigits = (n: i32): i32 => {
|
|
|
2373
2694
|
*/
|
|
2374
2695
|
export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
|
|
2375
2696
|
const n: i32 = toI32(text.length)
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
}
|
|
2382
|
-
return at + n
|
|
2697
|
+
// Every caller has made sure it fits; outside `out` nothing is written.
|
|
2698
|
+
// Before its start the text is skipped, so a negative `at` writes the tail.
|
|
2699
|
+
let k: i32 = 0
|
|
2700
|
+
if (at < 0) {
|
|
2701
|
+
k = at > -n ? http1Less(0, at) : n
|
|
2383
2702
|
}
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2703
|
+
let j: i32 = http1Past(at, k)
|
|
2704
|
+
while (k >= 0 && k < n && j >= 0 && j < toI32(out.length)) {
|
|
2705
|
+
out[j] = toU8(text.charCodeAt(k))
|
|
2706
|
+
j += 1
|
|
2707
|
+
k += 1
|
|
2388
2708
|
}
|
|
2389
|
-
return at
|
|
2709
|
+
return http1Past(at, n)
|
|
2390
2710
|
}
|
|
2391
2711
|
|
|
2712
|
+
/**
|
|
2713
|
+
* `at + n` for an `n` that is not negative, wrapping as an i32 where the sum
|
|
2714
|
+
* would pass 2^31: the writers' callers measure the head first, so it never
|
|
2715
|
+
* does for them.
|
|
2716
|
+
*/
|
|
2717
|
+
const http1Past = (at: i32, n: i32): i32 => toI32(toU32(at) + toU32(n))
|
|
2718
|
+
|
|
2719
|
+
/**
|
|
2720
|
+
* `at - n`, wrapping as an i32 where the difference would leave its range:
|
|
2721
|
+
* the parser takes it only between offsets in one buffer, so it never does.
|
|
2722
|
+
*/
|
|
2723
|
+
const http1Less = (at: i32, n: i32): i32 => toI32(toU32(at) - toU32(n))
|
|
2724
|
+
|
|
2392
2725
|
/** Writes CR LF at `out[at ..)`, likewise: two stores, where `http1PutText` walks a string. */
|
|
2393
2726
|
const http1PutCrlf = (out: u8[], at: i32): i32 => {
|
|
2394
2727
|
if (at >= 0 && at < toI32(out.length) - 1) {
|
|
2395
2728
|
out[at] = 13
|
|
2396
2729
|
out[at + 1] = 10
|
|
2397
2730
|
}
|
|
2398
|
-
return at
|
|
2731
|
+
return http1Past(at, 2)
|
|
2399
2732
|
}
|
|
2400
2733
|
|
|
2401
2734
|
/** Writes `: ` at `out[at ..)`, likewise. */
|
|
@@ -2404,22 +2737,20 @@ const http1PutColonSpace = (out: u8[], at: i32): i32 => {
|
|
|
2404
2737
|
out[at] = 58
|
|
2405
2738
|
out[at + 1] = 32
|
|
2406
2739
|
}
|
|
2407
|
-
return at
|
|
2740
|
+
return http1Past(at, 2)
|
|
2408
2741
|
}
|
|
2409
2742
|
|
|
2410
2743
|
/** Writes `bytes` at `out[at ..)`, likewise, as one copy when it fits. */
|
|
2411
2744
|
const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
|
|
2412
2745
|
const n: i32 = toI32(bytes.length)
|
|
2413
|
-
if (at
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
if (at + k >= 0) {
|
|
2419
|
-
out[at + k] = bytes[k]
|
|
2746
|
+
if (!tryCopyBytes(out, at, bytes, 0, n)) {
|
|
2747
|
+
let j: i32 = at
|
|
2748
|
+
for (let k: i32 = 0; k < n && k < toI32(bytes.length) && j >= 0 && j < toI32(out.length); k += 1) {
|
|
2749
|
+
out[j] = bytes[k]
|
|
2750
|
+
j += 1
|
|
2420
2751
|
}
|
|
2421
2752
|
}
|
|
2422
|
-
return at
|
|
2753
|
+
return http1Past(at, n)
|
|
2423
2754
|
}
|
|
2424
2755
|
|
|
2425
2756
|
/**
|
|
@@ -2430,9 +2761,9 @@ const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
|
|
|
2430
2761
|
* `text` are overwritten before the head is done. Byte by byte otherwise.
|
|
2431
2762
|
*/
|
|
2432
2763
|
const http1PutFixed = (out: u8[], at: i32, limit: i32, v: u8x16, text: string): i32 => {
|
|
2433
|
-
if (at >= 0 && at <= limit - 16 &&
|
|
2764
|
+
if (at >= 0 && limit >= 16 && at <= limit - 16 && at <= toI32(out.length) - 16) {
|
|
2434
2765
|
store(v, out, at)
|
|
2435
|
-
return at
|
|
2766
|
+
return http1Past(at, toI32(text.length))
|
|
2436
2767
|
}
|
|
2437
2768
|
return http1PutText(out, at, text)
|
|
2438
2769
|
}
|
|
@@ -2446,7 +2777,7 @@ const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 =>
|
|
|
2446
2777
|
const hundreds: i32 = status / 100
|
|
2447
2778
|
const tens: i32 = (status / 10) % 10
|
|
2448
2779
|
const ones: i32 = status % 10
|
|
2449
|
-
if (at >= 0 && at <= limit - 16 &&
|
|
2780
|
+
if (at >= 0 && limit >= 16 && at <= limit - 16 && at <= toI32(out.length) - 16) {
|
|
2450
2781
|
// "HTTP/1.1 000 ", and three lanes the head's next bytes overwrite.
|
|
2451
2782
|
const line: u8x16 = u8x16(72, 84, 84, 80, 47, 49, 46, 49, 32, 48, 48, 48, 32, 32, 32, 32)
|
|
2452
2783
|
store(
|
|
@@ -2454,7 +2785,7 @@ const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 =>
|
|
|
2454
2785
|
out,
|
|
2455
2786
|
at
|
|
2456
2787
|
)
|
|
2457
|
-
return at
|
|
2788
|
+
return http1Past(at, 13)
|
|
2458
2789
|
}
|
|
2459
2790
|
const end: i32 = http1PutText(out, at, "HTTP/1.1 ")
|
|
2460
2791
|
if (end >= 0 && end < toI32(out.length) - 3) {
|
|
@@ -2463,19 +2794,21 @@ const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 =>
|
|
|
2463
2794
|
out[end + 2] = toU8(48 + ones)
|
|
2464
2795
|
out[end + 3] = 32
|
|
2465
2796
|
}
|
|
2466
|
-
return end
|
|
2797
|
+
return http1Past(end, 4)
|
|
2467
2798
|
}
|
|
2468
2799
|
|
|
2469
2800
|
/** Writes the decimal digits of `n`, which is not negative, at `out[at ..)`, likewise. */
|
|
2470
2801
|
const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
2471
2802
|
const digits: i32 = http1DecimalDigits(n)
|
|
2803
|
+
const end: i32 = http1Past(at, digits)
|
|
2472
2804
|
let v: i32 = n
|
|
2473
|
-
|
|
2805
|
+
// The digits are written last first, `out[k - 1]` each time.
|
|
2806
|
+
for (let k: i32 = end; k > 0 && k > at && k <= toI32(out.length); k -= 1) {
|
|
2474
2807
|
const digit: i32 = v % 10
|
|
2475
|
-
out[k] = toU8(48 + digit)
|
|
2808
|
+
out[k - 1] = toU8(48 + digit)
|
|
2476
2809
|
v = v / 10
|
|
2477
2810
|
}
|
|
2478
|
-
return
|
|
2811
|
+
return end
|
|
2479
2812
|
}
|
|
2480
2813
|
|
|
2481
2814
|
/**
|
|
@@ -2533,7 +2866,7 @@ export const http1WriteResponseHead = (
|
|
|
2533
2866
|
// "HTTP/1.1 " status SP reason CRLF, then each field, then the framing
|
|
2534
2867
|
// field, the connection field and the empty line: counted before anything
|
|
2535
2868
|
// is written, in 64 bits so that no caller's lengths can wrap the sum.
|
|
2536
|
-
let need:
|
|
2869
|
+
let need: u64 = toU64(17) + toU64(reason.length)
|
|
2537
2870
|
let dated: boolean = false
|
|
2538
2871
|
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
2539
2872
|
const name: u8[] = names[i]
|
|
@@ -2554,26 +2887,27 @@ export const http1WriteResponseHead = (
|
|
|
2554
2887
|
}
|
|
2555
2888
|
dated = true
|
|
2556
2889
|
}
|
|
2557
|
-
need = need +
|
|
2890
|
+
need = need + toU64(name.length) + toU64(value.length) + toU64(4)
|
|
2558
2891
|
}
|
|
2559
2892
|
const sendDate: boolean = status >= 200 && toI32(date.length) > 0 && !dated
|
|
2560
2893
|
if (sendDate) {
|
|
2561
2894
|
// "Date: " value CRLF: 37 bytes for an IMF-fixdate.
|
|
2562
|
-
need = need +
|
|
2895
|
+
need = need + toU64(8) + toU64(date.length)
|
|
2563
2896
|
}
|
|
2564
2897
|
if (bodyLength >= 0) {
|
|
2565
|
-
need = need +
|
|
2898
|
+
need = need + toU64(18) + toU64(http1DecimalDigits(bodyLength))
|
|
2566
2899
|
} else if (bodyLength === HTTP1_CHUNKED) {
|
|
2567
|
-
need = need +
|
|
2900
|
+
need = need + toU64(28)
|
|
2568
2901
|
}
|
|
2569
2902
|
if (toI32(connection.length) > 0) {
|
|
2570
|
-
need = need +
|
|
2903
|
+
need = need + toU64(14) + toU64(connection.length)
|
|
2571
2904
|
}
|
|
2572
|
-
if (at < 0 || at > toI32(out.length) || need >
|
|
2905
|
+
if (at < 0 || at > toI32(out.length) || need > toU64(toI32(out.length) - at)) {
|
|
2573
2906
|
return HTTP1_NO_ROOM
|
|
2574
2907
|
}
|
|
2575
2908
|
// A status is three digits, checked above: no division loop for them.
|
|
2576
|
-
|
|
2909
|
+
// `need` fits in the room past `at`, so the end is an i32.
|
|
2910
|
+
const limit: i32 = http1Past(at, toI32(need))
|
|
2577
2911
|
let end: i32 = http1PutStatusLine(out, at, limit, status)
|
|
2578
2912
|
end = http1PutText(out, end, reason)
|
|
2579
2913
|
end = http1PutCrlf(out, end)
|