@amritk/nish-x86_64-linux 0.33.0 → 0.34.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/INSTALL.md +10 -8
- package/bin/nish +0 -0
- package/package.json +1 -1
- package/runtime/core.d.ts +83 -6
- package/runtime/core.mjs +90 -0
- package/runtime/nish.d.ts +83 -6
- package/runtime/nish.h +27 -4
- package/runtime/runtime-chacha20.c +366 -0
- package/scripts/build.sh +12 -9
- package/std/README.md +1 -1
- package/std/bytes.ts +26 -3
- package/std/crypto/base64url.ts +42 -21
- package/std/crypto/bcrypt.ts +222 -183
- package/std/net/http1-codec.ts +260 -62
- package/std/net/http1-server.ts +62 -47
- package/std/net/postgres.ts +50 -30
package/std/net/http1-codec.ts
CHANGED
|
@@ -81,7 +81,25 @@
|
|
|
81
81
|
* Written from RFC 9112 and RFC 9110, not ported from another implementation.
|
|
82
82
|
*/
|
|
83
83
|
import { copyBytes } from "nish/bytes"
|
|
84
|
-
import {
|
|
84
|
+
import {
|
|
85
|
+
and,
|
|
86
|
+
any,
|
|
87
|
+
bitmask,
|
|
88
|
+
eq,
|
|
89
|
+
extract,
|
|
90
|
+
gt,
|
|
91
|
+
loadU8x16,
|
|
92
|
+
lt,
|
|
93
|
+
mask8x16,
|
|
94
|
+
ne,
|
|
95
|
+
or,
|
|
96
|
+
replace,
|
|
97
|
+
select,
|
|
98
|
+
splatU8x16,
|
|
99
|
+
store,
|
|
100
|
+
subWrapping,
|
|
101
|
+
u8x16,
|
|
102
|
+
} from "nish:simd"
|
|
85
103
|
|
|
86
104
|
/** `next()`: the input so far holds no further event; feed more. */
|
|
87
105
|
export const HTTP1_NEED_MORE: i32 = 0
|
|
@@ -506,17 +524,19 @@ const http1StartRequest = (p: Http1Parser): void => {
|
|
|
506
524
|
}
|
|
507
525
|
|
|
508
526
|
/**
|
|
509
|
-
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
* `
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
527
|
+
* The bytes of `x` below 32 or equal to 127 — a control byte or DEL, which
|
|
528
|
+
* `http1TakeLine` stops at — as their high bits: zero when there is none,
|
|
529
|
+
* and else the lowest set bit is the first such byte's. Two of the bit
|
|
530
|
+
* tricks of "Bit Twiddling Hacks" (S. E. Anderson, public domain): a byte
|
|
531
|
+
* below `n` leaves its high bit set in `(x - n * ones) & ~x`, for `n` up to
|
|
532
|
+
* 128, and `127` is the byte that is zero in `x ^ 127 * ones`. A byte from
|
|
533
|
+
* 128 up has its high bit set in `x`, so `~x` clears it: obs-text never
|
|
534
|
+
* matches. A borrow only runs up from a byte that matched, so the bits above
|
|
535
|
+
* the first match may be false and the lowest is exact, in each mask and so
|
|
536
|
+
* in their union. Each subtraction is masked to 64 bits because a `u64` is a
|
|
537
|
+
* BigInt under Node, which does not wrap; natively the mask is a no-op.
|
|
518
538
|
*/
|
|
519
|
-
const
|
|
539
|
+
const http1ControlBits = (x: u64): u64 => {
|
|
520
540
|
const ones32: u64 = toU64(16843009)
|
|
521
541
|
const ones: u64 = (ones32 << toU64(32)) | ones32
|
|
522
542
|
const low32: u64 = 0xffffffff
|
|
@@ -525,7 +545,18 @@ const http1WordHasControl = (x: u64): boolean => {
|
|
|
525
545
|
const below: u64 = (x - ones * toU64(32)) & all & ~x & high
|
|
526
546
|
const y: u64 = x ^ (ones * toU64(127))
|
|
527
547
|
const del: u64 = (y - ones) & all & ~y & high
|
|
528
|
-
return
|
|
548
|
+
return below | del
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** Which byte of a word `bits`'s lowest set bit is in, 0 to 7; `bits` is not zero. */
|
|
552
|
+
const http1FirstByte = (bits: u64): i32 => {
|
|
553
|
+
const mask32: u64 = 0xffffffff
|
|
554
|
+
const lo: u32 = toU32(bits & mask32)
|
|
555
|
+
if (lo !== toU32(0)) {
|
|
556
|
+
return (toI32(31) - toI32(Math.clz32(lo & (toU32(0) - lo)))) >> toI32(3)
|
|
557
|
+
}
|
|
558
|
+
const hi: u32 = toU32(bits >> toU64(32))
|
|
559
|
+
return toI32(4) + ((toI32(31) - toI32(Math.clz32(hi & (toU32(0) - hi)))) >> toI32(3))
|
|
529
560
|
}
|
|
530
561
|
|
|
531
562
|
/**
|
|
@@ -585,22 +616,39 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
585
616
|
}
|
|
586
617
|
while (i < stop) {
|
|
587
618
|
// Most bytes are printable: sixteen at a time while a whole vector is,
|
|
588
|
-
//
|
|
589
|
-
//
|
|
590
|
-
|
|
619
|
+
// and the vector that is not answers where its first control byte is.
|
|
620
|
+
// The bytes after a control byte, and a tail shorter than a vector, go
|
|
621
|
+
// one at a time.
|
|
622
|
+
let found: boolean = false
|
|
623
|
+
while (!found && i >= 0 && i <= stop - 16 && i <= toI32(buf.length) - 16) {
|
|
591
624
|
const v: u8x16 = loadU8x16(buf, i)
|
|
592
|
-
|
|
593
|
-
|
|
625
|
+
const low: u64 = http1ControlBits(http1Lanes(v, 0))
|
|
626
|
+
if (low !== toU64(0)) {
|
|
627
|
+
i += http1FirstByte(low)
|
|
628
|
+
found = true
|
|
629
|
+
} else {
|
|
630
|
+
const high: u64 = http1ControlBits(http1Lanes(v, 8))
|
|
631
|
+
if (high !== toU64(0)) {
|
|
632
|
+
i += 8 + http1FirstByte(high)
|
|
633
|
+
found = true
|
|
634
|
+
} else {
|
|
635
|
+
i += 16
|
|
636
|
+
}
|
|
594
637
|
}
|
|
595
|
-
i += 16
|
|
596
638
|
}
|
|
597
|
-
while (i >= 0 && i < stop && i < toI32(buf.length) && buf[i] >= 32 && buf[i] !== 127) {
|
|
639
|
+
while (!found && i >= 0 && i < stop && i < toI32(buf.length) && buf[i] >= 32 && buf[i] !== 127) {
|
|
598
640
|
i += 1
|
|
599
641
|
}
|
|
600
642
|
if (i < 0 || i >= stop || i >= toI32(buf.length)) {
|
|
601
643
|
break
|
|
602
644
|
}
|
|
603
|
-
|
|
645
|
+
let c: i32 = toI32(buf[i])
|
|
646
|
+
if (c === 13 && i + 1 < stop && i + 1 < toI32(buf.length) && buf[i + 1] === 10) {
|
|
647
|
+
// CRLF, the end of almost every line: its LF is the next byte, so it
|
|
648
|
+
// is taken here rather than by another trip through the scan.
|
|
649
|
+
i += 1
|
|
650
|
+
c = 10
|
|
651
|
+
}
|
|
604
652
|
if (c === 10) {
|
|
605
653
|
if (i === p.start || buf[i - 1] !== 13) {
|
|
606
654
|
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
@@ -636,9 +684,12 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
636
684
|
* `c - 64` and `c - 96` of each: `!` `#`-`'` `*` `+` `-` `.` and the digits
|
|
637
685
|
* in the first (0x03ff6cfa); the upper-case letters, `^` and `_` in the
|
|
638
686
|
* second (0xc7fffffe); `` ` ``, the lower-case letters, `|` and `~` in the
|
|
639
|
-
* third (0x57ffffff). No byte below 33 or above 126 is a tchar.
|
|
640
|
-
*
|
|
641
|
-
*
|
|
687
|
+
* third (0x57ffffff). No byte below 33 or above 126 is a tchar.
|
|
688
|
+
* `http1IsTokenByte` reads them as two 64-bit masks, one for the bytes 0 to
|
|
689
|
+
* 63 and one for 64 to 127, so the test is a select and a shift with no
|
|
690
|
+
* branch on the byte. A field name goes sixteen bytes at a time through
|
|
691
|
+
* `http1NonTokenLanes`, the same set; this test is for the method, a trailer's
|
|
692
|
+
* name, and a name's last bytes when fewer than sixteen are left in the head.
|
|
642
693
|
*/
|
|
643
694
|
const HTTP1_TCHAR_32: i32 = 67071226
|
|
644
695
|
const HTTP1_TCHAR_64: i32 = -939524098
|
|
@@ -646,16 +697,70 @@ const HTTP1_TCHAR_96: i32 = 1476395007
|
|
|
646
697
|
|
|
647
698
|
/** Whether `c` is a tchar of RFC 9110 §5.6.2, the bytes a token is made of. */
|
|
648
699
|
export const http1IsTokenByte = (c: i32): boolean => {
|
|
649
|
-
|
|
650
|
-
|
|
700
|
+
const low: u64 = toU64(toU32(HTTP1_TCHAR_32)) << toU64(32)
|
|
701
|
+
const high: u64 = (toU64(toU32(HTTP1_TCHAR_96)) << toU64(32)) | toU64(toU32(HTTP1_TCHAR_64))
|
|
702
|
+
const word: u64 = c < 64 ? low : high
|
|
703
|
+
return c >= 0 && c < 128 && ((word >> toU64(c & 63)) & toU64(1)) !== toU64(0)
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Which lanes of `v` are not tchars, as `http1IsTokenByte` answers for each,
|
|
708
|
+
* bit `k` for lane `k`: a byte outside 33 to 126, or one of the seventeen
|
|
709
|
+
* delimiters among them, `"` `(` `)` `,` `/` `:` to `@` (seven), `[` `\` `]`
|
|
710
|
+
* `{` and `}`. A name is mostly one vector, so these compares run once for
|
|
711
|
+
* the sixteen bytes where the table test ran once for each.
|
|
712
|
+
*/
|
|
713
|
+
const http1NonTokenLanes = (v: u8x16): i32 => {
|
|
714
|
+
const outside: mask8x16 = gt(subWrapping(v, splatU8x16(33)), splatU8x16(93))
|
|
715
|
+
const quote: mask8x16 = or(eq(v, splatU8x16(34)), lt(subWrapping(v, splatU8x16(40)), splatU8x16(2)))
|
|
716
|
+
const comma: mask8x16 = or(eq(v, splatU8x16(44)), eq(v, splatU8x16(47)))
|
|
717
|
+
const colon: mask8x16 = lt(subWrapping(v, splatU8x16(58)), splatU8x16(7))
|
|
718
|
+
const bracket: mask8x16 = lt(subWrapping(v, splatU8x16(91)), splatU8x16(3))
|
|
719
|
+
const brace: mask8x16 = or(eq(v, splatU8x16(123)), eq(v, splatU8x16(125)))
|
|
720
|
+
return bitmask(or(or(or(outside, quote), or(comma, colon)), or(bracket, brace)))
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Lowercases a field name in `head` from `k`, sixteen bytes at a time, and
|
|
725
|
+
* answers where it ends: the first byte before `stop` that is not a tchar, or
|
|
726
|
+
* `stop`. Each vector's name lanes are lowered, and every other lane is
|
|
727
|
+
* stored back as it was, so the value after the name and the bytes past the
|
|
728
|
+
* line keep their case. A tail of under sixteen bytes before `head`'s end
|
|
729
|
+
* goes a byte at a time. The answer and the bytes written are the byte loop's.
|
|
730
|
+
*/
|
|
731
|
+
const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
732
|
+
// Lane `j` holds `j`: the name's lanes are those below its length.
|
|
733
|
+
const lanes: u8x16 = u8x16(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
|
|
734
|
+
let k: i32 = k0
|
|
735
|
+
while (k >= 0 && k < stop && k <= toI32(head.length) - 16) {
|
|
736
|
+
const v: u8x16 = loadU8x16(head, k)
|
|
737
|
+
const bad: i32 = http1NonTokenLanes(v)
|
|
738
|
+
let run: i32 = 16
|
|
739
|
+
if (bad !== 0) {
|
|
740
|
+
run = toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
741
|
+
}
|
|
742
|
+
if (run > stop - k) {
|
|
743
|
+
run = stop - k
|
|
744
|
+
}
|
|
745
|
+
const upper: mask8x16 = and(
|
|
746
|
+
lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)),
|
|
747
|
+
lt(lanes, splatU8x16(toU8(run)))
|
|
748
|
+
)
|
|
749
|
+
store(select(upper, or(v, splatU8x16(32)), v), head, k)
|
|
750
|
+
k += run
|
|
751
|
+
if (run < 16) {
|
|
752
|
+
return k
|
|
753
|
+
}
|
|
651
754
|
}
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
755
|
+
while (k >= 0 && k < stop && k < toI32(head.length)) {
|
|
756
|
+
const c: i32 = toI32(head[k])
|
|
757
|
+
if (!http1IsTokenByte(c)) {
|
|
758
|
+
break
|
|
759
|
+
}
|
|
760
|
+
head[k] = toU8(c >= 65 && c <= 90 ? c + 32 : c)
|
|
761
|
+
k += 1
|
|
657
762
|
}
|
|
658
|
-
return
|
|
763
|
+
return k
|
|
659
764
|
}
|
|
660
765
|
|
|
661
766
|
/** Whether `c` may appear in a field value (RFC 9110 §5.5): HTAB, SP, VCHAR or obs-text. */
|
|
@@ -978,9 +1083,19 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
|
978
1083
|
if (a >= 0 && a < b && (buf[a] === 32 || buf[a] === 9)) {
|
|
979
1084
|
return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
|
|
980
1085
|
}
|
|
1086
|
+
// A kept line is copied into the head first, and its name is read there,
|
|
1087
|
+
// each upper-case letter lowered as the scan passes it: one walk over the
|
|
1088
|
+
// name, not a check of `buf` and then a second walk of the copy. The copy
|
|
1089
|
+
// of a line refused below is never read, since a refusal ends the parse.
|
|
1090
|
+
let at: i32 = 0
|
|
981
1091
|
let i: i32 = a
|
|
982
|
-
|
|
983
|
-
|
|
1092
|
+
if (keep) {
|
|
1093
|
+
at = http1HeadAppend(p, a, b)
|
|
1094
|
+
i = a + (http1LowerName(p.head, at, at + (b - a)) - at)
|
|
1095
|
+
} else {
|
|
1096
|
+
while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1097
|
+
i += 1
|
|
1098
|
+
}
|
|
984
1099
|
}
|
|
985
1100
|
if (i === a || i >= b || buf[i] !== 58) {
|
|
986
1101
|
return http1Fail(p, 400, "a malformed field name")
|
|
@@ -999,19 +1114,17 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
|
999
1114
|
if (keep) {
|
|
1000
1115
|
let valueStart: i32 = nameEnd + 1
|
|
1001
1116
|
let valueEnd: i32 = b
|
|
1002
|
-
while (
|
|
1117
|
+
while (
|
|
1118
|
+
valueStart >= 0 &&
|
|
1119
|
+
valueStart < valueEnd &&
|
|
1120
|
+
valueStart < toI32(buf.length) &&
|
|
1121
|
+
http1IsOws(toI32(buf[valueStart]))
|
|
1122
|
+
) {
|
|
1003
1123
|
valueStart += 1
|
|
1004
1124
|
}
|
|
1005
1125
|
while (valueEnd > valueStart && valueEnd - 1 >= 0 && http1IsOws(toI32(buf[valueEnd - 1]))) {
|
|
1006
1126
|
valueEnd -= 1
|
|
1007
1127
|
}
|
|
1008
|
-
const at: i32 = http1HeadAppend(p, a, b)
|
|
1009
|
-
const head: u8[] = p.head
|
|
1010
|
-
for (let k: i32 = at; k < at + (nameEnd - a) && k < toI32(head.length); k += 1) {
|
|
1011
|
-
if (k >= 0 && head[k] >= 65 && head[k] <= 90) {
|
|
1012
|
-
head[k] = head[k] + 32
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
1128
|
const base: i32 = 4 * p.headerCount
|
|
1016
1129
|
http1SpanSet(p, base + 3, at + (valueEnd - a))
|
|
1017
1130
|
// The last of the four is set first, so the array has room for them all.
|
|
@@ -1551,14 +1664,39 @@ const http1BytesAreToken = (bytes: u8[]): boolean => {
|
|
|
1551
1664
|
return toI32(bytes.length) > 0
|
|
1552
1665
|
}
|
|
1553
1666
|
|
|
1554
|
-
/**
|
|
1667
|
+
/**
|
|
1668
|
+
* Whether no lane of `v` is a byte a field value may not hold: below 32 and
|
|
1669
|
+
* not HTAB, or DEL. `http1IsValueByte` on sixteen bytes at once, exactly, so
|
|
1670
|
+
* a vector may overlap one already checked.
|
|
1671
|
+
*/
|
|
1672
|
+
const http1LanesAreFieldText = (v: u8x16): boolean => {
|
|
1673
|
+
const control: mask8x16 = and(lt(v, splatU8x16(32)), ne(v, splatU8x16(9)))
|
|
1674
|
+
return !any(or(control, eq(v, splatU8x16(127))))
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
/**
|
|
1678
|
+
* Whether every byte of `bytes` may stand in a field value: sixteen at a
|
|
1679
|
+
* time, the last vector ending at the last byte and overlapping the one
|
|
1680
|
+
* before it, and one at a time below sixteen.
|
|
1681
|
+
*/
|
|
1555
1682
|
const http1BytesAreFieldText = (bytes: u8[]): boolean => {
|
|
1556
|
-
|
|
1557
|
-
|
|
1683
|
+
const n: i32 = toI32(bytes.length)
|
|
1684
|
+
if (n < 16) {
|
|
1685
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
1686
|
+
if (!http1IsValueByte(toI32(bytes[k]))) {
|
|
1687
|
+
return false
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
return true
|
|
1691
|
+
}
|
|
1692
|
+
let k: i32 = 0
|
|
1693
|
+
while (k >= 0 && k <= n - 16) {
|
|
1694
|
+
if (!http1LanesAreFieldText(loadU8x16(bytes, k))) {
|
|
1558
1695
|
return false
|
|
1559
1696
|
}
|
|
1697
|
+
k += 16
|
|
1560
1698
|
}
|
|
1561
|
-
return
|
|
1699
|
+
return k === n || http1LanesAreFieldText(loadU8x16(bytes, n - 16))
|
|
1562
1700
|
}
|
|
1563
1701
|
|
|
1564
1702
|
/** Whether the octets of `bytes` spell `word`, which is lowercase, in any ASCII case: a field name compared as RFC 9110 §5.1 says. */
|
|
@@ -1588,6 +1726,14 @@ const http1DecimalDigits = (n: i32): i32 => {
|
|
|
1588
1726
|
/** Writes `text` at `out[at ..)`, which the caller has made sure holds it, and answers where it ends. */
|
|
1589
1727
|
export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
|
|
1590
1728
|
const n: i32 = toI32(text.length)
|
|
1729
|
+
if (at >= 0 && at <= toI32(out.length) - n) {
|
|
1730
|
+
// It fits, the case every caller makes sure of: one test, then a loop
|
|
1731
|
+
// with no bounds check in it.
|
|
1732
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
1733
|
+
out[at + k] = toU8(text.charCodeAt(k))
|
|
1734
|
+
}
|
|
1735
|
+
return at + n
|
|
1736
|
+
}
|
|
1591
1737
|
for (let k: i32 = 0; k < n && at + k < toI32(out.length); k += 1) {
|
|
1592
1738
|
if (at + k >= 0) {
|
|
1593
1739
|
out[at + k] = toU8(text.charCodeAt(k))
|
|
@@ -1629,6 +1775,50 @@ const http1PutBytes = (out: u8[], at: i32, bytes: u8[]): i32 => {
|
|
|
1629
1775
|
return at + n
|
|
1630
1776
|
}
|
|
1631
1777
|
|
|
1778
|
+
/**
|
|
1779
|
+
* Writes `text`, at most sixteen ASCII bytes, at `out[at ..)`, likewise, as
|
|
1780
|
+
* one sixteen-byte store of `v`, which holds `text` in its first lanes: when
|
|
1781
|
+
* `at + 16` does not pass `limit`, the end of the head being written, whose
|
|
1782
|
+
* every byte from `at` on is written after this one, so the lanes past
|
|
1783
|
+
* `text` are overwritten before the head is done. Byte by byte otherwise.
|
|
1784
|
+
*/
|
|
1785
|
+
const http1PutFixed = (out: u8[], at: i32, limit: i32, v: u8x16, text: string): i32 => {
|
|
1786
|
+
if (at >= 0 && at <= limit - 16 && limit <= toI32(out.length)) {
|
|
1787
|
+
store(v, out, at)
|
|
1788
|
+
return at + toI32(text.length)
|
|
1789
|
+
}
|
|
1790
|
+
return http1PutText(out, at, text)
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
/**
|
|
1794
|
+
* Writes `HTTP/1.1 `, the three digits of `status`, which is 100 to 599, and
|
|
1795
|
+
* a space at `out[at ..)`, likewise: one store, the digits replacing three
|
|
1796
|
+
* lanes of the constant.
|
|
1797
|
+
*/
|
|
1798
|
+
const http1PutStatusLine = (out: u8[], at: i32, limit: i32, status: i32): i32 => {
|
|
1799
|
+
const hundreds: i32 = status / 100
|
|
1800
|
+
const tens: i32 = (status / 10) % 10
|
|
1801
|
+
const ones: i32 = status % 10
|
|
1802
|
+
if (at >= 0 && at <= limit - 16 && limit <= toI32(out.length)) {
|
|
1803
|
+
// "HTTP/1.1 000 ", and three lanes the head's next bytes overwrite.
|
|
1804
|
+
const line: u8x16 = u8x16(72, 84, 84, 80, 47, 49, 46, 49, 32, 48, 48, 48, 32, 32, 32, 32)
|
|
1805
|
+
store(
|
|
1806
|
+
replace(replace(replace(line, 9, toU8(48 + hundreds)), 10, toU8(48 + tens)), 11, toU8(48 + ones)),
|
|
1807
|
+
out,
|
|
1808
|
+
at
|
|
1809
|
+
)
|
|
1810
|
+
return at + 13
|
|
1811
|
+
}
|
|
1812
|
+
const end: i32 = http1PutText(out, at, "HTTP/1.1 ")
|
|
1813
|
+
if (end >= 0 && end < toI32(out.length) - 3) {
|
|
1814
|
+
out[end] = toU8(48 + hundreds)
|
|
1815
|
+
out[end + 1] = toU8(48 + tens)
|
|
1816
|
+
out[end + 2] = toU8(48 + ones)
|
|
1817
|
+
out[end + 3] = 32
|
|
1818
|
+
}
|
|
1819
|
+
return end + 4
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1632
1822
|
/** Writes the decimal digits of `n`, which is not negative, at `out[at ..)`, likewise. */
|
|
1633
1823
|
const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
1634
1824
|
const digits: i32 = http1DecimalDigits(n)
|
|
@@ -1685,7 +1875,7 @@ export const http1WriteResponseHead = (
|
|
|
1685
1875
|
// is written, in 64 bits so that no caller's lengths can wrap the sum.
|
|
1686
1876
|
let need: i64 = toI64(17) + toI64(reason.length)
|
|
1687
1877
|
let sendDate: boolean = status >= 200 && toI32(date.length) > 0
|
|
1688
|
-
for (let i: i32 = 0; i <
|
|
1878
|
+
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
1689
1879
|
const name: u8[] = names[i]
|
|
1690
1880
|
const value: u8[] = values[i]
|
|
1691
1881
|
if (!http1BytesAreToken(name) || !http1BytesAreFieldText(value)) {
|
|
@@ -1717,41 +1907,49 @@ export const http1WriteResponseHead = (
|
|
|
1717
1907
|
if (at < 0 || at > toI32(out.length) || need > toI64(toI32(out.length) - at)) {
|
|
1718
1908
|
return HTTP1_NO_ROOM
|
|
1719
1909
|
}
|
|
1720
|
-
let end: i32 = http1PutText(out, at, "HTTP/1.1 ")
|
|
1721
1910
|
// A status is three digits, checked above: no division loop for them.
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
const tens: i32 = status / 10
|
|
1725
|
-
const tensDigit: i32 = tens % 10
|
|
1726
|
-
const ones: i32 = status % 10
|
|
1727
|
-
out[end] = toU8(48 + hundreds)
|
|
1728
|
-
out[end + 1] = toU8(48 + tensDigit)
|
|
1729
|
-
out[end + 2] = toU8(48 + ones)
|
|
1730
|
-
out[end + 3] = 32
|
|
1731
|
-
}
|
|
1732
|
-
end = end + 4
|
|
1911
|
+
const limit: i32 = at + toI32(need)
|
|
1912
|
+
let end: i32 = http1PutStatusLine(out, at, limit, status)
|
|
1733
1913
|
end = http1PutText(out, end, reason)
|
|
1734
1914
|
end = http1PutCrlf(out, end)
|
|
1735
|
-
for (let i: i32 = 0; i <
|
|
1915
|
+
for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
|
|
1736
1916
|
end = http1PutBytes(out, end, names[i])
|
|
1737
1917
|
end = http1PutColonSpace(out, end)
|
|
1738
1918
|
end = http1PutBytes(out, end, values[i])
|
|
1739
1919
|
end = http1PutCrlf(out, end)
|
|
1740
1920
|
}
|
|
1741
1921
|
if (sendDate) {
|
|
1742
|
-
end =
|
|
1922
|
+
end = http1PutFixed(
|
|
1923
|
+
out,
|
|
1924
|
+
end,
|
|
1925
|
+
limit,
|
|
1926
|
+
u8x16(68, 97, 116, 101, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32),
|
|
1927
|
+
"Date: "
|
|
1928
|
+
)
|
|
1743
1929
|
end = http1PutBytes(out, end, date)
|
|
1744
1930
|
end = http1PutCrlf(out, end)
|
|
1745
1931
|
}
|
|
1746
1932
|
if (bodyLength >= 0) {
|
|
1747
|
-
end =
|
|
1933
|
+
end = http1PutFixed(
|
|
1934
|
+
out,
|
|
1935
|
+
end,
|
|
1936
|
+
limit,
|
|
1937
|
+
u8x16(67, 111, 110, 116, 101, 110, 116, 45, 76, 101, 110, 103, 116, 104, 58, 32),
|
|
1938
|
+
"Content-Length: "
|
|
1939
|
+
)
|
|
1748
1940
|
end = http1PutDecimal(out, end, bodyLength)
|
|
1749
1941
|
end = http1PutCrlf(out, end)
|
|
1750
1942
|
} else if (bodyLength === HTTP1_CHUNKED) {
|
|
1751
1943
|
end = http1PutText(out, end, "Transfer-Encoding: chunked\r\n")
|
|
1752
1944
|
}
|
|
1753
1945
|
if (toI32(connection.length) > 0) {
|
|
1754
|
-
end =
|
|
1946
|
+
end = http1PutFixed(
|
|
1947
|
+
out,
|
|
1948
|
+
end,
|
|
1949
|
+
limit,
|
|
1950
|
+
u8x16(67, 111, 110, 110, 101, 99, 116, 105, 111, 110, 58, 32, 32, 32, 32, 32),
|
|
1951
|
+
"Connection: "
|
|
1952
|
+
)
|
|
1755
1953
|
end = http1PutText(out, end, connection)
|
|
1756
1954
|
end = http1PutCrlf(out, end)
|
|
1757
1955
|
}
|
package/std/net/http1-server.ts
CHANGED
|
@@ -525,28 +525,19 @@ export class Http1OutputPool {
|
|
|
525
525
|
* `output[outputStart .. outputEnd)` for its carrier to send.
|
|
526
526
|
*/
|
|
527
527
|
export class Http1Connection {
|
|
528
|
-
//
|
|
529
|
-
//
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
* put another, to share it or to pin its clock.
|
|
536
|
-
*/
|
|
537
|
-
date: HttpDate
|
|
528
|
+
// What a request reads and writes comes first, so that `inputRoom`, `feed`,
|
|
529
|
+
// `next`, `respond`, `write`, `end`, `consume`, `release` and `park` touch
|
|
530
|
+
// the front of the object: the parser, the output and its pool, the clocks,
|
|
531
|
+
// the state and the flags. What a WebSocket, an error or a configured cap
|
|
532
|
+
// needs follows. Within each part the layout packs as widest first does:
|
|
533
|
+
// the narrow fields of the front end on a 4-byte boundary and the cold
|
|
534
|
+
// 32-bit fields bring the references behind them to an 8-byte one.
|
|
538
535
|
/**
|
|
539
536
|
* The request parser: its head (`methodIs`, `targetIs`, `header`, spans) is
|
|
540
537
|
* the current request's from its `H1_REQUEST` until its response is done,
|
|
541
538
|
* `H1_END` included, however much of a pipelined request is fed behind it.
|
|
542
539
|
*/
|
|
543
540
|
parser: Http1Parser
|
|
544
|
-
/**
|
|
545
|
-
* The frame decoder: made when the slot first switches to WebSocket, so a
|
|
546
|
-
* slot that only ever speaks HTTP never holds its buffers, then kept for
|
|
547
|
-
* the slot's later peers.
|
|
548
|
-
*/
|
|
549
|
-
ws: WsDecoder | null
|
|
550
541
|
/**
|
|
551
542
|
* Bytes to send are `output[outputStart .. outputEnd)`. Empty until the
|
|
552
543
|
* connection first needs room to write: then its own buffer, made once, or
|
|
@@ -560,27 +551,17 @@ export class Http1Connection {
|
|
|
560
551
|
/** Where the parser's input and head buffers go while the slot rests, with `Http1Config.idleInput`; null otherwise. */
|
|
561
552
|
inputs: Http1OutputPool | null
|
|
562
553
|
heads: Http1OutputPool | null
|
|
563
|
-
/**
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
554
|
+
/**
|
|
555
|
+
* The `Date` every final response carries. The connection makes its own;
|
|
556
|
+
* `Http1Server` and `Http1TlsServer` put theirs here, so that every slot of a
|
|
557
|
+
* pool shares one and the pool formats at most once a second. A program may
|
|
558
|
+
* put another, to share it or to pin its clock.
|
|
559
|
+
*/
|
|
560
|
+
date: HttpDate
|
|
569
561
|
/** When the carrier last moved a byte for the connection, in `monotonicNanos`. */
|
|
570
562
|
lastActive: i64 = 0
|
|
571
563
|
/** When the head being read began to arrive, in `monotonicNanos`; meaningful while `headTimed`. */
|
|
572
564
|
headSince: i64 = 0
|
|
573
|
-
outputStart: i32 = 0
|
|
574
|
-
outputEnd: i32 = 0
|
|
575
|
-
dataStart: i32 = 0
|
|
576
|
-
dataLength: i32 = 0
|
|
577
|
-
/** `H1_WS_MESSAGE`'s opcode: text or binary. */
|
|
578
|
-
opcode: i32 = 0
|
|
579
|
-
/** `H1_ERROR`'s HTTP status or WebSocket close code, or `H1_WS_CLOSE`'s close code. */
|
|
580
|
-
status: i32 = 0
|
|
581
|
-
/** The most input the connection buffers, unread: the cap `inputRoom` holds it to. */
|
|
582
|
-
bufferLimit: i32 = 0
|
|
583
|
-
pongLength: i32 = 0
|
|
584
565
|
state: i32 = 0
|
|
585
566
|
response: i32 = 0
|
|
586
567
|
/**
|
|
@@ -590,12 +571,23 @@ export class Http1Connection {
|
|
|
590
571
|
responseFraming: i32 = 0
|
|
591
572
|
/** A response with a length: the bytes it still owes. */
|
|
592
573
|
responseLeft: i32 = 0
|
|
593
|
-
|
|
594
|
-
|
|
574
|
+
outputStart: i32 = 0
|
|
575
|
+
outputEnd: i32 = 0
|
|
576
|
+
/**
|
|
577
|
+
* `output.length`, kept beside the offsets so that `acquire` and `release`
|
|
578
|
+
* learn whether the connection holds a buffer without reading an array:
|
|
579
|
+
* the empty one is out of cache by the time a resting connection's next
|
|
580
|
+
* request comes. Only the connection assigns `output`, and with it this.
|
|
581
|
+
*/
|
|
582
|
+
outputCap: i32 = 0
|
|
583
|
+
/** The most input the connection buffers, unread: the cap `inputRoom` holds it to. */
|
|
584
|
+
bufferLimit: i32 = 0
|
|
595
585
|
/** A request's head has been read and its exchange is not over. */
|
|
596
586
|
requestOpen: boolean = false
|
|
597
587
|
/** The request's body has been read to its end. */
|
|
598
588
|
requestEnded: boolean = false
|
|
589
|
+
/** The response carries no body (HEAD, 204, 304): what is written is dropped. */
|
|
590
|
+
responseEmpty: boolean = false
|
|
599
591
|
/** The connection closes once this exchange is over. */
|
|
600
592
|
closeAfter: boolean = false
|
|
601
593
|
/** The program accepted the request's WebSocket handshake. */
|
|
@@ -607,6 +599,13 @@ export class Http1Connection {
|
|
|
607
599
|
inputEnded: boolean = false
|
|
608
600
|
/** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
|
|
609
601
|
headTimed: boolean = false
|
|
602
|
+
dataStart: i32 = 0
|
|
603
|
+
dataLength: i32 = 0
|
|
604
|
+
/** `H1_WS_MESSAGE`'s opcode: text or binary. */
|
|
605
|
+
opcode: i32 = 0
|
|
606
|
+
/** `H1_ERROR`'s HTTP status or WebSocket close code, or `H1_WS_CLOSE`'s close code. */
|
|
607
|
+
status: i32 = 0
|
|
608
|
+
pongLength: i32 = 0
|
|
610
609
|
/**
|
|
611
610
|
* The TCP maximum segment size the program sets for this connection, for
|
|
612
611
|
* `respondEncoded` under `HttpCompressor.segmentMode`; 0, the default,
|
|
@@ -616,6 +615,19 @@ export class Http1Connection {
|
|
|
616
615
|
mss: i32 = 0
|
|
617
616
|
/** The MSS `Http1Server.accept` read from this connection's socket (`netMss`), or 0 when it reports none. */
|
|
618
617
|
socketMss: i32 = 0
|
|
618
|
+
config: Http1Config
|
|
619
|
+
/**
|
|
620
|
+
* The frame decoder: made when the slot first switches to WebSocket, so a
|
|
621
|
+
* slot that only ever speaks HTTP never holds its buffers, then kept for
|
|
622
|
+
* the slot's later peers.
|
|
623
|
+
*/
|
|
624
|
+
ws: WsDecoder | null
|
|
625
|
+
/** `H1_BODY`'s and `H1_WS_MESSAGE`'s bytes. */
|
|
626
|
+
data: u8[]
|
|
627
|
+
/** A ping's payload, kept until its pong fits: empty until the first ping. */
|
|
628
|
+
pong: u8[]
|
|
629
|
+
/** No fields, for the responses the connection writes itself: made once. */
|
|
630
|
+
noFields: u8[][]
|
|
619
631
|
|
|
620
632
|
/** A connection under `config`. A cap outside what the connection can work with panics. */
|
|
621
633
|
constructor(config: Http1Config) {
|
|
@@ -759,10 +771,15 @@ export class Http1Connection {
|
|
|
759
771
|
if (this.writeHeld || this.inputEnded || this.state === H1_S_CLOSING || this.state === H1_S_FAILED) {
|
|
760
772
|
return 0
|
|
761
773
|
}
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
774
|
+
let room: i32 = 0
|
|
775
|
+
if (this.state === H1_S_WEBSOCKET) {
|
|
776
|
+
// Only a WebSocket reads the decoder and the configured message cap,
|
|
777
|
+
// so a request's check stays on the connection's front fields.
|
|
778
|
+
const ws: WsDecoder | null = this.ws
|
|
779
|
+
room = this.inputLimit() - (ws !== null ? ws.end - ws.start : this.parser.buffered())
|
|
780
|
+
} else {
|
|
781
|
+
room = this.bufferLimit - this.parser.buffered()
|
|
782
|
+
}
|
|
766
783
|
return room > 0 ? room : 0
|
|
767
784
|
}
|
|
768
785
|
|
|
@@ -809,11 +826,13 @@ export class Http1Connection {
|
|
|
809
826
|
* the pool has no free buffer, or for a connection's own, once.
|
|
810
827
|
*/
|
|
811
828
|
acquire(): void {
|
|
812
|
-
if (
|
|
829
|
+
if (this.outputCap > 0) {
|
|
813
830
|
return
|
|
814
831
|
}
|
|
815
832
|
const pool: Http1OutputPool | null = this.pool
|
|
816
|
-
|
|
833
|
+
const output: u8[] = pool !== null ? pool.take() : new Array<u8>(this.config.outputSize)
|
|
834
|
+
this.output = output
|
|
835
|
+
this.outputCap = toI32(output.length)
|
|
817
836
|
this.outputStart = 0
|
|
818
837
|
this.outputEnd = 0
|
|
819
838
|
}
|
|
@@ -825,16 +844,12 @@ export class Http1Connection {
|
|
|
825
844
|
*/
|
|
826
845
|
release(): void {
|
|
827
846
|
const pool: Http1OutputPool | null = this.pool
|
|
828
|
-
if (
|
|
829
|
-
pool === null ||
|
|
830
|
-
toI32(this.output.length) === 0 ||
|
|
831
|
-
this.outputEnd > this.outputStart ||
|
|
832
|
-
this.writeHeld
|
|
833
|
-
) {
|
|
847
|
+
if (pool === null || this.outputCap === 0 || this.outputEnd > this.outputStart || this.writeHeld) {
|
|
834
848
|
return
|
|
835
849
|
}
|
|
836
850
|
pool.give(this.output)
|
|
837
851
|
this.output = this.noOutput
|
|
852
|
+
this.outputCap = 0
|
|
838
853
|
this.outputStart = 0
|
|
839
854
|
this.outputEnd = 0
|
|
840
855
|
}
|