h2lean 0.2.6 → 0.4.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/index.js +1 -1
- package/lib/client-session.js +133 -7
- package/lib/client-stream.js +5 -1
- package/lib/headers.js +42 -3
- package/lib/session.js +61 -3
- package/lib/stream.js +95 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Servers: h2c and h2s (prior-knowledge, ALPN 'h2'), the `stream` and compat
|
|
4
4
|
// (`request`) APIs. Client: `connect()` + `session.request()` over h2c and h2s
|
|
5
|
-
// (see lib/client.js); server push
|
|
5
|
+
// (see lib/client.js); server push (pushStream / PUSH_PROMISE) on both sides.
|
|
6
6
|
// Deliberately reused from node:http2, because re-typing them would only create
|
|
7
7
|
// drift: the 240 protocol constants and the `sensitiveHeaders` symbol. A peer
|
|
8
8
|
// must see the same rstCode/goawayCode numbers as with node, and user code
|
package/lib/client-session.js
CHANGED
|
@@ -143,7 +143,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
143
143
|
// before that node's getters read undefined.
|
|
144
144
|
this.pendingSettingsAck = true;
|
|
145
145
|
this.originSet = [];
|
|
146
|
-
|
|
146
|
+
// enablePush defaults to true on the client (node's initializeOptions sets
|
|
147
|
+
// NGHTTP2_SETTINGS_ENABLE_PUSH = 1 for clients; the server's OUR_DEFAULTS has
|
|
148
|
+
// false because a server does not receive pushes). The application can override
|
|
149
|
+
// via opts.settings.enablePush.
|
|
150
|
+
this.localSettings = { ...OUR_DEFAULTS, enablePush: true, ...opts.settings };
|
|
147
151
|
this.remoteSettings = { ...PEER_DEFAULTS };
|
|
148
152
|
this._scheme = scheme || 'http';
|
|
149
153
|
this._authority = authority || 'localhost';
|
|
@@ -156,6 +160,10 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
156
160
|
.filter((k) => ID_BY_SETTING[k] !== undefined));
|
|
157
161
|
|
|
158
162
|
this._nextSid = 1; // client owns odd ids
|
|
163
|
+
// Highest even id promised by the peer via PUSH_PROMISE. Used to silently
|
|
164
|
+
// ignore late frames for a pushed stream the application already destroyed
|
|
165
|
+
// (node does the same: session stays alive, measured v26.12.1).
|
|
166
|
+
this._maxPushSid = 0;
|
|
159
167
|
this._streams = new Map();
|
|
160
168
|
this._outboundQueue = []; // requests waiting for a concurrency slot
|
|
161
169
|
this._decoder = new Decoder(this.localSettings.headerTableSize);
|
|
@@ -237,6 +245,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
237
245
|
this._hdrSid = -1;
|
|
238
246
|
this._hdrEnd = false;
|
|
239
247
|
this._hdrCont = 0;
|
|
248
|
+
// Non-zero while a PUSH_PROMISE block is being assembled: the promised even
|
|
249
|
+
// stream id. _finishHeaders routes to _processPushPromise when this is set.
|
|
250
|
+
this._hdrPush = 0;
|
|
240
251
|
// The first octets of a HEADERS payload -- the pad length and the priority
|
|
241
252
|
// block -- are at most six, and a payload that arrived in pieces has no
|
|
242
253
|
// buffer to read them from: they are copied into this scratch.
|
|
@@ -1170,10 +1181,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1170
1181
|
}
|
|
1171
1182
|
return this._open; // contents deprecated (RFC 9218)
|
|
1172
1183
|
case PUSH_PROMISE:
|
|
1173
|
-
|
|
1174
|
-
// is for the server side); RFC 9113 6.6 makes an unexpected PUSH_PROMISE a
|
|
1175
|
-
// connection error of type PROTOCOL_ERROR, which is what node answers too.
|
|
1176
|
-
return this._fatal(PROTOCOL_ERROR, 'peer sent PUSH_PROMISE');
|
|
1184
|
+
return this._beginPushPromise(sid, flags, buf, start, len);
|
|
1177
1185
|
default:
|
|
1178
1186
|
return this._open; // RFC 9113 5.5: discard unknown
|
|
1179
1187
|
}
|
|
@@ -1385,19 +1393,132 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1385
1393
|
|
|
1386
1394
|
_finishHeaders() {
|
|
1387
1395
|
const sid = this._hdrSid, end = this._hdrEnd;
|
|
1396
|
+
const push = this._hdrPush;
|
|
1397
|
+
this._hdrPush = 0;
|
|
1388
1398
|
if (this._hdrPieces !== null) {
|
|
1389
1399
|
const pieces = this._hdrPieces;
|
|
1390
1400
|
const len = this._hdrLen;
|
|
1391
1401
|
this._hdrSid = -1;
|
|
1392
1402
|
this._hdrLen = 0;
|
|
1393
1403
|
this._hdrPieces = null;
|
|
1394
|
-
|
|
1404
|
+
if (push !== 0) {
|
|
1405
|
+
this._processPushPromise(sid, push, pieceConcat(pieces, 0, len));
|
|
1406
|
+
} else {
|
|
1407
|
+
this._processBlockPieces(sid, end, pieces, 0, len);
|
|
1408
|
+
}
|
|
1395
1409
|
return;
|
|
1396
1410
|
}
|
|
1397
1411
|
const block = this._hdr.subarray(0, this._hdrLen);
|
|
1398
1412
|
this._hdrSid = -1;
|
|
1399
1413
|
this._hdrLen = 0;
|
|
1400
|
-
|
|
1414
|
+
if (push !== 0) {
|
|
1415
|
+
this._processPushPromise(sid, push, block);
|
|
1416
|
+
} else {
|
|
1417
|
+
this._processHeaders(sid, end, block);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// ---- PUSH_PROMISE (RFC 9113 6.6) --------------------------------------------
|
|
1422
|
+
// A server promising a resource on a client-owned (odd) stream. The payload is
|
|
1423
|
+
// a 4-octet promised even stream id followed by an HPACK request-header block.
|
|
1424
|
+
// The block obeys the same CONTINUATION rules as HEADERS, so the assembly
|
|
1425
|
+
// machinery (_hdrSid/_hdrLen/_appendHdr/_finishHeaders) is reused with _hdrPush
|
|
1426
|
+
// holding the promised id to route the finish to _processPushPromise.
|
|
1427
|
+
_beginPushPromise(sid, flags, buf, start, len) {
|
|
1428
|
+
// RFC 9113 6.6: PUSH_PROMISE MUST be sent on a client-initiated (odd) stream
|
|
1429
|
+
// that is open or half-closed(remote). Stream 0 or an even id is a connection
|
|
1430
|
+
// error. Measured on node v26.12.1: GOAWAY(PROTOCOL_ERROR).
|
|
1431
|
+
if (sid === 0 || (sid & 1) === 0) {
|
|
1432
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE on a non-client stream');
|
|
1433
|
+
}
|
|
1434
|
+
// RFC 9113 6.6: a client that has set SETTINGS_ENABLE_PUSH to 0 MUST treat
|
|
1435
|
+
// receipt as a connection error of type PROTOCOL_ERROR (measured on node).
|
|
1436
|
+
if (!this.localSettings.enablePush) {
|
|
1437
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE with push disabled');
|
|
1438
|
+
}
|
|
1439
|
+
if (len < 4) {
|
|
1440
|
+
return this._fatal(FRAME_SIZE_ERROR, 'PUSH_PROMISE payload too short');
|
|
1441
|
+
}
|
|
1442
|
+
let p = start;
|
|
1443
|
+
let l = len;
|
|
1444
|
+
if (flags & F_PADDED) {
|
|
1445
|
+
if (len < 1) {
|
|
1446
|
+
return this._fatal(PROTOCOL_ERROR, 'padded PUSH_PROMISE with an empty payload');
|
|
1447
|
+
}
|
|
1448
|
+
const pad = buf[p];
|
|
1449
|
+
p += 1;
|
|
1450
|
+
l -= 1 + pad;
|
|
1451
|
+
if (l < 4) {
|
|
1452
|
+
return this._fatal(PROTOCOL_ERROR, 'pad larger than the PUSH_PROMISE payload');
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
const promisedSid = buf.readUInt32BE(p) & 0x7fffffff;
|
|
1456
|
+
p += 4;
|
|
1457
|
+
l -= 4;
|
|
1458
|
+
// The promised id must be even and non-zero (RFC 9113 6.6).
|
|
1459
|
+
if (promisedSid === 0 || (promisedSid & 1) !== 0) {
|
|
1460
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE promised id is not even');
|
|
1461
|
+
}
|
|
1462
|
+
// A promised id already in use is a connection error (RFC 9113 5.1.1).
|
|
1463
|
+
if (this._streams.has(promisedSid)) {
|
|
1464
|
+
return this._fatal(PROTOCOL_ERROR, 'PUSH_PROMISE promised id already in use');
|
|
1465
|
+
}
|
|
1466
|
+
this._hdrCont = 0;
|
|
1467
|
+
if (flags & F_END_HEADERS) {
|
|
1468
|
+
this._processPushPromise(sid, promisedSid, buf.subarray(p, p + l));
|
|
1469
|
+
return this._open;
|
|
1470
|
+
}
|
|
1471
|
+
this._hdrSid = sid;
|
|
1472
|
+
this._hdrEnd = false;
|
|
1473
|
+
this._hdrPush = promisedSid;
|
|
1474
|
+
this._hdrLen = 0;
|
|
1475
|
+
this._appendHdr(buf, p, l);
|
|
1476
|
+
return this._open;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// Decode the promise block and create the pushed stream. The HPACK block MUST
|
|
1480
|
+
// be decoded even if the application will refuse the push, because the dynamic
|
|
1481
|
+
// table state advances for every block on the connection.
|
|
1482
|
+
_processPushPromise(parentSid, promisedSid, block) {
|
|
1483
|
+
const flat = FLAT;
|
|
1484
|
+
try {
|
|
1485
|
+
this._decoder.decode(block, 0, block.length, flat);
|
|
1486
|
+
} catch (err) {
|
|
1487
|
+
if (err instanceof HPackError) {
|
|
1488
|
+
this._fatal(COMPRESSION_ERROR, err.message);
|
|
1489
|
+
} else {
|
|
1490
|
+
throw err;
|
|
1491
|
+
}
|
|
1492
|
+
return;
|
|
1493
|
+
}
|
|
1494
|
+
const fields = flat.items;
|
|
1495
|
+
const fcount = flat.count;
|
|
1496
|
+
const headers = this._toObject(fields, fcount);
|
|
1497
|
+
// Create the pushed stream. Its writable half is pre-ended: the client never
|
|
1498
|
+
// sends a request body for a push (node: writableEnded=true, writableFinished=true
|
|
1499
|
+
// at the 'stream' event, measured v26.12.1).
|
|
1500
|
+
const stream = new ClientHttp2Stream(this, promisedSid);
|
|
1501
|
+
stream._pushed = true;
|
|
1502
|
+
stream.rstCode = 0;
|
|
1503
|
+
stream._headersSent = true;
|
|
1504
|
+
stream._endSent = true;
|
|
1505
|
+
if (promisedSid > this._maxPushSid) {
|
|
1506
|
+
this._maxPushSid = promisedSid;
|
|
1507
|
+
}
|
|
1508
|
+
this._streams.set(promisedSid, stream);
|
|
1509
|
+
// End the writable without putting anything on the wire: _pump sees
|
|
1510
|
+
// _endSent=true and does nothing; _writableDone fires the Duplex final callback.
|
|
1511
|
+
stream.end();
|
|
1512
|
+
stream._writableDone();
|
|
1513
|
+
// The Duplex defers writableFinished to nextTick, but the application must see
|
|
1514
|
+
// it true at the 'stream' event (node: measured v26.12.1). Settle it now.
|
|
1515
|
+
stream._writableState.finished = true;
|
|
1516
|
+
// node emits session 'stream' with (pushedStream, requestHeaders, flags=4).
|
|
1517
|
+
// flags 4 = NGHTTP2_FLAG_END_HEADERS, the only flag PUSH_PROMISE carries that
|
|
1518
|
+
// the JS layer surfaces (measured v26.12.1). Synchronous so the application's
|
|
1519
|
+
// 'push' subscription is in place before the response HEADERS arrive in the
|
|
1520
|
+
// same read batch.
|
|
1521
|
+
this.emit('stream', stream, headers, 4);
|
|
1401
1522
|
}
|
|
1402
1523
|
|
|
1403
1524
|
// The shape cache: the server's design, ported. A block whose bytes have been
|
|
@@ -1504,6 +1625,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1504
1625
|
if ((sid & 1) === 1 && sid < this._nextSid) {
|
|
1505
1626
|
return;
|
|
1506
1627
|
}
|
|
1628
|
+
// A pushed stream the application destroyed: late frames are silently
|
|
1629
|
+
// ignored, the session lives (measured on node v26.12.1).
|
|
1630
|
+
if ((sid & 1) === 0 && sid <= this._maxPushSid) {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1507
1633
|
return this._fatal(PROTOCOL_ERROR, 'response HEADERS on an unopened stream id');
|
|
1508
1634
|
}
|
|
1509
1635
|
if (this._refuseHeaders(sid, stream, fields, fcount)) {
|
package/lib/client-stream.js
CHANGED
|
@@ -59,6 +59,10 @@ class ClientHttp2Stream extends Duplex {
|
|
|
59
59
|
this.sentHeaders = null;
|
|
60
60
|
this.sentStatus = null;
|
|
61
61
|
this.responseEmitted = false;
|
|
62
|
+
// Set by the session for a stream created from PUSH_PROMISE: the first final
|
|
63
|
+
// response block emits 'push' instead of 'response' (node's event model,
|
|
64
|
+
// measured v26.12.1).
|
|
65
|
+
this._pushed = false;
|
|
62
66
|
this.additionalHeaders = [];
|
|
63
67
|
this.trailers = null;
|
|
64
68
|
}
|
|
@@ -118,7 +122,7 @@ class ClientHttp2Stream extends Duplex {
|
|
|
118
122
|
this.responseEmitted = true;
|
|
119
123
|
this.responseHeaders = headers;
|
|
120
124
|
this.sentStatus = status;
|
|
121
|
-
this.emit('response', headers);
|
|
125
|
+
this.emit(this._pushed ? 'push' : 'response', headers);
|
|
122
126
|
} else {
|
|
123
127
|
// A second final-status block after the response is the trailer block.
|
|
124
128
|
this.trailers = headers;
|
package/lib/headers.js
CHANGED
|
@@ -282,7 +282,7 @@ function refreshDate() {
|
|
|
282
282
|
// `info` marks the informational (1xx) block that additionalHeaders() sends: node applies
|
|
283
283
|
// a different status rule and different error codes there, and it generates no date for it.
|
|
284
284
|
function parse(headers, sensitive,
|
|
285
|
-
{ trailer = false, sendDate = false, info = false, strict = true } = {}) {
|
|
285
|
+
{ trailer = false, sendDate = false, info = false, strict = true, request = false } = {}) {
|
|
286
286
|
if (headers === null || typeof headers !== 'object') {
|
|
287
287
|
throw httpError('ERR_HTTP2_INVALID_HEADER_VALUE', 'headers must be an object');
|
|
288
288
|
}
|
|
@@ -361,6 +361,27 @@ function parse(headers, sensitive,
|
|
|
361
361
|
if (trailer) {
|
|
362
362
|
throw httpError('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'cannot send trailers with pseudo-headers');
|
|
363
363
|
}
|
|
364
|
+
if (request) {
|
|
365
|
+
// Request pseudo-headers for PUSH_PROMISE (node: assertValidPseudoHeader,
|
|
366
|
+
// internal/http2/util.js). :status is response-only; the five request
|
|
367
|
+
// pseudo-headers are the ones RFC 9113 8.3.1 defines.
|
|
368
|
+
if (key !== ':method' && key !== ':path' && key !== ':scheme'
|
|
369
|
+
&& key !== ':authority' && key !== ':protocol') {
|
|
370
|
+
throw httpError('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', `cannot send ${key} in a request`);
|
|
371
|
+
}
|
|
372
|
+
const val = String(Array.isArray(raw) ? raw[0] : raw);
|
|
373
|
+
if (key === ':method' && !isToken.test(val)) {
|
|
374
|
+
throw httpError('ERR_INVALID_HTTP_TOKEN', `${val} is an invalid method`);
|
|
375
|
+
}
|
|
376
|
+
if (key === ':path' && val === '') {
|
|
377
|
+
throw httpError('ERR_HTTP2_INVALID_HEADER_VALUE', ':path must not be empty');
|
|
378
|
+
}
|
|
379
|
+
if (key === ':scheme' && !isToken.test(val)) {
|
|
380
|
+
throw httpError('ERR_INVALID_HTTP_TOKEN', `${val} is an invalid scheme`);
|
|
381
|
+
}
|
|
382
|
+
pseudo.push(key, val);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
364
385
|
if (key !== ':status') {
|
|
365
386
|
throw httpError('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', `cannot send ${key} in a response`);
|
|
366
387
|
}
|
|
@@ -421,7 +442,8 @@ function parse(headers, sensitive,
|
|
|
421
442
|
// answers 200. Measured: respond({'x-a': 1}) arrives as :status 200 with a date, where
|
|
422
443
|
// this used to throw. An informational block has no such default -- node validates a
|
|
423
444
|
// :status only if the application put one there -- and trailers have no status at all.
|
|
424
|
-
|
|
445
|
+
// A request block (PUSH_PROMISE) has no status and no date.
|
|
446
|
+
if (!trailer && !request) {
|
|
425
447
|
if (info) {
|
|
426
448
|
if (sawStatus) {
|
|
427
449
|
pseudo.push(':status', String(status));
|
|
@@ -553,5 +575,22 @@ function encodeAt(parsed, enc, at) {
|
|
|
553
575
|
return off;
|
|
554
576
|
}
|
|
555
577
|
|
|
556
|
-
|
|
578
|
+
// The HPACK block for a PUSH_PROMISE payload: request-style pseudo-headers, no date,
|
|
579
|
+
// no status default, no cache (a promise is one-shot). The encoder is the session's,
|
|
580
|
+
// so the block shares the connection's dynamic table with every other outbound block.
|
|
581
|
+
// Returns a view into the shared scratch, valid until the next encode call -- the
|
|
582
|
+
// caller (session.sendPushPromise) copies it into the frame immediately.
|
|
583
|
+
function pushBlock(headers, sensitive, session) {
|
|
584
|
+
const enc = session._encoder;
|
|
585
|
+
const update = enc.takeSizeUpdate();
|
|
586
|
+
const parsed = parse(headers, sensitive, { request: true, strict: session._strictSingle });
|
|
587
|
+
const at = update === null ? 0 : update.length;
|
|
588
|
+
const end = encodeAt(parsed, enc, at);
|
|
589
|
+
if (at !== 0) {
|
|
590
|
+
update.copy(scratch, 0);
|
|
591
|
+
}
|
|
592
|
+
return scratch.subarray(0, end);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
module.exports = { block, pushBlock, encode, parse, nullProto, isFieldName, trimName, lowerName,
|
|
557
596
|
nameCacheSize, hasBadValue, CONNECTION_SPECIFIC, KSensitiveHeaders, SINGLE_VALUE_FIELDS };
|
package/lib/session.js
CHANGED
|
@@ -498,6 +498,10 @@ class Http2Session extends EventEmitter {
|
|
|
498
498
|
this._rstSec = (Date.now() / 1000) | 0;
|
|
499
499
|
|
|
500
500
|
this._maxInboundSid = 0;
|
|
501
|
+
// The next even stream id to reserve for a PUSH_PROMISE (RFC 9113 5.1.1: server-initiated
|
|
502
|
+
// streams are even). Starts at 2; _allocPushSid increments by 2 and throws
|
|
503
|
+
// ERR_HTTP2_OUT_OF_STREAMS when the 31-bit space is exhausted.
|
|
504
|
+
this._nextPushSid = 2;
|
|
501
505
|
// The highest id of a HEADERS frame this side has accepted -- nghttp2 opens the
|
|
502
506
|
// stream in prep_frame, after the length, padding and priority checks but BEFORE
|
|
503
507
|
// the block is decoded or any verdict on it is given, and its last-proc-stream-id
|
|
@@ -825,6 +829,53 @@ class Http2Session extends EventEmitter {
|
|
|
825
829
|
}
|
|
826
830
|
}
|
|
827
831
|
|
|
832
|
+
// Reserve the next even stream id for a PUSH_PROMISE. RFC 9113 5.1.1: server-initiated
|
|
833
|
+
// streams use even ids; the 31-bit space is exhausted at 2^31-1 (the R bit is not an id).
|
|
834
|
+
// node: nghttp2_session_get_next_stream_id returns NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE,
|
|
835
|
+
// which core.js maps to ERR_HTTP2_OUT_OF_STREAMS.
|
|
836
|
+
_allocPushSid() {
|
|
837
|
+
const sid = this._nextPushSid;
|
|
838
|
+
if (sid > 0x7fffffff) {
|
|
839
|
+
throw errorOf('ERR_HTTP2_OUT_OF_STREAMS', 'No stream ID is available');
|
|
840
|
+
}
|
|
841
|
+
this._nextPushSid += 2;
|
|
842
|
+
return sid;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// PUSH_PROMISE frame (RFC 9113 6.6): the payload is a 4-octet promised stream id (R=0)
|
|
846
|
+
// followed by the header block fragment. The frame rides on the PARENT stream id (odd);
|
|
847
|
+
// the promised id inside the payload is even. CONTINUATION split mirrors sendHeaders,
|
|
848
|
+
// with the first frame carrying the 4-octet prefix and the block fragment that fits.
|
|
849
|
+
sendPushPromise(parentSid, promisedSid, block) {
|
|
850
|
+
this.state.deflateDynamicTableSize = this._encoder.size;
|
|
851
|
+
const max = this.remoteSettings.maxFrameSize;
|
|
852
|
+
// The payload is the 4-byte promised id + the block. The first frame can carry
|
|
853
|
+
// max - 4 bytes of block; the rest goes into CONTINUATION frames.
|
|
854
|
+
const firstCap = max - 4;
|
|
855
|
+
if (block.length <= firstCap) {
|
|
856
|
+
const payload = Buffer.allocUnsafe(4 + block.length);
|
|
857
|
+
payload.writeUInt32BE(promisedSid, 0);
|
|
858
|
+
block.copy(payload, 4);
|
|
859
|
+
this._frame(PUSH_PROMISE, F_END_HEADERS, parentSid, payload);
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
const first = Buffer.allocUnsafe(4 + firstCap);
|
|
863
|
+
first.writeUInt32BE(promisedSid, 0);
|
|
864
|
+
block.copy(first, 4, 0, firstCap);
|
|
865
|
+
this._frame(PUSH_PROMISE, 0, parentSid, first);
|
|
866
|
+
let off = firstCap;
|
|
867
|
+
for (;;) {
|
|
868
|
+
const n = Math.min(block.length - off, max);
|
|
869
|
+
const last = off + n >= block.length;
|
|
870
|
+
this._frame(CONTINUATION, last ? F_END_HEADERS : 0, parentSid,
|
|
871
|
+
block.subarray(off, off + n));
|
|
872
|
+
off += n;
|
|
873
|
+
if (last) {
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
828
879
|
// Writes DATA within the stream and connection windows. Returns true when
|
|
829
880
|
// everything went out; false means the caller should wait for WINDOW_UPDATE.
|
|
830
881
|
sendData(stream, buf, end) {
|
|
@@ -2006,6 +2057,7 @@ class Http2Session extends EventEmitter {
|
|
|
2006
2057
|
return;
|
|
2007
2058
|
}
|
|
2008
2059
|
const stream = new Http2Stream(this, sid);
|
|
2060
|
+
stream._requestHeaders = headers;
|
|
2009
2061
|
this._streams.set(sid, stream);
|
|
2010
2062
|
this._liveStreams++;
|
|
2011
2063
|
// content-length is a promise about the body, and the two checks that hold the peer to it
|
|
@@ -2102,7 +2154,11 @@ class Http2Session extends EventEmitter {
|
|
|
2102
2154
|
// the 'idle' state of 5.1, and DATA is allowed in 'open' and 'half-closed (local)' only.
|
|
2103
2155
|
// Measured on node (probe cases U5, U13, U14): GOAWAY(PROTOCOL_ERROR) -- including for the
|
|
2104
2156
|
// connection-level sid 0 and for an even id, neither of which a client may ever open.
|
|
2105
|
-
|
|
2157
|
+
// Even ids this side pushed are known (reserved-local): a late DATA for one that already
|
|
2158
|
+
// closed is the discard of 5.1, not the idle error. Only even ids above the highest push
|
|
2159
|
+
// id ever allocated are truly unknown.
|
|
2160
|
+
if (sid === 0 || ((sid & 1) === 0 && sid > this._nextPushSid - 2)
|
|
2161
|
+
|| ((sid & 1) !== 0 && sid > this._maxInboundSid)) {
|
|
2106
2162
|
this._fatal(PROTOCOL_ERROR, 'DATA on a stream in the idle state');
|
|
2107
2163
|
return undefined;
|
|
2108
2164
|
}
|
|
@@ -2236,7 +2292,8 @@ class Http2Session extends EventEmitter {
|
|
|
2236
2292
|
// is GOAWAY(PROTOCOL_ERROR). For an id at or below the highest one opened the stream has
|
|
2237
2293
|
// been answered, refused or reset -- a credit note that arrived late -- and node drops it,
|
|
2238
2294
|
// so this does too rather than turning the peer's own in-flight frame into an error.
|
|
2239
|
-
|
|
2295
|
+
// Even push ids are compared against the highest push id allocated, not the inbound line.
|
|
2296
|
+
if (sid > ((sid & 1) !== 0 ? this._maxInboundSid : this._nextPushSid - 2)) {
|
|
2240
2297
|
this._fatal(PROTOCOL_ERROR, 'WINDOW_UPDATE on an idle stream');
|
|
2241
2298
|
}
|
|
2242
2299
|
return;
|
|
@@ -2366,7 +2423,8 @@ class Http2Session extends EventEmitter {
|
|
|
2366
2423
|
// allows RST_STREAM in 'open' and 'half-closed (local)' only. Measured on node (h2spec
|
|
2367
2424
|
// http2/5.1 and 6.4): GOAWAY(PROTOCOL_ERROR). A reset for a stream that has already been
|
|
2368
2425
|
// answered or refused is the peer's own late frame and is dropped.
|
|
2369
|
-
|
|
2426
|
+
// Even push ids are compared against the highest push id allocated, not the inbound line.
|
|
2427
|
+
if (sid > ((sid & 1) !== 0 ? this._maxInboundSid : this._nextPushSid - 2)) {
|
|
2370
2428
|
this._fatal(PROTOCOL_ERROR, 'RST_STREAM on an idle stream');
|
|
2371
2429
|
return;
|
|
2372
2430
|
}
|
package/lib/stream.js
CHANGED
|
@@ -270,6 +270,101 @@ class Http2Stream extends EventEmitter {
|
|
|
270
270
|
return true;
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
+
// node: ServerHttp2Stream.pushAllowed (internal/http2/core.js:2961).
|
|
274
|
+
// Truthiness of remoteSettings.enablePush: the CLIENT's advertisement decides whether
|
|
275
|
+
// the server may push. Measured on v26.12.1: a client with settings:{enablePush:false}
|
|
276
|
+
// makes pushAllowed false and pushStream throw ERR_HTTP2_PUSH_DISABLED synchronously.
|
|
277
|
+
get pushAllowed() {
|
|
278
|
+
return !this._destroyed && !this.closed && !this.session.closed
|
|
279
|
+
&& !this.session.destroyed && !!this.session.remoteSettings.enablePush;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// node: ServerHttp2Stream.pushStream (internal/http2/core.js:2971).
|
|
283
|
+
// Synchronous throws: ERR_HTTP2_PUSH_DISABLED, ERR_HTTP2_NESTED_PUSH.
|
|
284
|
+
// Asynchronous errors (allocation, closed stream) go to callback via nextTick.
|
|
285
|
+
// The pushed stream's readable side is pre-ended (a push has no request body);
|
|
286
|
+
// the application answers it through the normal respond()/write()/end() path.
|
|
287
|
+
pushStream(headers, options, callback) {
|
|
288
|
+
if (!this.pushAllowed) {
|
|
289
|
+
throw errorOf('ERR_HTTP2_PUSH_DISABLED', 'Push is disabled');
|
|
290
|
+
}
|
|
291
|
+
if ((this.id & 1) === 0) {
|
|
292
|
+
throw errorOf('ERR_HTTP2_NESTED_PUSH', 'Nested push is not allowed');
|
|
293
|
+
}
|
|
294
|
+
if (typeof options === 'function') {
|
|
295
|
+
callback = options;
|
|
296
|
+
options = undefined;
|
|
297
|
+
}
|
|
298
|
+
if (options === undefined || options === null) {
|
|
299
|
+
options = {};
|
|
300
|
+
}
|
|
301
|
+
if (typeof options !== 'object' || Array.isArray(options)) {
|
|
302
|
+
const err = new TypeError('The "options" argument must be of type object');
|
|
303
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
304
|
+
throw err;
|
|
305
|
+
}
|
|
306
|
+
if (callback !== undefined && typeof callback !== 'function') {
|
|
307
|
+
const err = new TypeError('The "callback" argument must be of type function');
|
|
308
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
309
|
+
throw err;
|
|
310
|
+
}
|
|
311
|
+
const endStream = !!options.endStream;
|
|
312
|
+
|
|
313
|
+
// Defaults from the parent request (node: core.js pushStream fills :method GET,
|
|
314
|
+
// :authority and :scheme from the request, :path '/'). The headers object is copied
|
|
315
|
+
// into a null-proto map so the application's object is not mutated.
|
|
316
|
+
const req = this._requestHeaders || {};
|
|
317
|
+
const h = Object.assign(Object.create(null), headers);
|
|
318
|
+
if (h[':method'] === undefined) {
|
|
319
|
+
h[':method'] = 'GET';
|
|
320
|
+
}
|
|
321
|
+
if (h[':authority'] === undefined && req[':authority'] !== undefined) {
|
|
322
|
+
h[':authority'] = req[':authority'];
|
|
323
|
+
}
|
|
324
|
+
if (h[':scheme'] === undefined && req[':scheme'] !== undefined) {
|
|
325
|
+
h[':scheme'] = req[':scheme'];
|
|
326
|
+
}
|
|
327
|
+
if (h[':path'] === undefined) {
|
|
328
|
+
h[':path'] = '/';
|
|
329
|
+
}
|
|
330
|
+
const isHead = h[':method'] === 'HEAD';
|
|
331
|
+
|
|
332
|
+
// Allocate the promised id. Errors here are asynchronous (callback via nextTick),
|
|
333
|
+
// matching node's nghttp2_submit_push_promise error path.
|
|
334
|
+
let promisedId;
|
|
335
|
+
try {
|
|
336
|
+
promisedId = this.session._allocPushSid();
|
|
337
|
+
} catch (err) {
|
|
338
|
+
if (callback) {
|
|
339
|
+
process.nextTick(callback, err);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
throw err;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Validation errors are synchronous in node (buildNgHeaderString throws before
|
|
346
|
+
// the frame is submitted). The id was already reserved; it stays consumed.
|
|
347
|
+
const block = hdr.pushBlock(h, options.sensitiveHeaders, this.session);
|
|
348
|
+
this.session.sendPushPromise(this.id, promisedId, block);
|
|
349
|
+
|
|
350
|
+
// Create the pushed stream and register it. The readable side is pre-ended:
|
|
351
|
+
// remoteClose=1 means _endLocalSide() takes the graceful close path (no RST),
|
|
352
|
+
// and no inbound credit is ever charged for a request body that does not exist.
|
|
353
|
+
const stream = new Http2Stream(this.session, promisedId);
|
|
354
|
+
stream.sentHeaders = h;
|
|
355
|
+
stream._requestHeaders = h;
|
|
356
|
+
this.session._streams.set(promisedId, stream);
|
|
357
|
+
this.session._liveStreams++;
|
|
358
|
+
stream._remoteEnded();
|
|
359
|
+
|
|
360
|
+
if (endStream || isHead) {
|
|
361
|
+
stream.end();
|
|
362
|
+
}
|
|
363
|
+
if (callback) {
|
|
364
|
+
process.nextTick(callback, null, stream, h, 0);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
273
368
|
// Readable-side control, node's contract: while paused no credit is released,
|
|
274
369
|
// so the sender is the one that waits, and whatever arrives in the meantime is
|
|
275
370
|
// buffered and replayed on resume(). Returns this, like the Duplex does.
|
package/package.json
CHANGED