Skip to content

Commit cc25f22

Browse files
sagitsofanBethGriggs
authored andcommitted
http2: improve compatibility with http/1
When using the compatibility API the connection header is from now on ignored instead of throwing an `ERR_HTTP2_INVALID_CONNECTION_HEADERS` error. This logs a warning in such case to notify the user about the ignored header. PR-URL: #23908 Fixes: #23748 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 281eb0f commit cc25f22

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: lib/internal/http2/compat.js

+25
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const {
4747
} = constants;
4848

4949
let statusMessageWarned = false;
50+
let statusConnectionHeaderWarned = false;
5051

5152
// Defines and implements an API compatibility layer on top of the core
5253
// HTTP/2 implementation, intended to provide an interface that is as
@@ -60,6 +61,8 @@ function assertValidHeader(name, value) {
6061
err = new ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED();
6162
} else if (value === undefined || value === null) {
6263
err = new ERR_HTTP2_INVALID_HEADER_VALUE(value, name);
64+
} else if (!isConnectionHeaderAllowed(name, value)) {
65+
connectionHeaderMessageWarn();
6366
}
6467
if (err !== undefined) {
6568
Error.captureStackTrace(err, assertValidHeader);
@@ -90,6 +93,23 @@ function statusMessageWarn() {
9093
}
9194
}
9295

96+
function isConnectionHeaderAllowed(name, value) {
97+
return name !== constants.HTTP2_HEADER_CONNECTION ||
98+
value === 'trailers';
99+
}
100+
101+
function connectionHeaderMessageWarn() {
102+
if (statusConnectionHeaderWarned === false) {
103+
process.emitWarning(
104+
'The provided connection header is not valid, ' +
105+
'the value will be dropped from the header and ' +
106+
'will never be in use.',
107+
'UnsupportedWarning'
108+
);
109+
statusConnectionHeaderWarned = true;
110+
}
111+
}
112+
93113
function onStreamData(chunk) {
94114
const request = this[kRequest];
95115
if (request !== undefined && !request.push(chunk))
@@ -541,6 +561,11 @@ class Http2ServerResponse extends Stream {
541561
[kSetHeader](name, value) {
542562
name = name.trim().toLowerCase();
543563
assertValidHeader(name, value);
564+
565+
if (!isConnectionHeaderAllowed(name, value)) {
566+
return;
567+
}
568+
544569
this[kHeaders][name] = value;
545570
}
546571

Diff for: test/parallel/test-http2-server-set-header.js

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const body =
1111
const server = http2.createServer((req, res) => {
1212
res.setHeader('foobar', 'baz');
1313
res.setHeader('X-POWERED-BY', 'node-test');
14+
res.setHeader('connection', 'connection-test');
1415
res.end(body);
1516
});
1617

@@ -34,4 +35,10 @@ server.listen(0, common.mustCall(() => {
3435
req.end();
3536
}));
3637

38+
const compatMsg = 'The provided connection header is not valid, ' +
39+
'the value will be dropped from the header and ' +
40+
'will never be in use.';
41+
42+
common.expectWarning('UnsupportedWarning', compatMsg);
43+
3744
server.on('error', common.mustNotCall());

0 commit comments

Comments
 (0)