nish 0.44.0 → 0.46.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 +1 -1
- package/docs/AI.md +18 -16
- package/docs/INSTALL.md +3 -1
- package/package.json +5 -5
- 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 +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +1315 -222
- 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/sqlite.ts +171 -0
- 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.ts
CHANGED
|
@@ -125,6 +125,7 @@
|
|
|
125
125
|
*
|
|
126
126
|
* Written from RFC 9113 and RFC 8441, not ported from another implementation.
|
|
127
127
|
*/
|
|
128
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
128
129
|
import { BufferPool } from "nish/net/buffer-pool"
|
|
129
130
|
import {
|
|
130
131
|
HPACK_DEFAULT_TABLE_SIZE,
|
|
@@ -329,7 +330,11 @@ export class Http2Pools {
|
|
|
329
330
|
* @param ceiling how many buffers of each kind may be lent at once
|
|
330
331
|
*/
|
|
331
332
|
constructor(name: string, config: Http2Config, ceiling: i32) {
|
|
332
|
-
|
|
333
|
+
// A frame size the connection accepts is at most 2^24 - 1, so the header fits beside it; any other is
|
|
334
|
+
// passed on unchanged, and the connection built on that config refuses it.
|
|
335
|
+
const frame: i32 = config.maxFrameSize
|
|
336
|
+
const inputSize: i32 = frame >= 0 && frame <= 16777215 ? frame + H2_FRAME_HEADER_SIZE : frame
|
|
337
|
+
this.input = new BufferPool(`${name}.input`, inputSize, ceiling)
|
|
333
338
|
this.output = new BufferPool(`${name}.output`, config.outputSize, ceiling)
|
|
334
339
|
this.block = new BufferPool(`${name}.block`, config.maxHeaderBlock, ceiling)
|
|
335
340
|
}
|
|
@@ -340,7 +345,8 @@ export class Http2Pools {
|
|
|
340
345
|
* @returns the buffers lent and not given back yet, of all three pools together
|
|
341
346
|
*/
|
|
342
347
|
lent(): i32 {
|
|
343
|
-
|
|
348
|
+
// Each count is a live buffer of one pool, far below 2^31 together; the sum is taken in i64.
|
|
349
|
+
return toI32(toI64(this.input.lent()) + toI64(this.output.lent()) + toI64(this.block.lent()))
|
|
344
350
|
}
|
|
345
351
|
|
|
346
352
|
/**
|
|
@@ -349,7 +355,8 @@ export class Http2Pools {
|
|
|
349
355
|
* @returns the buffers the three pools have allocated, together; at most three times the ceiling
|
|
350
356
|
*/
|
|
351
357
|
made(): i32 {
|
|
352
|
-
|
|
358
|
+
// Each count is at most its pool's ceiling, far below 2^31 together; the sum is taken in i64.
|
|
359
|
+
return toI32(toI64(this.input.made) + toI64(this.output.made) + toI64(this.block.made))
|
|
353
360
|
}
|
|
354
361
|
}
|
|
355
362
|
|
|
@@ -381,38 +388,67 @@ export class Http2Stream {
|
|
|
381
388
|
* @internal Read by `Http2Connection` to answer H2_WINDOW; not for callers.
|
|
382
389
|
*/
|
|
383
390
|
blocked: boolean = false
|
|
391
|
+
/** `hold` marked the stream: its window is credited by `consumeBody`, not as its DATA arrives. */
|
|
392
|
+
held: boolean = false
|
|
393
|
+
/** Body bytes of a held stream that arrived and that `consumeBody` has not credited yet. */
|
|
394
|
+
owed: i32 = 0
|
|
384
395
|
}
|
|
385
396
|
|
|
386
|
-
/** Whether `buf[off ..]` holds the preface. */
|
|
397
|
+
/** Whether `buf[off ..]` holds the whole preface; a buffer that ends before it is not one. */
|
|
387
398
|
const http2IsPreface = (buf: u8[], off: i32): boolean => {
|
|
388
|
-
|
|
389
|
-
|
|
399
|
+
const preface: string = H2_PREFACE
|
|
400
|
+
const length: i32 = toI32(buf.length)
|
|
401
|
+
// `at` walks `buf` beside `k`; it stays in `[0, length)` when read, so `at + 1` cannot wrap.
|
|
402
|
+
let at: i32 = off
|
|
403
|
+
for (let k: i32 = 0; k < toI32(preface.length); k++) {
|
|
404
|
+
if (at < 0 || at >= length) {
|
|
390
405
|
return false
|
|
391
406
|
}
|
|
407
|
+
if (toI32(buf[at]) !== toI32(preface.charCodeAt(k))) {
|
|
408
|
+
return false
|
|
409
|
+
}
|
|
410
|
+
at = at + 1
|
|
392
411
|
}
|
|
393
412
|
return true
|
|
394
413
|
}
|
|
395
414
|
|
|
396
415
|
/** Moves `buf[start, end)` to the front of `buf` and answers where it now ends. */
|
|
397
416
|
const http2Compact = (buf: u8[], start: i32, end: i32): i32 => {
|
|
417
|
+
// The connection keeps `0 <= start <= end`; a span outside that is left where it is.
|
|
418
|
+
if (start < 0 || end < 0 || end < start) {
|
|
419
|
+
return end
|
|
420
|
+
}
|
|
398
421
|
const keep: i32 = end - start
|
|
422
|
+
// A span past `buf` is not copied; the connection keeps `end` inside its buffer, so the copy runs.
|
|
399
423
|
if (start > 0) {
|
|
400
|
-
|
|
401
|
-
buf[k] = buf[start + k]
|
|
402
|
-
}
|
|
424
|
+
tryCopyBytes(buf, 0, buf, start, keep)
|
|
403
425
|
}
|
|
404
426
|
return keep
|
|
405
427
|
}
|
|
406
428
|
|
|
407
429
|
/** Writes `id` at `at` in the ring `ring` and answers where the next one goes. */
|
|
408
430
|
const http2Remember = (ring: i32[], at: i32, id: i32): i32 => {
|
|
409
|
-
const
|
|
410
|
-
if (
|
|
411
|
-
|
|
431
|
+
const size: i32 = toI32(ring.length)
|
|
432
|
+
if (size <= 0) {
|
|
433
|
+
return 0
|
|
412
434
|
}
|
|
435
|
+
const slot: i32 = at % size
|
|
436
|
+
if (slot < 0 || slot >= size) {
|
|
437
|
+
return 0
|
|
438
|
+
}
|
|
439
|
+
ring[slot] = id
|
|
413
440
|
return slot + 1
|
|
414
441
|
}
|
|
415
442
|
|
|
443
|
+
/** A ring of `count` zeroed identifiers, none when `count` is not positive. */
|
|
444
|
+
const http2Ring = (count: i32): i32[] => {
|
|
445
|
+
const ring: i32[] = []
|
|
446
|
+
for (let k: i32 = 0; k < count; k++) {
|
|
447
|
+
ring.push(0)
|
|
448
|
+
}
|
|
449
|
+
return ring
|
|
450
|
+
}
|
|
451
|
+
|
|
416
452
|
/** Panics unless `[off, off + len)` lies inside `buf`. */
|
|
417
453
|
const http2CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
|
|
418
454
|
if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)) {
|
|
@@ -420,6 +456,29 @@ const http2CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void =>
|
|
|
420
456
|
}
|
|
421
457
|
}
|
|
422
458
|
|
|
459
|
+
/**
|
|
460
|
+
* How long before `now` the instant `since` was, both `monotonicNanos` readings. The difference is taken
|
|
461
|
+
* modulo 2^64: two readings of one monotonic clock are under 2^63 apart, so it is the exact one.
|
|
462
|
+
*/
|
|
463
|
+
const http2Since = (now: i64, since: i64): i64 => toI64(toU64(now) - toU64(since))
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* The `monotonicNanos` instant `ms` milliseconds after `since`. The product is under 2^52 for an `i32`
|
|
467
|
+
* `ms`; the sum is taken modulo 2^64, and a reading plus it stays far below 2^63.
|
|
468
|
+
*/
|
|
469
|
+
const http2After = (since: i64, ms: i32): i64 => toI64(toU64(since) + toU64(toI64(ms) * toI64(1000000)))
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* `a + b` for flow-control figures: a window, the bytes owed to one, or the change a SETTINGS makes to
|
|
473
|
+
* every window. Each stays within 2^33 either side of 0, since a window that passes 2^31 - 1 fails its
|
|
474
|
+
* stream or the connection and no frame moves one by more than 2^31 - 1, so the sum taken modulo 2^64
|
|
475
|
+
* is the exact one.
|
|
476
|
+
*/
|
|
477
|
+
const http2Plus = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
|
|
478
|
+
|
|
479
|
+
/** `a - b` for flow-control figures, exact for the reason `http2Plus` gives. */
|
|
480
|
+
const http2Minus = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
|
|
481
|
+
|
|
423
482
|
/** Whether one of `names` is `date` in any ASCII case (RFC 9110 §5.1). */
|
|
424
483
|
const http2NamesHoldDate = (names: u8[][]): boolean => {
|
|
425
484
|
// An ASCII letter's case is its 0x20 bit, and every octet of `date` is a letter.
|
|
@@ -582,6 +641,8 @@ export class Http2Connection {
|
|
|
582
641
|
resetByPeer: boolean = false
|
|
583
642
|
/** The server's SETTINGS are not written yet: the first `holdOutput` writes them. */
|
|
584
643
|
private prefaceOwed: boolean = false
|
|
644
|
+
/** A held stream's WINDOW_UPDATE did not fit in the output, so `next` or `settleCredit` sends it. */
|
|
645
|
+
private owing: boolean = false
|
|
585
646
|
|
|
586
647
|
/**
|
|
587
648
|
* A connection under `config`, owing its SETTINGS — and the WINDOW_UPDATE
|
|
@@ -621,8 +682,11 @@ export class Http2Connection {
|
|
|
621
682
|
for (let k: i32 = 0; k < config.maxStreams; k++) {
|
|
622
683
|
this.streams.push(new Http2Stream())
|
|
623
684
|
}
|
|
624
|
-
|
|
625
|
-
|
|
685
|
+
// http2CheckCap held maxStreams to [1, 65536] above, so the first arm is the one taken and cannot wrap.
|
|
686
|
+
const streams: i32 = config.maxStreams
|
|
687
|
+
const remembered: i32 = streams >= 0 && streams <= 65536 ? streams + H2_RECENT_RESETS : H2_RECENT_RESETS
|
|
688
|
+
this.resets = http2Ring(remembered)
|
|
689
|
+
this.closed = http2Ring(remembered)
|
|
626
690
|
this.statusName = httpFieldBytes(":status")
|
|
627
691
|
this.statusValue = [48, 48, 48]
|
|
628
692
|
this.date = []
|
|
@@ -723,7 +787,8 @@ export class Http2Connection {
|
|
|
723
787
|
/** Whether the server's SETTINGS have waited longer than `settingsTimeout` at `now` (`monotonicNanos`). */
|
|
724
788
|
private settingsLate(now: i64): boolean {
|
|
725
789
|
return (
|
|
726
|
-
!this.settingsAcked &&
|
|
790
|
+
!this.settingsAcked &&
|
|
791
|
+
http2Since(now, this.settingsSince) > toI64(this.config.settingsTimeout) * toI64(1000000)
|
|
727
792
|
)
|
|
728
793
|
}
|
|
729
794
|
|
|
@@ -736,7 +801,10 @@ export class Http2Connection {
|
|
|
736
801
|
* @returns true when the carrier should end the connection, after `timeOut` has written its GOAWAY
|
|
737
802
|
*/
|
|
738
803
|
expired(now: i64): boolean {
|
|
739
|
-
return
|
|
804
|
+
return (
|
|
805
|
+
this.settingsLate(now) ||
|
|
806
|
+
http2Since(now, this.lastActive) > toI64(this.config.idleTimeout) * toI64(1000000)
|
|
807
|
+
)
|
|
740
808
|
}
|
|
741
809
|
|
|
742
810
|
/**
|
|
@@ -748,9 +816,9 @@ export class Http2Connection {
|
|
|
748
816
|
* @returns the earliest `monotonicNanos` instant at which `expired` could answer true
|
|
749
817
|
*/
|
|
750
818
|
expiresNoSooner(now: i64): i64 {
|
|
751
|
-
let soonest: i64 = this.lastActive
|
|
819
|
+
let soonest: i64 = http2After(this.lastActive, this.config.idleTimeout)
|
|
752
820
|
if (!this.settingsAcked) {
|
|
753
|
-
const settings: i64 = this.settingsSince
|
|
821
|
+
const settings: i64 = http2After(this.settingsSince, this.config.settingsTimeout)
|
|
754
822
|
soonest = settings < soonest ? settings : soonest
|
|
755
823
|
}
|
|
756
824
|
return soonest > now ? soonest : now
|
|
@@ -802,9 +870,9 @@ export class Http2Connection {
|
|
|
802
870
|
if (this.prefaceOwed) {
|
|
803
871
|
this.prefaceOwed = false
|
|
804
872
|
this.wrote(http2WriteSettings(this.output, this.outputEnd, this.settingIds, this.settingValues))
|
|
805
|
-
const
|
|
806
|
-
if (
|
|
807
|
-
this.wrote(http2WriteWindowUpdate(this.output, this.outputEnd, H2_ZERO,
|
|
873
|
+
const size: i32 = this.config.connectionWindowSize
|
|
874
|
+
if (size >= 0 && size > H2_DEFAULT_WINDOW) {
|
|
875
|
+
this.wrote(http2WriteWindowUpdate(this.output, this.outputEnd, H2_ZERO, size - H2_DEFAULT_WINDOW))
|
|
808
876
|
}
|
|
809
877
|
}
|
|
810
878
|
}
|
|
@@ -893,7 +961,9 @@ export class Http2Connection {
|
|
|
893
961
|
* @internal Unused: nothing calls it, and tsc's noUnusedLocals refuses a private member nothing reads.
|
|
894
962
|
*/
|
|
895
963
|
inputRoom(): i32 {
|
|
896
|
-
|
|
964
|
+
// Each term is an i32, so the i64 sum cannot wrap; it is the documented range once taken back to i32.
|
|
965
|
+
const frame: i64 = toI64(this.config.maxFrameSize) + toI64(H2_FRAME_HEADER_SIZE)
|
|
966
|
+
return toI32(http2Minus(frame, toI64(this.inputEnd) - toI64(this.inputStart)))
|
|
897
967
|
}
|
|
898
968
|
|
|
899
969
|
/**
|
|
@@ -916,10 +986,13 @@ export class Http2Connection {
|
|
|
916
986
|
}
|
|
917
987
|
const room: i32 = this.inputTail()
|
|
918
988
|
const n: i32 = len < room ? len : room
|
|
919
|
-
|
|
920
|
-
|
|
989
|
+
// `inputTail` left `room` bytes free from `inputEnd` and `http2CheckWindow` held `[off, off + len)`
|
|
990
|
+
// inside `buf`, so the copy runs; were either window wrong, nothing would be taken.
|
|
991
|
+
if (!tryCopyBytes(this.input, this.inputEnd, buf, off, n)) {
|
|
992
|
+
return 0
|
|
921
993
|
}
|
|
922
|
-
|
|
994
|
+
// `n` is at most the room after `inputEnd` in the input buffer, so the sum stays an i32.
|
|
995
|
+
this.inputEnd = toI32(toI64(this.inputEnd) + toI64(n))
|
|
923
996
|
this.inputUsed = this.inputEnd > this.inputUsed ? this.inputEnd : this.inputUsed
|
|
924
997
|
return n
|
|
925
998
|
}
|
|
@@ -932,24 +1005,31 @@ export class Http2Connection {
|
|
|
932
1005
|
*/
|
|
933
1006
|
inputTail(): i32 {
|
|
934
1007
|
this.holdInput()
|
|
935
|
-
|
|
1008
|
+
const end: i32 = http2Compact(this.input, this.inputStart, this.inputEnd)
|
|
1009
|
+
this.inputEnd = end
|
|
936
1010
|
this.inputStart = 0
|
|
937
|
-
|
|
1011
|
+
const size: i32 = toI32(this.input.length)
|
|
1012
|
+
return end >= 0 && end <= size ? size - end : 0
|
|
938
1013
|
}
|
|
939
1014
|
|
|
940
1015
|
/**
|
|
941
|
-
* Takes `n` bytes a carrier read into `input` at `inputEnd`, after `inputTail` gave it the room
|
|
1016
|
+
* Takes `n` bytes a carrier read into `input` at `inputEnd`, after `inputTail` gave it the room; `n`
|
|
1017
|
+
* below 0 or past that room panics, since the carrier's read could not have written them.
|
|
942
1018
|
* @internal Read by `http2-tls.ts`'s server loop; not for callers.
|
|
943
1019
|
*/
|
|
944
1020
|
received(n: i32): void {
|
|
945
|
-
const
|
|
1021
|
+
const end: i32 = this.inputEnd
|
|
1022
|
+
const size: i32 = toI32(this.input.length)
|
|
1023
|
+
const room: i32 = end >= 0 && end <= size ? size - end : 0
|
|
946
1024
|
if (n < 0 || n > room) {
|
|
947
1025
|
panic(`Http2Connection.received: ${n} bytes into ${room} of room`)
|
|
948
1026
|
}
|
|
1027
|
+
// Both terms are i32s, so the i64 sum is exact; it is at most `size`, so it fits back in an i32.
|
|
1028
|
+
const after: i32 = toI32(toI64(end) + toI64(n))
|
|
949
1029
|
// A carrier's read wrote them whatever the state, so they are zeroed when the input goes back.
|
|
950
|
-
this.inputUsed =
|
|
1030
|
+
this.inputUsed = after > this.inputUsed ? after : this.inputUsed
|
|
951
1031
|
if (this.state !== H2_STATE_FAILED) {
|
|
952
|
-
this.inputEnd =
|
|
1032
|
+
this.inputEnd = after
|
|
953
1033
|
}
|
|
954
1034
|
}
|
|
955
1035
|
|
|
@@ -959,8 +1039,11 @@ export class Http2Connection {
|
|
|
959
1039
|
* @param n how many bytes of `output[outputStart .. outputEnd)` were sent; more than that counts as all
|
|
960
1040
|
*/
|
|
961
1041
|
consume(n: i32): void {
|
|
962
|
-
const
|
|
963
|
-
|
|
1042
|
+
const start: i32 = this.outputStart
|
|
1043
|
+
const end: i32 = this.outputEnd
|
|
1044
|
+
// The connection keeps `0 <= outputStart <= outputEnd`, and the i64 sum below cannot wrap.
|
|
1045
|
+
const pending: i32 = start >= 0 && end >= 0 ? end - start : 0
|
|
1046
|
+
this.outputStart = toI32(toI64(start) + toI64(n < pending ? n : pending))
|
|
964
1047
|
if (this.outputStart === this.outputEnd) {
|
|
965
1048
|
this.giveOutput()
|
|
966
1049
|
}
|
|
@@ -1006,15 +1089,17 @@ export class Http2Connection {
|
|
|
1006
1089
|
/** The room left at the end of the output, once what was sent is moved out of the way. */
|
|
1007
1090
|
private room(): i32 {
|
|
1008
1091
|
this.holdOutput()
|
|
1009
|
-
|
|
1092
|
+
const end: i32 = http2Compact(this.output, this.outputStart, this.outputEnd)
|
|
1093
|
+
this.outputEnd = end
|
|
1010
1094
|
this.outputStart = 0
|
|
1011
|
-
|
|
1095
|
+
const size: i32 = toI32(this.output.length)
|
|
1096
|
+
return end >= 0 && end <= size ? size - end : 0
|
|
1012
1097
|
}
|
|
1013
1098
|
|
|
1014
1099
|
/** The room DATA and HEADERS may use: all but the control reserve. */
|
|
1015
1100
|
private bodyRoom(): i32 {
|
|
1016
|
-
const r: i32 = this.room()
|
|
1017
|
-
return r
|
|
1101
|
+
const r: i32 = this.room()
|
|
1102
|
+
return r >= 0 && r > H2_CONTROL_RESERVE ? r - H2_CONTROL_RESERVE : 0
|
|
1018
1103
|
}
|
|
1019
1104
|
|
|
1020
1105
|
/** Appends a frame a writer put at the end of the output, when it fit (`end >= 0`), and answers whether it did. */
|
|
@@ -1047,8 +1132,8 @@ export class Http2Connection {
|
|
|
1047
1132
|
return null
|
|
1048
1133
|
}
|
|
1049
1134
|
|
|
1050
|
-
/** Opens stream `id` in a free slot;
|
|
1051
|
-
private open(id: i32): Http2Stream {
|
|
1135
|
+
/** Opens stream `id` in a free slot and answers it; null, opening nothing, when no slot is free. */
|
|
1136
|
+
private open(id: i32): Http2Stream | null {
|
|
1052
1137
|
for (const s of this.streams) {
|
|
1053
1138
|
if (s.id === 0) {
|
|
1054
1139
|
s.id = id
|
|
@@ -1061,11 +1146,14 @@ export class Http2Connection {
|
|
|
1061
1146
|
s.localEnded = false
|
|
1062
1147
|
s.responded = false
|
|
1063
1148
|
s.blocked = false
|
|
1064
|
-
|
|
1149
|
+
s.held = false
|
|
1150
|
+
s.owed = 0
|
|
1151
|
+
// At most `maxStreams` slots are in use, so the i64 count fits back in an i32.
|
|
1152
|
+
this.active = toI32(toI64(this.active) + toI64(1))
|
|
1065
1153
|
return s
|
|
1066
1154
|
}
|
|
1067
1155
|
}
|
|
1068
|
-
|
|
1156
|
+
return null
|
|
1069
1157
|
}
|
|
1070
1158
|
|
|
1071
1159
|
/** Frees the slot of a stream that is closed. */
|
|
@@ -1073,7 +1161,7 @@ export class Http2Connection {
|
|
|
1073
1161
|
if (s.id !== 0) {
|
|
1074
1162
|
this.closedAt = http2Remember(this.closed, this.closedAt, s.id)
|
|
1075
1163
|
s.id = 0
|
|
1076
|
-
this.active = this.active - 1
|
|
1164
|
+
this.active = toI32(toI64(this.active) - toI64(1))
|
|
1077
1165
|
}
|
|
1078
1166
|
}
|
|
1079
1167
|
|
|
@@ -1089,7 +1177,7 @@ export class Http2Connection {
|
|
|
1089
1177
|
private localEnd(s: Http2Stream): void {
|
|
1090
1178
|
s.localEnded = true
|
|
1091
1179
|
if (this.resetCredit < this.config.resetBudget) {
|
|
1092
|
-
this.resetCredit = this.resetCredit + 1
|
|
1180
|
+
this.resetCredit = toI32(toI64(this.resetCredit) + toI64(1))
|
|
1093
1181
|
}
|
|
1094
1182
|
if (s.remoteEnded) {
|
|
1095
1183
|
this.release(s)
|
|
@@ -1134,7 +1222,9 @@ export class Http2Connection {
|
|
|
1134
1222
|
* once the budget is spent the connection error ENHANCE_YOUR_CALM (§10.5).
|
|
1135
1223
|
*/
|
|
1136
1224
|
private chargeReset(): i32 {
|
|
1137
|
-
|
|
1225
|
+
// The credit is at most `resetBudget` and falls by one per reset before the connection fails, so the
|
|
1226
|
+
// i64 count fits back in an i32.
|
|
1227
|
+
this.resetCredit = toI32(toI64(this.resetCredit) - toI64(1))
|
|
1138
1228
|
return this.resetCredit < 0 ? this.fail(H2_ENHANCE_YOUR_CALM) : H2_NEED_MORE
|
|
1139
1229
|
}
|
|
1140
1230
|
|
|
@@ -1198,24 +1288,67 @@ export class Http2Connection {
|
|
|
1198
1288
|
* window is owed.
|
|
1199
1289
|
*/
|
|
1200
1290
|
private credit(s: Http2Stream | null, n: i32): void {
|
|
1201
|
-
this.recvUnacked = this.recvUnacked
|
|
1291
|
+
this.recvUnacked = http2Plus(this.recvUnacked, toI64(n))
|
|
1202
1292
|
const half: i64 = toI64(this.config.connectionWindowSize / 2)
|
|
1203
1293
|
if (this.recvUnacked > toI64(0) && this.recvUnacked >= half) {
|
|
1204
1294
|
const inc: i32 = toI32(this.recvUnacked)
|
|
1205
1295
|
this.wrote(http2WriteWindowUpdate(this.output, this.outputEnd, H2_ZERO, inc))
|
|
1206
|
-
this.recvWindow = this.recvWindow
|
|
1296
|
+
this.recvWindow = http2Plus(this.recvWindow, this.recvUnacked)
|
|
1207
1297
|
this.recvUnacked = 0
|
|
1208
1298
|
}
|
|
1209
|
-
if (s !== null
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1299
|
+
if (s !== null) {
|
|
1300
|
+
this.creditStream(s, n)
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
/**
|
|
1305
|
+
* Hands `n` bytes back to `s`'s window when it is still open for
|
|
1306
|
+
* receiving, with a WINDOW_UPDATE once half a window is owed. Answers
|
|
1307
|
+
* false when that frame did not fit, and the bytes stay owed.
|
|
1308
|
+
*/
|
|
1309
|
+
private creditStream(s: Http2Stream, n: i32): boolean {
|
|
1310
|
+
if (s.remoteEnded) {
|
|
1311
|
+
return true
|
|
1312
|
+
}
|
|
1313
|
+
// Below half a window owed plus one frame's bytes: the i64 sum fits back in an i32.
|
|
1314
|
+
s.recvUnacked = toI32(toI64(s.recvUnacked) + toI64(n))
|
|
1315
|
+
if (s.recvUnacked > 0 && s.recvUnacked >= this.config.initialWindowSize / 2) {
|
|
1316
|
+
if (!this.wrote(http2WriteWindowUpdate(this.output, this.outputEnd, s.id, s.recvUnacked))) {
|
|
1317
|
+
this.owing = true
|
|
1318
|
+
return false
|
|
1319
|
+
}
|
|
1320
|
+
s.recvWindow = http2Plus(s.recvWindow, toI64(s.recvUnacked))
|
|
1321
|
+
s.recvUnacked = 0
|
|
1322
|
+
}
|
|
1323
|
+
return true
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
/** Sends the WINDOW_UPDATEs `consumeBody` owed when the output had no room for them. */
|
|
1327
|
+
private settleOwed(): void {
|
|
1328
|
+
this.owing = false
|
|
1329
|
+
for (const s of this.streams) {
|
|
1330
|
+
if (s.id !== 0 && s.held) {
|
|
1331
|
+
this.creditStream(s, toI32(0))
|
|
1215
1332
|
}
|
|
1216
1333
|
}
|
|
1217
1334
|
}
|
|
1218
1335
|
|
|
1336
|
+
/**
|
|
1337
|
+
* Sends the WINDOW_UPDATEs `consumeBody` owed when the output had no room
|
|
1338
|
+
* for them, for a caller that has just drained the output and may call
|
|
1339
|
+
* `next` only once the peer sends more, which a peer at a closed window
|
|
1340
|
+
* never does.
|
|
1341
|
+
*
|
|
1342
|
+
* @returns whether any were owed, so the output may have grown
|
|
1343
|
+
*/
|
|
1344
|
+
settleCredit(): boolean {
|
|
1345
|
+
if (!this.owing || this.state === H2_STATE_FAILED || this.room() < H2_CONTROL_RESERVE) {
|
|
1346
|
+
return false
|
|
1347
|
+
}
|
|
1348
|
+
this.settleOwed()
|
|
1349
|
+
return true
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1219
1352
|
// ---- Reading ------------------------------------------------------------------
|
|
1220
1353
|
|
|
1221
1354
|
/**
|
|
@@ -1234,7 +1367,13 @@ export class Http2Connection {
|
|
|
1234
1367
|
if (this.room() < H2_CONTROL_RESERVE) {
|
|
1235
1368
|
return H2_NEED_MORE
|
|
1236
1369
|
}
|
|
1237
|
-
|
|
1370
|
+
if (this.owing) {
|
|
1371
|
+
this.settleOwed()
|
|
1372
|
+
}
|
|
1373
|
+
const start: i32 = this.inputStart
|
|
1374
|
+
const end: i32 = this.inputEnd
|
|
1375
|
+
// The connection keeps `0 <= inputStart <= inputEnd`.
|
|
1376
|
+
const available: i32 = start >= 0 && end >= 0 ? end - start : 0
|
|
1238
1377
|
if (this.state === H2_STATE_PREFACE) {
|
|
1239
1378
|
if (available < H2_PREFACE_SIZE) {
|
|
1240
1379
|
return H2_NEED_MORE
|
|
@@ -1242,7 +1381,8 @@ export class Http2Connection {
|
|
|
1242
1381
|
if (!http2IsPreface(this.input, this.inputStart)) {
|
|
1243
1382
|
return this.fail(H2_PROTOCOL_ERROR)
|
|
1244
1383
|
}
|
|
1245
|
-
|
|
1384
|
+
// The preface lies inside `[inputStart, inputEnd)`, so the i64 sum fits back in an i32.
|
|
1385
|
+
this.inputStart = toI32(toI64(start) + toI64(H2_PREFACE_SIZE))
|
|
1246
1386
|
this.state = H2_STATE_SETTINGS
|
|
1247
1387
|
continue
|
|
1248
1388
|
}
|
|
@@ -1253,11 +1393,13 @@ export class Http2Connection {
|
|
|
1253
1393
|
if (f.length > this.config.maxFrameSize) {
|
|
1254
1394
|
return this.fail(H2_FRAME_SIZE_ERROR)
|
|
1255
1395
|
}
|
|
1256
|
-
|
|
1396
|
+
const frameSize: i64 = toI64(H2_FRAME_HEADER_SIZE) + toI64(f.length)
|
|
1397
|
+
if (toI64(available) < frameSize) {
|
|
1257
1398
|
return H2_NEED_MORE
|
|
1258
1399
|
}
|
|
1259
1400
|
const error: i32 = http2ParseFrame(f, this.input, this.inputStart, this.config.maxFrameSize)
|
|
1260
|
-
|
|
1401
|
+
// The frame lies inside `[inputStart, inputEnd)`, so the i64 sum fits back in an i32.
|
|
1402
|
+
this.inputStart = toI32(toI64(start) + toI64(toI32(frameSize)))
|
|
1261
1403
|
if (error !== H2_NO_ERROR) {
|
|
1262
1404
|
return this.fail(error)
|
|
1263
1405
|
}
|
|
@@ -1338,13 +1480,14 @@ export class Http2Connection {
|
|
|
1338
1480
|
if (toI64(size) > this.recvWindow) {
|
|
1339
1481
|
return this.fail(H2_FLOW_CONTROL_ERROR)
|
|
1340
1482
|
}
|
|
1341
|
-
this.recvWindow = this.recvWindow
|
|
1483
|
+
this.recvWindow = http2Minus(this.recvWindow, toI64(size))
|
|
1342
1484
|
if (s === null) {
|
|
1343
1485
|
this.credit(null, size)
|
|
1344
1486
|
return this.discarded(id) ? H2_NEED_MORE : this.refuseStream(id, H2_STREAM_CLOSED)
|
|
1345
1487
|
}
|
|
1346
1488
|
const end: boolean = f.has(H2_FLAG_END_STREAM)
|
|
1347
|
-
|
|
1489
|
+
// A count of body bytes: 2^63 of them cannot arrive, so the sum modulo 2^64 is the exact one.
|
|
1490
|
+
const received: i64 = toI64(toU64(s.received) + toU64(toI64(f.contentLength)))
|
|
1348
1491
|
const declared: boolean = s.contentLength >= toI64(0)
|
|
1349
1492
|
let error: i32 = H2_NO_ERROR
|
|
1350
1493
|
if (s.remoteEnded) {
|
|
@@ -1358,9 +1501,19 @@ export class Http2Connection {
|
|
|
1358
1501
|
this.credit(null, size)
|
|
1359
1502
|
return this.streamError(s, error)
|
|
1360
1503
|
}
|
|
1361
|
-
s.recvWindow = s.recvWindow
|
|
1504
|
+
s.recvWindow = http2Minus(s.recvWindow, toI64(size))
|
|
1362
1505
|
s.received = received
|
|
1363
|
-
|
|
1506
|
+
if (s.held && !end) {
|
|
1507
|
+
// The connection takes the whole frame back on arrival, as for every
|
|
1508
|
+
// stream; the stream only its padding, its body waiting for `consumeBody`.
|
|
1509
|
+
this.credit(null, size)
|
|
1510
|
+
// The body lies inside the frame, so both i64 figures fit back in an i32: the padding, and what the
|
|
1511
|
+
// stream owes, which its window bounds.
|
|
1512
|
+
this.creditStream(s, toI32(toI64(size) - toI64(f.contentLength)))
|
|
1513
|
+
s.owed = toI32(toI64(s.owed) + toI64(f.contentLength))
|
|
1514
|
+
} else {
|
|
1515
|
+
this.credit(end ? null : s, size)
|
|
1516
|
+
}
|
|
1364
1517
|
this.stream = id
|
|
1365
1518
|
this.endStream = end
|
|
1366
1519
|
this.data = this.input
|
|
@@ -1377,18 +1530,22 @@ export class Http2Connection {
|
|
|
1377
1530
|
/** Appends a fragment of a header block; a block past `maxHeaderBlock` or `H2_MAX_FRAGMENTS` ends the connection. */
|
|
1378
1531
|
private gather(f: Http2Frame): boolean {
|
|
1379
1532
|
this.holdBlock()
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1533
|
+
// Past H2_MAX_FRAGMENTS the connection fails just below, so the i64 count fits back in an i32.
|
|
1534
|
+
this.blockFragments = toI32(toI64(this.blockFragments) + toI64(1))
|
|
1535
|
+
const size: i32 = toI32(this.block.length)
|
|
1536
|
+
const start: i32 = this.blockLength
|
|
1537
|
+
const n: i32 = f.contentLength
|
|
1538
|
+
if (this.blockFragments > H2_MAX_FRAGMENTS || start < 0 || start > size || n > size - start) {
|
|
1385
1539
|
return false
|
|
1386
1540
|
}
|
|
1387
|
-
|
|
1388
|
-
|
|
1541
|
+
// The fragment fits after `blockLength` by the test above, and the frame's content lies inside the input.
|
|
1542
|
+
if (!tryCopyBytes(this.block, start, this.input, f.contentStart, n)) {
|
|
1543
|
+
return false
|
|
1389
1544
|
}
|
|
1390
|
-
|
|
1391
|
-
|
|
1545
|
+
// `start + n <= size` by the test above, so the sum stays an i32.
|
|
1546
|
+
const to: i32 = toI32(toI64(start) + toI64(n))
|
|
1547
|
+
this.blockLength = to
|
|
1548
|
+
this.blockUsed = to > this.blockUsed ? to : this.blockUsed
|
|
1392
1549
|
return true
|
|
1393
1550
|
}
|
|
1394
1551
|
|
|
@@ -1466,7 +1623,10 @@ export class Http2Connection {
|
|
|
1466
1623
|
if (shape !== HTTP_FIELDS_OK || (this.blockEndStream && this.fields.contentLength > toI64(0))) {
|
|
1467
1624
|
return this.refuseStream(id, H2_PROTOCOL_ERROR)
|
|
1468
1625
|
}
|
|
1469
|
-
const opened: Http2Stream = this.open(id)
|
|
1626
|
+
const opened: Http2Stream | null = this.open(id)
|
|
1627
|
+
if (opened === null) {
|
|
1628
|
+
return this.refuseStream(id, H2_REFUSED_STREAM)
|
|
1629
|
+
}
|
|
1470
1630
|
opened.contentLength = this.fields.contentLength
|
|
1471
1631
|
this.stream = id
|
|
1472
1632
|
this.endStream = this.blockEndStream
|
|
@@ -1535,11 +1695,13 @@ export class Http2Connection {
|
|
|
1535
1695
|
while (toI32(this.encoded.length) > 0) {
|
|
1536
1696
|
this.encoded.pop()
|
|
1537
1697
|
}
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1698
|
+
// `statusValue` is made with three digits and never resized.
|
|
1699
|
+
const digits: u8[] = this.statusValue
|
|
1700
|
+
if (status > 0 && toI32(digits.length) >= 3) {
|
|
1701
|
+
digits[0] = toU8(status / 100 + H2_DIGIT_ZERO)
|
|
1702
|
+
digits[1] = toU8(((status / 10) % 10) + H2_DIGIT_ZERO)
|
|
1703
|
+
digits[2] = toU8((status % 10) + H2_DIGIT_ZERO)
|
|
1704
|
+
this.encoder.encodeField(this.encoded, this.statusName, digits, HPACK_INDEX_WITHOUT, false)
|
|
1543
1705
|
}
|
|
1544
1706
|
if (withDate && status >= 200 && toI32(this.date.length) > 0) {
|
|
1545
1707
|
const huffman: boolean =
|
|
@@ -1592,11 +1754,11 @@ export class Http2Connection {
|
|
|
1592
1754
|
if (!this.settingsAcked) {
|
|
1593
1755
|
this.settingsAcked = true
|
|
1594
1756
|
this.decoder.setSettingsLimit(this.config.headerTableSize)
|
|
1595
|
-
const delta: i64 = toI64(this.config.initialWindowSize)
|
|
1757
|
+
const delta: i64 = http2Minus(toI64(this.config.initialWindowSize), this.localInitialWindow)
|
|
1596
1758
|
this.localInitialWindow = toI64(this.config.initialWindowSize)
|
|
1597
1759
|
for (const s of this.streams) {
|
|
1598
1760
|
if (s.id !== 0) {
|
|
1599
|
-
s.recvWindow = s.recvWindow
|
|
1761
|
+
s.recvWindow = http2Plus(s.recvWindow, delta)
|
|
1600
1762
|
}
|
|
1601
1763
|
}
|
|
1602
1764
|
}
|
|
@@ -1641,11 +1803,11 @@ export class Http2Connection {
|
|
|
1641
1803
|
return this.fail(H2_FLOW_CONTROL_ERROR)
|
|
1642
1804
|
}
|
|
1643
1805
|
if (window !== this.peerInitialWindow) {
|
|
1644
|
-
const delta: i64 = window
|
|
1806
|
+
const delta: i64 = http2Minus(window, this.peerInitialWindow)
|
|
1645
1807
|
this.peerInitialWindow = window
|
|
1646
1808
|
for (const s of this.streams) {
|
|
1647
1809
|
if (s.id !== 0) {
|
|
1648
|
-
s.sendWindow = s.sendWindow
|
|
1810
|
+
s.sendWindow = http2Plus(s.sendWindow, delta)
|
|
1649
1811
|
if (delta > toI64(0) && s.blocked) {
|
|
1650
1812
|
s.blocked = false
|
|
1651
1813
|
opened = true
|
|
@@ -1669,9 +1831,9 @@ export class Http2Connection {
|
|
|
1669
1831
|
if (initial <= this.peerInitialWindow) {
|
|
1670
1832
|
return false
|
|
1671
1833
|
}
|
|
1672
|
-
const delta: i64 = initial
|
|
1834
|
+
const delta: i64 = http2Minus(initial, this.peerInitialWindow)
|
|
1673
1835
|
for (const s of this.streams) {
|
|
1674
|
-
if (s.id !== 0 && s.sendWindow
|
|
1836
|
+
if (s.id !== 0 && http2Plus(s.sendWindow, delta) > toI64(H2_MAX_WINDOW)) {
|
|
1675
1837
|
return true
|
|
1676
1838
|
}
|
|
1677
1839
|
}
|
|
@@ -1682,7 +1844,7 @@ export class Http2Connection {
|
|
|
1682
1844
|
private handleWindowUpdate(f: Http2Frame): i32 {
|
|
1683
1845
|
const inc: i64 = toI64(f.increment)
|
|
1684
1846
|
if (f.streamId === 0) {
|
|
1685
|
-
this.sendWindow = this.sendWindow
|
|
1847
|
+
this.sendWindow = http2Plus(this.sendWindow, inc)
|
|
1686
1848
|
if (this.sendWindow > toI64(H2_MAX_WINDOW)) {
|
|
1687
1849
|
return this.fail(H2_FLOW_CONTROL_ERROR)
|
|
1688
1850
|
}
|
|
@@ -1700,7 +1862,7 @@ export class Http2Connection {
|
|
|
1700
1862
|
if (f.streamError !== 0) {
|
|
1701
1863
|
return this.streamError(s, f.streamError)
|
|
1702
1864
|
}
|
|
1703
|
-
s.sendWindow = s.sendWindow
|
|
1865
|
+
s.sendWindow = http2Plus(s.sendWindow, inc)
|
|
1704
1866
|
if (s.sendWindow > toI64(H2_MAX_WINDOW)) {
|
|
1705
1867
|
return this.streamError(s, H2_FLOW_CONTROL_ERROR)
|
|
1706
1868
|
}
|
|
@@ -1739,20 +1901,27 @@ export class Http2Connection {
|
|
|
1739
1901
|
}
|
|
1740
1902
|
// A literal costs its strings and at most eleven bytes of prefixes; two
|
|
1741
1903
|
// table size updates and `:status` are inside the forty-eight on top.
|
|
1742
|
-
|
|
1904
|
+
// The bound is unsigned: it counts bytes the program holds, far below 2^64, so it cannot wrap.
|
|
1905
|
+
let bound: u64 = toU64(48)
|
|
1743
1906
|
for (let k: i32 = 0; k < toI32(names.length) && k < toI32(values.length); k++) {
|
|
1744
|
-
bound = bound + toI64(names[k].length) + toI64(values[k].length) +
|
|
1907
|
+
bound = bound + toU64(toI64(names[k].length)) + toU64(toI64(values[k].length)) + toU64(11)
|
|
1745
1908
|
}
|
|
1746
1909
|
// The server's `date` goes on a final response unless the program sent
|
|
1747
1910
|
// its own, so a response never carries two.
|
|
1748
1911
|
const withDate: boolean = status >= 200 && toI32(this.date.length) > 0 && !http2NamesHoldDate(names)
|
|
1749
1912
|
if (withDate) {
|
|
1750
|
-
bound = bound + toI64(this.dateName.length) + toI64(this.date.length) +
|
|
1913
|
+
bound = bound + toU64(toI64(this.dateName.length)) + toU64(toI64(this.date.length)) + toU64(11)
|
|
1914
|
+
}
|
|
1915
|
+
// SETTINGS_MAX_FRAME_SIZE is at least 16,384 from the default and every value the peer may send.
|
|
1916
|
+
const most: i32 = this.peerMaxFrameSize
|
|
1917
|
+
const divisor: u64 = toU64(toI64(most))
|
|
1918
|
+
if (most <= 0 || divisor === toU64(0)) {
|
|
1919
|
+
return H2_INVALID
|
|
1751
1920
|
}
|
|
1752
|
-
const frames:
|
|
1753
|
-
const need:
|
|
1754
|
-
if (need > toI64(this.bodyRoom())) {
|
|
1755
|
-
return toI64(this.output.length) < need +
|
|
1921
|
+
const frames: u64 = bound / divisor + toU64(1)
|
|
1922
|
+
const need: u64 = bound + frames * toU64(H2_FRAME_HEADER_SIZE)
|
|
1923
|
+
if (need > toU64(toI64(this.bodyRoom()))) {
|
|
1924
|
+
return toU64(toI64(this.output.length)) < need + toU64(H2_CONTROL_RESERVE) ? H2_INVALID : H2_AGAIN
|
|
1756
1925
|
}
|
|
1757
1926
|
this.encodeStatus(status, withDate)
|
|
1758
1927
|
for (let k: i32 = 0; k < toI32(names.length) && k < toI32(values.length); k++) {
|
|
@@ -1771,9 +1940,11 @@ export class Http2Connection {
|
|
|
1771
1940
|
let at: i32 = 0
|
|
1772
1941
|
let first: boolean = true
|
|
1773
1942
|
while (first || at < total) {
|
|
1774
|
-
|
|
1775
|
-
const
|
|
1776
|
-
const
|
|
1943
|
+
// `at` starts at 0 and moves by `chunk`, never past `total`.
|
|
1944
|
+
const left: i32 = at >= 0 && total >= 0 ? total - at : 0
|
|
1945
|
+
const chunk: i32 = left < most ? left : most
|
|
1946
|
+
const end: i32 = toI32(toI64(at) + toI64(chunk))
|
|
1947
|
+
const last: boolean = end >= total
|
|
1777
1948
|
const flags: i32 =
|
|
1778
1949
|
(last ? H2_FLAG_END_HEADERS : H2_ZERO) | (first && endStream ? H2_FLAG_END_STREAM : H2_ZERO)
|
|
1779
1950
|
this.wrote(
|
|
@@ -1781,7 +1952,7 @@ export class Http2Connection {
|
|
|
1781
1952
|
? http2WriteHeaders(this.output, this.outputEnd, s.id, this.encoded, at, chunk, flags, H2_NO_PAD)
|
|
1782
1953
|
: http2WriteContinuation(this.output, this.outputEnd, s.id, this.encoded, at, chunk, flags)
|
|
1783
1954
|
)
|
|
1784
|
-
at =
|
|
1955
|
+
at = end
|
|
1785
1956
|
first = false
|
|
1786
1957
|
}
|
|
1787
1958
|
if (endStream) {
|
|
@@ -1878,8 +2049,9 @@ export class Http2Connection {
|
|
|
1878
2049
|
}
|
|
1879
2050
|
let sent: i32 = 0
|
|
1880
2051
|
while (sent < len) {
|
|
1881
|
-
const
|
|
1882
|
-
|
|
2052
|
+
const body: i32 = this.bodyRoom()
|
|
2053
|
+
const room: i32 = body >= 0 && body > H2_FRAME_HEADER_SIZE ? body - H2_FRAME_HEADER_SIZE : 0
|
|
2054
|
+
let chunk: i64 = Math.min(toI64(len) - toI64(sent), toI64(this.peerMaxFrameSize))
|
|
1883
2055
|
chunk = Math.min(chunk, Math.min(s.sendWindow, this.sendWindow))
|
|
1884
2056
|
chunk = Math.min(chunk, toI64(room))
|
|
1885
2057
|
if (chunk <= toI64(0)) {
|
|
@@ -1892,22 +2064,24 @@ export class Http2Connection {
|
|
|
1892
2064
|
break
|
|
1893
2065
|
}
|
|
1894
2066
|
const n: i32 = toI32(chunk)
|
|
1895
|
-
|
|
2067
|
+
// `chunk` is at most `len - sent`, so each i64 sum below is inside `buf`'s window and fits an i32.
|
|
2068
|
+
const after: i32 = toI32(toI64(sent) + toI64(n))
|
|
2069
|
+
const last: boolean = endStream && after === len
|
|
1896
2070
|
this.wrote(
|
|
1897
2071
|
http2WriteData(
|
|
1898
2072
|
this.output,
|
|
1899
2073
|
this.outputEnd,
|
|
1900
2074
|
id,
|
|
1901
2075
|
buf,
|
|
1902
|
-
off + sent,
|
|
2076
|
+
toI32(toI64(off) + toI64(sent)),
|
|
1903
2077
|
n,
|
|
1904
2078
|
last ? H2_FLAG_END_STREAM : 0,
|
|
1905
2079
|
H2_NO_PAD
|
|
1906
2080
|
)
|
|
1907
2081
|
)
|
|
1908
|
-
s.sendWindow = s.sendWindow
|
|
1909
|
-
this.sendWindow = this.sendWindow
|
|
1910
|
-
sent =
|
|
2082
|
+
s.sendWindow = http2Minus(s.sendWindow, chunk)
|
|
2083
|
+
this.sendWindow = http2Minus(this.sendWindow, chunk)
|
|
2084
|
+
sent = after
|
|
1911
2085
|
}
|
|
1912
2086
|
if (len === 0 && endStream) {
|
|
1913
2087
|
if (this.bodyRoom() < H2_FRAME_HEADER_SIZE) {
|
|
@@ -1923,6 +2097,58 @@ export class Http2Connection {
|
|
|
1923
2097
|
return sent === 0 && len > 0 ? H2_AGAIN : sent
|
|
1924
2098
|
}
|
|
1925
2099
|
|
|
2100
|
+
/**
|
|
2101
|
+
* The window each stream this side opens advertises:
|
|
2102
|
+
* `max(initialWindowSize, 65535)`, and so the most body a held stream can
|
|
2103
|
+
* have unconsumed.
|
|
2104
|
+
* @returns the stream receive window, in bytes
|
|
2105
|
+
*/
|
|
2106
|
+
streamWindow(): i32 {
|
|
2107
|
+
return toI32(this.localInitialWindow)
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
/**
|
|
2111
|
+
* Holds back stream `id`'s window: from now on its DATA credits the
|
|
2112
|
+
* connection's window on arrival, as every stream's does, but the
|
|
2113
|
+
* stream's own only when `consumeBody` says the program read the bytes. A
|
|
2114
|
+
* peer that sends past the window gets RST_STREAM FLOW_CONTROL_ERROR, so
|
|
2115
|
+
* at most the stream's initial window is ever unconsumed.
|
|
2116
|
+
*
|
|
2117
|
+
* @param id the stream identifier
|
|
2118
|
+
* @returns true, or false when no open stream has that identifier
|
|
2119
|
+
*/
|
|
2120
|
+
hold(id: i32): boolean {
|
|
2121
|
+
const s: Http2Stream | null = this.find(id)
|
|
2122
|
+
if (s === null) {
|
|
2123
|
+
return false
|
|
2124
|
+
}
|
|
2125
|
+
s.held = true
|
|
2126
|
+
return true
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
/**
|
|
2130
|
+
* Credits `n` bytes of held stream `id`'s body back to its window, with a
|
|
2131
|
+
* WINDOW_UPDATE once half a window is owed. `n` is cut to what the stream
|
|
2132
|
+
* owes, so a stream gets back no more than it was sent; nothing happens
|
|
2133
|
+
* once the stream is closed, not held, or ended by the peer.
|
|
2134
|
+
*
|
|
2135
|
+
* @param id the stream identifier, held by `hold`
|
|
2136
|
+
* @param n how many of its body bytes the program consumed
|
|
2137
|
+
* @returns 0, or `H2_AGAIN` when the output has no room for the WINDOW_UPDATE yet; the connection sends it
|
|
2138
|
+
* from a later `next`
|
|
2139
|
+
*/
|
|
2140
|
+
consumeBody(id: i32, n: i32): i32 {
|
|
2141
|
+
const s: Http2Stream | null = this.find(id)
|
|
2142
|
+
if (this.state === H2_STATE_FAILED || s === null || !s.held || n <= 0) {
|
|
2143
|
+
return 0
|
|
2144
|
+
}
|
|
2145
|
+
const owed: i32 = s.owed
|
|
2146
|
+
const k: i32 = n < owed ? n : owed
|
|
2147
|
+
s.owed = toI32(toI64(owed) - toI64(k))
|
|
2148
|
+
this.room()
|
|
2149
|
+
return this.creditStream(s, k) ? 0 : H2_AGAIN
|
|
2150
|
+
}
|
|
2151
|
+
|
|
1926
2152
|
/**
|
|
1927
2153
|
* Resets stream `id` with `code` (§6.4), from the program's side: CANCEL,
|
|
1928
2154
|
* say. Answers 0, `H2_AGAIN` when the output has no room for the frame yet
|