@amritk/nish-aarch64-linux 0.44.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/http2-frame.ts
CHANGED
|
@@ -65,6 +65,8 @@
|
|
|
65
65
|
* Written from RFC 9113, not ported from another implementation.
|
|
66
66
|
*/
|
|
67
67
|
|
|
68
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
69
|
+
|
|
68
70
|
/** The size of a frame header: a 24-bit length, a type, flags and a 31-bit stream identifier (§4.1). */
|
|
69
71
|
export const H2_FRAME_HEADER_SIZE: i32 = 9
|
|
70
72
|
|
|
@@ -243,8 +245,8 @@ export class Http2Frame {
|
|
|
243
245
|
}
|
|
244
246
|
|
|
245
247
|
/** Panics unless `[off, off + len)` lies inside `buf`: a window outside it is the program's mistake. */
|
|
246
|
-
const http2FrameCheckWindow = (what: string, buf: u8[], off: i32, len:
|
|
247
|
-
if (off < 0 || len <
|
|
248
|
+
const http2FrameCheckWindow = (what: string, buf: u8[], off: i32, len: i64): void => {
|
|
249
|
+
if (off < 0 || len < toI64(0) || len > toI64(buf.length) - toI64(off)) {
|
|
248
250
|
panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${buf.length} bytes`)
|
|
249
251
|
}
|
|
250
252
|
}
|
|
@@ -256,24 +258,55 @@ const http2FrameCheckStream = (what: string, id: i32): void => {
|
|
|
256
258
|
}
|
|
257
259
|
}
|
|
258
260
|
|
|
259
|
-
/**
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
261
|
+
/**
|
|
262
|
+
* The big-endian 32-bit value at `buf[at]`, or -1 when the four bytes are not
|
|
263
|
+
* all inside `buf`; every caller has checked they are.
|
|
264
|
+
*/
|
|
265
|
+
const http2FrameU32 = (buf: u8[], at: i32): i64 => {
|
|
266
|
+
// `at` is below `length - 3`, so `at + 3` is inside `buf` and cannot wrap.
|
|
267
|
+
if (at < 0 || at >= toI32(buf.length) - 3) {
|
|
268
|
+
return toI64(-1)
|
|
269
|
+
}
|
|
270
|
+
return (
|
|
271
|
+
(toI64(buf[at]) << toI64(24)) |
|
|
272
|
+
(toI64(buf[at + 1]) << toI64(16)) |
|
|
273
|
+
(toI64(buf[at + 2]) << toI64(8)) |
|
|
274
|
+
toI64(buf[at + 3])
|
|
275
|
+
)
|
|
276
|
+
}
|
|
265
277
|
|
|
266
|
-
/** The big-endian 31-bit value at `buf[at]`, its reserved top bit dropped. */
|
|
267
|
-
const http2FrameU31 = (buf: u8[], at: i32): i32 =>
|
|
278
|
+
/** The big-endian 31-bit value at `buf[at]`, its reserved top bit dropped, or -1 as `http2FrameU32` answers it. */
|
|
279
|
+
const http2FrameU31 = (buf: u8[], at: i32): i32 => {
|
|
280
|
+
const value: i64 = http2FrameU32(buf, at)
|
|
281
|
+
return value < toI64(0) ? H2_FRAME_NONE : toI32(value & toI64(2147483647))
|
|
282
|
+
}
|
|
268
283
|
|
|
269
|
-
/**
|
|
270
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Writes `value`'s low 32 bits big-endian at `out[at]` and answers true, or
|
|
286
|
+
* answers false and writes nothing when the four bytes are not all inside
|
|
287
|
+
* `out`; every caller has checked they are.
|
|
288
|
+
*/
|
|
289
|
+
const http2FramePut32 = (out: u8[], at: i32, value: i64): boolean => {
|
|
290
|
+
// `at` is below `length - 3`, so `at + 3` is inside `out` and cannot wrap.
|
|
291
|
+
if (at < 0 || at >= toI32(out.length) - 3) {
|
|
292
|
+
return false
|
|
293
|
+
}
|
|
271
294
|
out[at] = toU8((value >> toI64(24)) & toI64(255))
|
|
272
295
|
out[at + 1] = toU8((value >> toI64(16)) & toI64(255))
|
|
273
296
|
out[at + 2] = toU8((value >> toI64(8)) & toI64(255))
|
|
274
297
|
out[at + 3] = toU8(value & toI64(255))
|
|
298
|
+
return true
|
|
275
299
|
}
|
|
276
300
|
|
|
301
|
+
/**
|
|
302
|
+
* A frame length summed in `i64`, held at its largest when past an `i32`: `http2WriteHeader` refuses
|
|
303
|
+
* that as past 2^24 − 1, as it does any length there.
|
|
304
|
+
*/
|
|
305
|
+
const http2FrameLength = (total: i64): i32 => (total > toI64(H2_MAX_WINDOW) ? H2_MAX_WINDOW : toI32(total))
|
|
306
|
+
|
|
307
|
+
/** `a + b` for two offsets whose sum a caller has checked fits an `i32`; widened, so it cannot wrap. */
|
|
308
|
+
const http2FrameAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
|
|
309
|
+
|
|
277
310
|
/**
|
|
278
311
|
* Reads the nine-byte header at `buf[off]` into `frame`, when `len` bytes from
|
|
279
312
|
* `off` hold one, and answers whether they did. The payload need not be there
|
|
@@ -289,14 +322,20 @@ const http2FramePut32 = (out: u8[], at: i32, value: i64): void => {
|
|
|
289
322
|
* untouched, so the reader waits for more.
|
|
290
323
|
*/
|
|
291
324
|
export const http2ReadHeader = (frame: Http2Frame, buf: u8[], off: i32, len: i32): boolean => {
|
|
292
|
-
http2FrameCheckWindow("http2ReadHeader", buf, off, len)
|
|
293
|
-
|
|
325
|
+
http2FrameCheckWindow("http2ReadHeader", buf, off, toI64(len))
|
|
326
|
+
// The window is inside `buf`, so with nine bytes in it `off` is below
|
|
327
|
+
// `length - 8`: every byte read is inside `buf` and `off + 9` cannot wrap.
|
|
328
|
+
if (len < H2_FRAME_HEADER_SIZE || off < 0 || off >= toI32(buf.length) - 8) {
|
|
294
329
|
return false
|
|
295
330
|
}
|
|
296
331
|
frame.length = (toI32(buf[off]) << 16) | (toI32(buf[off + 1]) << 8) | toI32(buf[off + 2])
|
|
297
332
|
frame.type = toI32(buf[off + 3])
|
|
298
333
|
frame.flags = toI32(buf[off + 4])
|
|
299
|
-
frame.streamId =
|
|
334
|
+
frame.streamId =
|
|
335
|
+
((toI32(buf[off + 5]) & 127) << 24) |
|
|
336
|
+
(toI32(buf[off + 6]) << 16) |
|
|
337
|
+
(toI32(buf[off + 7]) << 8) |
|
|
338
|
+
toI32(buf[off + 8])
|
|
300
339
|
frame.payload = off + H2_FRAME_HEADER_SIZE
|
|
301
340
|
return true
|
|
302
341
|
}
|
|
@@ -309,29 +348,59 @@ export const http2ReadHeader = (frame: Http2Frame, buf: u8[], off: i32, len: i32
|
|
|
309
348
|
*/
|
|
310
349
|
const http2FrameUnpad = (frame: Http2Frame, buf: u8[], fixed: i32): i32 => {
|
|
311
350
|
const padded: boolean = frame.has(H2_FLAG_PADDED)
|
|
312
|
-
|
|
313
|
-
|
|
351
|
+
// The callers pass 0, 4 or 5 fixed bytes, so `head` is 0 to 6; widened,
|
|
352
|
+
// the sum cannot wrap.
|
|
353
|
+
const head: i32 = padded ? toI32(toI64(fixed) + toI64(1)) : fixed
|
|
354
|
+
const length: i32 = frame.length
|
|
355
|
+
if (fixed < 0 || length < head) {
|
|
314
356
|
return H2_FRAME_SIZE_ERROR
|
|
315
357
|
}
|
|
316
|
-
|
|
358
|
+
const p: i32 = frame.payload
|
|
359
|
+
if (padded) {
|
|
360
|
+
// `http2ParseFrame` checked the payload is inside `buf`, and it is at
|
|
361
|
+
// least `head` bytes, so its first byte is there.
|
|
362
|
+
if (p < 0 || p >= toI32(buf.length)) {
|
|
363
|
+
return H2_FRAME_SIZE_ERROR
|
|
364
|
+
}
|
|
365
|
+
frame.padLength = toI32(buf[p])
|
|
366
|
+
} else {
|
|
367
|
+
frame.padLength = H2_FRAME_NONE
|
|
368
|
+
}
|
|
317
369
|
const pad: i32 = padded ? frame.padLength : H2_FRAME_ZERO
|
|
318
|
-
|
|
370
|
+
// Widened, so neither the difference nor the sums below can wrap.
|
|
371
|
+
if (toI64(pad) > toI64(length) - toI64(head)) {
|
|
319
372
|
return H2_PROTOCOL_ERROR
|
|
320
373
|
}
|
|
321
|
-
|
|
322
|
-
frame.
|
|
374
|
+
// Both lie inside the payload `http2ParseFrame` checked, so both fit an `i32`.
|
|
375
|
+
frame.contentStart = toI32(toI64(p) + toI64(head))
|
|
376
|
+
frame.contentLength = toI32(toI64(length) - toI64(head) - toI64(pad))
|
|
323
377
|
return H2_NO_ERROR
|
|
324
378
|
}
|
|
325
379
|
|
|
326
|
-
/**
|
|
327
|
-
|
|
380
|
+
/**
|
|
381
|
+
* Reads the five bytes of priority fields at `buf[at]` into `frame` (§6.3). A
|
|
382
|
+
* stream may not depend on itself (§5.3.1). Answers 0, or
|
|
383
|
+
* `H2_FRAME_SIZE_ERROR` with `frame` unchanged when the five bytes are not all
|
|
384
|
+
* inside `buf`; every caller has checked they are.
|
|
385
|
+
*/
|
|
386
|
+
const http2FramePriority = (frame: Http2Frame, buf: u8[], at: i32): i32 => {
|
|
387
|
+
// `at` is below `length - 4`, so `at + 4` is inside `buf` and cannot wrap.
|
|
388
|
+
if (at < 0 || at >= toI32(buf.length) - 4) {
|
|
389
|
+
return H2_FRAME_SIZE_ERROR
|
|
390
|
+
}
|
|
328
391
|
frame.hasPriority = true
|
|
329
392
|
frame.exclusive = (toI32(buf[at]) & 128) !== 0
|
|
330
|
-
frame.dependency =
|
|
331
|
-
|
|
393
|
+
frame.dependency =
|
|
394
|
+
((toI32(buf[at]) & 127) << 24) |
|
|
395
|
+
(toI32(buf[at + 1]) << 16) |
|
|
396
|
+
(toI32(buf[at + 2]) << 8) |
|
|
397
|
+
toI32(buf[at + 3])
|
|
398
|
+
// A byte is 0 to 255, so the weight is 1 to 256.
|
|
399
|
+
frame.weight = toI32(toU32(buf[at + 4]) + toU32(1))
|
|
332
400
|
if (frame.dependency === frame.streamId) {
|
|
333
401
|
frame.streamError = H2_PROTOCOL_ERROR
|
|
334
402
|
}
|
|
403
|
+
return H2_NO_ERROR
|
|
335
404
|
}
|
|
336
405
|
|
|
337
406
|
/**
|
|
@@ -352,7 +421,13 @@ const http2FramePriority = (frame: Http2Frame, buf: u8[], at: i32): void => {
|
|
|
352
421
|
* with, `H2_FRAME_SIZE_ERROR` or `H2_PROTOCOL_ERROR`.
|
|
353
422
|
*/
|
|
354
423
|
export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrameSize: i32): i32 => {
|
|
355
|
-
http2FrameCheckWindow("http2ParseFrame", buf, off, H2_FRAME_HEADER_SIZE + frame.length)
|
|
424
|
+
http2FrameCheckWindow("http2ParseFrame", buf, off, toI64(H2_FRAME_HEADER_SIZE) + toI64(frame.length))
|
|
425
|
+
// The window holds the nine header bytes unless `frame.length` is negative,
|
|
426
|
+
// which no header `http2ReadHeader` read can give; with `off` below
|
|
427
|
+
// `length - 8`, `off + 9` cannot wrap.
|
|
428
|
+
if (frame.length < 0 || off < 0 || off >= toI32(buf.length) - 8) {
|
|
429
|
+
return H2_FRAME_SIZE_ERROR
|
|
430
|
+
}
|
|
356
431
|
frame.payload = off + H2_FRAME_HEADER_SIZE
|
|
357
432
|
frame.contentStart = frame.payload
|
|
358
433
|
frame.contentLength = frame.length
|
|
@@ -377,7 +452,8 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
377
452
|
const priority: boolean = frame.has(H2_FLAG_PRIORITY)
|
|
378
453
|
const error: i32 = http2FrameUnpad(frame, buf, priority ? 5 : 0)
|
|
379
454
|
if (error === H2_NO_ERROR && priority) {
|
|
380
|
-
|
|
455
|
+
// `http2FrameUnpad` put the content 5 bytes or more past the payload's start.
|
|
456
|
+
return http2FramePriority(frame, buf, http2FrameAdd(frame.contentStart, -5))
|
|
381
457
|
}
|
|
382
458
|
return error
|
|
383
459
|
}
|
|
@@ -389,8 +465,7 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
389
465
|
frame.streamError = H2_FRAME_SIZE_ERROR
|
|
390
466
|
return H2_NO_ERROR
|
|
391
467
|
}
|
|
392
|
-
http2FramePriority(frame, buf, p)
|
|
393
|
-
return H2_NO_ERROR
|
|
468
|
+
return http2FramePriority(frame, buf, p)
|
|
394
469
|
case H2_FRAME_RST_STREAM:
|
|
395
470
|
if (!onStream) {
|
|
396
471
|
return H2_PROTOCOL_ERROR
|
|
@@ -398,8 +473,9 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
398
473
|
if (frame.length !== 4) {
|
|
399
474
|
return H2_FRAME_SIZE_ERROR
|
|
400
475
|
}
|
|
476
|
+
// The four bytes are inside the window checked above; -1 cannot come back.
|
|
401
477
|
frame.errorCode = http2FrameU32(buf, p)
|
|
402
|
-
return H2_NO_ERROR
|
|
478
|
+
return frame.errorCode < toI64(0) ? H2_FRAME_SIZE_ERROR : H2_NO_ERROR
|
|
403
479
|
case H2_FRAME_SETTINGS:
|
|
404
480
|
if (onStream) {
|
|
405
481
|
return H2_PROTOCOL_ERROR
|
|
@@ -413,10 +489,13 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
413
489
|
return H2_PROTOCOL_ERROR
|
|
414
490
|
}
|
|
415
491
|
const error: i32 = http2FrameUnpad(frame, buf, 4)
|
|
416
|
-
if (error
|
|
417
|
-
|
|
492
|
+
if (error !== H2_NO_ERROR) {
|
|
493
|
+
return error
|
|
418
494
|
}
|
|
419
|
-
|
|
495
|
+
// `http2FrameUnpad` put the content 4 bytes or more past the payload's
|
|
496
|
+
// start, inside the window checked above; -1 cannot come back.
|
|
497
|
+
frame.promisedId = http2FrameU31(buf, http2FrameAdd(frame.contentStart, -4))
|
|
498
|
+
return frame.promisedId < 0 ? H2_FRAME_SIZE_ERROR : H2_NO_ERROR
|
|
420
499
|
}
|
|
421
500
|
case H2_FRAME_PING:
|
|
422
501
|
if (onStream) {
|
|
@@ -430,16 +509,22 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
430
509
|
if (frame.length < 8) {
|
|
431
510
|
return H2_FRAME_SIZE_ERROR
|
|
432
511
|
}
|
|
512
|
+
// The 8 bytes are inside the window checked above, so the sums fit an
|
|
513
|
+
// `i32` and -1 cannot come back.
|
|
433
514
|
frame.lastStreamId = http2FrameU31(buf, p)
|
|
434
|
-
frame.errorCode = http2FrameU32(buf, p
|
|
435
|
-
frame.contentStart = p
|
|
436
|
-
frame.contentLength = frame.length -
|
|
437
|
-
return H2_NO_ERROR
|
|
515
|
+
frame.errorCode = http2FrameU32(buf, http2FrameAdd(p, 4))
|
|
516
|
+
frame.contentStart = http2FrameAdd(p, 8)
|
|
517
|
+
frame.contentLength = http2FrameAdd(frame.length, -8)
|
|
518
|
+
return frame.lastStreamId < 0 || frame.errorCode < toI64(0) ? H2_FRAME_SIZE_ERROR : H2_NO_ERROR
|
|
438
519
|
case H2_FRAME_WINDOW_UPDATE:
|
|
439
520
|
if (frame.length !== 4) {
|
|
440
521
|
return H2_FRAME_SIZE_ERROR
|
|
441
522
|
}
|
|
523
|
+
// The four bytes are inside the window checked above; -1 cannot come back.
|
|
442
524
|
frame.increment = http2FrameU31(buf, p)
|
|
525
|
+
if (frame.increment < 0) {
|
|
526
|
+
return H2_FRAME_SIZE_ERROR
|
|
527
|
+
}
|
|
443
528
|
if (frame.increment === 0) {
|
|
444
529
|
if (!onStream) {
|
|
445
530
|
return H2_PROTOCOL_ERROR
|
|
@@ -462,16 +547,38 @@ export const http2ParseFrame = (frame: Http2Frame, buf: u8[], off: i32, maxFrame
|
|
|
462
547
|
*/
|
|
463
548
|
export const http2SettingCount = (frame: Http2Frame): i32 => frame.length / H2_SETTING_SIZE
|
|
464
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Where setting `k` of the SETTINGS frame in `buf` starts, or -1 for a `k`
|
|
552
|
+
* outside the frame's settings or six bytes not all inside `buf`. Summed in
|
|
553
|
+
* `i64`, where a `k` from any `i32` cannot wrap it.
|
|
554
|
+
*/
|
|
555
|
+
const http2SettingAt = (frame: Http2Frame, buf: u8[], k: i32): i32 => {
|
|
556
|
+
if (k < 0 || k >= http2SettingCount(frame)) {
|
|
557
|
+
return -1
|
|
558
|
+
}
|
|
559
|
+
// `k` is below the count, so the widened product and sum cannot wrap.
|
|
560
|
+
const at: i64 = toI64(frame.payload) + toI64(k) * toI64(H2_SETTING_SIZE)
|
|
561
|
+
if (at < toI64(0) || at > toI64(buf.length) - toI64(H2_SETTING_SIZE)) {
|
|
562
|
+
return -1
|
|
563
|
+
}
|
|
564
|
+
return toI32(at)
|
|
565
|
+
}
|
|
566
|
+
|
|
465
567
|
/**
|
|
466
568
|
* The identifier of setting `k` of the SETTINGS frame in `buf`.
|
|
467
569
|
*
|
|
468
570
|
* @param frame The SETTINGS frame `http2ParseFrame` accepted.
|
|
469
571
|
* @param buf The buffer that frame was read from, unchanged since.
|
|
470
|
-
* @param k Which setting, 0 to `http2SettingCount(frame)` − 1
|
|
471
|
-
* @returns The 16-bit identifier, 0 to 65,535: an `H2_SETTINGS_*`, or one this module does not know
|
|
572
|
+
* @param k Which setting, 0 to `http2SettingCount(frame)` − 1.
|
|
573
|
+
* @returns The 16-bit identifier, 0 to 65,535: an `H2_SETTINGS_*`, or one this module does not know; -1
|
|
574
|
+
* for a `k` outside 0 to `http2SettingCount(frame)` − 1, or a setting not inside `buf`.
|
|
472
575
|
*/
|
|
473
576
|
export const http2SettingId = (frame: Http2Frame, buf: u8[], k: i32): i32 => {
|
|
474
|
-
const at: i32 = frame
|
|
577
|
+
const at: i32 = http2SettingAt(frame, buf, k)
|
|
578
|
+
// `http2SettingAt` answers -1 or an offset below `length - 5`, so `at + 1` cannot wrap.
|
|
579
|
+
if (at < 0 || at >= toI32(buf.length) - 1) {
|
|
580
|
+
return -1
|
|
581
|
+
}
|
|
475
582
|
return (toI32(buf[at]) << 8) | toI32(buf[at + 1])
|
|
476
583
|
}
|
|
477
584
|
|
|
@@ -480,11 +587,15 @@ export const http2SettingId = (frame: Http2Frame, buf: u8[], k: i32): i32 => {
|
|
|
480
587
|
*
|
|
481
588
|
* @param frame The SETTINGS frame `http2ParseFrame` accepted.
|
|
482
589
|
* @param buf The buffer that frame was read from, unchanged since.
|
|
483
|
-
* @param k Which setting, 0 to `http2SettingCount(frame)` − 1
|
|
484
|
-
* @returns The value as sent, 0 to 2^32 − 1, not yet checked: `http2SettingError` checks it
|
|
590
|
+
* @param k Which setting, 0 to `http2SettingCount(frame)` − 1.
|
|
591
|
+
* @returns The value as sent, 0 to 2^32 − 1, not yet checked: `http2SettingError` checks it; -1 for a `k`
|
|
592
|
+
* outside 0 to `http2SettingCount(frame)` − 1, or a setting not inside `buf`.
|
|
485
593
|
*/
|
|
486
|
-
export const http2SettingValue = (frame: Http2Frame, buf: u8[], k: i32): i64 =>
|
|
487
|
-
|
|
594
|
+
export const http2SettingValue = (frame: Http2Frame, buf: u8[], k: i32): i64 => {
|
|
595
|
+
const at: i32 = http2SettingAt(frame, buf, k)
|
|
596
|
+
// `http2SettingAt` answers -1 or an offset below `length - 5`, so `at + 2` cannot wrap.
|
|
597
|
+
return at < 0 ? toI64(-1) : http2FrameU32(buf, http2FrameAdd(at, 2))
|
|
598
|
+
}
|
|
488
599
|
|
|
489
600
|
/**
|
|
490
601
|
* The connection error a setting's value is, or 0 (§6.5.2, RFC 8441 §3). A
|
|
@@ -539,7 +650,12 @@ export const http2WriteHeader = (
|
|
|
539
650
|
if (at < 0 || length < 0 || length > H2_MAX_FRAME_LIMIT) {
|
|
540
651
|
panic(`http2WriteHeader: a frame of ${length} bytes at ${at}`)
|
|
541
652
|
}
|
|
542
|
-
|
|
653
|
+
// With room for the header, `at` is below `length - 8`: the nine bytes are
|
|
654
|
+
// inside `out` and `at + 9` cannot wrap.
|
|
655
|
+
if (
|
|
656
|
+
toI64(at) + toI64(H2_FRAME_HEADER_SIZE) + toI64(length) > toI64(out.length) ||
|
|
657
|
+
at >= toI32(out.length) - 8
|
|
658
|
+
) {
|
|
543
659
|
return -1
|
|
544
660
|
}
|
|
545
661
|
out[at] = toU8((length >> 16) & 255)
|
|
@@ -547,17 +663,13 @@ export const http2WriteHeader = (
|
|
|
547
663
|
out[at + 2] = toU8(length & 255)
|
|
548
664
|
out[at + 3] = toU8(type)
|
|
549
665
|
out[at + 4] = toU8(flags)
|
|
550
|
-
|
|
666
|
+
out[at + 5] = toU8((streamId >> 24) & 255)
|
|
667
|
+
out[at + 6] = toU8((streamId >> 16) & 255)
|
|
668
|
+
out[at + 7] = toU8((streamId >> 8) & 255)
|
|
669
|
+
out[at + 8] = toU8(streamId & 255)
|
|
551
670
|
return at + H2_FRAME_HEADER_SIZE
|
|
552
671
|
}
|
|
553
672
|
|
|
554
|
-
/** Copies `src[off, off + len)` to `out[at]`; the callers have checked both windows. */
|
|
555
|
-
const http2FrameCopy = (out: u8[], at: i32, src: u8[], off: i32, len: i32): void => {
|
|
556
|
-
for (let k: i32 = 0; k < len; k++) {
|
|
557
|
-
out[at + k] = src[off + k]
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
673
|
/**
|
|
562
674
|
* The shared shape of DATA, HEADERS, PUSH_PROMISE and CONTINUATION: the
|
|
563
675
|
* header, the Pad Length when `pad` is 0 to 255 (-1 for none), `fixed` bytes
|
|
@@ -576,17 +688,19 @@ const http2FrameWritePadded = (
|
|
|
576
688
|
len: i32,
|
|
577
689
|
pad: i32
|
|
578
690
|
): i32 => {
|
|
579
|
-
http2FrameCheckWindow("http2Write", src, off, len)
|
|
691
|
+
http2FrameCheckWindow("http2Write", src, off, toI64(len))
|
|
580
692
|
if (pad > 255) {
|
|
581
693
|
panic(`http2Write: ${pad} bytes of padding, past 255`)
|
|
582
694
|
}
|
|
583
695
|
const padded: boolean = pad >= 0
|
|
584
|
-
|
|
696
|
+
// `fixed` is 0 or 4, `pad` -1 to 255 and `len` 0 to 2^31 − 1, so the widened sums cannot wrap.
|
|
697
|
+
const head: i32 = padded ? toI32(toI64(fixed) + toI64(1)) : fixed
|
|
585
698
|
const tail: i32 = padded ? pad : H2_FRAME_ZERO
|
|
699
|
+
const total: i64 = toI64(head) + toI64(len) + toI64(tail)
|
|
586
700
|
const at2: i32 = http2WriteHeader(
|
|
587
701
|
out,
|
|
588
702
|
at,
|
|
589
|
-
|
|
703
|
+
http2FrameLength(total),
|
|
590
704
|
type,
|
|
591
705
|
padded ? flags | H2_FLAG_PADDED : flags & ~H2_FLAG_PADDED,
|
|
592
706
|
streamId
|
|
@@ -594,13 +708,21 @@ const http2FrameWritePadded = (
|
|
|
594
708
|
if (at2 < 0) {
|
|
595
709
|
return -1
|
|
596
710
|
}
|
|
711
|
+
// `http2WriteHeader` found room for `total` bytes from `at2`, so every
|
|
712
|
+
// offset below is inside `out` and fits an `i32`.
|
|
713
|
+
const body: i32 = toI32(toI64(at2) + toI64(head))
|
|
714
|
+
const end: i32 = toI32(toI64(body) + toI64(len) + toI64(tail))
|
|
597
715
|
if (padded) {
|
|
716
|
+
if (at2 >= toI32(out.length)) {
|
|
717
|
+
return -1
|
|
718
|
+
}
|
|
598
719
|
out[at2] = toU8(pad)
|
|
599
720
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
721
|
+
if (!tryCopyBytes(out, body, src, off, len)) {
|
|
722
|
+
return -1
|
|
723
|
+
}
|
|
724
|
+
out.fill(0, toI32(toI64(body) + toI64(len)), end)
|
|
725
|
+
return end
|
|
604
726
|
}
|
|
605
727
|
|
|
606
728
|
/**
|
|
@@ -719,11 +841,13 @@ export const http2WritePriority = (
|
|
|
719
841
|
panic(`http2WritePriority: stream ${streamId} on ${dependency} with weight ${weight}`)
|
|
720
842
|
}
|
|
721
843
|
const body: i32 = http2WriteHeader(out, at, 5, H2_FRAME_PRIORITY, H2_FRAME_ZERO, streamId)
|
|
722
|
-
|
|
844
|
+
// With room for the 5 bytes, `body` is below `length - 4`, so `body + 5` cannot wrap.
|
|
845
|
+
if (body < 0 || body >= toI32(out.length) - 4) {
|
|
723
846
|
return -1
|
|
724
847
|
}
|
|
725
848
|
http2FramePut32(out, body, toI64(dependency) | (exclusive ? H2_FRAME_EXCLUSIVE : toI64(0)))
|
|
726
|
-
|
|
849
|
+
// The check above put `weight` at 1 to 256.
|
|
850
|
+
out[body + 4] = toU8(toU32(weight) - toU32(1))
|
|
727
851
|
return body + 5
|
|
728
852
|
}
|
|
729
853
|
|
|
@@ -741,10 +865,10 @@ export const http2WriteRstStream = (out: u8[], at: i32, streamId: i32, code: i64
|
|
|
741
865
|
panic("http2WriteRstStream: RST_STREAM on stream 0")
|
|
742
866
|
}
|
|
743
867
|
const body: i32 = http2WriteHeader(out, at, 4, H2_FRAME_RST_STREAM, H2_FRAME_ZERO, streamId)
|
|
744
|
-
|
|
868
|
+
// With room for the 4 bytes, `body` is below `length - 3`, so `body + 4` cannot wrap.
|
|
869
|
+
if (body < 0 || !http2FramePut32(out, body, code) || body >= toI32(out.length) - 3) {
|
|
745
870
|
return -1
|
|
746
871
|
}
|
|
747
|
-
http2FramePut32(out, body, code)
|
|
748
872
|
return body + 4
|
|
749
873
|
}
|
|
750
874
|
|
|
@@ -775,10 +899,12 @@ export const http2WriteSettings = (out: u8[], at: i32, ids: i32[], values: i64[]
|
|
|
775
899
|
panic(`http2WriteSettings: setting ${ids[k]} = ${values[k]}`)
|
|
776
900
|
}
|
|
777
901
|
}
|
|
902
|
+
// Summed in `i64`, where `n` settings cannot wrap.
|
|
903
|
+
const total: i64 = toI64(n) * toI64(H2_SETTING_SIZE)
|
|
778
904
|
const body: i32 = http2WriteHeader(
|
|
779
905
|
out,
|
|
780
906
|
at,
|
|
781
|
-
|
|
907
|
+
http2FrameLength(total),
|
|
782
908
|
H2_FRAME_SETTINGS,
|
|
783
909
|
H2_FRAME_ZERO,
|
|
784
910
|
H2_FRAME_ZERO
|
|
@@ -787,14 +913,19 @@ export const http2WriteSettings = (out: u8[], at: i32, ids: i32[], values: i64[]
|
|
|
787
913
|
return -1
|
|
788
914
|
}
|
|
789
915
|
for (let k: i32 = 0; k < n && k < toI32(ids.length) && k < toI32(values.length); k++) {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
916
|
+
// Inside the `total` bytes `http2WriteHeader` found room for, so it fits an `i32`.
|
|
917
|
+
const at2: i32 = toI32(toI64(body) + toI64(k) * toI64(H2_SETTING_SIZE))
|
|
918
|
+
// Below `length - 5`, so `at2 + 2` cannot wrap.
|
|
919
|
+
if (at2 < 0 || at2 >= toI32(out.length) - 5) {
|
|
920
|
+
return -1
|
|
921
|
+
}
|
|
922
|
+
out[at2] = toU8((ids[k] >> 8) & 255)
|
|
923
|
+
out[at2 + 1] = toU8(ids[k] & 255)
|
|
924
|
+
if (!http2FramePut32(out, at2 + 2, values[k])) {
|
|
925
|
+
return -1
|
|
794
926
|
}
|
|
795
|
-
http2FramePut32(out, at2 + 2, values[k])
|
|
796
927
|
}
|
|
797
|
-
return body + n * H2_SETTING_SIZE
|
|
928
|
+
return toI32(toI64(body) + toI64(n) * toI64(H2_SETTING_SIZE))
|
|
798
929
|
}
|
|
799
930
|
|
|
800
931
|
/**
|
|
@@ -853,12 +984,10 @@ export const http2WritePushPromise = (
|
|
|
853
984
|
len,
|
|
854
985
|
pad
|
|
855
986
|
)
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
toI64(promisedId)
|
|
861
|
-
)
|
|
987
|
+
// The frame was written from `at`, so the field's offset is inside `out` and fits an `i32`.
|
|
988
|
+
const field: i64 = toI64(at) + toI64(H2_FRAME_HEADER_SIZE) + toI64(pad >= 0 ? H2_FRAME_ONE : H2_FRAME_ZERO)
|
|
989
|
+
if (end < 0 || !http2FramePut32(out, toI32(field), toI64(promisedId))) {
|
|
990
|
+
return -1
|
|
862
991
|
}
|
|
863
992
|
return end
|
|
864
993
|
}
|
|
@@ -874,12 +1003,12 @@ export const http2WritePushPromise = (
|
|
|
874
1003
|
* @returns The offset just past the frame in `out`, or -1 when it would not fit, `out` then unchanged.
|
|
875
1004
|
*/
|
|
876
1005
|
export const http2WritePing = (out: u8[], at: i32, opaque: u8[], off: i32, ack: boolean): i32 => {
|
|
877
|
-
http2FrameCheckWindow("http2WritePing", opaque, off, 8)
|
|
1006
|
+
http2FrameCheckWindow("http2WritePing", opaque, off, toI64(8))
|
|
878
1007
|
const body: i32 = http2WriteHeader(out, at, 8, H2_FRAME_PING, ack ? H2_FLAG_ACK : 0, H2_FRAME_ZERO)
|
|
879
|
-
|
|
1008
|
+
// With room for the 8 bytes, `body` is below `length - 7`, so `body + 8` cannot wrap.
|
|
1009
|
+
if (body < 0 || body >= toI32(out.length) - 7 || !tryCopyBytes(out, body, opaque, off, 8)) {
|
|
880
1010
|
return -1
|
|
881
1011
|
}
|
|
882
|
-
http2FrameCopy(out, body, opaque, off, 8)
|
|
883
1012
|
return body + 8
|
|
884
1013
|
}
|
|
885
1014
|
|
|
@@ -908,15 +1037,29 @@ export const http2WriteGoaway = (
|
|
|
908
1037
|
len: i32
|
|
909
1038
|
): i32 => {
|
|
910
1039
|
http2FrameCheckStream("http2WriteGoaway", lastStreamId)
|
|
911
|
-
http2FrameCheckWindow("http2WriteGoaway", debug, off, len)
|
|
912
|
-
|
|
913
|
-
|
|
1040
|
+
http2FrameCheckWindow("http2WriteGoaway", debug, off, toI64(len))
|
|
1041
|
+
// `len` is 0 to 2^31 − 1, so the sum fits an `i64`.
|
|
1042
|
+
const total: i64 = toI64(8) + toI64(len)
|
|
1043
|
+
const body: i32 = http2WriteHeader(
|
|
1044
|
+
out,
|
|
1045
|
+
at,
|
|
1046
|
+
http2FrameLength(total),
|
|
1047
|
+
H2_FRAME_GOAWAY,
|
|
1048
|
+
H2_FRAME_ZERO,
|
|
1049
|
+
H2_FRAME_ZERO
|
|
1050
|
+
)
|
|
1051
|
+
// With room for `total` bytes, `body` is below `length - 7`, so `body + 4`
|
|
1052
|
+
// and `body + 8` cannot wrap, and `body + total` fits an `i32`.
|
|
1053
|
+
if (
|
|
1054
|
+
body < 0 ||
|
|
1055
|
+
body >= toI32(out.length) - 7 ||
|
|
1056
|
+
!http2FramePut32(out, body, toI64(lastStreamId)) ||
|
|
1057
|
+
!http2FramePut32(out, body + 4, code) ||
|
|
1058
|
+
!tryCopyBytes(out, body + 8, debug, off, len)
|
|
1059
|
+
) {
|
|
914
1060
|
return -1
|
|
915
1061
|
}
|
|
916
|
-
|
|
917
|
-
http2FramePut32(out, body + 4, code)
|
|
918
|
-
http2FrameCopy(out, body + 8, debug, off, len)
|
|
919
|
-
return body + 8 + len
|
|
1062
|
+
return toI32(toI64(body) + toI64(8) + toI64(len))
|
|
920
1063
|
}
|
|
921
1064
|
|
|
922
1065
|
/**
|
|
@@ -934,10 +1077,10 @@ export const http2WriteWindowUpdate = (out: u8[], at: i32, streamId: i32, increm
|
|
|
934
1077
|
panic(`http2WriteWindowUpdate: an increment of ${increment}`)
|
|
935
1078
|
}
|
|
936
1079
|
const body: i32 = http2WriteHeader(out, at, 4, H2_FRAME_WINDOW_UPDATE, H2_FRAME_ZERO, streamId)
|
|
937
|
-
|
|
1080
|
+
// With room for the 4 bytes, `body` is below `length - 3`, so `body + 4` cannot wrap.
|
|
1081
|
+
if (body < 0 || body >= toI32(out.length) - 3 || !http2FramePut32(out, body, toI64(increment))) {
|
|
938
1082
|
return -1
|
|
939
1083
|
}
|
|
940
|
-
http2FramePut32(out, body, toI64(increment))
|
|
941
1084
|
return body + 4
|
|
942
1085
|
}
|
|
943
1086
|
|