@amritk/nish-x86_64-linux 0.38.0 → 0.40.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 +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/net/http1-codec.ts
CHANGED
|
@@ -35,7 +35,8 @@
|
|
|
35
35
|
* (§2.2); whitespace before the first field or an obs-fold (§2.2, §5.2);
|
|
36
36
|
* whitespace between a field name and its colon (§5.1); a field value with a
|
|
37
37
|
* control character; a 1.1 request without exactly one `Host`, or a 1.0
|
|
38
|
-
* request with more than one (§3.2, RFC 9110 §7.2)
|
|
38
|
+
* request with more than one (§3.2, RFC 9110 §7.2), or a `Host` that is not
|
|
39
|
+
* `uri-host [":" port]` (§3.2, by `urlIsHostPort`); `Content-Length` that is
|
|
39
40
|
* not one run of digits, or that appears twice (§6.3); `Transfer-Encoding`
|
|
40
41
|
* together with `Content-Length`, in a 1.0 request, or whose last coding is
|
|
41
42
|
* not `chunked` (§6.1, §6.3); a malformed chunk size, chunk extension or
|
|
@@ -81,6 +82,7 @@
|
|
|
81
82
|
* Written from RFC 9112 and RFC 9110, not ported from another implementation.
|
|
82
83
|
*/
|
|
83
84
|
import { copyBytes } from "nish/bytes"
|
|
85
|
+
import { urlIsHostPort } from "nish/net/url"
|
|
84
86
|
import {
|
|
85
87
|
and,
|
|
86
88
|
any,
|
|
@@ -182,8 +184,8 @@ export class Http1Parser {
|
|
|
182
184
|
// `buffered`, `http1TakeLine` and the head's lines move, then the flags,
|
|
183
185
|
// then the rest of the request's numbers and the caller's limits. What only
|
|
184
186
|
// `keepText`, an error or a chunked body reads follows. Each group packs as
|
|
185
|
-
// widest first does, and the flags' two bytes of padding are the only ones
|
|
186
|
-
//
|
|
187
|
+
// widest first does, and the flags' two bytes of padding are the only ones
|
|
188
|
+
// inside it: its fields sum to 228 bytes, which round to 232.
|
|
187
189
|
|
|
188
190
|
// --- the request's buffers -------------------------------------------------
|
|
189
191
|
/**
|
|
@@ -194,10 +196,14 @@ export class Http1Parser {
|
|
|
194
196
|
*/
|
|
195
197
|
buf: u8[]
|
|
196
198
|
/**
|
|
197
|
-
* The request line
|
|
199
|
+
* The request line (method to version, no CRLF) and then the header
|
|
200
|
+
* section's lines with their CRLFs, the closing CRLF last, copied out of
|
|
198
201
|
* `buf` as each is read, names in lowercase: what the spans below index.
|
|
199
|
-
*
|
|
200
|
-
*
|
|
202
|
+
* A field's line runs from its name's start to its value's end, any OWS
|
|
203
|
+
* around the value, and a CRLF; nothing else is in the head. The bytes from
|
|
204
|
+
* `headLength` on are slack: the section scan stores a line's first bytes
|
|
205
|
+
* there before the line is whole. Kept from one request to the next, so a
|
|
206
|
+
* slot that has seen its largest head allocates nothing for the next one.
|
|
201
207
|
*/
|
|
202
208
|
head: u8[]
|
|
203
209
|
/** Four entries per field, into `head`: name start and end, value start and end (OWS trimmed). */
|
|
@@ -245,7 +251,10 @@ export class Http1Parser {
|
|
|
245
251
|
lineEnd: i32 = 0
|
|
246
252
|
/** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
|
|
247
253
|
headerCount: i32 = 0
|
|
248
|
-
/**
|
|
254
|
+
/**
|
|
255
|
+
* How much of `head` the request uses: the request line, each kept field
|
|
256
|
+
* line and its CRLF, and the closing CRLF, so `headerCount + 1` CRLFs.
|
|
257
|
+
*/
|
|
249
258
|
headLength: i32 = 0
|
|
250
259
|
/**
|
|
251
260
|
* Whether the line being scanned holds a byte a field value may not: a
|
|
@@ -289,6 +298,46 @@ export class Http1Parser {
|
|
|
289
298
|
* @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
|
|
290
299
|
*/
|
|
291
300
|
fieldCount: i32 = 0
|
|
301
|
+
/**
|
|
302
|
+
* Host fields the request's head has kept so far.
|
|
303
|
+
* @internal Counted by the field-line parser in this module as each name is lowered, and read by `http1EndOfHead`.
|
|
304
|
+
*/
|
|
305
|
+
hostFields: i32 = 0
|
|
306
|
+
/**
|
|
307
|
+
* The index of the last Host field, or -1.
|
|
308
|
+
* @internal Set by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
309
|
+
*/
|
|
310
|
+
hostField: i32 = -1
|
|
311
|
+
/**
|
|
312
|
+
* Content-Length fields the request's head has kept so far.
|
|
313
|
+
* @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
314
|
+
*/
|
|
315
|
+
lengthFields: i32 = 0
|
|
316
|
+
/**
|
|
317
|
+
* The index of the last Content-Length field, or -1.
|
|
318
|
+
* @internal Set by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
319
|
+
*/
|
|
320
|
+
lengthField: i32 = -1
|
|
321
|
+
/**
|
|
322
|
+
* Transfer-Encoding fields the request's head has kept so far.
|
|
323
|
+
* @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
324
|
+
*/
|
|
325
|
+
codingFields: i32 = 0
|
|
326
|
+
/**
|
|
327
|
+
* Upgrade fields the request's head has kept so far.
|
|
328
|
+
* @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
329
|
+
*/
|
|
330
|
+
upgradeFields: i32 = 0
|
|
331
|
+
/**
|
|
332
|
+
* The bytes of the Upgrade fields' values, OWS trimmed, summed.
|
|
333
|
+
* @internal Counted by the field-line parser in this module, and read by `http1EndOfHead`.
|
|
334
|
+
*/
|
|
335
|
+
upgradeValueBytes: i32 = 0
|
|
336
|
+
/**
|
|
337
|
+
* The `HTTP1_CONNECTION_` options the Connection fields so far name.
|
|
338
|
+
* @internal Gathered by the field-line parser in this module as each Connection field is read, and read by `http1EndOfHead`.
|
|
339
|
+
*/
|
|
340
|
+
connectionOptions: i32 = 0
|
|
292
341
|
/** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
|
|
293
342
|
minor: i32 = 1
|
|
294
343
|
/**
|
|
@@ -380,15 +429,18 @@ export class Http1Parser {
|
|
|
380
429
|
// head kept, at HTTP1_FIRST_INPUT and HTTP1_FIRST_HEAD bytes: a parser
|
|
381
430
|
// that is built and never fed (a pool's spare slot) costs neither, and a
|
|
382
431
|
// caller that lends them out between requests puts empty ones here.
|
|
383
|
-
// The spans
|
|
384
|
-
//
|
|
385
|
-
//
|
|
386
|
-
|
|
432
|
+
// The spans likewise wait for the first field line (`http1SpanSet`). An
|
|
433
|
+
// empty array cannot be written, only replaced, so one serves for both
|
|
434
|
+
// buffers. The lists share one only until the `restart` below, which
|
|
435
|
+
// replaces both while `keepText` is on, as it is here even for a parser
|
|
436
|
+
// `Http1Connection` turns it off on: the sharing saves one empty array,
|
|
437
|
+
// not both.
|
|
438
|
+
this.spans = []
|
|
387
439
|
this.buf = []
|
|
388
440
|
this.body = this.buf
|
|
389
|
-
this.head =
|
|
441
|
+
this.head = this.buf
|
|
390
442
|
this.names = []
|
|
391
|
-
this.values =
|
|
443
|
+
this.values = this.names
|
|
392
444
|
this.tally = new Http1ListTally()
|
|
393
445
|
this.restart()
|
|
394
446
|
}
|
|
@@ -601,6 +653,8 @@ export class Http1Parser {
|
|
|
601
653
|
export const HTTP1_FIRST_INPUT: i32 = 1024
|
|
602
654
|
/** The head buffer a parser makes for its first head. */
|
|
603
655
|
export const HTTP1_FIRST_HEAD: i32 = 256
|
|
656
|
+
/** The spans a parser makes at its first field line: four per field, room for sixteen. */
|
|
657
|
+
const HTTP1_FIRST_SPANS: i32 = 64
|
|
604
658
|
|
|
605
659
|
/** Makes room for `len` more bytes at `p.end`, moving the live bytes down first. */
|
|
606
660
|
const http1Reserve = (p: Http1Parser, len: i32): void => {
|
|
@@ -667,6 +721,14 @@ const http1StartRequest = (p: Http1Parser): void => {
|
|
|
667
721
|
p.bodyTotal = 0
|
|
668
722
|
p.sectionBytes = 0
|
|
669
723
|
p.fieldCount = 0
|
|
724
|
+
p.hostFields = 0
|
|
725
|
+
p.hostField = -1
|
|
726
|
+
p.lengthFields = 0
|
|
727
|
+
p.lengthField = -1
|
|
728
|
+
p.codingFields = 0
|
|
729
|
+
p.upgradeFields = 0
|
|
730
|
+
p.upgradeValueBytes = 0
|
|
731
|
+
p.connectionOptions = 0
|
|
670
732
|
p.headHeld = false
|
|
671
733
|
}
|
|
672
734
|
|
|
@@ -910,13 +972,21 @@ const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean):
|
|
|
910
972
|
if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
|
|
911
973
|
return false
|
|
912
974
|
}
|
|
913
|
-
|
|
975
|
+
if (!fold) {
|
|
976
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
977
|
+
if (toI32(word.charCodeAt(k)) !== toI32(bytes[a + k])) {
|
|
978
|
+
return false
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return true
|
|
982
|
+
}
|
|
983
|
+
for (let k: i32 = 0; k < n; k += 1) {
|
|
914
984
|
let c: i32 = toI32(word.charCodeAt(k))
|
|
915
985
|
let d: i32 = toI32(bytes[a + k])
|
|
916
|
-
if (
|
|
986
|
+
if (c >= 65 && c <= 90) {
|
|
917
987
|
c += 32
|
|
918
988
|
}
|
|
919
|
-
if (
|
|
989
|
+
if (d >= 65 && d <= 90) {
|
|
920
990
|
d += 32
|
|
921
991
|
}
|
|
922
992
|
if (c !== d) {
|
|
@@ -927,51 +997,70 @@ const http1HeadIs = (bytes: u8[], a: i32, b: i32, word: string, fold: boolean):
|
|
|
927
997
|
}
|
|
928
998
|
|
|
929
999
|
/**
|
|
930
|
-
*
|
|
931
|
-
*
|
|
1000
|
+
* Grows `p.head` so that `n` more bytes and sixteen of slack fit past
|
|
1001
|
+
* `p.headLength`, keeping every byte already there, those `http1Section` has
|
|
1002
|
+
* copied past `headLength` for a line not yet whole included: the rare path
|
|
1003
|
+
* of `http1HeadAppend` and of the section scan's copy, apart so that the
|
|
1004
|
+
* common one stays small enough to inline.
|
|
932
1005
|
*/
|
|
933
|
-
const
|
|
934
|
-
const n: i32 = to - from
|
|
1006
|
+
const http1HeadGrow = (p: Http1Parser, n: i32): void => {
|
|
935
1007
|
const at: i32 = p.headLength
|
|
936
1008
|
let size: i32 = toI32(p.head.length)
|
|
937
|
-
if (
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
panic("Http1Parser: a head of more than 2^31 - 1 bytes")
|
|
944
|
-
}
|
|
945
|
-
size = size * 2
|
|
946
|
-
}
|
|
947
|
-
const grown: u8[] = new Array<u8>(size)
|
|
948
|
-
const old: u8[] = p.head
|
|
949
|
-
for (let k: i32 = 0; k < at && k < toI32(grown.length) && k < toI32(old.length); k += 1) {
|
|
950
|
-
grown[k] = old[k]
|
|
1009
|
+
if (size < HTTP1_FIRST_HEAD) {
|
|
1010
|
+
size = HTTP1_FIRST_HEAD
|
|
1011
|
+
}
|
|
1012
|
+
while (size - at - 16 < n) {
|
|
1013
|
+
if (size > 1073741823) {
|
|
1014
|
+
panic("Http1Parser: a head of more than 2^31 - 1 bytes")
|
|
951
1015
|
}
|
|
952
|
-
|
|
1016
|
+
size = size * 2
|
|
1017
|
+
}
|
|
1018
|
+
const grown: u8[] = new Array<u8>(size)
|
|
1019
|
+
const old: u8[] = p.head
|
|
1020
|
+
const kept: i32 = toI32(old.length) < size ? toI32(old.length) : size
|
|
1021
|
+
if (kept > 0) {
|
|
1022
|
+
copyBytes(grown, 0, old, 0, kept)
|
|
953
1023
|
}
|
|
1024
|
+
p.head = grown
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Copies `buf[from .. to)` to the end of `head` and answers where the copy
|
|
1029
|
+
* starts: the request line, the one line the section scan does not copy
|
|
1030
|
+
* itself. It relies on the caller having called `http1HeadGrow` when the
|
|
1031
|
+
* line would leave fewer than sixteen bytes of slack; without that it copies
|
|
1032
|
+
* nothing and only moves `headLength`. The head keeps sixteen bytes past its
|
|
1033
|
+
* last line, so a short line is copied as whole vectors and a vector load at
|
|
1034
|
+
* any byte of the head is in bounds: the name and value compares read one. A
|
|
1035
|
+
* vector's lanes past the line write bytes past `headLength`, which nothing
|
|
1036
|
+
* reads until the section scan overwrites them.
|
|
1037
|
+
*/
|
|
1038
|
+
const http1HeadAppend = (p: Http1Parser, from: i32, to: i32): i32 => {
|
|
1039
|
+
const n: i32 = to - from
|
|
1040
|
+
const at: i32 = p.headLength
|
|
954
1041
|
const head: u8[] = p.head
|
|
955
1042
|
const buf: u8[] = p.buf
|
|
956
|
-
|
|
957
|
-
|
|
1043
|
+
// A line of up to 32 bytes is one or two vector stores; a longer one, or
|
|
1044
|
+
// one that ends within a vector of `buf`'s end, is one `copyBytes`, which
|
|
1045
|
+
// outruns a loop of stores at a Cookie or User-Agent line's length.
|
|
1046
|
+
const room: i32 = n <= 16 ? 16 : 32
|
|
1047
|
+
if (n <= 32 && from >= 0 && from <= toI32(buf.length) - room && at <= toI32(head.length) - room) {
|
|
1048
|
+
store(loadU8x16(buf, from), head, at)
|
|
1049
|
+
if (n > 16) {
|
|
1050
|
+
store(loadU8x16(buf, from + 16), head, at + 16)
|
|
1051
|
+
}
|
|
1052
|
+
} else if (n > 0 && from >= 0 && from <= toI32(buf.length) - n && at <= toI32(head.length) - n) {
|
|
958
1053
|
copyBytes(head, at, buf, from, n)
|
|
959
|
-
} else {
|
|
960
|
-
for (let k: i32 = 0; k < n && at + k < toI32(head.length) && from + k < toI32(buf.length); k += 1) {
|
|
961
|
-
if (from + k >= 0) {
|
|
962
|
-
head[at + k] = buf[from + k]
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
1054
|
}
|
|
966
1055
|
p.headLength = at + n
|
|
967
1056
|
return at
|
|
968
1057
|
}
|
|
969
1058
|
|
|
970
|
-
/** Sets `spans[at]`, growing the array when it must. */
|
|
1059
|
+
/** Sets `spans[at]`, growing the array when it must: first to `HTTP1_FIRST_SPANS`, then by doubling. */
|
|
971
1060
|
const http1SpanSet = (p: Http1Parser, at: i32, value: i32): void => {
|
|
972
1061
|
if (at >= toI32(p.spans.length)) {
|
|
973
1062
|
const old: i32[] = p.spans
|
|
974
|
-
const grown: i32[] = new Array<i32>(2 * at + 4)
|
|
1063
|
+
const grown: i32[] = new Array<i32>(at < HTTP1_FIRST_SPANS ? HTTP1_FIRST_SPANS : 2 * at + 4)
|
|
975
1064
|
for (let k: i32 = 0; k < toI32(old.length) && k < toI32(grown.length); k += 1) {
|
|
976
1065
|
grown[k] = old[k]
|
|
977
1066
|
}
|
|
@@ -1032,53 +1121,134 @@ const http1Tally = (p: Http1Parser, name: string, word: string): Http1ListTally
|
|
|
1032
1121
|
return tally
|
|
1033
1122
|
}
|
|
1034
1123
|
|
|
1035
|
-
/** `
|
|
1124
|
+
/** `http1FramingField`: a Host field. */
|
|
1125
|
+
const HTTP1_FIELD_HOST: i32 = 1
|
|
1126
|
+
/** `http1FramingField`: a Content-Length field. */
|
|
1127
|
+
const HTTP1_FIELD_LENGTH: i32 = 2
|
|
1128
|
+
/** `http1FramingField`: a Transfer-Encoding field. */
|
|
1129
|
+
const HTTP1_FIELD_CODING: i32 = 3
|
|
1130
|
+
/** `http1FramingField`: a Connection field. */
|
|
1131
|
+
const HTTP1_FIELD_CONNECTION: i32 = 4
|
|
1132
|
+
/** `http1FramingField`: an Upgrade field. */
|
|
1133
|
+
const HTTP1_FIELD_UPGRADE: i32 = 5
|
|
1134
|
+
|
|
1135
|
+
/**
|
|
1136
|
+
* Whether `head[at .. at + n)` is `word`'s first `n` bytes, `n` at most 16,
|
|
1137
|
+
* each upper-case ASCII letter of the head lowered when `fold`: one vector
|
|
1138
|
+
* compare, or `http1HeadIs` when fewer than sixteen bytes follow `at`.
|
|
1139
|
+
*/
|
|
1140
|
+
const http1HeadIsVector = (
|
|
1141
|
+
head: u8[],
|
|
1142
|
+
at: i32,
|
|
1143
|
+
n: i32,
|
|
1144
|
+
word: u8x16,
|
|
1145
|
+
text: string,
|
|
1146
|
+
fold: boolean
|
|
1147
|
+
): boolean => {
|
|
1148
|
+
if (at < 0 || at > toI32(head.length) - 16 || n > 16) {
|
|
1149
|
+
return http1HeadIs(head, at, at + n, text, fold)
|
|
1150
|
+
}
|
|
1151
|
+
let v: u8x16 = loadU8x16(head, at)
|
|
1152
|
+
if (fold) {
|
|
1153
|
+
v = select(lt(subWrapping(v, splatU8x16(65)), splatU8x16(26)), or(v, splatU8x16(32)), v)
|
|
1154
|
+
}
|
|
1155
|
+
const want: i32 = n >= 16 ? 65535 : (1 << n) - 1
|
|
1156
|
+
return (bitmask(eq(v, word)) & want) === want
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* Which of the fields that frame a message (RFC 9112 §6, §9.6, RFC 9110 §7.2,
|
|
1161
|
+
* §7.8) the lowercase name `head[at .. at + n)` is, as an `HTTP1_FIELD_`
|
|
1162
|
+
* kind, or 0. Every kept field line asks once, as its name is read, so the
|
|
1163
|
+
* end of the head counts them without reading a name again; the length picks
|
|
1164
|
+
* the one name a field could be, and one vector compare answers.
|
|
1165
|
+
*/
|
|
1166
|
+
const http1FramingField = (head: u8[], at: i32, n: i32): i32 => {
|
|
1167
|
+
if (n === 4) {
|
|
1168
|
+
const host: u8x16 = u8x16(104, 111, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
1169
|
+
return http1HeadIsVector(head, at, n, host, "host", false) ? HTTP1_FIELD_HOST : 0
|
|
1170
|
+
}
|
|
1171
|
+
if (n === 7) {
|
|
1172
|
+
const upgrade: u8x16 = u8x16(117, 112, 103, 114, 97, 100, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
1173
|
+
return http1HeadIsVector(head, at, n, upgrade, "upgrade", false) ? HTTP1_FIELD_UPGRADE : 0
|
|
1174
|
+
}
|
|
1175
|
+
if (n === 10) {
|
|
1176
|
+
const connection: u8x16 = u8x16(99, 111, 110, 110, 101, 99, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0)
|
|
1177
|
+
return http1HeadIsVector(head, at, n, connection, "connection", false) ? HTTP1_FIELD_CONNECTION : 0
|
|
1178
|
+
}
|
|
1179
|
+
if (n === 14) {
|
|
1180
|
+
const length: u8x16 = u8x16(99, 111, 110, 116, 101, 110, 116, 45, 108, 101, 110, 103, 116, 104, 0, 0)
|
|
1181
|
+
return http1HeadIsVector(head, at, n, length, "content-length", false) ? HTTP1_FIELD_LENGTH : 0
|
|
1182
|
+
}
|
|
1183
|
+
if (n === 17) {
|
|
1184
|
+
// "transfer-encodin" in the vector, and the "g" after it.
|
|
1185
|
+
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 + 16 < toI32(head.length) && head[at + 16] === 103
|
|
1187
|
+
return g && http1HeadIsVector(head, at, 16, coding, "transfer-encodin", false) ? HTTP1_FIELD_CODING : 0
|
|
1188
|
+
}
|
|
1189
|
+
return 0
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/** `http1ConnectionList`: the Connection list names `close`. */
|
|
1036
1193
|
const HTTP1_CONNECTION_CLOSE: i32 = 1
|
|
1037
|
-
/** `
|
|
1194
|
+
/** `http1ConnectionList`: the Connection list names `keep-alive`. */
|
|
1038
1195
|
const HTTP1_CONNECTION_KEEP_ALIVE: i32 = 2
|
|
1039
|
-
/** `
|
|
1196
|
+
/** `http1ConnectionList`: the Connection list names `upgrade`. */
|
|
1040
1197
|
const HTTP1_CONNECTION_UPGRADE: i32 = 4
|
|
1041
1198
|
|
|
1042
1199
|
/**
|
|
1043
|
-
* Which of `close`, `keep-alive` and `upgrade` the Connection
|
|
1044
|
-
* in any case, as `HTTP1_CONNECTION_` bits
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1200
|
+
* Which of `close`, `keep-alive` and `upgrade` the Connection value
|
|
1201
|
+
* `head[from .. to)` names, in any case, as `HTTP1_CONNECTION_` bits. The
|
|
1202
|
+
* commas are found sixteen bytes at a time; each element is trimmed of OWS and
|
|
1203
|
+
* compared whole (RFC 9110 §5.6.1, §7.6.1), its length picking the one word it
|
|
1204
|
+
* could be, and empty elements are skipped.
|
|
1047
1205
|
*/
|
|
1048
|
-
const
|
|
1049
|
-
const
|
|
1050
|
-
const
|
|
1206
|
+
const http1ConnectionList = (head: u8[], from: i32, to: i32): i32 => {
|
|
1207
|
+
const close: u8x16 = u8x16(99, 108, 111, 115, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
1208
|
+
const keepAlive: u8x16 = u8x16(107, 101, 101, 112, 45, 97, 108, 105, 118, 101, 0, 0, 0, 0, 0, 0)
|
|
1209
|
+
const upgrade: u8x16 = u8x16(117, 112, 103, 114, 97, 100, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
1210
|
+
const length: i32 = toI32(head.length)
|
|
1051
1211
|
let found: i32 = 0
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
}
|
|
1071
|
-
// The length picks the one word an element could be.
|
|
1072
|
-
const n: i32 = b - a
|
|
1073
|
-
if (n === 5 && http1HeadIs(head, a, b, "close", true)) {
|
|
1074
|
-
found = found | HTTP1_CONNECTION_CLOSE
|
|
1075
|
-
} else if (n === 10 && http1HeadIs(head, a, b, "keep-alive", true)) {
|
|
1076
|
-
found = found | HTTP1_CONNECTION_KEEP_ALIVE
|
|
1077
|
-
} else if (n === 7 && http1HeadIs(head, a, b, "upgrade", true)) {
|
|
1078
|
-
found = found | HTTP1_CONNECTION_UPGRADE
|
|
1212
|
+
let start: i32 = from
|
|
1213
|
+
while (start >= 0 && start <= to) {
|
|
1214
|
+
// The element runs to the next comma, or to the value's end.
|
|
1215
|
+
let end: i32 = start
|
|
1216
|
+
let seeking: boolean = true
|
|
1217
|
+
while (seeking && end < to) {
|
|
1218
|
+
if (end <= length - 16) {
|
|
1219
|
+
const commas: i32 = bitmask(eq(loadU8x16(head, end), splatU8x16(44)))
|
|
1220
|
+
if (commas !== 0) {
|
|
1221
|
+
end += toI32(31) - toI32(Math.clz32(toU32(commas & -commas)))
|
|
1222
|
+
seeking = false
|
|
1223
|
+
} else {
|
|
1224
|
+
end += 16
|
|
1225
|
+
}
|
|
1226
|
+
} else if (end >= 0 && end < toI32(head.length) && head[end] !== 44) {
|
|
1227
|
+
end += 1
|
|
1228
|
+
} else {
|
|
1229
|
+
seeking = false
|
|
1079
1230
|
}
|
|
1080
|
-
start = k + 1
|
|
1081
1231
|
}
|
|
1232
|
+
if (end > to) {
|
|
1233
|
+
end = to
|
|
1234
|
+
}
|
|
1235
|
+
let a: i32 = start
|
|
1236
|
+
let b: i32 = end
|
|
1237
|
+
while (a < b && a >= 0 && a < toI32(head.length) && http1IsOws(toI32(head[a]))) {
|
|
1238
|
+
a += 1
|
|
1239
|
+
}
|
|
1240
|
+
while (b > a && b - 1 < length && http1IsOws(toI32(head[b - 1]))) {
|
|
1241
|
+
b -= 1
|
|
1242
|
+
}
|
|
1243
|
+
const n: i32 = b - a
|
|
1244
|
+
if (n === 5 && http1HeadIsVector(head, a, n, close, "close", true)) {
|
|
1245
|
+
found = found | HTTP1_CONNECTION_CLOSE
|
|
1246
|
+
} else if (n === 10 && http1HeadIsVector(head, a, n, keepAlive, "keep-alive", true)) {
|
|
1247
|
+
found = found | HTTP1_CONNECTION_KEEP_ALIVE
|
|
1248
|
+
} else if (n === 7 && http1HeadIsVector(head, a, n, upgrade, "upgrade", true)) {
|
|
1249
|
+
found = found | HTTP1_CONNECTION_UPGRADE
|
|
1250
|
+
}
|
|
1251
|
+
start = end + 1
|
|
1082
1252
|
}
|
|
1083
1253
|
return found
|
|
1084
1254
|
}
|
|
@@ -1095,8 +1265,17 @@ const http1MethodOverrun = (p: Http1Parser): boolean => {
|
|
|
1095
1265
|
}
|
|
1096
1266
|
const buf: u8[] = p.buf
|
|
1097
1267
|
const n: i32 = toI32(buf.length)
|
|
1268
|
+
// A space, CR or LF in the first sixteen bytes, buffered or not, is
|
|
1269
|
+
// before `HTTP1_MAX_METHOD`: the loop below would answer false at it, or
|
|
1270
|
+
// at `end` before it.
|
|
1271
|
+
if (p.start >= 0 && p.start <= n - 16) {
|
|
1272
|
+
const v: u8x16 = loadU8x16(buf, p.start)
|
|
1273
|
+
if (any(or(or(eq(v, splatU8x16(32)), eq(v, splatU8x16(13))), eq(v, splatU8x16(10))))) {
|
|
1274
|
+
return false
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1098
1277
|
const stop: i32 = p.start + HTTP1_MAX_METHOD
|
|
1099
|
-
for (let k: i32 = p.start; k >= 0 && k < p.end && k <
|
|
1278
|
+
for (let k: i32 = p.start; k >= 0 && k < p.end && k < toI32(buf.length); k += 1) {
|
|
1100
1279
|
if (buf[k] === 32 || buf[k] === 13 || buf[k] === 10) {
|
|
1101
1280
|
return false
|
|
1102
1281
|
}
|
|
@@ -1116,8 +1295,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1116
1295
|
const n: i32 = toI32(buf.length)
|
|
1117
1296
|
const a: i32 = p.lineStart
|
|
1118
1297
|
const b: i32 = p.lineEnd < n ? p.lineEnd : n
|
|
1298
|
+
// The method is tchars, sixteen at a time as a field name is read; the
|
|
1299
|
+
// line's CR, a control byte, ends the run if no space or other byte does.
|
|
1119
1300
|
let i: i32 = a
|
|
1120
|
-
|
|
1301
|
+
let ended: boolean = false
|
|
1302
|
+
while (!ended && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
|
|
1303
|
+
const bad: i32 = http1NonTokenLanes(loadU8x16(buf, i))
|
|
1304
|
+
if (bad !== 0) {
|
|
1305
|
+
i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
1306
|
+
ended = true
|
|
1307
|
+
} else {
|
|
1308
|
+
i += 16
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
if (i > b) {
|
|
1312
|
+
i = b
|
|
1313
|
+
}
|
|
1314
|
+
while (!ended && i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
1121
1315
|
i += 1
|
|
1122
1316
|
}
|
|
1123
1317
|
if (i === a || i - a > HTTP1_MAX_METHOD || i >= b || buf[i] !== 32) {
|
|
@@ -1153,26 +1347,38 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1153
1347
|
}
|
|
1154
1348
|
const targetEnd: i32 = i
|
|
1155
1349
|
i += 1
|
|
1156
|
-
// "HTTP/" DIGIT "." DIGIT, case-sensitive (§2.3), and the end of the line
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1350
|
+
// "HTTP/" DIGIT "." DIGIT, case-sensitive (§2.3), and the end of the line:
|
|
1351
|
+
// with sixteen bytes to load, one compare for the five letters and the dot
|
|
1352
|
+
// and one range test for the two digits, else a byte at a time.
|
|
1353
|
+
let version: boolean = false
|
|
1354
|
+
if (b - i === 8 && i >= 0 && i <= toI32(buf.length) - 16) {
|
|
1355
|
+
const v: u8x16 = loadU8x16(buf, i)
|
|
1356
|
+
const fixed: i32 = bitmask(eq(v, u8x16(72, 84, 84, 80, 47, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0)))
|
|
1357
|
+
const digits: i32 = bitmask(lt(subWrapping(v, splatU8x16(48)), splatU8x16(10)))
|
|
1358
|
+
version = (fixed & 95) === 95 && (digits & 160) === 160
|
|
1359
|
+
} else if (b - i === 8) {
|
|
1360
|
+
version =
|
|
1361
|
+
buf[i] === 72 &&
|
|
1362
|
+
buf[i + 1] === 84 &&
|
|
1363
|
+
buf[i + 2] === 84 &&
|
|
1364
|
+
buf[i + 3] === 80 &&
|
|
1365
|
+
buf[i + 4] === 47 &&
|
|
1366
|
+
buf[i + 5] >= 48 &&
|
|
1367
|
+
buf[i + 5] <= 57 &&
|
|
1368
|
+
buf[i + 6] === 46 &&
|
|
1369
|
+
buf[i + 7] >= 48 &&
|
|
1370
|
+
buf[i + 7] <= 57
|
|
1371
|
+
}
|
|
1372
|
+
if (!version) {
|
|
1170
1373
|
return http1Fail(p, 400, "a malformed HTTP version")
|
|
1171
1374
|
}
|
|
1172
1375
|
if (buf[i + 5] !== 49) {
|
|
1173
1376
|
return http1Fail(p, 505, "an HTTP major version other than 1")
|
|
1174
1377
|
}
|
|
1175
1378
|
p.minor = buf[i + 7] === 48 ? 0 : 1
|
|
1379
|
+
if (b - a > toI32(p.head.length) - p.headLength - 16) {
|
|
1380
|
+
http1HeadGrow(p, b - a)
|
|
1381
|
+
}
|
|
1176
1382
|
const at: i32 = http1HeadAppend(p, a, b)
|
|
1177
1383
|
p.methodStart = at
|
|
1178
1384
|
p.methodEnd = at + (methodEnd - a)
|
|
@@ -1188,25 +1394,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1188
1394
|
|
|
1189
1395
|
/**
|
|
1190
1396
|
* Reads one field line in `lineStart .. lineEnd` (§5) into `names` and
|
|
1191
|
-
* `values`, or, for a trailer, only checks it.
|
|
1192
|
-
*
|
|
1397
|
+
* `values`, or, for a trailer, only checks it. A kept line is already in the
|
|
1398
|
+
* head, `http1Section`'s copy of it: `buf[x]` is at `head[x + delta]`.
|
|
1399
|
+
* Answers `HTTP1_NEED_MORE` or the refusal.
|
|
1193
1400
|
*/
|
|
1194
|
-
const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
1401
|
+
const http1FieldLine = (p: Http1Parser, keep: boolean, delta: i32): i32 => {
|
|
1195
1402
|
const buf: u8[] = p.buf
|
|
1196
1403
|
const n: i32 = toI32(buf.length)
|
|
1197
1404
|
const a: i32 = p.lineStart
|
|
1198
1405
|
const b: i32 = p.lineEnd < n ? p.lineEnd : n
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
//
|
|
1203
|
-
// each upper-case letter lowered as the scan passes it: one walk over the
|
|
1204
|
-
// name, not a check of `buf` and then a second walk of the copy. The copy
|
|
1205
|
-
// of a line refused below is never read, since a refusal ends the parse.
|
|
1406
|
+
// A kept line's name is read in the head's copy, each upper-case letter
|
|
1407
|
+
// lowered as the scan passes it: one walk over the name, not a check of
|
|
1408
|
+
// `buf` and then a second walk of the copy. The copy of a line refused
|
|
1409
|
+
// below is never read, since a refusal ends the parse.
|
|
1206
1410
|
let at: i32 = 0
|
|
1207
1411
|
let i: i32 = a
|
|
1208
1412
|
if (keep) {
|
|
1209
|
-
at =
|
|
1413
|
+
at = a + delta
|
|
1210
1414
|
i = a + (http1LowerName(p.head, at, at + (b - a)) - at)
|
|
1211
1415
|
} else {
|
|
1212
1416
|
while (i >= 0 && i < b && i < toI32(buf.length) && http1IsTokenByte(toI32(buf[i]))) {
|
|
@@ -1214,6 +1418,11 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
|
1214
1418
|
}
|
|
1215
1419
|
}
|
|
1216
1420
|
if (i === a || i >= b || buf[i] !== 58) {
|
|
1421
|
+
// Whitespace is no tchar, so an obs-fold line has an empty name: its
|
|
1422
|
+
// 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)) {
|
|
1424
|
+
return http1Fail(p, 400, "a field line that starts with whitespace (obs-fold)")
|
|
1425
|
+
}
|
|
1217
1426
|
return http1Fail(p, 400, "a malformed field name")
|
|
1218
1427
|
}
|
|
1219
1428
|
const nameEnd: i32 = i
|
|
@@ -1234,13 +1443,31 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
|
1234
1443
|
valueStart >= 0 &&
|
|
1235
1444
|
valueStart < valueEnd &&
|
|
1236
1445
|
valueStart < toI32(buf.length) &&
|
|
1237
|
-
|
|
1446
|
+
(buf[valueStart] === 32 || buf[valueStart] === 9)
|
|
1238
1447
|
) {
|
|
1239
1448
|
valueStart += 1
|
|
1240
1449
|
}
|
|
1241
|
-
while (valueEnd > valueStart && valueEnd - 1
|
|
1450
|
+
while (valueEnd > valueStart && (buf[valueEnd - 1] === 32 || buf[valueEnd - 1] === 9)) {
|
|
1242
1451
|
valueEnd -= 1
|
|
1243
1452
|
}
|
|
1453
|
+
// The fields that frame the message are counted here, as their names
|
|
1454
|
+
// are read, for `http1EndOfHead`: every kept field asks the same question.
|
|
1455
|
+
const kind: i32 = http1FramingField(p.head, at, nameEnd - a)
|
|
1456
|
+
if (kind === HTTP1_FIELD_HOST) {
|
|
1457
|
+
p.hostFields += 1
|
|
1458
|
+
p.hostField = p.headerCount
|
|
1459
|
+
} else if (kind === HTTP1_FIELD_LENGTH) {
|
|
1460
|
+
p.lengthFields += 1
|
|
1461
|
+
p.lengthField = p.headerCount
|
|
1462
|
+
} else if (kind === HTTP1_FIELD_CODING) {
|
|
1463
|
+
p.codingFields += 1
|
|
1464
|
+
} else if (kind === HTTP1_FIELD_CONNECTION) {
|
|
1465
|
+
p.connectionOptions =
|
|
1466
|
+
p.connectionOptions | http1ConnectionList(p.head, at + (valueStart - a), at + (valueEnd - a))
|
|
1467
|
+
} else if (kind === HTTP1_FIELD_UPGRADE) {
|
|
1468
|
+
p.upgradeFields += 1
|
|
1469
|
+
p.upgradeValueBytes += valueEnd - valueStart
|
|
1470
|
+
}
|
|
1244
1471
|
const base: i32 = 4 * p.headerCount
|
|
1245
1472
|
http1SpanSet(p, base + 3, at + (valueEnd - a))
|
|
1246
1473
|
// The last of the four is set first, so the array has room for them all.
|
|
@@ -1261,40 +1488,32 @@ const http1FieldLine = (p: Http1Parser, keep: boolean): i32 => {
|
|
|
1261
1488
|
|
|
1262
1489
|
/**
|
|
1263
1490
|
* The header section has ended: decide the framing and the connection's fate
|
|
1264
|
-
* (§6.3, §9.3) and answer `HTTP1_HEAD`, or the refusal. It reads the
|
|
1265
|
-
* and allocates nothing unless `keepText` asks for
|
|
1491
|
+
* (§6.3, §9.3) and answer `HTTP1_HEAD`, or the refusal. It reads the counts
|
|
1492
|
+
* the field lines left, and allocates nothing unless `keepText` asks for
|
|
1493
|
+
* `upgrade` as a string.
|
|
1266
1494
|
*/
|
|
1267
1495
|
const http1EndOfHead = (p: Http1Parser): i32 => {
|
|
1268
1496
|
const spans: i32[] = p.spans
|
|
1269
1497
|
const head: u8[] = p.head
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
let codingFields: i32 = 0
|
|
1274
|
-
let upgradeFields: i32 = 0
|
|
1275
|
-
let upgradeBytes: i32 = 0
|
|
1276
|
-
let connectionFields: i32 = 0
|
|
1277
|
-
for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
|
|
1278
|
-
const a: i32 = spans[4 * i]
|
|
1279
|
-
const b: i32 = spans[4 * i + 1]
|
|
1280
|
-
if (http1HeadIs(head, a, b, "host", false)) {
|
|
1281
|
-
hosts += 1
|
|
1282
|
-
} else if (http1HeadIs(head, a, b, "content-length", false)) {
|
|
1283
|
-
lengths += 1
|
|
1284
|
-
lengthField = i
|
|
1285
|
-
} else if (http1HeadIs(head, a, b, "transfer-encoding", false)) {
|
|
1286
|
-
codingFields += 1
|
|
1287
|
-
} else if (http1HeadIs(head, a, b, "connection", false)) {
|
|
1288
|
-
connectionFields += 1
|
|
1289
|
-
} else if (http1HeadIs(head, a, b, "upgrade", false)) {
|
|
1290
|
-
upgradeFields += 1
|
|
1291
|
-
upgradeBytes += spans[4 * i + 3] - spans[4 * i + 2]
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1498
|
+
const hosts: i32 = p.hostFields
|
|
1499
|
+
const lengths: i32 = p.lengthFields
|
|
1500
|
+
const lengthField: i32 = p.lengthField
|
|
1294
1501
|
if ((p.minor === 1 && hosts !== 1) || hosts > 1) {
|
|
1295
1502
|
return http1Fail(p, 400, "a request without exactly one Host")
|
|
1296
1503
|
}
|
|
1297
|
-
|
|
1504
|
+
// The one Host must be a host and an optional port (§3.2): a value such as
|
|
1505
|
+
// `a@b`, `a/b` or `a, b` is read one way by this program and another by a
|
|
1506
|
+
// proxy or a virtual-host table in front of it. An empty Host is allowed.
|
|
1507
|
+
const hostField: i32 = p.hostField
|
|
1508
|
+
if (
|
|
1509
|
+
hosts === 1 &&
|
|
1510
|
+
hostField >= 0 &&
|
|
1511
|
+
4 * hostField + 3 < toI32(spans.length) &&
|
|
1512
|
+
!urlIsHostPort(head, spans[4 * hostField + 2], spans[4 * hostField + 3])
|
|
1513
|
+
) {
|
|
1514
|
+
return http1Fail(p, 400, "a Host that is not a host and port")
|
|
1515
|
+
}
|
|
1516
|
+
if (p.codingFields > 0) {
|
|
1298
1517
|
if (lengths > 0) {
|
|
1299
1518
|
return http1Fail(p, 400, "both Content-Length and Transfer-Encoding")
|
|
1300
1519
|
}
|
|
@@ -1329,16 +1548,14 @@ const http1EndOfHead = (p: Http1Parser): i32 => {
|
|
|
1329
1548
|
}
|
|
1330
1549
|
p.contentLength = size
|
|
1331
1550
|
}
|
|
1332
|
-
|
|
1333
|
-
// made for a request without a Connection field.
|
|
1334
|
-
const options: i32 = connectionFields > 0 ? http1ConnectionOptions(p) : 0
|
|
1551
|
+
const options: i32 = p.connectionOptions
|
|
1335
1552
|
const close: boolean = (options & HTTP1_CONNECTION_CLOSE) !== 0
|
|
1336
1553
|
const keep: boolean = (options & HTTP1_CONNECTION_KEEP_ALIVE) !== 0
|
|
1337
1554
|
const upgradeAsked: boolean = (options & HTTP1_CONNECTION_UPGRADE) !== 0
|
|
1338
1555
|
p.keepAlive = p.minor === 1 ? !close : keep && !close
|
|
1339
1556
|
// The fields of one name joined with ", " are empty only when there is one
|
|
1340
1557
|
// field and it is empty: the same test, without the join.
|
|
1341
|
-
p.upgrading = p.minor === 1 && upgradeAsked && (upgradeFields > 1 ||
|
|
1558
|
+
p.upgrading = p.minor === 1 && upgradeAsked && (p.upgradeFields > 1 || p.upgradeValueBytes > 0)
|
|
1342
1559
|
if (p.keepText && p.upgrading) {
|
|
1343
1560
|
const upgrades: string[] = []
|
|
1344
1561
|
const names: string[] = p.names
|
|
@@ -1409,9 +1626,105 @@ export const http1HexValue = (c: i32): i32 => {
|
|
|
1409
1626
|
}
|
|
1410
1627
|
|
|
1411
1628
|
/**
|
|
1412
|
-
*
|
|
1413
|
-
*
|
|
1414
|
-
*
|
|
1629
|
+
* Where the quoted-string that opens at `bytes[from]` ends (RFC 9110 §5.6.4):
|
|
1630
|
+
* the index just past its closing DQUOTE, or -1 when the line ends first or
|
|
1631
|
+
* a byte inside is neither qdtext nor a quoted-pair. A backslash takes the
|
|
1632
|
+
* byte after it whatever it is, a DQUOTE included, as long as that byte is
|
|
1633
|
+
* HTAB, SP, VCHAR or obs-text.
|
|
1634
|
+
*/
|
|
1635
|
+
const http1QuotedStringEnd = (bytes: u8[], from: i32, to: i32): i32 => {
|
|
1636
|
+
let i: i32 = from + 1
|
|
1637
|
+
while (i >= 0 && i < to && i < toI32(bytes.length)) {
|
|
1638
|
+
const c: i32 = toI32(bytes[i])
|
|
1639
|
+
if (c === 34) {
|
|
1640
|
+
return i + 1
|
|
1641
|
+
}
|
|
1642
|
+
if (c === 92) {
|
|
1643
|
+
i += 1
|
|
1644
|
+
if (i < 0 || i >= to || i >= toI32(bytes.length) || !http1IsValueByte(toI32(bytes[i]))) {
|
|
1645
|
+
return -1
|
|
1646
|
+
}
|
|
1647
|
+
} else if (!http1IsValueByte(c)) {
|
|
1648
|
+
return -1
|
|
1649
|
+
}
|
|
1650
|
+
i += 1
|
|
1651
|
+
}
|
|
1652
|
+
return -1
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/** The first index from `from` on, before `to`, that is not OWS. */
|
|
1656
|
+
const http1SkipOws = (bytes: u8[], from: i32, to: i32): i32 => {
|
|
1657
|
+
let i: i32 = from
|
|
1658
|
+
while (i >= 0 && i < to && i < toI32(bytes.length) && http1IsOws(toI32(bytes[i]))) {
|
|
1659
|
+
i += 1
|
|
1660
|
+
}
|
|
1661
|
+
return i
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
/**
|
|
1665
|
+
* Whether `bytes[from .. to)`, what follows a chunk size on its line, is the
|
|
1666
|
+
* chunk-ext of RFC 9112 §7.1.1 and nothing else:
|
|
1667
|
+
* `*( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )`, the name
|
|
1668
|
+
* a token and the value a token or a quoted-string. An empty window holds no
|
|
1669
|
+
* extensions and is valid. The extensions are not interpreted, but they are
|
|
1670
|
+
* parsed whole: a parser that skips to the line's end and one that reads a
|
|
1671
|
+
* quoted-string across it disagree about where the chunk's data starts (the
|
|
1672
|
+
* chunk-extension smuggling of Jetty's CVE-2026-2332 and Netty's
|
|
1673
|
+
* CVE-2026-33870), so an unclosed quote, an empty name or value, a stray `;`
|
|
1674
|
+
* or `=`, and whitespace that leads nowhere are each refused. The server's
|
|
1675
|
+
* parser and the client's both read their chunk-size lines through this.
|
|
1676
|
+
* @param bytes the buffer holding the chunk-size line
|
|
1677
|
+
* @param from the index just past the chunk size's last hex digit
|
|
1678
|
+
* @param to the index of the line's CR, exclusive
|
|
1679
|
+
* @returns true when the window is zero or more well-formed chunk extensions
|
|
1680
|
+
*/
|
|
1681
|
+
export const http1ChunkExtValid = (bytes: u8[], from: i32, to: i32): boolean => {
|
|
1682
|
+
const end: i32 = to < toI32(bytes.length) ? to : toI32(bytes.length)
|
|
1683
|
+
let i: i32 = from
|
|
1684
|
+
while (i >= 0 && i < end) {
|
|
1685
|
+
i = http1SkipOws(bytes, i, end)
|
|
1686
|
+
if (i < 0 || i >= end || i >= toI32(bytes.length) || toI32(bytes[i]) !== 59) {
|
|
1687
|
+
return false
|
|
1688
|
+
}
|
|
1689
|
+
i = http1SkipOws(bytes, i + 1, end)
|
|
1690
|
+
const nameStart: i32 = i
|
|
1691
|
+
while (i >= 0 && i < end && i < toI32(bytes.length) && http1IsTokenByte(toI32(bytes[i]))) {
|
|
1692
|
+
i += 1
|
|
1693
|
+
}
|
|
1694
|
+
if (i === nameStart) {
|
|
1695
|
+
return false
|
|
1696
|
+
}
|
|
1697
|
+
const nameEnd: i32 = i
|
|
1698
|
+
i = http1SkipOws(bytes, i, end)
|
|
1699
|
+
if (i >= 0 && i < end && i < toI32(bytes.length) && toI32(bytes[i]) === 61) {
|
|
1700
|
+
i = http1SkipOws(bytes, i + 1, end)
|
|
1701
|
+
if (i >= 0 && i < end && i < toI32(bytes.length) && toI32(bytes[i]) === 34) {
|
|
1702
|
+
i = http1QuotedStringEnd(bytes, i, end)
|
|
1703
|
+
if (i < 0) {
|
|
1704
|
+
return false
|
|
1705
|
+
}
|
|
1706
|
+
} else {
|
|
1707
|
+
const valueStart: i32 = i
|
|
1708
|
+
while (i >= 0 && i < end && i < toI32(bytes.length) && http1IsTokenByte(toI32(bytes[i]))) {
|
|
1709
|
+
i += 1
|
|
1710
|
+
}
|
|
1711
|
+
if (i === valueStart) {
|
|
1712
|
+
return false
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
} else {
|
|
1716
|
+
// Whitespace after a name is BWS only before the next `;`, which the
|
|
1717
|
+
// loop's next turn reads from the name's end.
|
|
1718
|
+
i = nameEnd
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
return true
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/**
|
|
1725
|
+
* Reads a chunk-size line (§7.1): 1*HEXDIG, then the chunk extensions
|
|
1726
|
+
* `http1ChunkExtValid` reads, parsed but not interpreted. Sets `left` and
|
|
1727
|
+
* answers `HTTP1_NEED_MORE`, or the refusal.
|
|
1415
1728
|
*/
|
|
1416
1729
|
const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
1417
1730
|
const buf: u8[] = p.buf
|
|
@@ -1457,6 +1770,9 @@ const http1ChunkSize = (p: Http1Parser): i32 => {
|
|
|
1457
1770
|
return http1Fail(p, 400, "a control character in a chunk extension")
|
|
1458
1771
|
}
|
|
1459
1772
|
}
|
|
1773
|
+
if (!http1ChunkExtValid(buf, digitsEnd, b)) {
|
|
1774
|
+
return http1Fail(p, 400, "a malformed chunk extension")
|
|
1775
|
+
}
|
|
1460
1776
|
}
|
|
1461
1777
|
if (over) {
|
|
1462
1778
|
return http1Fail(p, 413, "a chunked body over the limit")
|
|
@@ -1493,6 +1809,179 @@ const http1BodyWindow = (p: Http1Parser, after: i32): i32 => {
|
|
|
1493
1809
|
return HTTP1_BODY
|
|
1494
1810
|
}
|
|
1495
1811
|
|
|
1812
|
+
/**
|
|
1813
|
+
* Reads a header or trailer section's lines while whole ones are buffered:
|
|
1814
|
+
* `http1TakeLine` and `http1FieldLine` in one loop, each line limited by what
|
|
1815
|
+
* is left of `maxHeaderBytes` as `http1TakeLine` limits it, and the section's
|
|
1816
|
+
* bytes counted after it. The control bytes of the vector that held one line's
|
|
1817
|
+
* CRLF are kept for the next line, which mostly starts in the same vector, so
|
|
1818
|
+
* each buffered byte is loaded once however many lines a read brought.
|
|
1819
|
+
* Answers `HTTP1_LINE` when a trailer section has ended and the state machine
|
|
1820
|
+
* goes on, or what `next()` answers: `HTTP1_HEAD`, `HTTP1_NEED_MORE` or the
|
|
1821
|
+
* refusal. Its limits, refusals, spans and `scan` are `http1TakeLine`'s.
|
|
1822
|
+
*/
|
|
1823
|
+
const http1Section = (p: Http1Parser): i32 => {
|
|
1824
|
+
const buf: u8[] = p.buf
|
|
1825
|
+
const length: i32 = toI32(buf.length)
|
|
1826
|
+
const stop: i32 = p.end < length ? p.end : length
|
|
1827
|
+
const trailer: boolean = p.state === HTTP1_S_TRAILERS
|
|
1828
|
+
let i: i32 = p.start
|
|
1829
|
+
if (p.scan > p.start) {
|
|
1830
|
+
i = p.scan
|
|
1831
|
+
} else {
|
|
1832
|
+
p.lineControl = false
|
|
1833
|
+
}
|
|
1834
|
+
if (i < 0) {
|
|
1835
|
+
i = 0
|
|
1836
|
+
}
|
|
1837
|
+
// A header section is copied into the head as it is scanned, each vector
|
|
1838
|
+
// stored as it is loaded, CRLFs and all: `buf[x]` lands at `head[x +
|
|
1839
|
+
// delta]`, so a field line is in the head before it is read, and a line
|
|
1840
|
+
// that ends in a later call finds its first bytes where this one left them.
|
|
1841
|
+
// Nothing past `copyStop` is copied: a line reaching it is over its limit
|
|
1842
|
+
// (see the 431 below; what is left of `maxHeaderBytes` is the same at every
|
|
1843
|
+
// line), so the copy, and the head, stay within the limit.
|
|
1844
|
+
const copy: boolean = !trailer
|
|
1845
|
+
const delta: i32 = p.headLength - p.start
|
|
1846
|
+
const room: i32 = p.maxHeaderBytes - p.sectionBytes + 2
|
|
1847
|
+
const copyStop: i32 = p.start > 2147483647 - room ? 2147483647 : p.start + room
|
|
1848
|
+
// A vector at `q` is copied when `q < copyEnd`; the head is grown when `q`
|
|
1849
|
+
// passes `growAt`, so most vectors pay one compare for each.
|
|
1850
|
+
const copyEnd: i32 = copy ? copyStop : -2147483647
|
|
1851
|
+
let head: u8[] = p.head
|
|
1852
|
+
let growAt: i32 = toI32(head.length) - 32 - delta
|
|
1853
|
+
// Bit `k` of `bits` is set when `buf[base + k]` is a control byte or DEL
|
|
1854
|
+
// at or past `i`: the vector last loaded, its lanes before `i` cleared.
|
|
1855
|
+
let base: i32 = i - 16
|
|
1856
|
+
let bits: i32 = 0
|
|
1857
|
+
while (true) {
|
|
1858
|
+
// The next control byte at or past `i`, or the end of the bytes so far.
|
|
1859
|
+
let j: i32 = stop
|
|
1860
|
+
let seeking: boolean = true
|
|
1861
|
+
while (seeking) {
|
|
1862
|
+
if (bits !== 0) {
|
|
1863
|
+
j = base + (toI32(31) - toI32(Math.clz32(toU32(bits & -bits))))
|
|
1864
|
+
seeking = false
|
|
1865
|
+
} else {
|
|
1866
|
+
const q: i32 = base + 16 > i ? base + 16 : i
|
|
1867
|
+
if (q >= stop) {
|
|
1868
|
+
seeking = false
|
|
1869
|
+
} else if (q >= 0 && q <= length - 16) {
|
|
1870
|
+
// The vector may run past `stop` into bytes not yet fed: its lanes
|
|
1871
|
+
// there are dropped from `bits`, and its copy of them is
|
|
1872
|
+
// overwritten when they arrive.
|
|
1873
|
+
const v: u8x16 = loadU8x16(buf, q)
|
|
1874
|
+
if (q < copyEnd) {
|
|
1875
|
+
if (q > growAt) {
|
|
1876
|
+
http1HeadGrow(p, q + delta + 16 - p.headLength)
|
|
1877
|
+
head = p.head
|
|
1878
|
+
growAt = toI32(head.length) - 32 - delta
|
|
1879
|
+
}
|
|
1880
|
+
store(v, head, q + delta)
|
|
1881
|
+
}
|
|
1882
|
+
bits = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
|
|
1883
|
+
if (q > stop - 16) {
|
|
1884
|
+
bits = bits & ((toI32(1) << (stop - q)) - 1)
|
|
1885
|
+
}
|
|
1886
|
+
base = q
|
|
1887
|
+
} else {
|
|
1888
|
+
// Fewer than sixteen bytes are left in `buf`: a byte at a time.
|
|
1889
|
+
let k: i32 = q
|
|
1890
|
+
while (k >= 0 && k < stop && k < toI32(buf.length) && buf[k] >= 32 && buf[k] !== 127) {
|
|
1891
|
+
k += 1
|
|
1892
|
+
}
|
|
1893
|
+
const last: i32 = k < stop ? k + 1 : stop
|
|
1894
|
+
const end: i32 = last < copyStop ? last : copyStop
|
|
1895
|
+
if (copy && q < end) {
|
|
1896
|
+
if (end + delta > toI32(p.head.length) - 16) {
|
|
1897
|
+
http1HeadGrow(p, end + delta - p.headLength)
|
|
1898
|
+
}
|
|
1899
|
+
copyBytes(p.head, q + delta, buf, q, end - q)
|
|
1900
|
+
head = p.head
|
|
1901
|
+
growAt = toI32(head.length) - 32 - delta
|
|
1902
|
+
}
|
|
1903
|
+
j = k
|
|
1904
|
+
base = k + 1 - 16
|
|
1905
|
+
seeking = false
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
if (j < 0 || j >= stop || j >= toI32(buf.length)) {
|
|
1910
|
+
i = j < 0 ? i : j
|
|
1911
|
+
break
|
|
1912
|
+
}
|
|
1913
|
+
const c: i32 = toI32(buf[j])
|
|
1914
|
+
let lf: i32 = -1
|
|
1915
|
+
if (c === 13 && j + 1 < stop && j + 1 < toI32(buf.length) && buf[j + 1] === 10) {
|
|
1916
|
+
lf = j + 1
|
|
1917
|
+
} else if (c === 10) {
|
|
1918
|
+
lf = j
|
|
1919
|
+
}
|
|
1920
|
+
if (lf >= 0) {
|
|
1921
|
+
const start: i32 = p.start
|
|
1922
|
+
if (lf === start || buf[lf - 1] !== 13) {
|
|
1923
|
+
return http1Fail(p, 400, "a line ended in a bare LF")
|
|
1924
|
+
}
|
|
1925
|
+
const sectionBytes: i32 = p.sectionBytes + (lf + 1 - start)
|
|
1926
|
+
if (sectionBytes - 2 > p.maxHeaderBytes) {
|
|
1927
|
+
return http1Fail(p, 431, "a line over its limit")
|
|
1928
|
+
}
|
|
1929
|
+
// The scan stops at the CR, so the LF after it may be in no vector
|
|
1930
|
+
// or tail copied: store it, and the head holds each line's CRLF.
|
|
1931
|
+
const at: i32 = lf + delta
|
|
1932
|
+
if (copy && at >= 0 && at < toI32(head.length)) {
|
|
1933
|
+
head[at] = 10
|
|
1934
|
+
}
|
|
1935
|
+
p.lineStart = start
|
|
1936
|
+
p.lineEnd = lf - 1
|
|
1937
|
+
p.start = lf + 1
|
|
1938
|
+
p.scan = lf + 1
|
|
1939
|
+
i = lf + 1
|
|
1940
|
+
// The lowest bit is the line's end at `j`, and a CR's LF is the next
|
|
1941
|
+
// one when it is in this vector, as a control byte: drop them both.
|
|
1942
|
+
bits = bits & (bits - 1)
|
|
1943
|
+
if (c === 13) {
|
|
1944
|
+
bits = bits & (bits - 1)
|
|
1945
|
+
}
|
|
1946
|
+
p.sectionBytes = sectionBytes
|
|
1947
|
+
if (sectionBytes > p.maxHeaderBytes) {
|
|
1948
|
+
return http1Fail(p, 431, "a header section over the limit")
|
|
1949
|
+
}
|
|
1950
|
+
if (lf - 1 === start) {
|
|
1951
|
+
if (trailer) {
|
|
1952
|
+
p.state = HTTP1_S_MESSAGE_END
|
|
1953
|
+
return HTTP1_LINE
|
|
1954
|
+
}
|
|
1955
|
+
p.headLength = p.start + delta
|
|
1956
|
+
return http1EndOfHead(p)
|
|
1957
|
+
}
|
|
1958
|
+
if (http1FieldLine(p, copy, delta) === HTTP1_ERROR) {
|
|
1959
|
+
return HTTP1_ERROR
|
|
1960
|
+
}
|
|
1961
|
+
p.lineControl = false
|
|
1962
|
+
} else if (c === 13 && j + 1 >= stop) {
|
|
1963
|
+
// A CR at the end of the bytes so far: whether it ends the line is
|
|
1964
|
+
// the next byte's to say, so the next call looks at it again.
|
|
1965
|
+
i = j
|
|
1966
|
+
break
|
|
1967
|
+
} else {
|
|
1968
|
+
if (c !== 9) {
|
|
1969
|
+
p.lineControl = true
|
|
1970
|
+
}
|
|
1971
|
+
i = j + 1
|
|
1972
|
+
bits = bits & (bits - 1)
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
p.scan = i
|
|
1976
|
+
if (copy) {
|
|
1977
|
+
p.headLength = p.start + delta
|
|
1978
|
+
}
|
|
1979
|
+
if (p.end - p.start > p.maxHeaderBytes - p.sectionBytes + 1) {
|
|
1980
|
+
return http1Fail(p, 431, "a line over its limit")
|
|
1981
|
+
}
|
|
1982
|
+
return HTTP1_NEED_MORE
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1496
1985
|
/** The state machine behind `next()`. */
|
|
1497
1986
|
const http1Next = (p: Http1Parser): i32 => {
|
|
1498
1987
|
while (true) {
|
|
@@ -1522,25 +2011,10 @@ const http1Next = (p: Http1Parser): i32 => {
|
|
|
1522
2011
|
}
|
|
1523
2012
|
case HTTP1_S_FIELDS:
|
|
1524
2013
|
case HTTP1_S_TRAILERS: {
|
|
1525
|
-
const got: i32 =
|
|
2014
|
+
const got: i32 = http1Section(p)
|
|
1526
2015
|
if (got !== HTTP1_LINE) {
|
|
1527
2016
|
return got
|
|
1528
2017
|
}
|
|
1529
|
-
p.sectionBytes += p.lineEnd - p.lineStart + 2
|
|
1530
|
-
if (p.sectionBytes > p.maxHeaderBytes) {
|
|
1531
|
-
return http1Fail(p, 431, "a header section over the limit")
|
|
1532
|
-
}
|
|
1533
|
-
const trailer: boolean = p.state === HTTP1_S_TRAILERS
|
|
1534
|
-
if (p.lineEnd === p.lineStart) {
|
|
1535
|
-
if (trailer) {
|
|
1536
|
-
p.state = HTTP1_S_MESSAGE_END
|
|
1537
|
-
break
|
|
1538
|
-
}
|
|
1539
|
-
return http1EndOfHead(p)
|
|
1540
|
-
}
|
|
1541
|
-
if (http1FieldLine(p, !trailer) === HTTP1_ERROR) {
|
|
1542
|
-
return HTTP1_ERROR
|
|
1543
|
-
}
|
|
1544
2018
|
break
|
|
1545
2019
|
}
|
|
1546
2020
|
case HTTP1_S_LENGTH_BODY:
|