nish 0.34.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/docs/AI.md +86 -16
- package/docs/INSTALL.md +22 -13
- package/llms.txt +5 -2
- package/package.json +15 -5
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/net/http1-codec.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* socket gave it, of any length and cut anywhere, and then calls `next()`
|
|
7
7
|
* until it answers `HTTP1_NEED_MORE`:
|
|
8
8
|
*
|
|
9
|
-
* import { Http1Parser, HTTP1_HEAD, HTTP1_BODY, HTTP1_END, HTTP1_ERROR } from "nish/net/http1-codec";
|
|
9
|
+
* import { Http1Parser, HTTP1_NEED_MORE, HTTP1_HEAD, HTTP1_BODY, HTTP1_END, HTTP1_ERROR } from "nish/net/http1-codec";
|
|
10
10
|
*
|
|
11
11
|
* const p = new Http1Parser(maxTarget, maxHeaderBytes, maxHeaders, maxBody);
|
|
12
12
|
* p.feed(received, 0, n);
|
|
@@ -155,8 +155,20 @@ const HTTP1_S_ERROR: i32 = 10
|
|
|
155
155
|
* has, how many of them are the word asked about, and whether the last is.
|
|
156
156
|
*/
|
|
157
157
|
class Http1ListTally {
|
|
158
|
+
/**
|
|
159
|
+
* How many elements the list has.
|
|
160
|
+
* @internal Written by `http1Tally` and read by the field checks in this module.
|
|
161
|
+
*/
|
|
158
162
|
elements: i32 = 0
|
|
163
|
+
/**
|
|
164
|
+
* How many elements are the word asked about.
|
|
165
|
+
* @internal Written by `http1Tally` and read by the field checks in this module.
|
|
166
|
+
*/
|
|
159
167
|
matches: i32 = 0
|
|
168
|
+
/**
|
|
169
|
+
* Whether the last element is the word asked about.
|
|
170
|
+
* @internal Written by `http1Tally` and read by the field checks in this module.
|
|
171
|
+
*/
|
|
160
172
|
lastMatches: boolean = false
|
|
161
173
|
}
|
|
162
174
|
|
|
@@ -165,22 +177,22 @@ class Http1ListTally {
|
|
|
165
177
|
* on a kept-alive connection follow one another through the same parser.
|
|
166
178
|
*/
|
|
167
179
|
export class Http1Parser {
|
|
168
|
-
// The fields
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
//
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
180
|
+
// The fields a request reads come first, so that a kept-alive connection
|
|
181
|
+
// whose parser has gone cold between requests brings back two or three
|
|
182
|
+
// cache lines of it, not all four: the buffers, then the offsets `feed`,
|
|
183
|
+
// `buffered`, `http1TakeLine` and the head's lines move, then the flags,
|
|
184
|
+
// then the rest of the request's numbers and the caller's limits. What only
|
|
185
|
+
// `keepText`, an error or a chunked body reads follows. Each group packs as
|
|
186
|
+
// widest first does, and the flags' two bytes of padding are the only ones:
|
|
187
|
+
// the struct is the 200 bytes its fields sum to, rounded to 8.
|
|
188
|
+
|
|
189
|
+
// --- the request's buffers -------------------------------------------------
|
|
190
|
+
/**
|
|
191
|
+
* The bytes fed and not yet consumed are `buf[start .. end)`.
|
|
192
|
+
* @internal Filled by `feed` in this module, and swapped for a pooled buffer
|
|
193
|
+
* by std/net/http1-server.ts when it parks or resumes the input; written
|
|
194
|
+
* directly by bench/http1-conn.
|
|
195
|
+
*/
|
|
184
196
|
buf: u8[]
|
|
185
197
|
/**
|
|
186
198
|
* The request line and the kept field lines of the request, copied out of
|
|
@@ -191,57 +203,57 @@ export class Http1Parser {
|
|
|
191
203
|
head: u8[]
|
|
192
204
|
/** Four entries per field, into `head`: name start and end, value start and end (OWS trimmed). */
|
|
193
205
|
spans: i32[]
|
|
194
|
-
/**
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// --- limits, the caller's --------------------------------------------------
|
|
198
|
-
maxTarget: i32 = 0
|
|
199
|
-
maxHeaderBytes: i32 = 0
|
|
200
|
-
maxHeaders: i32 = 0
|
|
201
|
-
maxBody: i32 = 0
|
|
202
|
-
/** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
|
|
203
|
-
maxChunk: i32 = 1073741824
|
|
204
|
-
|
|
205
|
-
/** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
|
|
206
|
-
minor: i32 = 1
|
|
207
|
-
/** The declared body length, or -1 when the request has none or is chunked. */
|
|
208
|
-
contentLength: i32 = -1
|
|
209
|
-
bodyOff: i32 = 0
|
|
210
|
-
bodyLen: i32 = 0
|
|
211
|
-
/** The status code to answer once `next()` said `HTTP1_ERROR`. */
|
|
212
|
-
status: i32 = 0
|
|
213
|
-
/** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
|
|
214
|
-
methodStart: i32 = 0
|
|
215
|
-
methodEnd: i32 = 0
|
|
216
|
-
targetStart: i32 = 0
|
|
217
|
-
targetEnd: i32 = 0
|
|
218
|
-
/** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
|
|
219
|
-
headerCount: i32 = 0
|
|
220
|
-
/** How much of `head` the request uses. */
|
|
221
|
-
headLength: i32 = 0
|
|
206
|
+
/** The buffer the last `HTTP1_BODY`'s bytes are in, at `bodyOff`, `bodyLen` long. */
|
|
207
|
+
body: u8[]
|
|
222
208
|
|
|
223
|
-
// ---
|
|
209
|
+
// --- where the parser is ----------------------------------------------------
|
|
210
|
+
/**
|
|
211
|
+
* Where the unconsumed input starts in `buf`.
|
|
212
|
+
* @internal Moved by the parser's free functions in this module, and reset by
|
|
213
|
+
* std/net/http1-server.ts when it parks the input buffer, and read there to
|
|
214
|
+
* hand the bytes after an upgrade to the WebSocket reader.
|
|
215
|
+
*/
|
|
224
216
|
start: i32 = 0
|
|
217
|
+
/**
|
|
218
|
+
* Where the unconsumed input ends in `buf`.
|
|
219
|
+
* @internal Moved by `feed` in this module, reset by
|
|
220
|
+
* std/net/http1-server.ts when it parks the input buffer, and moved directly
|
|
221
|
+
* by bench/http1-conn.
|
|
222
|
+
*/
|
|
225
223
|
end: i32 = 0
|
|
226
|
-
/**
|
|
224
|
+
/**
|
|
225
|
+
* Where the search for the next LF resumes, so a line fed a byte at a time is scanned once.
|
|
226
|
+
* @internal Moved by `http1TakeLine` in this module, and reset by
|
|
227
|
+
* std/net/http1-server.ts when it parks the input buffer.
|
|
228
|
+
*/
|
|
227
229
|
scan: i32 = 0
|
|
230
|
+
/**
|
|
231
|
+
* Which kind of thing the parser waits for, one of the `HTTP1_S_` states.
|
|
232
|
+
* @internal Read and written by the parser's free functions in this module to drive the state machine.
|
|
233
|
+
*/
|
|
228
234
|
state: i32 = 0
|
|
229
|
-
/**
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
sectionBytes: i32 = 0
|
|
235
|
-
/** Field lines of the request so far, its trailers included, against `maxHeaders`. */
|
|
236
|
-
fieldCount: i32 = 0
|
|
237
|
-
/** The line `http1TakeLine` found is `buf[lineStart .. lineEnd)`, its CRLF dropped. */
|
|
235
|
+
/**
|
|
236
|
+
* The line `http1TakeLine` found is `buf[lineStart .. lineEnd)`, its CRLF dropped.
|
|
237
|
+
* @internal Set by `http1TakeLine` in this module, and reset by
|
|
238
|
+
* std/net/http1-server.ts when it parks the input buffer.
|
|
239
|
+
*/
|
|
238
240
|
lineStart: i32 = 0
|
|
241
|
+
/**
|
|
242
|
+
* Where the line `http1TakeLine` found ends, before its CRLF.
|
|
243
|
+
* @internal Set by `http1TakeLine` in this module, and reset by
|
|
244
|
+
* std/net/http1-server.ts when it parks the input buffer.
|
|
245
|
+
*/
|
|
239
246
|
lineEnd: i32 = 0
|
|
247
|
+
/** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
|
|
248
|
+
headerCount: i32 = 0
|
|
249
|
+
/** How much of `head` the request uses. */
|
|
250
|
+
headLength: i32 = 0
|
|
240
251
|
/**
|
|
241
252
|
* Whether the line being scanned holds a byte a field value may not: a
|
|
242
253
|
* control byte other than HTAB, or DEL (RFC 9110 §5.5). `http1TakeLine`
|
|
243
254
|
* sets it as it looks for the line's end, so a field line's bytes are read
|
|
244
255
|
* once, not once for the LF and again for the check.
|
|
256
|
+
* @internal Set by `http1TakeLine` and read by the field-line parser in this module.
|
|
245
257
|
*/
|
|
246
258
|
lineControl: boolean = false
|
|
247
259
|
chunked: boolean = false
|
|
@@ -251,6 +263,7 @@ export class Http1Parser {
|
|
|
251
263
|
/**
|
|
252
264
|
* Whether a kept-alive request ended and its head is still held for the
|
|
253
265
|
* response: the next `next()` clears it before it reads a byte more.
|
|
266
|
+
* @internal Read and written by the parser's free functions in this module to keep a finished request's head readable.
|
|
254
267
|
*/
|
|
255
268
|
headHeld: boolean = false
|
|
256
269
|
/**
|
|
@@ -260,10 +273,90 @@ export class Http1Parser {
|
|
|
260
273
|
*/
|
|
261
274
|
keepText: boolean = true
|
|
262
275
|
|
|
276
|
+
// --- the request's numbers: valid from its `HTTP1_HEAD` until the `next()` after its `HTTP1_END`
|
|
277
|
+
/**
|
|
278
|
+
* Bytes of the header or trailer section so far, against `maxHeaderBytes`.
|
|
279
|
+
* @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
|
|
280
|
+
*/
|
|
281
|
+
sectionBytes: i32 = 0
|
|
282
|
+
/**
|
|
283
|
+
* Field lines of the request so far, its trailers included, against `maxHeaders`.
|
|
284
|
+
* @internal Read and written by the parser's free functions in this module to enforce the 431 limit.
|
|
285
|
+
*/
|
|
286
|
+
fieldCount: i32 = 0
|
|
287
|
+
/** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
|
|
288
|
+
minor: i32 = 1
|
|
289
|
+
/**
|
|
290
|
+
* Bytes left in a length-delimited body or in the current chunk.
|
|
291
|
+
* @internal Read and written by the parser's free functions in this module to count body bytes rather than guess them.
|
|
292
|
+
*/
|
|
293
|
+
left: i32 = 0
|
|
294
|
+
/** The declared body length, or -1 when the request has none or is chunked. */
|
|
295
|
+
contentLength: i32 = -1
|
|
296
|
+
bodyOff: i32 = 0
|
|
297
|
+
bodyLen: i32 = 0
|
|
298
|
+
/** The method is `head[methodStart .. methodEnd)` and the target `head[targetStart .. targetEnd)`. */
|
|
299
|
+
methodStart: i32 = 0
|
|
300
|
+
methodEnd: i32 = 0
|
|
301
|
+
targetStart: i32 = 0
|
|
302
|
+
targetEnd: i32 = 0
|
|
303
|
+
|
|
304
|
+
// --- limits, the caller's --------------------------------------------------
|
|
305
|
+
/**
|
|
306
|
+
* The longest request target, in bytes, before a 414.
|
|
307
|
+
* @internal Set by the constructor and read by the request-line parser in this module.
|
|
308
|
+
*/
|
|
309
|
+
maxTarget: i32 = 0
|
|
310
|
+
/**
|
|
311
|
+
* The longest header or trailer section, in bytes, before a 431.
|
|
312
|
+
* @internal Set by the constructor and read by the field-line parser in this module.
|
|
313
|
+
*/
|
|
314
|
+
maxHeaderBytes: i32 = 0
|
|
315
|
+
/**
|
|
316
|
+
* The most field lines one request may carry, before a 431.
|
|
317
|
+
* @internal Set by the constructor and read by the field-line parser in this module.
|
|
318
|
+
*/
|
|
319
|
+
maxHeaders: i32 = 0
|
|
320
|
+
/**
|
|
321
|
+
* The longest body, declared or de-chunked, in bytes, before a 413.
|
|
322
|
+
* @internal Set by the constructor and read by the body readers in this module.
|
|
323
|
+
*/
|
|
324
|
+
maxBody: i32 = 0
|
|
325
|
+
/** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
|
|
326
|
+
maxChunk: i32 = 1073741824
|
|
327
|
+
|
|
328
|
+
// --- what `keepText`, an error or a chunked body reads ----------------------
|
|
329
|
+
method: string = ""
|
|
330
|
+
target: string = ""
|
|
331
|
+
/** The field names, in lowercase, in the order they arrived; `values` beside them. */
|
|
332
|
+
names: string[]
|
|
333
|
+
values: string[]
|
|
334
|
+
/** The `Upgrade` field when `Connection` asked for it, else empty. */
|
|
335
|
+
upgrade: string = ""
|
|
336
|
+
/** What was wrong, once `next()` said `HTTP1_ERROR`; for a log, not for the response. */
|
|
337
|
+
reason: string = ""
|
|
338
|
+
/**
|
|
339
|
+
* Reused by every list `http1Tally` walks.
|
|
340
|
+
* @internal Filled by `http1Tally` and read by the field checks in this module, so a list costs no allocation.
|
|
341
|
+
*/
|
|
342
|
+
tally: Http1ListTally
|
|
343
|
+
/** The status code to answer once `next()` said `HTTP1_ERROR`. */
|
|
344
|
+
status: i32 = 0
|
|
345
|
+
/**
|
|
346
|
+
* De-chunked body bytes so far, against `maxBody`.
|
|
347
|
+
* @internal Read and written by the parser's free functions in this module to enforce the 413 limit on a chunked body.
|
|
348
|
+
*/
|
|
349
|
+
bodyTotal: i32 = 0
|
|
350
|
+
|
|
263
351
|
/**
|
|
264
352
|
* Each limit is clamped into 0 to 2^30, so that the sums the parser makes
|
|
265
353
|
* of them (a request line is the method, the target and ten bytes more)
|
|
266
354
|
* cannot overflow an `i32`.
|
|
355
|
+
*
|
|
356
|
+
* @param maxTarget the longest request target in bytes; a longer one is a 414
|
|
357
|
+
* @param maxHeaderBytes the longest header or trailer section in bytes; a longer one is a 431
|
|
358
|
+
* @param maxHeaders the most field lines a request may carry, trailers included; more is a 431
|
|
359
|
+
* @param maxBody the longest body in bytes, declared or de-chunked; a longer one is a 413
|
|
267
360
|
*/
|
|
268
361
|
constructor(maxTarget: i32, maxHeaderBytes: i32, maxHeaders: i32, maxBody: i32) {
|
|
269
362
|
this.maxTarget = http1Clamp(maxTarget)
|
|
@@ -274,12 +367,15 @@ export class Http1Parser {
|
|
|
274
367
|
// head kept, at HTTP1_FIRST_INPUT and HTTP1_FIRST_HEAD bytes: a parser
|
|
275
368
|
// that is built and never fed (a pool's spare slot) costs neither, and a
|
|
276
369
|
// caller that lends them out between requests puts empty ones here.
|
|
370
|
+
// The spans are made first: the arena places each block after the last,
|
|
371
|
+
// so their header and first entries sit just behind the parser's fields
|
|
372
|
+
// rather than behind four empty arrays.
|
|
373
|
+
this.spans = new Array<i32>(64)
|
|
277
374
|
this.buf = []
|
|
278
375
|
this.body = this.buf
|
|
376
|
+
this.head = []
|
|
279
377
|
this.names = []
|
|
280
378
|
this.values = []
|
|
281
|
-
this.head = []
|
|
282
|
-
this.spans = new Array<i32>(64)
|
|
283
379
|
this.tally = new Http1ListTally()
|
|
284
380
|
this.restart()
|
|
285
381
|
}
|
|
@@ -289,6 +385,10 @@ export class Http1Parser {
|
|
|
289
385
|
* panics, as the crypto modules' `update` does. Once the parser has closed
|
|
290
386
|
* or refused, the input is dropped; once it has upgraded, it is kept for
|
|
291
387
|
* `upgradeBytes()`.
|
|
388
|
+
*
|
|
389
|
+
* @param data the bytes the socket gave, which the parser copies and does not keep
|
|
390
|
+
* @param off where in `data` the new bytes start
|
|
391
|
+
* @param len how many bytes from `off` to append
|
|
292
392
|
*/
|
|
293
393
|
feed(data: u8[], off: i32, len: i32): void {
|
|
294
394
|
const size: i32 = toI32(data.length)
|
|
@@ -307,7 +407,10 @@ export class Http1Parser {
|
|
|
307
407
|
this.end = at + len
|
|
308
408
|
}
|
|
309
409
|
|
|
310
|
-
/**
|
|
410
|
+
/**
|
|
411
|
+
* The next event the input holds; see the module comment.
|
|
412
|
+
* @returns one of the `HTTP1_` events, `HTTP1_NEED_MORE` when the input must grow before anything more can happen
|
|
413
|
+
*/
|
|
311
414
|
next(): i32 {
|
|
312
415
|
return http1Next(this)
|
|
313
416
|
}
|
|
@@ -316,6 +419,9 @@ export class Http1Parser {
|
|
|
316
419
|
* The value of the first field named `name` (any case), or `null`. A field
|
|
317
420
|
* that may repeat is read through the spans. The answer is a fresh string;
|
|
318
421
|
* `headerIndex` and `headerIs` allocate nothing.
|
|
422
|
+
*
|
|
423
|
+
* @param name the field name to look for, compared in any case
|
|
424
|
+
* @returns the first such field's value with its surrounding whitespace trimmed, or `null` when the request has none
|
|
319
425
|
*/
|
|
320
426
|
header(name: string): string | null {
|
|
321
427
|
const i: i32 = this.headerIndex(name)
|
|
@@ -333,7 +439,11 @@ export class Http1Parser {
|
|
|
333
439
|
return http1Text(this.head, spans[at + 2], spans[at + 3], false)
|
|
334
440
|
}
|
|
335
441
|
|
|
336
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* The index of the first field named `name` (any case), or -1.
|
|
444
|
+
* @param name the field name to look for, compared in any case
|
|
445
|
+
* @returns which field it is, so its spans are `spans[4 * i ..]`, or -1 when the request has none
|
|
446
|
+
*/
|
|
337
447
|
headerIndex(name: string): i32 {
|
|
338
448
|
const spans: i32[] = this.spans
|
|
339
449
|
const count: i32 = this.headerCount
|
|
@@ -345,7 +455,12 @@ export class Http1Parser {
|
|
|
345
455
|
return -1
|
|
346
456
|
}
|
|
347
457
|
|
|
348
|
-
/**
|
|
458
|
+
/**
|
|
459
|
+
* Whether the first field named `name` (any case) has exactly the value `value`.
|
|
460
|
+
* @param name the field name to look for, compared in any case
|
|
461
|
+
* @param value the value to compare with, byte for byte
|
|
462
|
+
* @returns true when that field exists and its trimmed value is `value`, with nothing allocated
|
|
463
|
+
*/
|
|
349
464
|
headerIs(name: string, value: string): boolean {
|
|
350
465
|
const i: i32 = this.headerIndex(name)
|
|
351
466
|
const spans: i32[] = this.spans
|
|
@@ -355,12 +470,20 @@ export class Http1Parser {
|
|
|
355
470
|
return http1HeadIs(this.head, spans[4 * i + 2], spans[4 * i + 3], value, false)
|
|
356
471
|
}
|
|
357
472
|
|
|
358
|
-
/**
|
|
473
|
+
/**
|
|
474
|
+
* Whether the method is exactly `text` (methods are case-sensitive, RFC 9110 §9.1).
|
|
475
|
+
* @param text the method to compare with, such as `"GET"`
|
|
476
|
+
* @returns true when the request's method is those bytes in that case
|
|
477
|
+
*/
|
|
359
478
|
methodIs(text: string): boolean {
|
|
360
479
|
return http1HeadIs(this.head, this.methodStart, this.methodEnd, text, false)
|
|
361
480
|
}
|
|
362
481
|
|
|
363
|
-
/**
|
|
482
|
+
/**
|
|
483
|
+
* Whether the request target is exactly `text`.
|
|
484
|
+
* @param text the target to compare with, such as `"/index.html"`
|
|
485
|
+
* @returns true when the request's target is those bytes, compared without decoding
|
|
486
|
+
*/
|
|
364
487
|
targetIs(text: string): boolean {
|
|
365
488
|
return http1HeadIs(this.head, this.targetStart, this.targetEnd, text, false)
|
|
366
489
|
}
|
|
@@ -368,6 +491,9 @@ export class Http1Parser {
|
|
|
368
491
|
/**
|
|
369
492
|
* Whether the request asked to switch to exactly `protocol` (any case): one
|
|
370
493
|
* `Upgrade` field holding that one token, with `upgrade` in `Connection`.
|
|
494
|
+
*
|
|
495
|
+
* @param protocol the protocol token to look for, such as `"websocket"`, compared in any case
|
|
496
|
+
* @returns true only when the request asks for that protocol and nothing else
|
|
371
497
|
*/
|
|
372
498
|
upgradeIs(protocol: string): boolean {
|
|
373
499
|
if (!this.upgrading) {
|
|
@@ -394,17 +520,25 @@ export class Http1Parser {
|
|
|
394
520
|
* so its input and head buffers hold nothing it needs: a caller may lend
|
|
395
521
|
* them out and leave empty arrays in `buf`, `body` and `head`, which the
|
|
396
522
|
* next feed and head make or the caller puts back first.
|
|
523
|
+
*
|
|
524
|
+
* @returns true when the parser holds no bytes it still needs, so its buffers may be lent out
|
|
397
525
|
*/
|
|
398
526
|
resting(): boolean {
|
|
399
527
|
return this.state === HTTP1_S_REQUEST_LINE && this.end === this.start && !this.headHeld
|
|
400
528
|
}
|
|
401
529
|
|
|
402
|
-
/**
|
|
530
|
+
/**
|
|
531
|
+
* How many bytes were fed and not consumed yet: what a caller bounds its reads by.
|
|
532
|
+
* @returns the count of unread input bytes, at `buf[start ..]`
|
|
533
|
+
*/
|
|
403
534
|
buffered(): i32 {
|
|
404
535
|
return this.end - this.start
|
|
405
536
|
}
|
|
406
537
|
|
|
407
|
-
/**
|
|
538
|
+
/**
|
|
539
|
+
* After `HTTP1_UPGRADE`: every byte fed past the request, as a fresh array.
|
|
540
|
+
* @returns a copy the caller owns of the bytes that belong to the next protocol, empty when none arrived yet
|
|
541
|
+
*/
|
|
408
542
|
upgradeBytes(): u8[] {
|
|
409
543
|
let n: i32 = this.end - this.start
|
|
410
544
|
if (n < 0) {
|
|
@@ -695,7 +829,11 @@ const HTTP1_TCHAR_32: i32 = 67071226
|
|
|
695
829
|
const HTTP1_TCHAR_64: i32 = -939524098
|
|
696
830
|
const HTTP1_TCHAR_96: i32 = 1476395007
|
|
697
831
|
|
|
698
|
-
/**
|
|
832
|
+
/**
|
|
833
|
+
* Whether `c` is a tchar of RFC 9110 §5.6.2, the bytes a token is made of.
|
|
834
|
+
* @param c the byte to test, as a number 0 to 255
|
|
835
|
+
* @returns true when `c` may stand in a method, a field name or another token
|
|
836
|
+
*/
|
|
699
837
|
export const http1IsTokenByte = (c: i32): boolean => {
|
|
700
838
|
const low: u64 = toU64(toU32(HTTP1_TCHAR_32)) << toU64(32)
|
|
701
839
|
const high: u64 = (toU64(toU32(HTTP1_TCHAR_96)) << toU64(32)) | toU64(toU32(HTTP1_TCHAR_64))
|
|
@@ -763,10 +901,21 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
|
763
901
|
return k
|
|
764
902
|
}
|
|
765
903
|
|
|
766
|
-
/**
|
|
904
|
+
/**
|
|
905
|
+
* Whether `c` may appear in a field value (RFC 9110 §5.5): HTAB, SP, VCHAR or obs-text.
|
|
906
|
+
* @param c the byte to test, as a number 0 to 255
|
|
907
|
+
* @returns true when `c` cannot end the line early or smuggle a control byte into the value
|
|
908
|
+
*/
|
|
767
909
|
export const http1IsValueByte = (c: i32): boolean => c === 9 || (c >= 32 && c !== 127)
|
|
768
910
|
|
|
769
|
-
/**
|
|
911
|
+
/**
|
|
912
|
+
* `buf[from .. to)` as a string, each upper-case ASCII letter lowered when `lower`.
|
|
913
|
+
* @param buf the bytes to read from
|
|
914
|
+
* @param from where the text starts in `buf`
|
|
915
|
+
* @param to where the text ends in `buf`, exclusive
|
|
916
|
+
* @param lower true to lowercase ASCII letters, as a field name wants
|
|
917
|
+
* @returns a fresh string of those bytes, one character per byte
|
|
918
|
+
*/
|
|
770
919
|
export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string => {
|
|
771
920
|
const parts: string[] = []
|
|
772
921
|
for (let k: i32 = from; k >= 0 && k < to && k < toI32(buf.length); k += 1) {
|
|
@@ -779,10 +928,21 @@ export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string
|
|
|
779
928
|
return parts.join("")
|
|
780
929
|
}
|
|
781
930
|
|
|
782
|
-
/**
|
|
931
|
+
/**
|
|
932
|
+
* Whether `c` is optional whitespace, a space or a tab (OWS, RFC 9110 §5.6.3).
|
|
933
|
+
* @param c the byte to test, as a number 0 to 255
|
|
934
|
+
* @returns true when `c` is whitespace a field value is trimmed of
|
|
935
|
+
*/
|
|
783
936
|
export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
|
|
784
937
|
|
|
785
|
-
/**
|
|
938
|
+
/**
|
|
939
|
+
* Whether `text[a .. b)` is `word`, which is lowercase, in any ASCII case.
|
|
940
|
+
* @param text the string holding the candidate
|
|
941
|
+
* @param a where the candidate starts in `text`
|
|
942
|
+
* @param b where the candidate ends in `text`, exclusive
|
|
943
|
+
* @param word the lowercase word to compare with
|
|
944
|
+
* @returns true when the slice is `word` with any letters in either case
|
|
945
|
+
*/
|
|
786
946
|
export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
|
|
787
947
|
const n: i32 = toI32(word.length)
|
|
788
948
|
if (a < 0 || b - a !== n || b > toI32(text.length)) {
|
|
@@ -1274,7 +1434,11 @@ const http1ContentLength = (bytes: u8[], from: i32, to: i32, limit: i32): i32 =>
|
|
|
1274
1434
|
return over ? -2 : value
|
|
1275
1435
|
}
|
|
1276
1436
|
|
|
1277
|
-
/**
|
|
1437
|
+
/**
|
|
1438
|
+
* The value of the hex digit `c`, or -1.
|
|
1439
|
+
* @param c the byte to read, as a number 0 to 255
|
|
1440
|
+
* @returns 0 to 15 for a hex digit in either case, or -1 when `c` is not one
|
|
1441
|
+
*/
|
|
1278
1442
|
export const http1HexValue = (c: i32): i32 => {
|
|
1279
1443
|
if (c >= 48 && c <= 57) {
|
|
1280
1444
|
return c - 48
|
|
@@ -1484,7 +1648,11 @@ const http1BytesOf = (text: string): u8[] => {
|
|
|
1484
1648
|
return out
|
|
1485
1649
|
}
|
|
1486
1650
|
|
|
1487
|
-
/**
|
|
1651
|
+
/**
|
|
1652
|
+
* Whether every byte of `text` is a tchar, and there is at least one.
|
|
1653
|
+
* @param text the candidate token, such as a field name a caller wants to send
|
|
1654
|
+
* @returns true when `text` is a valid token, false for an empty string
|
|
1655
|
+
*/
|
|
1488
1656
|
export const http1IsToken = (text: string): boolean => {
|
|
1489
1657
|
const n: i32 = toI32(text.length)
|
|
1490
1658
|
if (n === 0) {
|
|
@@ -1498,7 +1666,11 @@ export const http1IsToken = (text: string): boolean => {
|
|
|
1498
1666
|
return true
|
|
1499
1667
|
}
|
|
1500
1668
|
|
|
1501
|
-
/**
|
|
1669
|
+
/**
|
|
1670
|
+
* Whether every byte of `text` may stand in a field value or a reason phrase.
|
|
1671
|
+
* @param text the candidate value or reason phrase
|
|
1672
|
+
* @returns true when nothing in `text` could end the line early; an empty string passes
|
|
1673
|
+
*/
|
|
1502
1674
|
export const http1IsFieldText = (text: string): boolean => {
|
|
1503
1675
|
const n: i32 = toI32(text.length)
|
|
1504
1676
|
for (let k: i32 = 0; k < n; k += 1) {
|
|
@@ -1523,6 +1695,13 @@ export const http1IsFieldText = (text: string): boolean => {
|
|
|
1523
1695
|
* caller's fields, since the framing is decided here; a 1xx or 204 response
|
|
1524
1696
|
* with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
|
|
1525
1697
|
* below zero.
|
|
1698
|
+
*
|
|
1699
|
+
* @param status the status code, 100 to 599
|
|
1700
|
+
* @param reason the reason phrase after the code, which may be empty
|
|
1701
|
+
* @param names the caller's field names, one per field, beside `values`
|
|
1702
|
+
* @param values the caller's field values, `values[i]` for `names[i]`
|
|
1703
|
+
* @param bodyLength the body's length in bytes, or `HTTP1_CHUNKED` or `HTTP1_NO_BODY`
|
|
1704
|
+
* @returns the head's bytes in a fresh array the caller owns, or `null` when the head is refused
|
|
1526
1705
|
*/
|
|
1527
1706
|
export const http1ResponseHead = (
|
|
1528
1707
|
status: i32,
|
|
@@ -1584,8 +1763,15 @@ const http1HexDigits = (len: i32): i32 => {
|
|
|
1584
1763
|
* CRLF — and answers where it ends, or `HTTP1_NO_ROOM` when it does not fit,
|
|
1585
1764
|
* with `out` untouched. An empty window writes nothing and answers `at`,
|
|
1586
1765
|
* because a chunk of size zero ends the body. A window outside `data` panics.
|
|
1766
|
+
*
|
|
1767
|
+
* @param out the caller's buffer the chunk is written into
|
|
1768
|
+
* @param at where in `out` the chunk starts
|
|
1769
|
+
* @param data the body bytes to send
|
|
1770
|
+
* @param off where in `data` the chunk's bytes start
|
|
1771
|
+
* @param len how many bytes of `data` the chunk carries
|
|
1772
|
+
* @returns the offset in `out` just past the chunk, or `HTTP1_NO_ROOM` when it does not fit
|
|
1587
1773
|
*/
|
|
1588
|
-
export const http1WriteChunk = (out: u8[], at: i32, data: u8[], off: i32, len: i32): i32 => {
|
|
1774
|
+
export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
|
|
1589
1775
|
const size: i32 = toI32(data.length)
|
|
1590
1776
|
if (off < 0 || len < 0 || off > size || len > size - off) {
|
|
1591
1777
|
panic("http1WriteChunk: the window is outside the buffer")
|
|
@@ -1621,6 +1807,11 @@ export const http1WriteChunk = (out: u8[], at: i32, data: u8[], off: i32, len: i
|
|
|
1621
1807
|
* An empty window answers an empty array, because a chunk of size zero is the
|
|
1622
1808
|
* end of the body and only `http1LastChunk` writes that. A window outside
|
|
1623
1809
|
* `data` panics.
|
|
1810
|
+
*
|
|
1811
|
+
* @param data the body bytes to send
|
|
1812
|
+
* @param off where in `data` the chunk's bytes start
|
|
1813
|
+
* @param len how many bytes of `data` the chunk carries
|
|
1814
|
+
* @returns the chunk's bytes, framing included, in a fresh array the caller owns
|
|
1624
1815
|
*/
|
|
1625
1816
|
export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
|
|
1626
1817
|
const size: i32 = toI32(data.length)
|
|
@@ -1635,12 +1826,19 @@ export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
|
|
|
1635
1826
|
return out
|
|
1636
1827
|
}
|
|
1637
1828
|
|
|
1638
|
-
/**
|
|
1829
|
+
/**
|
|
1830
|
+
* The last chunk and the empty trailer section that end a chunked body: `0\r\n\r\n`.
|
|
1831
|
+
* @returns those five bytes in a fresh array the caller owns
|
|
1832
|
+
*/
|
|
1639
1833
|
export const http1LastChunk = (): u8[] => [48, 13, 10, 13, 10]
|
|
1640
1834
|
|
|
1641
1835
|
/**
|
|
1642
1836
|
* Writes `http1LastChunk`'s five bytes into `out` at `at` and answers where
|
|
1643
1837
|
* they end, or `HTTP1_NO_ROOM` with `out` untouched.
|
|
1838
|
+
*
|
|
1839
|
+
* @param out the caller's buffer the last chunk is written into
|
|
1840
|
+
* @param at where in `out` the last chunk starts
|
|
1841
|
+
* @returns the offset in `out` just past the five bytes, or `HTTP1_NO_ROOM` when they do not fit
|
|
1644
1842
|
*/
|
|
1645
1843
|
export const http1WriteLastChunk = (out: u8[], at: i32): i32 => {
|
|
1646
1844
|
if (at < 0 || at > toI32(out.length) - 5) {
|
|
@@ -1699,7 +1897,12 @@ const http1BytesAreFieldText = (bytes: u8[]): boolean => {
|
|
|
1699
1897
|
return k === n || http1LanesAreFieldText(loadU8x16(bytes, n - 16))
|
|
1700
1898
|
}
|
|
1701
1899
|
|
|
1702
|
-
/**
|
|
1900
|
+
/**
|
|
1901
|
+
* Whether the octets of `bytes` spell `word`, which is lowercase, in any ASCII case: a field name compared as RFC 9110 §5.1 says.
|
|
1902
|
+
* @param bytes the field name as octets, as `nish/net/http-fields` holds it
|
|
1903
|
+
* @param word the lowercase name to compare with
|
|
1904
|
+
* @returns true when `bytes` is `word` with any letters in either case
|
|
1905
|
+
*/
|
|
1703
1906
|
export const http1BytesAre = (bytes: u8[], word: string): boolean =>
|
|
1704
1907
|
http1HeadIs(bytes, 0, toI32(bytes.length), word, true)
|
|
1705
1908
|
|
|
@@ -1723,7 +1926,13 @@ const http1DecimalDigits = (n: i32): i32 => {
|
|
|
1723
1926
|
return digits
|
|
1724
1927
|
}
|
|
1725
1928
|
|
|
1726
|
-
/**
|
|
1929
|
+
/**
|
|
1930
|
+
* Writes `text` at `out[at ..)`, which the caller has made sure holds it, and answers where it ends.
|
|
1931
|
+
* @param out the caller's buffer
|
|
1932
|
+
* @param at where in `out` the text starts
|
|
1933
|
+
* @param text the text to write, one byte per character
|
|
1934
|
+
* @returns the offset in `out` just past the text
|
|
1935
|
+
*/
|
|
1727
1936
|
export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
|
|
1728
1937
|
const n: i32 = toI32(text.length)
|
|
1729
1938
|
if (at >= 0 && at <= toI32(out.length) - n) {
|
|
@@ -1845,6 +2054,17 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
|
1845
2054
|
* that is not field text on a final status, the only one it is sent on;
|
|
1846
2055
|
* `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
|
|
1847
2056
|
* counted when it is sent. Either way `out` is untouched.
|
|
2057
|
+
*
|
|
2058
|
+
* @param out the caller's buffer the head is written into
|
|
2059
|
+
* @param at where in `out` the head starts
|
|
2060
|
+
* @param status the status code, 100 to 599
|
|
2061
|
+
* @param reason the reason phrase after the code, which may be empty
|
|
2062
|
+
* @param names the caller's field names as octets, beside `values`
|
|
2063
|
+
* @param values the caller's field values as octets, `values[i]` for `names[i]`
|
|
2064
|
+
* @param bodyLength the body's length in bytes, or `HTTP1_CHUNKED` or `HTTP1_NO_BODY`
|
|
2065
|
+
* @param connection the `Connection` field's value to send, such as `"close"`, or empty for none
|
|
2066
|
+
* @param date the IMF-fixdate bytes to send as `Date`, or empty for none
|
|
2067
|
+
* @returns the offset in `out` just past the head, `HTTP1_REFUSED` for a head it will not write, or `HTTP1_NO_ROOM`
|
|
1848
2068
|
*/
|
|
1849
2069
|
export const http1WriteResponseHead = (
|
|
1850
2070
|
out: u8[],
|