h2lean 0.19.8 → 0.19.13
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 +12 -22
- package/index.js +7 -7
- package/lib/Http2Stream.js +13 -5
- package/lib/client/Http2ClientSession.js +388 -61
- package/lib/client/Http2ClientStream.js +135 -48
- package/lib/client/client.js +39 -11
- package/lib/headers.js +14 -7
- package/lib/server/Http2ServerSession.js +177 -42
- package/lib/server/Http2ServerStream.js +57 -12
- package/lib/server/compat.js +291 -72
- package/lib/server/server.js +86 -16
- package/package.json +1 -1
package/lib/server/compat.js
CHANGED
|
@@ -22,24 +22,17 @@
|
|
|
22
22
|
// reports 'finish' on both stacks.
|
|
23
23
|
// * the order of the teardown events differs: node reports the request's
|
|
24
24
|
// events (its 'end', its 'aborted', its 'close') before the response's
|
|
25
|
-
// ('finish', then 'close'), h2lean
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
25
|
+
// ('finish', then 'close'), h2lean can report the response's 'finish'
|
|
26
|
+
// first, at end() rather than at the stream close. Every event fires on
|
|
27
|
+
// both stacks -- measured against a live node server over the four shapes
|
|
28
|
+
// of bench/micro/probe-teardown-order.js, the sets are equal -- and only
|
|
29
|
+
// their order within the teardown turn differs. One pair is not a fixed
|
|
30
|
+
// difference at all: the request's 'end' against the 'aborted' depends on
|
|
31
|
+
// whether the peer's END_STREAM and its reset arrive in one read or in
|
|
32
|
+
// two, and the two stacks order that pair differently.
|
|
32
33
|
// * the body is pulled, not pushed: nothing is read off the wire until the
|
|
33
34
|
// application consumes it, so an unread upload applies backpressure to the
|
|
34
35
|
// sender (node does the same) instead of buffering here.
|
|
35
|
-
// * a stream 'error' reaches the compat request: onRequestError destroys req
|
|
36
|
-
// with it, so an application that listens only on res gets an unhandled
|
|
37
|
-
// 'error'. node's onStreamError (internal/http2/compat.js) is purposefully
|
|
38
|
-
// blank -- "errors in compatibility mode are not forwarded to the request
|
|
39
|
-
// and response objects". Measured with a content-length promise broken by a
|
|
40
|
-
// short body (content-length 10, two octets sent): node reports st-error
|
|
41
|
-
// then st-close:1 and the client its own error/close:1, h2lean reports
|
|
42
|
-
// st-close:1, st-error, then an uncaught ERR_HTTP2_STREAM_ERROR out of req.
|
|
43
36
|
// * with the server option shareHeaders:true, req.headers is frozen and shared
|
|
44
37
|
// between requests whose header block is identical, so req.url = ... and
|
|
45
38
|
// req.method = ... throw instead of rewriting the request.
|
|
@@ -54,11 +47,58 @@
|
|
|
54
47
|
|
|
55
48
|
const { Readable, Stream } = require('node:stream');
|
|
56
49
|
|
|
57
|
-
const { nullProto, trimName
|
|
50
|
+
const { nullProto, trimName } = require('../headers');
|
|
58
51
|
const { kCompatSocket } = require('../Http2Stream');
|
|
52
|
+
const { NO_ERROR } = require('../frames');
|
|
53
|
+
const { Http2ServerStream } = require('./Http2ServerStream');
|
|
54
|
+
|
|
55
|
+
// A peer reset and a teardown the application starts reach the stream's
|
|
56
|
+
// _destroy with different arguments (sendRst true for the latter; session
|
|
57
|
+
// destroy sets session.destroyed before it destroys its streams). node's
|
|
58
|
+
// request sees 'close' before the queued EOF only for the local teardown;
|
|
59
|
+
// a peer reset that is not an error has its EOF delivered first. Mark the
|
|
60
|
+
// local case at the point the two shapes are still distinct. One wrapper at
|
|
61
|
+
// module load, one boolean write on a stream that is being destroyed -- the
|
|
62
|
+
// per-request dispatch path stays untouched.
|
|
63
|
+
const originalStreamDestroy = Http2ServerStream.prototype._destroy;
|
|
64
|
+
Http2ServerStream.prototype._destroy = function compatTrackedDestroy(code, sendRst, error) {
|
|
65
|
+
const req = this._request;
|
|
66
|
+
if (req !== undefined && (sendRst ||
|
|
67
|
+
(this.session !== undefined && this.session.destroyed))) {
|
|
68
|
+
req._localTeardown = true;
|
|
69
|
+
}
|
|
70
|
+
return originalStreamDestroy.call(this, code, sendRst, error);
|
|
71
|
+
};
|
|
59
72
|
|
|
73
|
+
// The inbound trailers callback on node carries (trailers, flags, rawTrailers).
|
|
74
|
+
// The session hands the flat raw field list over on this private event first,
|
|
75
|
+
// because a stream's compat dispatch forwards one argument; a symbol keeps the
|
|
76
|
+
// event off the application-visible names. Symbol.for keeps the two modules in
|
|
77
|
+
// step without an extra require on the session's hot path.
|
|
78
|
+
const kCompatRawTrailers = Symbol.for('h2lean.compat.rawTrailers');
|
|
79
|
+
|
|
80
|
+
// node declares its validation errors with a class (internal/errors.js):
|
|
81
|
+
// header and argument validation is TypeError, status codes are RangeError,
|
|
82
|
+
// stream-state errors stay plain Error. ERR_HTTP2_HEADERS_SENT stays an Error
|
|
83
|
+
// here too -- h2lean's message differs from node's and that is written down,
|
|
84
|
+
// while the class the application catches is the same.
|
|
85
|
+
const TYPE_ERROR_CODES = new Set([
|
|
86
|
+
'ERR_INVALID_ARG_TYPE',
|
|
87
|
+
'ERR_INVALID_ARG_VALUE',
|
|
88
|
+
'ERR_INVALID_HTTP_TOKEN',
|
|
89
|
+
'ERR_HTTP2_INVALID_HEADER_VALUE',
|
|
90
|
+
'ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
91
|
+
'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'
|
|
92
|
+
]);
|
|
93
|
+
const RANGE_ERROR_CODES = new Set([
|
|
94
|
+
'ERR_HTTP2_STATUS_INVALID',
|
|
95
|
+
'ERR_HTTP2_INFO_STATUS_NOT_ALLOWED',
|
|
96
|
+
'ERR_HTTP2_INVALID_INFO_STATUS'
|
|
97
|
+
]);
|
|
60
98
|
function err(code, msg) {
|
|
61
|
-
const
|
|
99
|
+
const Ctor = RANGE_ERROR_CODES.has(code) ? RangeError
|
|
100
|
+
: TYPE_ERROR_CODES.has(code) ? TypeError : Error;
|
|
101
|
+
const e = new Ctor(msg);
|
|
62
102
|
e.code = code;
|
|
63
103
|
return e;
|
|
64
104
|
}
|
|
@@ -218,6 +258,69 @@ function checkName(name) {
|
|
|
218
258
|
}
|
|
219
259
|
}
|
|
220
260
|
|
|
261
|
+
// node's compat layer warns once per process for a status message, whether it
|
|
262
|
+
// was read, written or passed to writeHead (internal/http2/compat.js:75,112).
|
|
263
|
+
// The warning is an UnsupportedWarning with node's exact text; the round trip
|
|
264
|
+
// and the wire bytes are unaffected.
|
|
265
|
+
let statusMessageWarned = false;
|
|
266
|
+
function statusMessageWarn() {
|
|
267
|
+
if (!statusMessageWarned) {
|
|
268
|
+
process.emitWarning(
|
|
269
|
+
'Status message is not supported by HTTP/2 (RFC7540 8.1.2.4)',
|
|
270
|
+
'UnsupportedWarning');
|
|
271
|
+
statusMessageWarned = true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// node's one header check for every name/value accessor (internal/http2/compat.js:82).
|
|
276
|
+
// The five defined pseudo headers are refused here; an unknown ':' name is
|
|
277
|
+
// refused downstream by assertValidPseudoHeader as ERR_HTTP2_INVALID_PSEUDOHEADER.
|
|
278
|
+
// A token check is deliberately not added here: node's setHeader destroys the
|
|
279
|
+
// stream for a bad token rather than throwing, which is a different path.
|
|
280
|
+
const PSEUDO_HEADERS = new Set([':status', ':method', ':path', ':authority', ':scheme']);
|
|
281
|
+
function assertValidHeader(name, value) {
|
|
282
|
+
if (typeof name !== 'string' || name === '' || name.includes(' ')) {
|
|
283
|
+
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
284
|
+
}
|
|
285
|
+
if (PSEUDO_HEADERS.has(name)) {
|
|
286
|
+
throw err('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'Cannot set HTTP/2 pseudo-headers');
|
|
287
|
+
}
|
|
288
|
+
if (name.charCodeAt(0) === 58) { // ':'
|
|
289
|
+
throw err('ERR_HTTP2_INVALID_PSEUDOHEADER',
|
|
290
|
+
`"${name}" is an invalid pseudoheader or is used incorrectly`);
|
|
291
|
+
}
|
|
292
|
+
if (value === undefined || value === null) {
|
|
293
|
+
throw err('ERR_HTTP2_INVALID_HEADER_VALUE', `Invalid value "${value}" for header "${name}"`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// node's own token grammar (_http_common.js:216), not headers.js's isFieldName:
|
|
298
|
+
// that one writes to the bounded process-wide VALID_NAMES cache on every new
|
|
299
|
+
// name, which a per-header compat path must not grow.
|
|
300
|
+
const HTTP_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
301
|
+
|
|
302
|
+
// h2lean's timer is on the session, so arming both the request and the response
|
|
303
|
+
// leaves two once('timeout') wrappers on it; one emission calls both. node
|
|
304
|
+
// re-emits 'timeout' once on each compat object, so the first wrapper emits
|
|
305
|
+
// both events and the later ones run their callback only. The flag lives on
|
|
306
|
+
// the stream and is cleared in the next tick, the turn of one emission.
|
|
307
|
+
function clearCompatTimeoutEmitted(stream) {
|
|
308
|
+
stream._compatTimeoutEmitted = false;
|
|
309
|
+
}
|
|
310
|
+
function emitCompatTimeout(stream, req, res) {
|
|
311
|
+
if (stream._compatTimeoutEmitted === true) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
stream._compatTimeoutEmitted = true;
|
|
315
|
+
stream._tick(clearCompatTimeoutEmitted, stream);
|
|
316
|
+
if (req !== undefined) {
|
|
317
|
+
req.emit('timeout');
|
|
318
|
+
}
|
|
319
|
+
if (res !== undefined) {
|
|
320
|
+
res.emit('timeout');
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
221
324
|
// node's wording for a value of the wrong type (internal/errors.js,
|
|
222
325
|
// determineSpecificType): the message is what an application reads when it
|
|
223
326
|
// catches the error, so it says what node says.
|
|
@@ -233,6 +336,15 @@ function describeType(value) {
|
|
|
233
336
|
if (value === 0) {
|
|
234
337
|
return 1 / value === -Infinity ? 'type number (-0)' : 'type number (0)';
|
|
235
338
|
}
|
|
339
|
+
if (Number.isNaN(value)) {
|
|
340
|
+
return 'type number (NaN)';
|
|
341
|
+
}
|
|
342
|
+
if (value === Infinity) {
|
|
343
|
+
return 'type number (Infinity)';
|
|
344
|
+
}
|
|
345
|
+
if (value === -Infinity) {
|
|
346
|
+
return 'type number (-Infinity)';
|
|
347
|
+
}
|
|
236
348
|
return `type number (${value})`;
|
|
237
349
|
case 'bigint': return `type bigint (${value}n)`;
|
|
238
350
|
case 'boolean': return `type boolean (${value})`;
|
|
@@ -242,6 +354,12 @@ function describeType(value) {
|
|
|
242
354
|
const name = value.constructor?.name;
|
|
243
355
|
return typeof name === 'string' && name !== '' ? `an instance of ${name}` : 'an instance of Object';
|
|
244
356
|
}
|
|
357
|
+
case 'string': {
|
|
358
|
+
// determineSpecificType truncates long strings and quotes with the
|
|
359
|
+
// delimiter that does not occur in the value (internal/errors.js:1048).
|
|
360
|
+
const str = value.length > 28 ? `${value.slice(0, 25)}...` : value;
|
|
361
|
+
return str.includes("'") ? `type string (${JSON.stringify(str)})` : `type string ('${str}')`;
|
|
362
|
+
}
|
|
245
363
|
default: return `type ${typeof value}`;
|
|
246
364
|
}
|
|
247
365
|
}
|
|
@@ -287,12 +405,11 @@ function onRequestEnd() {
|
|
|
287
405
|
req.push(null);
|
|
288
406
|
}
|
|
289
407
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
408
|
+
// node's onStreamError (internal/http2/compat.js:160, v26.12.1) is purposefully
|
|
409
|
+
// blank: errors in compatibility mode are not forwarded to the request or the
|
|
410
|
+
// response. The subscription itself stays -- a stream the application never
|
|
411
|
+
// listens to must not throw on an unhandled 'error'.
|
|
412
|
+
function onRequestError() {}
|
|
296
413
|
|
|
297
414
|
function onRequestAborted() {
|
|
298
415
|
const req = this._request;
|
|
@@ -303,12 +420,29 @@ function onRequestAborted() {
|
|
|
303
420
|
req.emit('aborted');
|
|
304
421
|
}
|
|
305
422
|
|
|
423
|
+
function onRequestRawTrailers(raw) {
|
|
424
|
+
const req = this._request;
|
|
425
|
+
if (req !== undefined) {
|
|
426
|
+
this._rawTrailerFields = raw;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
306
430
|
function onRequestTrailers(trailers) {
|
|
307
431
|
const req = this._request;
|
|
308
432
|
if (req === undefined) {
|
|
309
433
|
return;
|
|
310
434
|
}
|
|
311
|
-
|
|
435
|
+
// node's onStreamTrailers (internal/http2/compat.js:145) merges into the
|
|
436
|
+
// request's own plain object and spreads the flat raw pair list, in wire
|
|
437
|
+
// order, into its own rawTrailers array.
|
|
438
|
+
Object.assign(req._trailers, trailers);
|
|
439
|
+
const raw = this._rawTrailerFields;
|
|
440
|
+
if (raw !== undefined) {
|
|
441
|
+
for (let i = 0; i < raw.length; i += 2) {
|
|
442
|
+
req._rawTrailers.push(raw[i], raw[i + 1]);
|
|
443
|
+
}
|
|
444
|
+
this._rawTrailerFields = undefined;
|
|
445
|
+
}
|
|
312
446
|
req.emit('trailers', trailers);
|
|
313
447
|
}
|
|
314
448
|
|
|
@@ -331,20 +465,28 @@ function onRequestClose() {
|
|
|
331
465
|
if (!req._didRead) {
|
|
332
466
|
req.resume();
|
|
333
467
|
}
|
|
334
|
-
// '
|
|
335
|
-
//
|
|
336
|
-
//
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
468
|
+
// node's onStreamCloseRequest (internal/http2/compat.js:282) emits 'close'
|
|
469
|
+
// in the same turn as the stream close, before the queued EOF reaches the
|
|
470
|
+
// reader, for a local teardown or a reset carrying a code. A peer's NO_ERROR
|
|
471
|
+
// reset still delivers the owed EOF first (measured on 24 and 26), so that
|
|
472
|
+
// shape keeps the deferred close and reports readableEnded true; using the
|
|
473
|
+
// stream's own reset code rather than whether EOF happened to be emitted
|
|
474
|
+
// keeps the rule the same when the two teardown paths share a turn.
|
|
475
|
+
if (req._localTeardown || this.rstCode !== NO_ERROR) {
|
|
476
|
+
req.emit('close');
|
|
477
|
+
} else {
|
|
478
|
+
req._tick(emitCloseNT, req);
|
|
479
|
+
}
|
|
342
480
|
}
|
|
343
481
|
|
|
344
482
|
function emitFinishNT(target) {
|
|
345
483
|
target.emit('finish');
|
|
346
484
|
}
|
|
347
485
|
|
|
486
|
+
function emitCloseNT(target) {
|
|
487
|
+
target.emit('close');
|
|
488
|
+
}
|
|
489
|
+
|
|
348
490
|
function onRequestTimeout() {
|
|
349
491
|
const req = this._request;
|
|
350
492
|
if (req !== undefined && !req._closed) {
|
|
@@ -369,6 +511,9 @@ function compatDispatch(name, arg) {
|
|
|
369
511
|
// closing it here would report the 'close' without the 'finish' and
|
|
370
512
|
// before the request's own 'close'.
|
|
371
513
|
break;
|
|
514
|
+
case kCompatRawTrailers:
|
|
515
|
+
onRequestRawTrailers.call(this, arg);
|
|
516
|
+
break;
|
|
372
517
|
case 'trailers':
|
|
373
518
|
onRequestTrailers.call(this, arg);
|
|
374
519
|
break;
|
|
@@ -410,7 +555,10 @@ class Http2ServerRequest extends Readable {
|
|
|
410
555
|
this._method = stream._reqMethod;
|
|
411
556
|
this._path = stream._reqPath;
|
|
412
557
|
this._rawHeaders = undefined;
|
|
413
|
-
|
|
558
|
+
// node's request constructor uses a plain object for trailers (compat.js:323),
|
|
559
|
+
// unlike the null-prototype header maps; the incoming case variants are
|
|
560
|
+
// merged into it, not made the request's map.
|
|
561
|
+
this._trailers = {};
|
|
414
562
|
this._rawTrailers = [];
|
|
415
563
|
this._closed = false;
|
|
416
564
|
this._ended = false;
|
|
@@ -504,11 +652,20 @@ class Http2ServerRequest extends Readable {
|
|
|
504
652
|
return this._headers !== undefined ? this._headers[':method'] : this._method;
|
|
505
653
|
}
|
|
506
654
|
set method(method) {
|
|
655
|
+
// node's setter runs validateString(method, 'method') and then
|
|
656
|
+
// ERR_INVALID_ARG_VALUE('method', method) (internal/http2/compat.js:411).
|
|
657
|
+
// Both are TypeError instances; the message text is measured off the
|
|
658
|
+
// running binary, not paraphrased.
|
|
507
659
|
if (typeof method !== 'string') {
|
|
508
|
-
|
|
660
|
+
const e = new TypeError(
|
|
661
|
+
`The "method" argument must be of type string. Received ${describeType(method)}`);
|
|
662
|
+
e.code = 'ERR_INVALID_ARG_TYPE';
|
|
663
|
+
throw e;
|
|
509
664
|
}
|
|
510
665
|
if (method.trim() === '') {
|
|
511
|
-
|
|
666
|
+
const e = new TypeError(`The argument 'method' is invalid. Received ${inspect(method)}`);
|
|
667
|
+
e.code = 'ERR_INVALID_ARG_VALUE';
|
|
668
|
+
throw e;
|
|
512
669
|
}
|
|
513
670
|
assertWritable(this._materialize());
|
|
514
671
|
this._headers[':method'] = method;
|
|
@@ -520,8 +677,9 @@ class Http2ServerRequest extends Readable {
|
|
|
520
677
|
if (auth !== undefined) {
|
|
521
678
|
return auth;
|
|
522
679
|
}
|
|
523
|
-
|
|
524
|
-
|
|
680
|
+
// node's getAuthority (internal/http2/util.js:974) returns headers.host
|
|
681
|
+
// unchanged when :authority is absent; a port is part of that value.
|
|
682
|
+
return h.host;
|
|
525
683
|
}
|
|
526
684
|
|
|
527
685
|
get scheme() {
|
|
@@ -537,8 +695,25 @@ class Http2ServerRequest extends Readable {
|
|
|
537
695
|
}
|
|
538
696
|
|
|
539
697
|
setTimeout(msecs, callback) {
|
|
540
|
-
if (!this._closed) {
|
|
541
|
-
|
|
698
|
+
if (!this._closed && !this._stream.destroyed) {
|
|
699
|
+
// node's setStreamTimeout (internal/stream_base_commons.js:246) returns
|
|
700
|
+
// the stream untouched once it is destroyed; a setTimeout after finish
|
|
701
|
+
// therefore arms nothing. The rest of the method mirrors its timer:
|
|
702
|
+
// stream timer and registers the callback as a once('timeout') listener;
|
|
703
|
+
// the compat request and response subscriptions (compat.js:303, :307)
|
|
704
|
+
// re-emit 'timeout' on both objects before that callback runs. h2lean's
|
|
705
|
+
// idle timer lives on the session, so re-emit here. One closure per
|
|
706
|
+
// setTimeout call, only on the opt-in path; the timer path itself is
|
|
707
|
+
// unchanged.
|
|
708
|
+
const req = this;
|
|
709
|
+
const stream = this._stream;
|
|
710
|
+
const res = stream._response;
|
|
711
|
+
stream.setTimeout(msecs, function onCompatTimeout() {
|
|
712
|
+
emitCompatTimeout(stream, req, res);
|
|
713
|
+
if (callback !== undefined) {
|
|
714
|
+
callback.apply(this, arguments);
|
|
715
|
+
}
|
|
716
|
+
});
|
|
542
717
|
}
|
|
543
718
|
return this;
|
|
544
719
|
}
|
|
@@ -594,6 +769,14 @@ function onResponseClose() {
|
|
|
594
769
|
if (res === undefined || res._state.closed) {
|
|
595
770
|
return;
|
|
596
771
|
}
|
|
772
|
+
// node's onStreamCloseResponse (internal/http2/compat.js:446) opens with
|
|
773
|
+
// `if (this.headRequest !== state.headRequest) return`, and state.headRequest
|
|
774
|
+
// is only set by end(). A HEAD answer closed before end() therefore reports
|
|
775
|
+
// neither 'finish' nor 'close' on node; end() calls this handler itself once
|
|
776
|
+
// the flag is flipped.
|
|
777
|
+
if (this.headRequest !== res._state.headRequest) {
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
597
780
|
res._state.closed = true;
|
|
598
781
|
res.writable = false;
|
|
599
782
|
// node emits 'finish' and 'close' together, when the stream closes
|
|
@@ -628,7 +811,10 @@ class Http2ServerResponse extends Stream {
|
|
|
628
811
|
finishEmitted: false,
|
|
629
812
|
sendDate: true,
|
|
630
813
|
statusCode: 200,
|
|
631
|
-
headRequest
|
|
814
|
+
// node's response state starts headRequest false and copies the stream's
|
|
815
|
+
// value in end() (compat.js:849); the stream itself closes HEAD answers
|
|
816
|
+
// at respond() time (Http2ServerStream.js:342-348).
|
|
817
|
+
headRequest: false
|
|
632
818
|
};
|
|
633
819
|
// A fast null-prototype map, not a { __proto__: null } literal: the
|
|
634
820
|
// response's headers are written once and read once per response, and the
|
|
@@ -699,21 +885,21 @@ class Http2ServerResponse extends Stream {
|
|
|
699
885
|
set statusCode(code) {
|
|
700
886
|
code |= 0;
|
|
701
887
|
if (code >= 100 && code < 200) {
|
|
702
|
-
throw err('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', '
|
|
888
|
+
throw err('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', 'Informational status codes cannot be used');
|
|
703
889
|
}
|
|
704
890
|
if (code < 100 || code > 599) {
|
|
705
|
-
throw err('ERR_HTTP2_STATUS_INVALID', `
|
|
891
|
+
throw err('ERR_HTTP2_STATUS_INVALID', `Invalid status code: ${code}`);
|
|
706
892
|
}
|
|
707
893
|
this._state.statusCode = code;
|
|
708
894
|
}
|
|
709
895
|
|
|
710
|
-
// HTTP/2 has no reason phrase; node warns and returns ''.
|
|
896
|
+
// HTTP/2 has no reason phrase; node warns once and returns ''.
|
|
711
897
|
get statusMessage() {
|
|
712
|
-
|
|
898
|
+
statusMessageWarn();
|
|
713
899
|
return '';
|
|
714
900
|
}
|
|
715
901
|
set statusMessage(msg) {
|
|
716
|
-
|
|
902
|
+
statusMessageWarn();
|
|
717
903
|
}
|
|
718
904
|
|
|
719
905
|
get sendDate() {
|
|
@@ -770,7 +956,9 @@ class Http2ServerResponse extends Stream {
|
|
|
770
956
|
// validate the name either (internal/http2/compat.js), so a name that is not
|
|
771
957
|
// a string fails here the way it fails there: name.trim() is not a function.
|
|
772
958
|
_setHeader(name, value) {
|
|
773
|
-
|
|
959
|
+
const key = trimName(name);
|
|
960
|
+
assertValidHeader(key, value);
|
|
961
|
+
this._headers[key] = value;
|
|
774
962
|
}
|
|
775
963
|
|
|
776
964
|
appendHeader(name, value) {
|
|
@@ -779,6 +967,7 @@ class Http2ServerResponse extends Stream {
|
|
|
779
967
|
throw err('ERR_HTTP2_HEADERS_SENT', 'response has already been sent');
|
|
780
968
|
}
|
|
781
969
|
const key = trimName(name);
|
|
970
|
+
assertValidHeader(key, value);
|
|
782
971
|
const current = this._headers[key];
|
|
783
972
|
if (current === undefined) {
|
|
784
973
|
this._headers[key] = value;
|
|
@@ -797,10 +986,12 @@ class Http2ServerResponse extends Stream {
|
|
|
797
986
|
// fast path must recall its frame so the same shape holds here (item 44); after
|
|
798
987
|
// the flush the frame is gone and the trailer is lost on node too, its tick
|
|
799
988
|
// having already answered the empty block.
|
|
989
|
+
const key = trimName(name);
|
|
990
|
+
assertValidHeader(key, value);
|
|
800
991
|
if (this._stream._esStaged) {
|
|
801
992
|
this._stream._recallStagedEnd();
|
|
802
993
|
}
|
|
803
|
-
this._trailers[
|
|
994
|
+
this._trailers[key] = value;
|
|
804
995
|
}
|
|
805
996
|
|
|
806
997
|
addTrailers(headers) {
|
|
@@ -818,23 +1009,33 @@ class Http2ServerResponse extends Stream {
|
|
|
818
1009
|
throw err('ERR_HTTP2_HEADERS_SENT', 'response has already been sent');
|
|
819
1010
|
}
|
|
820
1011
|
if (typeof statusMessage === 'string') {
|
|
821
|
-
|
|
1012
|
+
statusMessageWarn();
|
|
822
1013
|
}
|
|
823
1014
|
if (headers === undefined && typeof statusMessage === 'object') {
|
|
824
1015
|
headers = statusMessage;
|
|
825
1016
|
}
|
|
826
1017
|
|
|
827
1018
|
if (Array.isArray(headers)) {
|
|
1019
|
+
// node removes every incoming name first, then appends each pair, so a
|
|
1020
|
+
// repeated name in the array is two field lines on the wire rather than
|
|
1021
|
+
// one overwritten value (internal/http2/compat.js:735-770). The object
|
|
1022
|
+
// branch below still replaces through _setHeader().
|
|
828
1023
|
if (headers.length && Array.isArray(headers[0])) {
|
|
829
1024
|
for (let i = 0; i < headers.length; i++) {
|
|
830
|
-
this.
|
|
1025
|
+
this.removeHeader(headers[i][0]);
|
|
1026
|
+
}
|
|
1027
|
+
for (let i = 0; i < headers.length; i++) {
|
|
1028
|
+
this.appendHeader(headers[i][0], headers[i][1]);
|
|
831
1029
|
}
|
|
832
1030
|
} else {
|
|
833
1031
|
if (headers.length % 2 !== 0) {
|
|
834
1032
|
throw err('ERR_INVALID_ARG_VALUE', 'headers array must be flat key/value pairs');
|
|
835
1033
|
}
|
|
836
1034
|
for (let i = 0; i < headers.length; i += 2) {
|
|
837
|
-
this.
|
|
1035
|
+
this.removeHeader(headers[i]);
|
|
1036
|
+
}
|
|
1037
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
1038
|
+
this.appendHeader(headers[i], headers[i + 1]);
|
|
838
1039
|
}
|
|
839
1040
|
}
|
|
840
1041
|
} else if (typeof headers === 'object' && headers !== null) {
|
|
@@ -868,9 +1069,10 @@ class Http2ServerResponse extends Stream {
|
|
|
868
1069
|
// Http2ServerResponse has no sentHeaders, so `res.sentHeaders` is undefined
|
|
869
1070
|
// on both stacks.
|
|
870
1071
|
stream.respond(this._headers, {
|
|
871
|
-
//
|
|
872
|
-
//
|
|
873
|
-
|
|
1072
|
+
// node's compat [kBeginSend] passes only its ending flag here
|
|
1073
|
+
// (internal/http2/compat.js:903): the stream closes a HEAD answer on
|
|
1074
|
+
// its own, so no extra END_STREAM option is needed.
|
|
1075
|
+
endStream: state.ending,
|
|
874
1076
|
sendDate: state.sendDate,
|
|
875
1077
|
waitForTrailers: true
|
|
876
1078
|
});
|
|
@@ -900,7 +1102,7 @@ class Http2ServerResponse extends Stream {
|
|
|
900
1102
|
// means "switch protocol", which HTTP/2 forbids outright (RFC 9113 8.5).
|
|
901
1103
|
if (typeof statusCode !== 'number' || statusCode < 100 || statusCode > 199
|
|
902
1104
|
|| statusCode === 101) {
|
|
903
|
-
throw err('ERR_HTTP2_STATUS_INVALID', `
|
|
1105
|
+
throw err('ERR_HTTP2_STATUS_INVALID', `Invalid status code: ${statusCode}`);
|
|
904
1106
|
}
|
|
905
1107
|
if (this._stream.headersSent || this._state.closed) {
|
|
906
1108
|
return false;
|
|
@@ -942,18 +1144,11 @@ class Http2ServerResponse extends Stream {
|
|
|
942
1144
|
continue;
|
|
943
1145
|
}
|
|
944
1146
|
const name = key.trim().toLowerCase();
|
|
945
|
-
if (name === '' || name.includes(' ')) {
|
|
946
|
-
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
947
|
-
}
|
|
948
|
-
if (name.charCodeAt(0) === 58) { // ':'
|
|
949
|
-
throw err('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', 'Cannot set HTTP/2 pseudo-headers');
|
|
950
|
-
}
|
|
951
1147
|
const value = hints[key];
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
if (!isFieldName(name)) {
|
|
1148
|
+
assertValidHeader(name, value);
|
|
1149
|
+
// writeEarlyHints is the one compat path that throws on a bad token
|
|
1150
|
+
// instead of destroying the stream (internal/http2/compat.js:955).
|
|
1151
|
+
if (!HTTP_TOKEN.test(name)) {
|
|
957
1152
|
throw err('ERR_INVALID_HTTP_TOKEN', `Header name must be a valid HTTP token ["${name}"]`);
|
|
958
1153
|
}
|
|
959
1154
|
headers[name] = value;
|
|
@@ -970,6 +1165,7 @@ class Http2ServerResponse extends Stream {
|
|
|
970
1165
|
cb = encoding; encoding = 'utf8';
|
|
971
1166
|
}
|
|
972
1167
|
const state = this._state;
|
|
1168
|
+
const stream = this._stream;
|
|
973
1169
|
let failure;
|
|
974
1170
|
if (state.ending) {
|
|
975
1171
|
failure = err('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
|
@@ -985,16 +1181,16 @@ class Http2ServerResponse extends Stream {
|
|
|
985
1181
|
this.destroy(failure);
|
|
986
1182
|
return false;
|
|
987
1183
|
}
|
|
988
|
-
if (
|
|
1184
|
+
if (stream.headRequest) {
|
|
989
1185
|
if (typeof cb === 'function') {
|
|
990
1186
|
this._tick(cb);
|
|
991
1187
|
}
|
|
992
1188
|
return true; // a HEAD response has no body
|
|
993
1189
|
}
|
|
994
|
-
if (!
|
|
1190
|
+
if (!stream.headersSent) {
|
|
995
1191
|
this.writeHead(state.statusCode);
|
|
996
1192
|
}
|
|
997
|
-
return
|
|
1193
|
+
return stream.write(chunk, encoding, cb);
|
|
998
1194
|
}
|
|
999
1195
|
|
|
1000
1196
|
end(chunk, encoding, cb) {
|
|
@@ -1006,15 +1202,19 @@ class Http2ServerResponse extends Stream {
|
|
|
1006
1202
|
cb = encoding; encoding = 'utf8';
|
|
1007
1203
|
}
|
|
1008
1204
|
|
|
1009
|
-
|
|
1205
|
+
// node's end() guard (internal/http2/compat.js:838) also compares the two
|
|
1206
|
+
// headRequest flags: a HEAD whose stream closed before end() must still run
|
|
1207
|
+
// this call so the deferred finish/close can be reported.
|
|
1208
|
+
if ((state.closed || state.ending) && state.headRequest === stream.headRequest) {
|
|
1010
1209
|
if (typeof cb === 'function') {
|
|
1011
1210
|
this._tick(cb);
|
|
1012
1211
|
}
|
|
1013
1212
|
return this;
|
|
1014
1213
|
}
|
|
1015
|
-
if (chunk !== null && chunk !== undefined && !
|
|
1214
|
+
if (chunk !== null && chunk !== undefined && !stream.headRequest) {
|
|
1016
1215
|
this.write(chunk, encoding);
|
|
1017
1216
|
}
|
|
1217
|
+
state.headRequest = stream.headRequest;
|
|
1018
1218
|
state.ending = true;
|
|
1019
1219
|
|
|
1020
1220
|
if (!stream.headersSent) {
|
|
@@ -1056,6 +1256,13 @@ class Http2ServerResponse extends Stream {
|
|
|
1056
1256
|
// staging the frame twice. A body parked on window credit, a refused block and a
|
|
1057
1257
|
// HEAD-request close keep the deferred path -- the staged frame would land before
|
|
1058
1258
|
// the queued DATA, and the refused shape records the end without wire work.
|
|
1259
|
+
if (state.closed || stream.destroyed || stream.closed) {
|
|
1260
|
+
// node's end() calls onStreamCloseResponse itself when the stream already
|
|
1261
|
+
// closed (compat.js:862); the stream's close ran while the HEAD guard
|
|
1262
|
+
// still failed, so nothing reported finish/close until this call.
|
|
1263
|
+
onResponseClose.call(stream);
|
|
1264
|
+
return this;
|
|
1265
|
+
}
|
|
1059
1266
|
if (!recorded && stream._waitForTrailers && stream._queued === null &&
|
|
1060
1267
|
!stream._blockRefused) {
|
|
1061
1268
|
stream.endWithStagedEnd();
|
|
@@ -1096,10 +1303,22 @@ class Http2ServerResponse extends Stream {
|
|
|
1096
1303
|
}
|
|
1097
1304
|
|
|
1098
1305
|
setTimeout(msecs, callback) {
|
|
1099
|
-
if (this._state.closed) {
|
|
1306
|
+
if (this._state.closed || this._stream.destroyed) {
|
|
1100
1307
|
return this;
|
|
1101
1308
|
}
|
|
1102
|
-
|
|
1309
|
+
// node attaches the callback as a 'timeout' listener on the stream, and
|
|
1310
|
+
// the compat response re-emits the stream event as its own 'timeout'
|
|
1311
|
+
// (internal/http2/compat.js:435,878). Mirror both here; the shared helper
|
|
1312
|
+
// keeps the single event when the request armed the same timer.
|
|
1313
|
+
const res = this;
|
|
1314
|
+
const stream = this._stream;
|
|
1315
|
+
const req = this.req;
|
|
1316
|
+
stream.setTimeout(msecs, function onCompatResponseTimeout() {
|
|
1317
|
+
emitCompatTimeout(stream, req, res);
|
|
1318
|
+
if (callback !== undefined) {
|
|
1319
|
+
callback.apply(this, arguments);
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1103
1322
|
return this;
|
|
1104
1323
|
}
|
|
1105
1324
|
|