h2lean 0.2.6 → 0.3.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/lib/headers.js +42 -3
- package/lib/session.js +61 -3
- package/lib/stream.js +95 -0
- package/package.json +1 -1
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