h2lean 0.18.0 → 0.19.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.
|
@@ -125,6 +125,7 @@
|
|
|
125
125
|
'use strict';
|
|
126
126
|
|
|
127
127
|
const { EventEmitter } = require('node:events');
|
|
128
|
+
const net = require('node:net');
|
|
128
129
|
const tls = require('node:tls');
|
|
129
130
|
const f = require('../frames');
|
|
130
131
|
const { Decoder, Encoder, HPackError } = require('../hpack');
|
|
@@ -162,6 +163,9 @@ const {
|
|
|
162
163
|
|
|
163
164
|
const MAX_SETTINGS_ENTRIES = 32; // CVE-2020-11080; same bound as the server
|
|
164
165
|
const MAX_CONTINUATIONS = 8; // CVE-2024-28182; same bound as the server
|
|
166
|
+
// Cap on the pool of completed staging buffers, so what one client retains for reuse is
|
|
167
|
+
// bounded independently of how many batches it sends. Same number as the server's.
|
|
168
|
+
const MAX_STAGE_POOL = 32;
|
|
165
169
|
// nghttp2 refuses a SETTINGS submit once ten frames are awaiting an ACK, and node
|
|
166
170
|
// turns that refusal into a destroyed session (core.js submitSettings: a false return
|
|
167
171
|
// from handle.settings() -> destroy(ERR_HTTP2_MAX_PENDING_SETTINGS_ACK)). Measured on
|
|
@@ -428,6 +432,25 @@ class Http2ClientSession extends EventEmitter {
|
|
|
428
432
|
this._stage = null;
|
|
429
433
|
this._stageLen = 0; // bytes of _stage not yet queued
|
|
430
434
|
this._stageCap = STAGE_MIN; // size of the next staging buffer
|
|
435
|
+
this._reuseBuffers = opts.reuseBuffers !== false;
|
|
436
|
+
// A pool is per connection here: connect() has no server object to own a shared one,
|
|
437
|
+
// so a caller that wants one across sessions passes it as `stagingPool`. Default on;
|
|
438
|
+
// `false` keeps the allocate-per-batch behaviour (`_pool === null`).
|
|
439
|
+
this._pool = this._reuseBuffers
|
|
440
|
+
? (opts.stagingPool === undefined ? [] : opts.stagingPool) : null;
|
|
441
|
+
// _pending collects the full stage buffers of a batch; _returning is the batch whose
|
|
442
|
+
// write callback is still owed, so only one list is in flight at a time (server's shape).
|
|
443
|
+
this._pending = [];
|
|
444
|
+
this._returning = null;
|
|
445
|
+
// A net/TLS socket copies (or encrypts) the bytes at flush, so its write callback is
|
|
446
|
+
// a real ownership boundary and a completed stage can be written into again. A
|
|
447
|
+
// generic stream (stream.duplexPair, a custom createConnection) can push the very
|
|
448
|
+
// buffer we hand it to its peer and hold it until the peer reads -- the callback then
|
|
449
|
+
// fires while the peer is still parsing, and a reused stage would rewrite bytes
|
|
450
|
+
// already promised. Measured on the paused-duplexPair case in
|
|
451
|
+
// test/server-handshake.test.js: GOAWAY(COMPRESSION_ERROR). Those sockets keep the
|
|
452
|
+
// old allocate-per-batch path; TLSSocket is a net.Socket, so one check covers both.
|
|
453
|
+
this._canPool = this._pool !== null && socket instanceof net.Socket;
|
|
431
454
|
this._outLen = 0; // bytes the socket has not been given
|
|
432
455
|
this._writing = false; // one batch in flight per connection
|
|
433
456
|
this._flushScheduled = false;
|
|
@@ -666,7 +689,9 @@ class Http2ClientSession extends EventEmitter {
|
|
|
666
689
|
cap *= 2;
|
|
667
690
|
}
|
|
668
691
|
this._stageCap = Math.min(cap, MAX_OUT_BATCH);
|
|
669
|
-
this.
|
|
692
|
+
const reused = !this._canPool ? undefined : this._pool.pop();
|
|
693
|
+
this._stage = reused !== undefined && reused.length >= cap
|
|
694
|
+
? reused : Buffer.allocUnsafe(cap);
|
|
670
695
|
}
|
|
671
696
|
|
|
672
697
|
_commit() {
|
|
@@ -674,6 +699,7 @@ class Http2ClientSession extends EventEmitter {
|
|
|
674
699
|
return;
|
|
675
700
|
}
|
|
676
701
|
this._outQ.push(this._stage.subarray(0, this._stageLen));
|
|
702
|
+
this._pending.push(this._stage); // keep the whole buffer, for reuse after the write
|
|
677
703
|
this._stage = null; // the queued bytes are not ours to write
|
|
678
704
|
this._stageLen = 0;
|
|
679
705
|
}
|
|
@@ -726,6 +752,9 @@ class Http2ClientSession extends EventEmitter {
|
|
|
726
752
|
this._commit();
|
|
727
753
|
const batch = this._outQ;
|
|
728
754
|
this._outQ = [];
|
|
755
|
+
const buffers = this._pending;
|
|
756
|
+
this._pending = [];
|
|
757
|
+
this._returning = buffers;
|
|
729
758
|
this._outLen = 0;
|
|
730
759
|
const s = this._socket;
|
|
731
760
|
if (batch.length === 1) {
|
|
@@ -747,6 +776,19 @@ class Http2ClientSession extends EventEmitter {
|
|
|
747
776
|
|
|
748
777
|
_onWritten() {
|
|
749
778
|
this._writing = false;
|
|
779
|
+
// The callback is the ownership boundary: every buffer of the batch is the socket's
|
|
780
|
+
// no longer, so _need can write into one of them again. Cap what we keep so a quiet
|
|
781
|
+
// connection cannot grow the pool without bound.
|
|
782
|
+
const done = this._returning;
|
|
783
|
+
this._returning = null;
|
|
784
|
+
if (done !== null && this._canPool) {
|
|
785
|
+
for (const b of done) {
|
|
786
|
+
if (this._pool.length >= MAX_STAGE_POOL) {
|
|
787
|
+
break;
|
|
788
|
+
}
|
|
789
|
+
this._pool.push(b);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
750
792
|
// Dropping the batch is the whole release: the frames were never assembled into
|
|
751
793
|
// anything shared, so there is no tail to move down and nothing to reset.
|
|
752
794
|
this._writePending();
|
|
@@ -759,9 +801,16 @@ class Http2ClientSession extends EventEmitter {
|
|
|
759
801
|
}
|
|
760
802
|
|
|
761
803
|
_discardOut() {
|
|
804
|
+
// The uncommitted stage never reached the socket, so it is ours to hand back; the
|
|
805
|
+
// committed ones in _pending/_returning may be in flight and are dropped, not reused.
|
|
806
|
+
if (this._stage !== null && this._canPool && this._pool.length < MAX_STAGE_POOL) {
|
|
807
|
+
this._pool.push(this._stage);
|
|
808
|
+
}
|
|
762
809
|
this._outQ = [];
|
|
763
810
|
this._stage = null;
|
|
764
811
|
this._stageLen = 0;
|
|
812
|
+
this._pending = [];
|
|
813
|
+
this._returning = null;
|
|
765
814
|
this._outLen = 0;
|
|
766
815
|
}
|
|
767
816
|
|
|
@@ -2999,6 +3048,11 @@ class Http2ClientSession extends EventEmitter {
|
|
|
2999
3048
|
const batch = this._outQ;
|
|
3000
3049
|
this._outQ = [];
|
|
3001
3050
|
this._outLen = 0;
|
|
3051
|
+
// These buffers are handed to the socket with NO callback and a terminal close
|
|
3052
|
+
// follows, so the socket may still be reading them after this returns. Never put
|
|
3053
|
+
// them back in the pool: drop both lists so the dying session retains nothing.
|
|
3054
|
+
this._pending = [];
|
|
3055
|
+
this._returning = null;
|
|
3002
3056
|
// _commit drops the staging buffer only when it held bytes; _discardOut dropped it
|
|
3003
3057
|
// either way, and so does this.
|
|
3004
3058
|
this._stage = null;
|
package/lib/headers.js
CHANGED
|
@@ -670,9 +670,12 @@ let hitPlan = null;
|
|
|
670
670
|
// OPTIMIZATION.md item 68.
|
|
671
671
|
function prepare(headers, opts, session) {
|
|
672
672
|
const sendDate = !!(opts && opts.sendDate);
|
|
673
|
+
const request = !!(opts && opts.request);
|
|
673
674
|
const enc = session._encoder;
|
|
674
675
|
const stamp = sendDate ? touchDate() : -1;
|
|
675
|
-
|
|
676
|
+
// A PUSH_PROMISE is one-shot: node never caches its block, and the request
|
|
677
|
+
// pseudo-headers would otherwise share a cache key with a response's map.
|
|
678
|
+
const cacheable = Object.isFrozen(headers) && enc.pendingSize === null && !request;
|
|
676
679
|
if (cacheable) {
|
|
677
680
|
const hit = session._blocks.get(headers);
|
|
678
681
|
if (hit !== undefined && hit.epoch === enc.epoch && hit.stamp === stamp) {
|
|
@@ -690,7 +693,7 @@ function prepare(headers, opts, session) {
|
|
|
690
693
|
}
|
|
691
694
|
const parsed = parse(headers,
|
|
692
695
|
{ trailer: !!(opts && opts.trailer), sendDate, info: !!(opts && opts.info),
|
|
693
|
-
strict: session._strictSingle });
|
|
696
|
+
request, strict: session._strictSingle });
|
|
694
697
|
sent.status = parsed.status;
|
|
695
698
|
sent.date = parsed.genDate;
|
|
696
699
|
// The size update is NOT consumed here: writePrepared() writes its octets into the
|
|
@@ -1841,78 +1841,55 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1841
1841
|
}
|
|
1842
1842
|
|
|
1843
1843
|
// Returns true when the block was refused, so the caller knows the wire state its
|
|
1844
|
-
//
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
// END_STREAM flag". Measured on node's wire for a trailer block over one frame:
|
|
1867
|
-
// HEADERS flags 0x1 len 16384, CONTINUATION flags 0x4. On the last CONTINUATION
|
|
1868
|
-
// instead, node's client answers ERR_HTTP2_STREAM_ERROR / rstCode 1 and h2lean's own
|
|
1869
|
-
// receiver -- which reads the flag off HEADERS -- never sees the message end.
|
|
1870
|
-
const es = end ? F_END_STREAM : 0;
|
|
1871
|
-
if (block.length <= max) {
|
|
1872
|
-
this._frame(HEADERS, F_END_HEADERS | es, sid, block);
|
|
1873
|
-
return false;
|
|
1874
|
-
}
|
|
1875
|
-
this._frame(HEADERS, es, sid, block.subarray(0, max));
|
|
1876
|
-
let off = max;
|
|
1877
|
-
for (;;) {
|
|
1878
|
-
const n = Math.min(block.length - off, max);
|
|
1879
|
-
const last = off + n >= block.length;
|
|
1880
|
-
this._frame(CONTINUATION, last ? F_END_HEADERS : 0,
|
|
1881
|
-
sid, block.subarray(off, off + n));
|
|
1882
|
-
off += n;
|
|
1883
|
-
if (last) {
|
|
1884
|
-
return false;
|
|
1844
|
+
// The single-scratch form of every outbound header block: prepare() reads the
|
|
1845
|
+
// caller's map (getters and all) BEFORE any room is reserved, then writePrepared()
|
|
1846
|
+
// writes the HPACK octets straight into this session's staging buffer. No module-level
|
|
1847
|
+
// scratch view, no subarray and no memcpy; the 9-octet frame headers are inserted in
|
|
1848
|
+
// place, from the last CONTINUATION backwards. END_STREAM rides on the first frame
|
|
1849
|
+
// even when CONTINUATIONs follow it (RFC 9113 6.2/5.1/8.1, measured on node's wire).
|
|
1850
|
+
// `prefixSid` adds PUSH_PROMISE's four-octet promised-id prefix; `priority` adds the
|
|
1851
|
+
// allowance node reserves on HEADERS only. `prep` lets a caller that already parsed
|
|
1852
|
+
// (respondWithFD, to keep its error handling around the parse) skip a second pass.
|
|
1853
|
+
// An over-estimate is refused at prep time, not thrown (nghttp2_session.c,
|
|
1854
|
+
// session_prep_frame): node emits 'frameError' and closes a turn later. Measured for
|
|
1855
|
+
// an oversize trailer on v26.12.1: frameError(1, 6, sid), rstCode 6, RST_STREAM(6)
|
|
1856
|
+
// then GOAWAY(0). OPTIMIZATION.md item 68.
|
|
1857
|
+
_writeBlockAt(sid, headers, opts, type, end, prefixSid, priority, prep) {
|
|
1858
|
+
if (prep === undefined) {
|
|
1859
|
+
prep = hdr.prepare(headers, opts, this);
|
|
1860
|
+
}
|
|
1861
|
+
if (prep.bound + (priority ? PRIORITY_SPECLEN : 0) > this._maxSendBlock) {
|
|
1862
|
+
if (type === PUSH_PROMISE) {
|
|
1863
|
+
this._refusePush(sid, prefixSid);
|
|
1864
|
+
} else {
|
|
1865
|
+
this._refuseBlock(sid, type);
|
|
1885
1866
|
}
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
|
|
1889
|
-
// Single-scratch form of sendHeaders: prepare() reads the caller's map (getters and
|
|
1890
|
-
// all) BEFORE any room is reserved, then writePrepared() writes HPACK octets straight
|
|
1891
|
-
// into this session's staging buffer. There is no module-level scratch view, no
|
|
1892
|
-
// subarray and no memcpy for the block; the 9-octet frame headers are inserted into
|
|
1893
|
-
// the reserved region in place, from the last CONTINUATION backwards.
|
|
1894
|
-
// OPTIMIZATION.md item 68.
|
|
1895
|
-
sendHeadersAt(sid, headers, opts, end) {
|
|
1896
|
-
const prep = hdr.prepare(headers, opts, this);
|
|
1897
|
-
if (prep.bound + PRIORITY_SPECLEN > this._maxSendBlock) {
|
|
1898
|
-
this._refuseBlock(sid, HEADERS);
|
|
1899
1867
|
return true;
|
|
1900
1868
|
}
|
|
1869
|
+
this.state.deflateDynamicTableSize = this._encoder.size;
|
|
1901
1870
|
const max = this.remoteSettings.maxFrameSize;
|
|
1871
|
+
const prefix = prefixSid === undefined ? 0 : 4;
|
|
1902
1872
|
const cap = prep.hit === undefined ? prep.cap : prep.hit.bytes.length;
|
|
1903
|
-
const frames = Math.max(1, Math.ceil(cap / max));
|
|
1904
|
-
this._need(HEADER_LEN + cap + HEADER_LEN * (frames - 1));
|
|
1873
|
+
const frames = Math.max(1, Math.ceil((prefix + cap) / max));
|
|
1874
|
+
this._need(HEADER_LEN + prefix + cap + HEADER_LEN * (frames - 1));
|
|
1905
1875
|
const dst = this._stage;
|
|
1906
1876
|
const off = this._stageLen;
|
|
1907
|
-
|
|
1877
|
+
if (prefix !== 0) {
|
|
1878
|
+
dst.writeUInt32BE(prefixSid, off + HEADER_LEN);
|
|
1879
|
+
}
|
|
1880
|
+
const payloadOff = off + HEADER_LEN + prefix;
|
|
1908
1881
|
const payloadEnd = hdr.writePrepared(prep, this, dst, payloadOff);
|
|
1909
|
-
const n = payloadEnd - payloadOff;
|
|
1882
|
+
const n = payloadEnd - payloadOff + prefix;
|
|
1910
1883
|
const used = Math.max(1, Math.ceil(n / max));
|
|
1911
1884
|
if (used === 1) {
|
|
1912
|
-
writeHeader(dst, off, n,
|
|
1885
|
+
writeHeader(dst, off, n, type, F_END_HEADERS | (end ? F_END_STREAM : 0), sid);
|
|
1913
1886
|
} else {
|
|
1887
|
+
// The first payload octet is the prefix, not the HPACK block: every later
|
|
1888
|
+
// frame starts max octets after the FIRST frame's payload, which is prefix
|
|
1889
|
+
// octets earlier than payloadOff.
|
|
1890
|
+
const bodyStart = payloadOff - prefix;
|
|
1914
1891
|
for (let i = used - 1; i >= 1; i--) {
|
|
1915
|
-
const src =
|
|
1892
|
+
const src = bodyStart + i * max;
|
|
1916
1893
|
const stop = Math.min(payloadEnd, src + max);
|
|
1917
1894
|
dst.copyWithin(src + i * HEADER_LEN, src, stop);
|
|
1918
1895
|
}
|
|
@@ -1921,7 +1898,7 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1921
1898
|
const flags = i === 0 ? (end ? F_END_STREAM : 0)
|
|
1922
1899
|
: (i === used - 1 ? F_END_HEADERS : 0);
|
|
1923
1900
|
writeHeader(dst, off + i * (HEADER_LEN + max), take,
|
|
1924
|
-
i === 0 ?
|
|
1901
|
+
i === 0 ? type : CONTINUATION, flags, sid);
|
|
1925
1902
|
}
|
|
1926
1903
|
}
|
|
1927
1904
|
const total = HEADER_LEN * used + n;
|
|
@@ -1933,6 +1910,10 @@ class Http2ServerSession extends EventEmitter {
|
|
|
1933
1910
|
return false;
|
|
1934
1911
|
}
|
|
1935
1912
|
|
|
1913
|
+
sendHeadersAt(sid, headers, opts, end) {
|
|
1914
|
+
return this._writeBlockAt(sid, headers, opts, HEADERS, end, undefined, true);
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1936
1917
|
// node's onFrameError (internal/http2/core.js): the emitter is the stream while the
|
|
1937
1918
|
// session still holds it and the session otherwise, 'frameError' goes out synchronously,
|
|
1938
1919
|
// and the close is deferred a turn -- node's own comment says the delay is there to let
|
|
@@ -2000,7 +1981,7 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2000
1981
|
// with the first frame carrying the 4-octet prefix and the block fragment that fits.
|
|
2001
1982
|
// Returns true when the block was refused, so pushStream can mark the promised stream
|
|
2002
1983
|
// before its callback runs (the same contract sendHeaders has with respond()).
|
|
2003
|
-
sendPushPromise(parentSid, promisedSid,
|
|
1984
|
+
sendPushPromise(parentSid, promisedSid, headers) {
|
|
2004
1985
|
// nghttp2's send cycle flushes control frames before DATA, so a PUSH_PROMISE
|
|
2005
1986
|
// opened in the same turn as the parent's end() rides ahead of the parent's
|
|
2006
1987
|
// END_STREAM on node's wire. An nghttp2 client refuses a push whose parent is
|
|
@@ -2043,47 +2024,19 @@ class Http2ServerSession extends EventEmitter {
|
|
|
2043
2024
|
this._esRequeue.push(esBytes !== null ? { bytes: esBytes } : { pair: esPair });
|
|
2044
2025
|
}
|
|
2045
2026
|
}
|
|
2046
|
-
// The
|
|
2047
|
-
// session_estimate_headers_payload passes 0
|
|
2048
|
-
//
|
|
2049
|
-
//
|
|
2027
|
+
// The prep-time estimate node uses has no priority allowance on PUSH_PROMISE
|
|
2028
|
+
// (session_estimate_headers_payload passes 0), so _writeBlockAt is called with
|
|
2029
|
+
// priority=false. Measured on v26.12.1 with a push of
|
|
2030
|
+
// {':path':'/pushed', ':authority':'a', 'x-big':'X'.repeat(n)} -- node fills
|
|
2031
|
+
// :method and :scheme, so five fields and an estimate of 121+n: under a bound of
|
|
2050
2032
|
// 150 n=29 is pushed and n=30 is refused, and under the default n=65415 / 65416
|
|
2051
|
-
// (test/outbound-bound.test.js). The refusal lands on the PARENT's
|
|
2052
|
-
//
|
|
2053
|
-
//
|
|
2054
|
-
//
|
|
2055
|
-
// the
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
return true;
|
|
2059
|
-
}
|
|
2060
|
-
this.state.deflateDynamicTableSize = this._encoder.size;
|
|
2061
|
-
const max = this.remoteSettings.maxFrameSize;
|
|
2062
|
-
// The payload is the 4-byte promised id + the block. The first frame can carry
|
|
2063
|
-
// max - 4 bytes of block; the rest goes into CONTINUATION frames.
|
|
2064
|
-
const firstCap = max - 4;
|
|
2065
|
-
if (block.length <= firstCap) {
|
|
2066
|
-
const payload = Buffer.allocUnsafe(4 + block.length);
|
|
2067
|
-
payload.writeUInt32BE(promisedSid, 0);
|
|
2068
|
-
f.copyInto(payload, 4, block, 0, block.length);
|
|
2069
|
-
this._frame(PUSH_PROMISE, F_END_HEADERS, parentSid, payload);
|
|
2070
|
-
return false;
|
|
2071
|
-
}
|
|
2072
|
-
const first = Buffer.allocUnsafe(4 + firstCap);
|
|
2073
|
-
first.writeUInt32BE(promisedSid, 0);
|
|
2074
|
-
f.copyInto(first, 4, block, 0, firstCap);
|
|
2075
|
-
this._frame(PUSH_PROMISE, 0, parentSid, first);
|
|
2076
|
-
let off = firstCap;
|
|
2077
|
-
for (;;) {
|
|
2078
|
-
const n = Math.min(block.length - off, max);
|
|
2079
|
-
const last = off + n >= block.length;
|
|
2080
|
-
this._frame(CONTINUATION, last ? F_END_HEADERS : 0, parentSid,
|
|
2081
|
-
block.subarray(off, off + n));
|
|
2082
|
-
off += n;
|
|
2083
|
-
if (last) {
|
|
2084
|
-
return false;
|
|
2085
|
-
}
|
|
2086
|
-
}
|
|
2033
|
+
// (test/outbound-bound.test.js). The refusal lands on the PARENT's 'frameError'
|
|
2034
|
+
// and closes the PROMISED stream with INTERNAL_ERROR while the parent's own answer
|
|
2035
|
+
// still goes out: nghttp2_session_close_stream(promised_sid, NGHTTP2_INTERNAL_ERROR).
|
|
2036
|
+
// The promised id is written as the four-octet prefix of the first frame; the rest
|
|
2037
|
+
// of the block is split into CONTINUATIONs by _writeBlockAt.
|
|
2038
|
+
return this._writeBlockAt(parentSid, headers, { request: true },
|
|
2039
|
+
PUSH_PROMISE, false, promisedSid, false);
|
|
2087
2040
|
}
|
|
2088
2041
|
|
|
2089
2042
|
// Remove a not-yet-committed END_STREAM frame that _frame staged for sid, the
|
|
@@ -3908,11 +3861,11 @@ class Http2ServerSession extends EventEmitter {
|
|
|
3908
3861
|
// all, the withheld END_STREAM rides on an empty DATA (lib/server/Http2ServerStream.js sendTrailers).
|
|
3909
3862
|
if (stream._pendingTrailers !== null) {
|
|
3910
3863
|
const trailers = stream._pendingTrailers;
|
|
3911
|
-
const bound = stream._pendingTrailersBound;
|
|
3912
3864
|
stream._pendingTrailers = null;
|
|
3913
3865
|
if (trailers.length === 0) {
|
|
3914
3866
|
this._frame(DATA, F_END_STREAM, stream.id, null);
|
|
3915
|
-
} else if (this.
|
|
3867
|
+
} else if (this._writeBlockAt(stream.id, trailers, { trailer: true },
|
|
3868
|
+
HEADERS, true, undefined, true)) {
|
|
3916
3869
|
// The bound is checked when the block is framed, not when sendTrailers() stashed
|
|
3917
3870
|
// it: nghttp2 compares the estimate at prep time, whenever that happens. Measured
|
|
3918
3871
|
// on v26.12.1 with a 200 000-octet body ahead of a 64 KiB trailer: frameError
|
|
@@ -93,7 +93,7 @@ const { inspect, deprecate } = require('node:util');
|
|
|
93
93
|
const frames = require('../frames');
|
|
94
94
|
const {
|
|
95
95
|
ST_OPEN, ST_HALF_CLOSED_LOCAL, ST_HALF_CLOSED_REMOTE, ST_CLOSED, NO_ERROR,
|
|
96
|
-
PROTOCOL_ERROR, INTERNAL_ERROR
|
|
96
|
+
PROTOCOL_ERROR, INTERNAL_ERROR, HEADERS
|
|
97
97
|
} = frames;
|
|
98
98
|
const hdr = require('../headers');
|
|
99
99
|
const { Http2Stream, EMPTY, READABLE_HWM, emitCloseNT, emitErrorCloseNT, emitErrorOnlyNT, emitEndNT, wantTrailersNT, finishNT, emitEndCloseNT, emitCloseDeliveredNT, autoCloseNT, noopClose, FILE_CHUNK, isFileHandle, argTypeError, fdOptions, prepareResponseHeadersObject, errorOf, validateRstCode, abortError, validateObjectArg, validateSignalArg, streamErrorOf, createStreamIterator } = require('../Http2Stream');
|
|
@@ -174,8 +174,7 @@ class Http2ServerStream extends Http2Stream {
|
|
|
174
174
|
// batch, so sendTrailers() closes the stream without staging a second one, and
|
|
175
175
|
// addTrailers() after end() recalls the frame instead
|
|
176
176
|
this._esStaged = false;
|
|
177
|
-
this._pendingTrailers = null; // trailer
|
|
178
|
-
this._pendingTrailersBound = 0; // ...and its outbound estimate, checked when it drains
|
|
177
|
+
this._pendingTrailers = null; // trailer map waiting behind a queued body
|
|
179
178
|
// A refused outbound HEADERS (session._refuseBlock) left the wire a step behind the
|
|
180
179
|
// application: no response HEADERS means no DATA may follow, and no END_STREAM means
|
|
181
180
|
// the stream must not close gracefully. An informational block sets neither.
|
|
@@ -560,9 +559,11 @@ class Http2ServerStream extends Http2Stream {
|
|
|
560
559
|
this.headersSent = true;
|
|
561
560
|
this.pending = false;
|
|
562
561
|
this._ending = true;
|
|
563
|
-
let
|
|
562
|
+
let prep;
|
|
564
563
|
try {
|
|
565
|
-
|
|
564
|
+
// prepare() reads the map here, inside the descriptor's error handling, and
|
|
565
|
+
// _writeBlockAt writes the octets straight into the session's staging buffer.
|
|
566
|
+
prep = hdr.prepare(prepared, { sendDate }, this.session);
|
|
566
567
|
} catch (err) {
|
|
567
568
|
if (this.ownsFd) {
|
|
568
569
|
fs.close(fd, noopClose);
|
|
@@ -573,8 +574,9 @@ class Http2ServerStream extends Http2Stream {
|
|
|
573
574
|
this._sentSrc = prepared;
|
|
574
575
|
this._sentStatus = hdr.sent.status;
|
|
575
576
|
this._sentDate = hdr.sent.date;
|
|
576
|
-
|
|
577
|
-
|
|
577
|
+
if (this._blockRefused ||
|
|
578
|
+
this.session._writeBlockAt(this.id, prepared, { sendDate }, HEADERS,
|
|
579
|
+
false, undefined, true, prep)) {
|
|
578
580
|
this._blockRefused = true;
|
|
579
581
|
// node has already called self.end() by this point (processRespondWithFD ends
|
|
580
582
|
// the writable before the wire write), so the half-close happens even here.
|
|
@@ -794,16 +796,12 @@ class Http2ServerStream extends Http2Stream {
|
|
|
794
796
|
throw errorOf('ERR_HTTP2_TRAILERS_NOT_READY',
|
|
795
797
|
'Trailing headers cannot be sent until after the wantTrailers event is emitted');
|
|
796
798
|
}
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
//
|
|
800
|
-
//
|
|
801
|
-
//
|
|
802
|
-
//
|
|
803
|
-
// empty trailer map, because the (req,res) layer always answers 'wantTrailers' with
|
|
804
|
-
// what it recorded: nothing at all when the application added none (item 44).
|
|
805
|
-
// hdr.sent must not be read for the skipped case: it still holds the previous
|
|
806
|
-
// block's result.
|
|
799
|
+
// An empty trailer map is skipped, not run through the encoder: nothing in it can
|
|
800
|
+
// fail validation (no name to check, no single-value rule to trip) and it encodes to
|
|
801
|
+
// zero octets, while prepare() still allocates the parse key list, the
|
|
802
|
+
// strict-singles scratch and the parse result -- and every compat response ends with
|
|
803
|
+
// an empty trailer map, because the (req,res) layer always answers 'wantTrailers'
|
|
804
|
+
// with what it recorded: nothing at all when the application added none (item 44).
|
|
807
805
|
let empty = true;
|
|
808
806
|
for (const key in trailers) {
|
|
809
807
|
if (Object.prototype.hasOwnProperty.call(trailers, key)) {
|
|
@@ -811,23 +809,12 @@ class Http2ServerStream extends Http2Stream {
|
|
|
811
809
|
break;
|
|
812
810
|
}
|
|
813
811
|
}
|
|
814
|
-
if (empty) {
|
|
815
|
-
block = EMPTY;
|
|
816
|
-
bound = 0;
|
|
817
|
-
} else {
|
|
818
|
-
block = hdr.block(trailers, { trailer: true }, this.session);
|
|
819
|
-
bound = hdr.sent.bound;
|
|
820
|
-
// Own these octets before the copy below reads the caller's getters. A getter
|
|
821
|
-
// can call block() for another response, and block() returns a view into the
|
|
822
|
-
// module-level scratch shared by every session: the bytes would be rewritten
|
|
823
|
-
// before sendHeaders() stages this frame (reproduced with a reentrant getter
|
|
824
|
-
// in the trailers object -- the peer saw COMPRESSION_ERROR instead of them).
|
|
825
|
-
// The queued path below uses this copy too, so it still copies only once.
|
|
826
|
-
block = Buffer.from(block);
|
|
827
|
-
}
|
|
828
812
|
// node records a null-prototype copy, not the caller's map (`ObjectAssign({
|
|
829
813
|
// __proto__: null }, headers)`, internal/http2/core.js sendTrailers -- the shared
|
|
830
|
-
// base method, so the client stream records the same shape).
|
|
814
|
+
// base method, so the client stream records the same shape). Reading the getters
|
|
815
|
+
// here once also fixes the values for a body that is still queued: the block is
|
|
816
|
+
// encoded when that body drains, and prepare()/writePrepared() write straight into
|
|
817
|
+
// the session's stage from parsed values, so a reentrant getter cannot rewrite it.
|
|
831
818
|
this.sentTrailers = Object.assign({ __proto__: null }, trailers);
|
|
832
819
|
// the trailers are out: the flag that permitted this call must not keep asking for
|
|
833
820
|
// wantTrailers, whatever happens to the bytes below
|
|
@@ -848,15 +835,12 @@ class Http2ServerStream extends Http2Stream {
|
|
|
848
835
|
// because node's (req,res) layer always asks for trailers (internal/http2/compat.js
|
|
849
836
|
// kBeginSend passes waitForTrailers: true) and answers with the empty trailer map
|
|
850
837
|
// when the application added none.
|
|
851
|
-
empty = block.length === 0;
|
|
852
838
|
if (this._queued !== null) {
|
|
853
839
|
// The body is still waiting for credit or for the socket, and this block has to
|
|
854
840
|
// follow it: END_STREAM rides on the trailers, so a HEADERS sent now would outrun
|
|
855
|
-
// DATA that has not gone out.
|
|
856
|
-
//
|
|
857
|
-
|
|
858
|
-
this._pendingTrailers = empty ? EMPTY : block;
|
|
859
|
-
this._pendingTrailersBound = bound;
|
|
841
|
+
// DATA that has not gone out. Stash the fixed copy; the session encodes it when
|
|
842
|
+
// the queue drains. A zero-length stash is the empty-map marker above.
|
|
843
|
+
this._pendingTrailers = empty ? EMPTY : this.sentTrailers;
|
|
860
844
|
return this;
|
|
861
845
|
}
|
|
862
846
|
if (empty) {
|
|
@@ -864,7 +848,7 @@ class Http2ServerStream extends Http2Stream {
|
|
|
864
848
|
this.session.sendData(this, EMPTY, true);
|
|
865
849
|
return this;
|
|
866
850
|
}
|
|
867
|
-
if (this.session.
|
|
851
|
+
if (this.session.sendHeadersAt(this.id, this.sentTrailers, { trailer: true }, true)) {
|
|
868
852
|
// The END_STREAM this block carried never went out, so the stream has not finished
|
|
869
853
|
// on the wire and must not close on its own terms ahead of the deferred
|
|
870
854
|
// FRAME_SIZE_ERROR close (session._refuseBlock).
|
|
@@ -926,12 +910,10 @@ class Http2ServerStream extends Http2Stream {
|
|
|
926
910
|
}
|
|
927
911
|
// `{ info: true }` is node's separate rule for these blocks: :status must be 100..199
|
|
928
912
|
// (101 has its own error), there is no default status and no generated date.
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
//
|
|
932
|
-
|
|
933
|
-
// into (the trailers path above has the same window).
|
|
934
|
-
const owned = Buffer.from(block);
|
|
913
|
+
// prepare() performs that validation before the copy is made, and the plan it
|
|
914
|
+
// returns lets _writeBlockAt write the octets straight into the session's stage
|
|
915
|
+
// without a scratch view or a second walk over getters.
|
|
916
|
+
const prep = hdr.prepare(headers, { info: true }, this.session);
|
|
935
917
|
// node stores a null-proto copy of each informational block, created at the
|
|
936
918
|
// first call (internal/http2/core.js:3265, 3285) -- the caller's object is
|
|
937
919
|
// not retained, and the array does not exist before the first call.
|
|
@@ -941,7 +923,8 @@ class Http2ServerStream extends Http2Stream {
|
|
|
941
923
|
} else {
|
|
942
924
|
this.sentInfoHeaders.push(copy);
|
|
943
925
|
}
|
|
944
|
-
this.session.
|
|
926
|
+
this.session._writeBlockAt(this.id, copy, { info: true }, HEADERS, false,
|
|
927
|
+
undefined, true, prep);
|
|
945
928
|
return this;
|
|
946
929
|
}
|
|
947
930
|
|
|
@@ -1057,8 +1040,9 @@ class Http2ServerStream extends Http2Stream {
|
|
|
1057
1040
|
// the frame is submitted). The id was already reserved; it stays consumed.
|
|
1058
1041
|
// Like respond(), the never-indexed names come only off the headers object's
|
|
1059
1042
|
// sensitiveHeaders symbol -- node's pushStream has no options.sensitiveHeaders.
|
|
1060
|
-
|
|
1061
|
-
|
|
1043
|
+
// sendPushPromise prepares the request-shaped block itself and writes its
|
|
1044
|
+
// promised-id prefix plus the HPACK octets straight into the session's stage.
|
|
1045
|
+
const refused = this.session.sendPushPromise(this.id, promisedId, h);
|
|
1062
1046
|
|
|
1063
1047
|
// Create the pushed stream and register it. The readable side is pre-ended:
|
|
1064
1048
|
// remoteClose=1 means _endLocalSide() takes the graceful close path (no RST),
|
package/package.json
CHANGED