nish 0.36.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/AI.md +43 -4
- package/docs/INSTALL.md +67 -58
- package/package.json +9 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +736 -499
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/scripts/build.sh +147 -5
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/bytes.ts +23 -19
- package/std/compress/checksum.ts +1 -1
- package/std/crypto/aes.ts +48 -43
- package/std/crypto/base64url.ts +7 -5
- package/std/crypto/bcrypt-batch.ts +13 -13
- package/std/crypto/bcrypt.ts +33 -27
- package/std/crypto/chacha20poly1305.ts +16 -14
- package/std/crypto/ed25519.ts +19 -16
- package/std/crypto/hmac.ts +7 -6
- package/std/crypto/jwt.ts +23 -20
- package/std/crypto/p256.ts +63 -56
- package/std/crypto/p384.ts +23 -20
- package/std/crypto/pki.ts +18 -15
- package/std/crypto/rsa.ts +23 -19
- package/std/crypto/x25519.ts +11 -10
- package/std/crypto/x509.ts +69 -62
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +12 -11
- package/std/json-number.ts +12 -12
- package/std/json-tokenizer.ts +26 -22
- package/std/json-typed.ts +92 -78
- package/std/json-value.ts +38 -33
- package/std/json.ts +32 -28
- package/std/net/fetch.ts +1117 -230
- package/std/net/handler.ts +18 -0
- package/std/net/hpack.ts +29 -26
- package/std/net/http-compress.ts +27 -21
- package/std/net/http1-client-codec.ts +29 -25
- package/std/net/http1-codec.ts +44 -87
- package/std/net/http1-server.ts +66 -54
- package/std/net/http2-frame.ts +17 -16
- package/std/net/http2-tls.ts +17 -15
- package/std/net/http2.ts +67 -54
- package/std/net/http3-frame.ts +11 -10
- package/std/net/http3-server.ts +36 -35
- package/std/net/http3.ts +70 -58
- package/std/net/postgres.ts +178 -160
- package/std/net/prefork.ts +38 -34
- package/std/net/qpack.ts +15 -14
- package/std/net/quic-conn-ack.ts +1 -0
- package/std/net/quic-conn-cid.ts +11 -0
- package/std/net/quic-conn-params.ts +41 -14
- package/std/net/quic-datagram.ts +7 -0
- package/std/net/quic-frame.ts +48 -12
- package/std/net/quic-listener.ts +61 -44
- package/std/net/quic-packet.ts +71 -52
- package/std/net/quic-recovery.ts +59 -21
- package/std/net/quic-stream.ts +83 -55
- package/std/net/quic.ts +204 -152
- package/std/net/tls/client.ts +1 -0
- package/std/net/tls/codec.ts +81 -12
- package/std/net/tls/record-client.ts +30 -18
- package/std/net/tls/record-server.ts +74 -46
- package/std/net/tls/record.ts +33 -24
- package/std/net/tls/ticket.ts +2 -0
- package/std/net/tls/verify.ts +8 -7
- package/std/net/tls-tcp.ts +58 -50
- package/std/net/tls.ts +55 -43
- package/std/net/url.ts +12 -9
- package/std/net/websocket.ts +23 -19
- package/std/net/webtransport.ts +24 -18
- package/std/random.ts +8 -6
- package/std/regex.ts +70 -59
- package/std/sync/ring.ts +24 -21
- package/std/sync/shm.ts +9 -7
- package/std/term-io.ts +17 -14
- package/std/term-probe.ts +11 -10
- package/std/term-rate.ts +71 -61
- package/std/term.ts +73 -59
- package/std/text.ts +11 -10
- package/std/threads.ts +19 -15
- package/std/yaml-json.ts +17 -15
- package/std/yaml-write.ts +21 -14
package/std/net/handler.ts
CHANGED
|
@@ -227,9 +227,13 @@ export class HttpRequest {
|
|
|
227
227
|
* @internal Set by `HttpHandler` in this module after routing; the param accessors check `k` against it.
|
|
228
228
|
*/
|
|
229
229
|
paramCount: i32 = 0
|
|
230
|
+
/** Where the method starts in `head`; the method is `head[methodStart .. methodEnd)`, for a log line or a `Router.match`. */
|
|
230
231
|
methodStart: i32 = 0
|
|
232
|
+
/** Where the method ends in `head`, one past its last byte. */
|
|
231
233
|
methodEnd: i32 = 0
|
|
234
|
+
/** Where the request target (path and query) starts in `head`; the target is `head[targetStart .. targetEnd)`. */
|
|
232
235
|
targetStart: i32 = 0
|
|
236
|
+
/** Where the request target ends in `head`, one past its last byte. */
|
|
233
237
|
targetEnd: i32 = 0
|
|
234
238
|
/**
|
|
235
239
|
* Where the path ends: the target's first `?`, or its end.
|
|
@@ -241,9 +245,14 @@ export class HttpRequest {
|
|
|
241
245
|
* @internal Set by `HttpHandler` in this module when it copies the head, and read by `header`.
|
|
242
246
|
*/
|
|
243
247
|
headerCount: i32 = 0
|
|
248
|
+
/** How many bytes of `body` are the request's body, 0 for none; read the body as `body[0 .. bodyLength)`. */
|
|
244
249
|
bodyLength: i32 = 0
|
|
245
250
|
/** The connection's slot, and its generation when this request was read. */
|
|
246
251
|
slot: i32 = 0
|
|
252
|
+
/**
|
|
253
|
+
* The slot's generation when this request was read.
|
|
254
|
+
* @internal Read by `HttpHandler` in this module, which drops a reply to a slot that has since closed and been reused.
|
|
255
|
+
*/
|
|
247
256
|
gen: i32 = 0
|
|
248
257
|
/** Its index in the handler's pool. */
|
|
249
258
|
index: i32 = 0
|
|
@@ -385,6 +394,7 @@ export class HttpRequest {
|
|
|
385
394
|
|
|
386
395
|
/** The span `queryIndex` found, raw. */
|
|
387
396
|
queryValueStart: i32 = 0
|
|
397
|
+
/** Where the raw value `queryIndex` found ends in `head`, one past its last byte; it starts at `queryValueStart`. */
|
|
388
398
|
queryValueEnd: i32 = 0
|
|
389
399
|
|
|
390
400
|
/**
|
|
@@ -463,6 +473,10 @@ export class HttpRequest {
|
|
|
463
473
|
* requests. One per thread; nothing in it is shared.
|
|
464
474
|
*/
|
|
465
475
|
export class HttpHandler {
|
|
476
|
+
/**
|
|
477
|
+
* The HTTP/1.1 carrier the handler drives.
|
|
478
|
+
* @internal Read only by this class, which accepts, reads, flushes and closes connections through it.
|
|
479
|
+
*/
|
|
466
480
|
server: Http1Server
|
|
467
481
|
private router: Router
|
|
468
482
|
/** Every request the pool has made, and the indices of the free ones. */
|
|
@@ -480,6 +494,10 @@ export class HttpHandler {
|
|
|
480
494
|
private watchedFired: boolean[]
|
|
481
495
|
/** `json` and `problem` write here. */
|
|
482
496
|
scratch: u8[]
|
|
497
|
+
/**
|
|
498
|
+
* The JSON writer over `scratch`.
|
|
499
|
+
* @internal Read only by this class; programs get it from `json`.
|
|
500
|
+
*/
|
|
483
501
|
writer: JsonWriter
|
|
484
502
|
/** `refuse`'s scratch for `Router.match`. */
|
|
485
503
|
private probe: i32[]
|
package/std/net/hpack.ts
CHANGED
|
@@ -35,12 +35,13 @@
|
|
|
35
35
|
* end of the tables, Huffman padding longer than seven bits or not all ones,
|
|
36
36
|
* the EOS symbol inside a string, a table size update above the SETTINGS limit
|
|
37
37
|
* or after the block's first field, and a block that does not begin with the
|
|
38
|
-
* update a lowered limit requires (§4.2).
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
38
|
+
* update a lowered limit requires (§4.2).
|
|
39
|
+
*
|
|
40
|
+
* Each of those is HTTP/2's COMPRESSION_ERROR, which closes the connection, so
|
|
41
|
+
* the code is sticky: every later `decode` answers it again without reading
|
|
42
|
+
* anything. So is `HPACK_ERR_COPY_BUDGET`, a block that would make the table
|
|
43
|
+
* copy more than `HPACK_COPY_FACTOR` times the larger of its size and the
|
|
44
|
+
* block's length, which HTTP/2 answers with ENHANCE_YOUR_CALM.
|
|
44
45
|
*
|
|
45
46
|
* A header list larger than the caller's limit is not one of them. The block
|
|
46
47
|
* is still decoded to its end, so that the table stays in step with the
|
|
@@ -777,14 +778,15 @@ export const hpackDecodeString = (h: HpackHuffman, c: HpackCursor, out: u8[]): i
|
|
|
777
778
|
* oldest entry first. An index says where each entry sits, two numbers to an
|
|
778
779
|
* entry — where it starts in `bytes` and how long its name is; its value runs
|
|
779
780
|
* to where the next one starts — oldest first from entry `first` on, so that
|
|
780
|
-
* adding one is two `push`es and evicting one moves `first`.
|
|
781
|
-
*
|
|
782
|
-
* of
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
787
|
-
*
|
|
781
|
+
* adding one is two `push`es and evicting one moves `first`.
|
|
782
|
+
*
|
|
783
|
+
* The evicted prefix of the index, and of `bytes`, is shifted out in place once
|
|
784
|
+
* it is half of either, which keeps both amortised constant time without a
|
|
785
|
+
* ring's index arithmetic. Shifting and popping keep an array's storage, and
|
|
786
|
+
* `bytes` never holds more than three times `maxSize`, so a table the peer
|
|
787
|
+
* fills and empties for as long as it likes stops allocating once its arrays
|
|
788
|
+
* have grown that far (`docs/security/http2.md`, H2-1). Dynamic index 1, the
|
|
789
|
+
* newest entry, is the last.
|
|
788
790
|
*/
|
|
789
791
|
export class HpackTable {
|
|
790
792
|
/**
|
|
@@ -1109,21 +1111,22 @@ export class HpackWork {
|
|
|
1109
1111
|
* One HTTP/2 connection's receiving side: its dynamic table, the SETTINGS
|
|
1110
1112
|
* limit the table may grow to, and the header-list limit.
|
|
1111
1113
|
*
|
|
1112
|
-
* `decode` answers the block's fields in `names`, `values` and
|
|
1113
|
-
*
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
*
|
|
1114
|
+
* `decode` answers the block's fields in `names`, `values` and `neverIndexed`,
|
|
1115
|
+
* which it empties and refills on every call. Each name and value is an array
|
|
1116
|
+
* the decoder owns, taken from its working storage's pools by its length and
|
|
1117
|
+
* given back, emptied, at the next block, so **a field is valid until the next
|
|
1118
|
+
* `decode`**: a caller that keeps one past it copies it, since the next block
|
|
1119
|
+
* may refill the same array with another field.
|
|
1120
|
+
*
|
|
1121
|
+
* That is what keeps the storage a peer can make a connection hold bounded by
|
|
1122
|
+
* the header-list limit, however many blocks it sends and whatever becomes of
|
|
1120
1123
|
* their streams (`docs/security/http2.md`, H2-1): a class `j` array holds a
|
|
1121
1124
|
* field of at least 2^(j-1) octets, so no class ever holds more than about
|
|
1122
|
-
* twice the limit, and only the classes up to log2 of the limit, plus one,
|
|
1123
|
-
*
|
|
1125
|
+
* twice the limit, and only the classes up to log2 of the limit, plus one, hold
|
|
1126
|
+
* any. `settingsLimit` is the SETTINGS_HEADER_TABLE_SIZE this endpoint
|
|
1124
1127
|
* advertised and the peer acknowledged; the table starts at it.
|
|
1125
|
-
* `maxHeaderListSize` counts each field as its name, its value and 32 bytes,
|
|
1126
|
-
*
|
|
1128
|
+
* `maxHeaderListSize` counts each field as its name, its value and 32 bytes, as
|
|
1129
|
+
* SETTINGS_MAX_HEADER_LIST_SIZE does (RFC 9113 §6.5.2).
|
|
1127
1130
|
*/
|
|
1128
1131
|
export class HpackDecoder {
|
|
1129
1132
|
/** The dynamic table, as the blocks decoded so far have left it. */
|
package/std/net/http-compress.ts
CHANGED
|
@@ -23,18 +23,19 @@
|
|
|
23
23
|
* its `minLength`, longer than its `maxLength`, or that gzip would not make
|
|
24
24
|
* smaller, answering -1 so the program sends the bytes as they are.
|
|
25
25
|
*
|
|
26
|
-
* **Compression that pays for itself.** A body is sent compressed only when
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* `_NO_FEWER_SEGMENTS`), for a
|
|
36
|
-
* form already fits one segment is
|
|
37
|
-
* at all, as no compression could
|
|
26
|
+
* **Compression that pays for itself.** A body is sent compressed only when it
|
|
27
|
+
* saves at least `minSaving` bytes and `minSavingPercent` percent of the body;
|
|
28
|
+
* otherwise the compressed bytes are dropped and the body goes as it is. With
|
|
29
|
+
* `segmentMode` set, `respondEncoded` also sends it compressed only when the
|
|
30
|
+
* whole response, head included, takes fewer TCP segments of `mss` bytes (the
|
|
31
|
+
* connection's `mss` if set, else the compressor's, 1448 by default): a
|
|
32
|
+
* 1300-byte body that gzips to 700 still fits in one segment either way.
|
|
33
|
+
*
|
|
34
|
+
* After each call `decision` says why (`HTTP_COMPRESS_SENT`, `_NOT_ACCEPTED`,
|
|
35
|
+
* `_TOO_SMALL`, `_TOO_LARGE`, `_TOO_LITTLE_SAVED`, `_NO_FEWER_SEGMENTS`), for a
|
|
36
|
+
* program to count. A response whose identity form already fits one segment is
|
|
37
|
+
* `_NO_FEWER_SEGMENTS` without running gzip at all, as no compression could
|
|
38
|
+
* take fewer.
|
|
38
39
|
*
|
|
39
40
|
* **Two encoders.** Bodies up to `fixedLimit` (16 KB) go through
|
|
40
41
|
* `GzipFixedCompressor`: one fixed-Huffman block, a 16 KB window, no state
|
|
@@ -51,15 +52,17 @@
|
|
|
51
52
|
*
|
|
52
53
|
* **Negotiation** (`httpAcceptEncoding`) follows §12.5.3: each listed coding
|
|
53
54
|
* with its q-value, `x-gzip` as `gzip`, `*` for every coding not listed, and
|
|
54
|
-
* `identity` acceptable unless it is given q=0, directly or through `*`.
|
|
55
|
-
*
|
|
56
|
-
* program
|
|
57
|
-
*
|
|
58
|
-
* `
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
55
|
+
* `identity` acceptable unless it is given q=0, directly or through `*`.
|
|
56
|
+
*
|
|
57
|
+
* Of the codings the program offers, the most preferred with q above 0 wins,
|
|
58
|
+
* the program's order (gzip, then deflate) breaking ties; it beats `identity`
|
|
59
|
+
* at an equal q, and beats an `identity` the field does not mention at any q.
|
|
60
|
+
* With no field at all the answer is `HTTP_CODING_IDENTITY` (the RFC allows any
|
|
61
|
+
* coding then, but a client that says nothing is safest sent nothing it may not
|
|
62
|
+
* read); an empty field asks for identity too. `HTTP_CODING_NONE` means nothing
|
|
63
|
+
* offered is acceptable and identity is refused: a server may answer 406 or
|
|
64
|
+
* send identity anyway. A malformed element is skipped. The parser allocates
|
|
65
|
+
* nothing.
|
|
63
66
|
*
|
|
64
67
|
* **Compressing allocates nothing** and caches nothing between responses:
|
|
65
68
|
* `HttpCompressor` holds a `GzipCompressor`, a `GzipFixedCompressor`, an
|
|
@@ -406,6 +409,7 @@ export class HttpCompressor {
|
|
|
406
409
|
readonly output: u8[]
|
|
407
410
|
/** The fields `addFields` built: the program's, then `Content-Encoding` and `Vary`. */
|
|
408
411
|
readonly names: u8[][]
|
|
412
|
+
/** The value of each field in `names`, at the same index; pass both to `respond`. */
|
|
409
413
|
readonly values: u8[][]
|
|
410
414
|
private readonly contentEncodingName: u8[]
|
|
411
415
|
private readonly gzipValue: u8[]
|
|
@@ -420,6 +424,7 @@ export class HttpCompressor {
|
|
|
420
424
|
* default (any saving at all); a value below those counts as them.
|
|
421
425
|
*/
|
|
422
426
|
minSaving: i32
|
|
427
|
+
/** The percent of the body a compressed body must save, as well as `minSaving` bytes: 0 by default, 100 or more sends nothing compressed. */
|
|
423
428
|
minSavingPercent: i32
|
|
424
429
|
/**
|
|
425
430
|
* Whether `addFields` adds `Vary: Accept-Encoding` (RFC 9110 §12.5.5), which
|
|
@@ -434,6 +439,7 @@ export class HttpCompressor {
|
|
|
434
439
|
* sets it, or a connection's own `mss` overrides it.
|
|
435
440
|
*/
|
|
436
441
|
segmentMode: boolean
|
|
442
|
+
/** The TCP segment size `segmentMode` counts in, in bytes: `HTTP_DEFAULT_MSS` by default, and 0 or less means that default. */
|
|
437
443
|
mss: i32
|
|
438
444
|
/** `HTTP_ENCODER_AUTO` (the default), `HTTP_ENCODER_FIXED` or `HTTP_ENCODER_LEVEL`. */
|
|
439
445
|
encoder: i32
|
|
@@ -34,19 +34,19 @@
|
|
|
34
34
|
* }
|
|
35
35
|
*
|
|
36
36
|
* The events are `HTTP1_INFORMATIONAL` (an interim 1xx response was read; the
|
|
37
|
-
* final one follows), `HTTP1_HEAD` (the final response's status line and
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* on the split —
|
|
37
|
+
* final one follows), `HTTP1_HEAD` (the final response's status line and header
|
|
38
|
+
* section are in), `HTTP1_BODY` (some body bytes, de-chunked), `HTTP1_END` (the
|
|
39
|
+
* response is complete; with `keepAlive()` the next one follows),
|
|
40
|
+
* `HTTP1_UPGRADE` (a CONNECT was answered with 2xx, or a request that
|
|
41
|
+
* `expectUpgrade` named was answered with 101, and the connection is a tunnel
|
|
42
|
+
* now: `tunnelBytes()` answers what was fed past the head), `HTTP1_CLOSED` (the
|
|
43
|
+
* connection is done: the server closed it between responses, or the last
|
|
44
|
+
* response was not kept alive) and `HTTP1_ERROR`.
|
|
45
|
+
*
|
|
46
|
+
* The last three repeat on every later call. Only complete lines are acted on,
|
|
47
|
+
* body bytes are counted, never guessed, and every refusal is decided by the
|
|
48
|
+
* same byte wherever the input was cut, so the sequence of events, the
|
|
49
|
+
* concatenated body and the error code do not depend on the split —
|
|
50
50
|
* `tests/link/net_http1_client` feeds a corpus at every split point.
|
|
51
51
|
*
|
|
52
52
|
* **How a response ends** (RFC 9112 §6.3): a response to HEAD, a 1xx, 204 or
|
|
@@ -59,16 +59,18 @@
|
|
|
59
59
|
* **What it refuses**, each with its own code in `error`: a malformed status
|
|
60
60
|
* line, an HTTP major version other than 1, a bare LF, a malformed field line
|
|
61
61
|
* (an obs-fold, whitespace before the colon, a control byte in a value), a 101
|
|
62
|
-
* to a request that did not ask to switch protocols, `Transfer-Encoding`
|
|
63
|
-
* with `Content-Length`, a transfer coding other than one `chunked` or
|
|
64
|
-
* an HTTP/1.0 response, a `Content-Length` that is not a decimal or
|
|
65
|
-
* repeats disagree, a malformed chunk, a status line, header section,
|
|
66
|
-
* count, body or trailer section over the caller's limit, a chunk-size
|
|
67
|
-
* over `HTTP1_MAX_CHUNK_LINE` and more than `HTTP1_MAX_INFORMATIONAL`
|
|
68
|
-
* responses.
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
62
|
+
* to a request that did not ask to switch protocols, `Transfer-Encoding`
|
|
63
|
+
* together with `Content-Length`, a transfer coding other than one `chunked` or
|
|
64
|
+
* one in an HTTP/1.0 response, a `Content-Length` that is not a decimal or
|
|
65
|
+
* whose repeats disagree, a malformed chunk, a status line, header section,
|
|
66
|
+
* field count, body or trailer section over the caller's limit, a chunk-size
|
|
67
|
+
* line over `HTTP1_MAX_CHUNK_LINE` and more than `HTTP1_MAX_INFORMATIONAL`
|
|
68
|
+
* interim responses.
|
|
69
|
+
*
|
|
70
|
+
* Each of these is a response-smuggling or a resource shape, and each is
|
|
71
|
+
* refused rather than repaired: a client that repairs one differently from a
|
|
72
|
+
* proxy disagrees with it about where the next response starts. An error is
|
|
73
|
+
* final.
|
|
72
74
|
*
|
|
73
75
|
* Private helpers share the importing program's flat symbol namespace
|
|
74
76
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
@@ -384,8 +386,10 @@ const http1ClientPutField = (out: u8[], at: i32, names: string[], values: string
|
|
|
384
386
|
* `values` of different lengths; not exactly one `Host`; a `Content-Length` or
|
|
385
387
|
* `Transfer-Encoding` among the caller's fields, since the framing is decided
|
|
386
388
|
* here; a `Connection` that lists `upgrade`, which only
|
|
387
|
-
* `http1UpgradeRequestHead` may write; and a `bodyLength` below -2.
|
|
388
|
-
*
|
|
389
|
+
* `http1UpgradeRequestHead` may write; and a `bodyLength` below -2.
|
|
390
|
+
*
|
|
391
|
+
* Answers `HTTP1_NO_ROOM` when it is well formed but does not fit. Either way
|
|
392
|
+
* `out` is untouched.
|
|
389
393
|
*
|
|
390
394
|
* @param out The buffer the head is written into; nothing outside the head's bytes changes.
|
|
391
395
|
* @param at The index in `out` the request line starts at; a negative one answers `HTTP1_NO_ROOM`.
|
package/std/net/http1-codec.ts
CHANGED
|
@@ -86,7 +86,6 @@ import {
|
|
|
86
86
|
any,
|
|
87
87
|
bitmask,
|
|
88
88
|
eq,
|
|
89
|
-
extract,
|
|
90
89
|
gt,
|
|
91
90
|
loadU8x16,
|
|
92
91
|
lt,
|
|
@@ -256,7 +255,13 @@ export class Http1Parser {
|
|
|
256
255
|
* @internal Set by `http1TakeLine` and read by the field-line parser in this module.
|
|
257
256
|
*/
|
|
258
257
|
lineControl: boolean = false
|
|
258
|
+
/** Whether the request's body is chunked (`Transfer-Encoding: chunked`), so it has no `contentLength`. */
|
|
259
259
|
chunked: boolean = false
|
|
260
|
+
/**
|
|
261
|
+
* Whether the connection stays open after this request: HTTP/1.1 unless it
|
|
262
|
+
* sent `Connection: close`, HTTP/1.0 only when it sent `keep-alive`. A
|
|
263
|
+
* server closes after its response when this is false.
|
|
264
|
+
*/
|
|
260
265
|
keepAlive: boolean = false
|
|
261
266
|
/** Whether the request asked to switch protocols (`upgrade` is its value when `keepText`). */
|
|
262
267
|
upgrading: boolean = false
|
|
@@ -293,12 +298,17 @@ export class Http1Parser {
|
|
|
293
298
|
left: i32 = 0
|
|
294
299
|
/** The declared body length, or -1 when the request has none or is chunked. */
|
|
295
300
|
contentLength: i32 = -1
|
|
301
|
+
/** Where the bytes the last `HTTP1_BODY` handed over start in `body`. */
|
|
296
302
|
bodyOff: i32 = 0
|
|
303
|
+
/** How many bytes the last `HTTP1_BODY` handed over, from `body[bodyOff]`; 0 before the first. */
|
|
297
304
|
bodyLen: i32 = 0
|
|
298
305
|
/** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
|
|
299
306
|
methodStart: i32 = 0
|
|
307
|
+
/** Where the method ends in `head`, one past its last byte. */
|
|
300
308
|
methodEnd: i32 = 0
|
|
309
|
+
/** Where the request target starts in `head`. */
|
|
301
310
|
targetStart: i32 = 0
|
|
311
|
+
/** Where the request target ends in `head`, one past its last byte. */
|
|
302
312
|
targetEnd: i32 = 0
|
|
303
313
|
|
|
304
314
|
// --- limits, the caller's --------------------------------------------------
|
|
@@ -326,10 +336,13 @@ export class Http1Parser {
|
|
|
326
336
|
maxChunk: i32 = 1073741824
|
|
327
337
|
|
|
328
338
|
// --- what `keepText`, an error or a chunked body reads ----------------------
|
|
339
|
+
/** The request's method as text when `keepText` is on, else empty. */
|
|
329
340
|
method: string = ""
|
|
341
|
+
/** The request target (path and query) as text when `keepText` is on, else empty. */
|
|
330
342
|
target: string = ""
|
|
331
343
|
/** The field names, in lowercase, in the order they arrived; `values` beside them. */
|
|
332
344
|
names: string[]
|
|
345
|
+
/** The field values, in the order they arrived, each at its name's index in `names`; filled only when `keepText`. */
|
|
333
346
|
values: string[]
|
|
334
347
|
/** The `Upgrade` field when `Connection` asked for it, else empty. */
|
|
335
348
|
upgrade: string = ""
|
|
@@ -657,73 +670,6 @@ const http1StartRequest = (p: Http1Parser): void => {
|
|
|
657
670
|
p.headHeld = false
|
|
658
671
|
}
|
|
659
672
|
|
|
660
|
-
/**
|
|
661
|
-
* The bytes of `x` below 32 or equal to 127 — a control byte or DEL, which
|
|
662
|
-
* `http1TakeLine` stops at — as their high bits: zero when there is none,
|
|
663
|
-
* and else the lowest set bit is the first such byte's. Two of the bit
|
|
664
|
-
* tricks of "Bit Twiddling Hacks" (S. E. Anderson, public domain): a byte
|
|
665
|
-
* below `n` leaves its high bit set in `(x - n * ones) & ~x`, for `n` up to
|
|
666
|
-
* 128, and `127` is the byte that is zero in `x ^ 127 * ones`. A byte from
|
|
667
|
-
* 128 up has its high bit set in `x`, so `~x` clears it: obs-text never
|
|
668
|
-
* matches. A borrow only runs up from a byte that matched, so the bits above
|
|
669
|
-
* the first match may be false and the lowest is exact, in each mask and so
|
|
670
|
-
* in their union. Each subtraction is masked to 64 bits because a `u64` is a
|
|
671
|
-
* BigInt under Node, which does not wrap; natively the mask is a no-op.
|
|
672
|
-
*/
|
|
673
|
-
const http1ControlBits = (x: u64): u64 => {
|
|
674
|
-
const ones32: u64 = toU64(16843009)
|
|
675
|
-
const ones: u64 = (ones32 << toU64(32)) | ones32
|
|
676
|
-
const low32: u64 = 0xffffffff
|
|
677
|
-
const all: u64 = (low32 << toU64(32)) | low32
|
|
678
|
-
const high: u64 = ones * toU64(128)
|
|
679
|
-
const below: u64 = (x - ones * toU64(32)) & all & ~x & high
|
|
680
|
-
const y: u64 = x ^ (ones * toU64(127))
|
|
681
|
-
const del: u64 = (y - ones) & all & ~y & high
|
|
682
|
-
return below | del
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
/** Which byte of a word `bits`'s lowest set bit is in, 0 to 7; `bits` is not zero. */
|
|
686
|
-
const http1FirstByte = (bits: u64): i32 => {
|
|
687
|
-
const mask32: u64 = 0xffffffff
|
|
688
|
-
const lo: u32 = toU32(bits & mask32)
|
|
689
|
-
if (lo !== toU32(0)) {
|
|
690
|
-
return (toI32(31) - toI32(Math.clz32(lo & (toU32(0) - lo)))) >> toI32(3)
|
|
691
|
-
}
|
|
692
|
-
const hi: u32 = toU32(bits >> toU64(32))
|
|
693
|
-
return toI32(4) + ((toI32(31) - toI32(Math.clz32(hi & (toU32(0) - hi)))) >> toI32(3))
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
/**
|
|
697
|
-
* Lanes `at` to `at + 7` of `v`, the first the lowest byte: eight bytes of a
|
|
698
|
-
* sixteen-byte load as one word, which `opt` folds to a bitcast of the
|
|
699
|
-
* vector's half. One load is one bounds check for sixteen bytes, where eight
|
|
700
|
-
* reads of `buf[i + k]` would be eight.
|
|
701
|
-
*/
|
|
702
|
-
const http1Lanes = (v: u8x16, at: i32): u64 => {
|
|
703
|
-
if (at === 0) {
|
|
704
|
-
return (
|
|
705
|
-
toU64(extract(v, 0)) |
|
|
706
|
-
(toU64(extract(v, 1)) << toU64(8)) |
|
|
707
|
-
(toU64(extract(v, 2)) << toU64(16)) |
|
|
708
|
-
(toU64(extract(v, 3)) << toU64(24)) |
|
|
709
|
-
(toU64(extract(v, 4)) << toU64(32)) |
|
|
710
|
-
(toU64(extract(v, 5)) << toU64(40)) |
|
|
711
|
-
(toU64(extract(v, 6)) << toU64(48)) |
|
|
712
|
-
(toU64(extract(v, 7)) << toU64(56))
|
|
713
|
-
)
|
|
714
|
-
}
|
|
715
|
-
return (
|
|
716
|
-
toU64(extract(v, 8)) |
|
|
717
|
-
(toU64(extract(v, 9)) << toU64(8)) |
|
|
718
|
-
(toU64(extract(v, 10)) << toU64(16)) |
|
|
719
|
-
(toU64(extract(v, 11)) << toU64(24)) |
|
|
720
|
-
(toU64(extract(v, 12)) << toU64(32)) |
|
|
721
|
-
(toU64(extract(v, 13)) << toU64(40)) |
|
|
722
|
-
(toU64(extract(v, 14)) << toU64(48)) |
|
|
723
|
-
(toU64(extract(v, 15)) << toU64(56))
|
|
724
|
-
)
|
|
725
|
-
}
|
|
726
|
-
|
|
727
673
|
/** `http1TakeLine`'s answer when a whole line is ready; never an event. */
|
|
728
674
|
const HTTP1_LINE: i32 = -1
|
|
729
675
|
|
|
@@ -756,18 +702,12 @@ const http1TakeLine = (p: Http1Parser, limit: i32, overStatus: i32): i32 => {
|
|
|
756
702
|
let found: boolean = false
|
|
757
703
|
while (!found && i >= 0 && i <= stop - 16 && i <= toI32(buf.length) - 16) {
|
|
758
704
|
const v: u8x16 = loadU8x16(buf, i)
|
|
759
|
-
const
|
|
760
|
-
if (
|
|
761
|
-
i +=
|
|
705
|
+
const bad: i32 = bitmask(or(lt(v, splatU8x16(32)), eq(v, splatU8x16(127))))
|
|
706
|
+
if (bad !== 0) {
|
|
707
|
+
i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
762
708
|
found = true
|
|
763
709
|
} else {
|
|
764
|
-
|
|
765
|
-
if (high !== toU64(0)) {
|
|
766
|
-
i += 8 + http1FirstByte(high)
|
|
767
|
-
found = true
|
|
768
|
-
} else {
|
|
769
|
-
i += 16
|
|
770
|
-
}
|
|
710
|
+
i += 16
|
|
771
711
|
}
|
|
772
712
|
}
|
|
773
713
|
while (!found && i >= 0 && i < stop && i < toI32(buf.length) && buf[i] >= 32 && buf[i] !== 127) {
|
|
@@ -1186,7 +1126,23 @@ const http1RequestLine = (p: Http1Parser): i32 => {
|
|
|
1186
1126
|
const methodEnd: i32 = i
|
|
1187
1127
|
i += 1
|
|
1188
1128
|
const targetStart: i32 = i
|
|
1189
|
-
|
|
1129
|
+
// The target is visible ASCII, 33 to 126: sixteen bytes at a time, as far
|
|
1130
|
+
// as the first that is not or the line's end, and a byte at a time for a
|
|
1131
|
+
// tail shorter than a vector before `buf`'s end.
|
|
1132
|
+
let found: boolean = false
|
|
1133
|
+
while (!found && i >= 0 && i < b && i <= toI32(buf.length) - 16) {
|
|
1134
|
+
const bad: i32 = bitmask(gt(subWrapping(loadU8x16(buf, i), splatU8x16(33)), splatU8x16(93)))
|
|
1135
|
+
if (bad !== 0) {
|
|
1136
|
+
i += toI32(31) - toI32(Math.clz32(toU32(bad & -bad)))
|
|
1137
|
+
found = true
|
|
1138
|
+
} else {
|
|
1139
|
+
i += 16
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
if (i > b) {
|
|
1143
|
+
i = b
|
|
1144
|
+
}
|
|
1145
|
+
while (!found && i >= 0 && i < b && i < toI32(buf.length) && buf[i] > 32 && buf[i] < 127) {
|
|
1190
1146
|
i += 1
|
|
1191
1147
|
}
|
|
1192
1148
|
if (i - targetStart > p.maxTarget) {
|
|
@@ -2041,14 +1997,15 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
|
2041
1997
|
}
|
|
2042
1998
|
|
|
2043
1999
|
/**
|
|
2044
|
-
* `http1ResponseHead`'s status line and header section, written into `out`
|
|
2045
|
-
*
|
|
2046
|
-
* `nish/net/http-fields` holds them), and `connection`, when not empty, is
|
|
2047
|
-
*
|
|
2048
|
-
*
|
|
2049
|
-
*
|
|
2050
|
-
*
|
|
2051
|
-
*
|
|
2000
|
+
* `http1ResponseHead`'s status line and header section, written into `out` at
|
|
2001
|
+
* `at` with nothing allocated: field names and values are octets (as
|
|
2002
|
+
* `nish/net/http-fields` holds them), and `connection`, when not empty, is sent
|
|
2003
|
+
* as a `Connection` field after the caller's. `date`, when not empty, is sent
|
|
2004
|
+
* as a `Date` field between the caller's fields and the framing ones (RFC 9110
|
|
2005
|
+
* §6.6.1), on a final status only, and not when the caller's fields already
|
|
2006
|
+
* hold a `date` in any case: the caller's wins, so a response carries one.
|
|
2007
|
+
*
|
|
2008
|
+
* Answers where the head ends; `HTTP1_REFUSED` for everything
|
|
2052
2009
|
* `http1ResponseHead` refuses, a `Connection` among the caller's fields when
|
|
2053
2010
|
* `connection` is given, a `connection` that is not field text, and a `date`
|
|
2054
2011
|
* that is not field text on a final status, the only one it is sent on;
|