Skip to content

Commit 2595fbc

Browse files
sagitsofanBridgeAR
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 <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent fc9ba36 commit 2595fbc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/internal/http2/compat.js

+25
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
} = constants;
4646

4747
let statusMessageWarned = false;
48+
let statusConnectionHeaderWarned = false;
4849

4950
// Defines and implements an API compatibility layer on top of the core
5051
// HTTP/2 implementation, intended to provide an interface that is as
@@ -58,6 +59,8 @@ function assertValidHeader(name, value) {
5859
err = new ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED();
5960
} else if (value === undefined || value === null) {
6061
err = new ERR_HTTP2_INVALID_HEADER_VALUE(value, name);
62+
} else if (!isConnectionHeaderAllowed(name, value)) {
63+
connectionHeaderMessageWarn();
6164
}
6265
if (err !== undefined) {
6366
Error.captureStackTrace(err, assertValidHeader);
@@ -88,6 +91,23 @@ function statusMessageWarn() {
8891
}
8992
}
9093

94+
function isConnectionHeaderAllowed(name, value) {
95+
return name !== constants.HTTP2_HEADER_CONNECTION ||
96+
value === 'trailers';
97+
}
98+
99+
function connectionHeaderMessageWarn() {
100+
if (statusConnectionHeaderWarned === false) {
101+
process.emitWarning(
102+
'The provided connection header is not valid, ' +
103+
'the value will be dropped from the header and ' +
104+
'will never be in use.',
105+
'UnsupportedWarning'
106+
);
107+
statusConnectionHeaderWarned = true;
108+
}
109+
}
110+
91111
function onStreamData(chunk) {
92112
const request = this[kRequest];
93113
if (request !== undefined && !request.push(chunk))
@@ -539,6 +559,11 @@ class Http2ServerResponse extends Stream {
539559
[kSetHeader](name, value) {
540560
name = name.trim().toLowerCase();
541561
assertValidHeader(name, value);
562+
563+
if (!isConnectionHeaderAllowed(name, value)) {
564+
return;
565+
}
566+
542567
this[kHeaders][name] = value;
543568
}
544569

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)