@amritk/nish-x86_64-linux 0.35.0 → 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/INSTALL.md +6 -1
- package/bin/nish +0 -0
- package/docs/AI.md +9 -2
- package/package.json +1 -1
- package/runtime/nish.d.ts +4612 -430
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/base64.ts +34 -5
- package/std/bytes.ts +256 -40
- 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 +235 -27
- package/std/net/http1-server.ts +343 -85
- 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 +246 -96
- 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 +723 -103
- 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 +228 -44
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +489 -171
- 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 +205 -35
- package/std/sync/shm.ts +194 -16
- 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
|
|
|
@@ -175,7 +187,12 @@ export class Http1Parser {
|
|
|
175
187
|
// the struct is the 200 bytes its fields sum to, rounded to 8.
|
|
176
188
|
|
|
177
189
|
// --- the request's buffers -------------------------------------------------
|
|
178
|
-
/**
|
|
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
|
+
*/
|
|
179
196
|
buf: u8[]
|
|
180
197
|
/**
|
|
181
198
|
* The request line and the kept field lines of the request, copied out of
|
|
@@ -190,13 +207,42 @@ export class Http1Parser {
|
|
|
190
207
|
body: u8[]
|
|
191
208
|
|
|
192
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
|
+
*/
|
|
193
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
|
+
*/
|
|
194
223
|
end: i32 = 0
|
|
195
|
-
/**
|
|
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
|
+
*/
|
|
196
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
|
+
*/
|
|
197
234
|
state: i32 = 0
|
|
198
|
-
/**
|
|
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
|
+
*/
|
|
199
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
|
+
*/
|
|
200
246
|
lineEnd: i32 = 0
|
|
201
247
|
/** How many fields the request's header section kept: the spans of field `i` are `spans[4 * i ..]`. */
|
|
202
248
|
headerCount: i32 = 0
|
|
@@ -207,6 +253,7 @@ export class Http1Parser {
|
|
|
207
253
|
* control byte other than HTAB, or DEL (RFC 9110 §5.5). `http1TakeLine`
|
|
208
254
|
* sets it as it looks for the line's end, so a field line's bytes are read
|
|
209
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.
|
|
210
257
|
*/
|
|
211
258
|
lineControl: boolean = false
|
|
212
259
|
chunked: boolean = false
|
|
@@ -216,6 +263,7 @@ export class Http1Parser {
|
|
|
216
263
|
/**
|
|
217
264
|
* Whether a kept-alive request ended and its head is still held for the
|
|
218
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.
|
|
219
267
|
*/
|
|
220
268
|
headHeld: boolean = false
|
|
221
269
|
/**
|
|
@@ -226,13 +274,22 @@ export class Http1Parser {
|
|
|
226
274
|
keepText: boolean = true
|
|
227
275
|
|
|
228
276
|
// --- the request's numbers: valid from its `HTTP1_HEAD` until the `next()` after its `HTTP1_END`
|
|
229
|
-
/**
|
|
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
|
+
*/
|
|
230
281
|
sectionBytes: i32 = 0
|
|
231
|
-
/**
|
|
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
|
+
*/
|
|
232
286
|
fieldCount: i32 = 0
|
|
233
287
|
/** The minor version: 0 for HTTP/1.0, 1 for HTTP/1.1 and anything above it. */
|
|
234
288
|
minor: i32 = 1
|
|
235
|
-
/**
|
|
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
|
+
*/
|
|
236
293
|
left: i32 = 0
|
|
237
294
|
/** The declared body length, or -1 when the request has none or is chunked. */
|
|
238
295
|
contentLength: i32 = -1
|
|
@@ -245,9 +302,25 @@ export class Http1Parser {
|
|
|
245
302
|
targetEnd: i32 = 0
|
|
246
303
|
|
|
247
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
|
+
*/
|
|
248
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
|
+
*/
|
|
249
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
|
+
*/
|
|
250
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
|
+
*/
|
|
251
324
|
maxBody: i32 = 0
|
|
252
325
|
/** The most body bytes one `HTTP1_BODY` hands over; a caller's number, 2^30 unless set. */
|
|
253
326
|
maxChunk: i32 = 1073741824
|
|
@@ -262,17 +335,28 @@ export class Http1Parser {
|
|
|
262
335
|
upgrade: string = ""
|
|
263
336
|
/** What was wrong, once `next()` said `HTTP1_ERROR`; for a log, not for the response. */
|
|
264
337
|
reason: string = ""
|
|
265
|
-
/**
|
|
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
|
+
*/
|
|
266
342
|
tally: Http1ListTally
|
|
267
343
|
/** The status code to answer once `next()` said `HTTP1_ERROR`. */
|
|
268
344
|
status: i32 = 0
|
|
269
|
-
/**
|
|
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
|
+
*/
|
|
270
349
|
bodyTotal: i32 = 0
|
|
271
350
|
|
|
272
351
|
/**
|
|
273
352
|
* Each limit is clamped into 0 to 2^30, so that the sums the parser makes
|
|
274
353
|
* of them (a request line is the method, the target and ten bytes more)
|
|
275
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
|
|
276
360
|
*/
|
|
277
361
|
constructor(maxTarget: i32, maxHeaderBytes: i32, maxHeaders: i32, maxBody: i32) {
|
|
278
362
|
this.maxTarget = http1Clamp(maxTarget)
|
|
@@ -301,6 +385,10 @@ export class Http1Parser {
|
|
|
301
385
|
* panics, as the crypto modules' `update` does. Once the parser has closed
|
|
302
386
|
* or refused, the input is dropped; once it has upgraded, it is kept for
|
|
303
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
|
|
304
392
|
*/
|
|
305
393
|
feed(data: u8[], off: i32, len: i32): void {
|
|
306
394
|
const size: i32 = toI32(data.length)
|
|
@@ -319,7 +407,10 @@ export class Http1Parser {
|
|
|
319
407
|
this.end = at + len
|
|
320
408
|
}
|
|
321
409
|
|
|
322
|
-
/**
|
|
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
|
+
*/
|
|
323
414
|
next(): i32 {
|
|
324
415
|
return http1Next(this)
|
|
325
416
|
}
|
|
@@ -328,6 +419,9 @@ export class Http1Parser {
|
|
|
328
419
|
* The value of the first field named `name` (any case), or `null`. A field
|
|
329
420
|
* that may repeat is read through the spans. The answer is a fresh string;
|
|
330
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
|
|
331
425
|
*/
|
|
332
426
|
header(name: string): string | null {
|
|
333
427
|
const i: i32 = this.headerIndex(name)
|
|
@@ -345,7 +439,11 @@ export class Http1Parser {
|
|
|
345
439
|
return http1Text(this.head, spans[at + 2], spans[at + 3], false)
|
|
346
440
|
}
|
|
347
441
|
|
|
348
|
-
/**
|
|
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
|
+
*/
|
|
349
447
|
headerIndex(name: string): i32 {
|
|
350
448
|
const spans: i32[] = this.spans
|
|
351
449
|
const count: i32 = this.headerCount
|
|
@@ -357,7 +455,12 @@ export class Http1Parser {
|
|
|
357
455
|
return -1
|
|
358
456
|
}
|
|
359
457
|
|
|
360
|
-
/**
|
|
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
|
+
*/
|
|
361
464
|
headerIs(name: string, value: string): boolean {
|
|
362
465
|
const i: i32 = this.headerIndex(name)
|
|
363
466
|
const spans: i32[] = this.spans
|
|
@@ -367,12 +470,20 @@ export class Http1Parser {
|
|
|
367
470
|
return http1HeadIs(this.head, spans[4 * i + 2], spans[4 * i + 3], value, false)
|
|
368
471
|
}
|
|
369
472
|
|
|
370
|
-
/**
|
|
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
|
+
*/
|
|
371
478
|
methodIs(text: string): boolean {
|
|
372
479
|
return http1HeadIs(this.head, this.methodStart, this.methodEnd, text, false)
|
|
373
480
|
}
|
|
374
481
|
|
|
375
|
-
/**
|
|
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
|
+
*/
|
|
376
487
|
targetIs(text: string): boolean {
|
|
377
488
|
return http1HeadIs(this.head, this.targetStart, this.targetEnd, text, false)
|
|
378
489
|
}
|
|
@@ -380,6 +491,9 @@ export class Http1Parser {
|
|
|
380
491
|
/**
|
|
381
492
|
* Whether the request asked to switch to exactly `protocol` (any case): one
|
|
382
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
|
|
383
497
|
*/
|
|
384
498
|
upgradeIs(protocol: string): boolean {
|
|
385
499
|
if (!this.upgrading) {
|
|
@@ -406,17 +520,25 @@ export class Http1Parser {
|
|
|
406
520
|
* so its input and head buffers hold nothing it needs: a caller may lend
|
|
407
521
|
* them out and leave empty arrays in `buf`, `body` and `head`, which the
|
|
408
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
|
|
409
525
|
*/
|
|
410
526
|
resting(): boolean {
|
|
411
527
|
return this.state === HTTP1_S_REQUEST_LINE && this.end === this.start && !this.headHeld
|
|
412
528
|
}
|
|
413
529
|
|
|
414
|
-
/**
|
|
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
|
+
*/
|
|
415
534
|
buffered(): i32 {
|
|
416
535
|
return this.end - this.start
|
|
417
536
|
}
|
|
418
537
|
|
|
419
|
-
/**
|
|
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
|
+
*/
|
|
420
542
|
upgradeBytes(): u8[] {
|
|
421
543
|
let n: i32 = this.end - this.start
|
|
422
544
|
if (n < 0) {
|
|
@@ -707,7 +829,11 @@ const HTTP1_TCHAR_32: i32 = 67071226
|
|
|
707
829
|
const HTTP1_TCHAR_64: i32 = -939524098
|
|
708
830
|
const HTTP1_TCHAR_96: i32 = 1476395007
|
|
709
831
|
|
|
710
|
-
/**
|
|
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
|
+
*/
|
|
711
837
|
export const http1IsTokenByte = (c: i32): boolean => {
|
|
712
838
|
const low: u64 = toU64(toU32(HTTP1_TCHAR_32)) << toU64(32)
|
|
713
839
|
const high: u64 = (toU64(toU32(HTTP1_TCHAR_96)) << toU64(32)) | toU64(toU32(HTTP1_TCHAR_64))
|
|
@@ -775,10 +901,21 @@ const http1LowerName = (head: u8[], k0: i32, stop: i32): i32 => {
|
|
|
775
901
|
return k
|
|
776
902
|
}
|
|
777
903
|
|
|
778
|
-
/**
|
|
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
|
+
*/
|
|
779
909
|
export const http1IsValueByte = (c: i32): boolean => c === 9 || (c >= 32 && c !== 127)
|
|
780
910
|
|
|
781
|
-
/**
|
|
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
|
+
*/
|
|
782
919
|
export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string => {
|
|
783
920
|
const parts: string[] = []
|
|
784
921
|
for (let k: i32 = from; k >= 0 && k < to && k < toI32(buf.length); k += 1) {
|
|
@@ -791,10 +928,21 @@ export const http1Text = (buf: u8[], from: i32, to: i32, lower: boolean): string
|
|
|
791
928
|
return parts.join("")
|
|
792
929
|
}
|
|
793
930
|
|
|
794
|
-
/**
|
|
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
|
+
*/
|
|
795
936
|
export const http1IsOws = (c: i32): boolean => c === 32 || c === 9
|
|
796
937
|
|
|
797
|
-
/**
|
|
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
|
+
*/
|
|
798
946
|
export const http1SameWord = (text: string, a: i32, b: i32, word: string): boolean => {
|
|
799
947
|
const n: i32 = toI32(word.length)
|
|
800
948
|
if (a < 0 || b - a !== n || b > toI32(text.length)) {
|
|
@@ -1286,7 +1434,11 @@ const http1ContentLength = (bytes: u8[], from: i32, to: i32, limit: i32): i32 =>
|
|
|
1286
1434
|
return over ? -2 : value
|
|
1287
1435
|
}
|
|
1288
1436
|
|
|
1289
|
-
/**
|
|
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
|
+
*/
|
|
1290
1442
|
export const http1HexValue = (c: i32): i32 => {
|
|
1291
1443
|
if (c >= 48 && c <= 57) {
|
|
1292
1444
|
return c - 48
|
|
@@ -1496,7 +1648,11 @@ const http1BytesOf = (text: string): u8[] => {
|
|
|
1496
1648
|
return out
|
|
1497
1649
|
}
|
|
1498
1650
|
|
|
1499
|
-
/**
|
|
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
|
+
*/
|
|
1500
1656
|
export const http1IsToken = (text: string): boolean => {
|
|
1501
1657
|
const n: i32 = toI32(text.length)
|
|
1502
1658
|
if (n === 0) {
|
|
@@ -1510,7 +1666,11 @@ export const http1IsToken = (text: string): boolean => {
|
|
|
1510
1666
|
return true
|
|
1511
1667
|
}
|
|
1512
1668
|
|
|
1513
|
-
/**
|
|
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
|
+
*/
|
|
1514
1674
|
export const http1IsFieldText = (text: string): boolean => {
|
|
1515
1675
|
const n: i32 = toI32(text.length)
|
|
1516
1676
|
for (let k: i32 = 0; k < n; k += 1) {
|
|
@@ -1535,6 +1695,13 @@ export const http1IsFieldText = (text: string): boolean => {
|
|
|
1535
1695
|
* caller's fields, since the framing is decided here; a 1xx or 204 response
|
|
1536
1696
|
* with a body length (RFC 9110 §15.2, §15.3.5); and any other `bodyLength`
|
|
1537
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
|
|
1538
1705
|
*/
|
|
1539
1706
|
export const http1ResponseHead = (
|
|
1540
1707
|
status: i32,
|
|
@@ -1596,6 +1763,13 @@ const http1HexDigits = (len: i32): i32 => {
|
|
|
1596
1763
|
* CRLF — and answers where it ends, or `HTTP1_NO_ROOM` when it does not fit,
|
|
1597
1764
|
* with `out` untouched. An empty window writes nothing and answers `at`,
|
|
1598
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
|
|
1599
1773
|
*/
|
|
1600
1774
|
export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i32, len: i32): i32 => {
|
|
1601
1775
|
const size: i32 = toI32(data.length)
|
|
@@ -1633,6 +1807,11 @@ export const http1WriteChunk = (out: u8[], at: i32, data: readonly u8[], off: i3
|
|
|
1633
1807
|
* An empty window answers an empty array, because a chunk of size zero is the
|
|
1634
1808
|
* end of the body and only `http1LastChunk` writes that. A window outside
|
|
1635
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
|
|
1636
1815
|
*/
|
|
1637
1816
|
export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
|
|
1638
1817
|
const size: i32 = toI32(data.length)
|
|
@@ -1647,12 +1826,19 @@ export const http1Chunk = (data: u8[], off: i32, len: i32): u8[] => {
|
|
|
1647
1826
|
return out
|
|
1648
1827
|
}
|
|
1649
1828
|
|
|
1650
|
-
/**
|
|
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
|
+
*/
|
|
1651
1833
|
export const http1LastChunk = (): u8[] => [48, 13, 10, 13, 10]
|
|
1652
1834
|
|
|
1653
1835
|
/**
|
|
1654
1836
|
* Writes `http1LastChunk`'s five bytes into `out` at `at` and answers where
|
|
1655
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
|
|
1656
1842
|
*/
|
|
1657
1843
|
export const http1WriteLastChunk = (out: u8[], at: i32): i32 => {
|
|
1658
1844
|
if (at < 0 || at > toI32(out.length) - 5) {
|
|
@@ -1711,7 +1897,12 @@ const http1BytesAreFieldText = (bytes: u8[]): boolean => {
|
|
|
1711
1897
|
return k === n || http1LanesAreFieldText(loadU8x16(bytes, n - 16))
|
|
1712
1898
|
}
|
|
1713
1899
|
|
|
1714
|
-
/**
|
|
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
|
+
*/
|
|
1715
1906
|
export const http1BytesAre = (bytes: u8[], word: string): boolean =>
|
|
1716
1907
|
http1HeadIs(bytes, 0, toI32(bytes.length), word, true)
|
|
1717
1908
|
|
|
@@ -1735,7 +1926,13 @@ const http1DecimalDigits = (n: i32): i32 => {
|
|
|
1735
1926
|
return digits
|
|
1736
1927
|
}
|
|
1737
1928
|
|
|
1738
|
-
/**
|
|
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
|
+
*/
|
|
1739
1936
|
export const http1PutText = (out: u8[], at: i32, text: string): i32 => {
|
|
1740
1937
|
const n: i32 = toI32(text.length)
|
|
1741
1938
|
if (at >= 0 && at <= toI32(out.length) - n) {
|
|
@@ -1857,6 +2054,17 @@ const http1PutDecimal = (out: u8[], at: i32, n: i32): i32 => {
|
|
|
1857
2054
|
* that is not field text on a final status, the only one it is sent on;
|
|
1858
2055
|
* `HTTP1_NO_ROOM` when it is well formed but does not fit, the `Date` line
|
|
1859
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`
|
|
1860
2068
|
*/
|
|
1861
2069
|
export const http1WriteResponseHead = (
|
|
1862
2070
|
out: u8[],
|