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: https://github.com/nodejs/node/pull/23908 Fixes: https://github.com/nodejs/node/issues/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>
This commit is contained in:
parent
5e23bac9dd
commit
8c597df350
@ -45,6 +45,7 @@ const {
|
|||||||
} = constants;
|
} = constants;
|
||||||
|
|
||||||
let statusMessageWarned = false;
|
let statusMessageWarned = false;
|
||||||
|
let statusConnectionHeaderWarned = false;
|
||||||
|
|
||||||
// Defines and implements an API compatibility layer on top of the core
|
// Defines and implements an API compatibility layer on top of the core
|
||||||
// HTTP/2 implementation, intended to provide an interface that is as
|
// HTTP/2 implementation, intended to provide an interface that is as
|
||||||
@ -58,6 +59,8 @@ function assertValidHeader(name, value) {
|
|||||||
err = new ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED();
|
err = new ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED();
|
||||||
} else if (value === undefined || value === null) {
|
} else if (value === undefined || value === null) {
|
||||||
err = new ERR_HTTP2_INVALID_HEADER_VALUE(value, name);
|
err = new ERR_HTTP2_INVALID_HEADER_VALUE(value, name);
|
||||||
|
} else if (!isConnectionHeaderAllowed(name, value)) {
|
||||||
|
connectionHeaderMessageWarn();
|
||||||
}
|
}
|
||||||
if (err !== undefined) {
|
if (err !== undefined) {
|
||||||
Error.captureStackTrace(err, assertValidHeader);
|
Error.captureStackTrace(err, assertValidHeader);
|
||||||
@ -88,6 +91,23 @@ function statusMessageWarn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isConnectionHeaderAllowed(name, value) {
|
||||||
|
return name !== constants.HTTP2_HEADER_CONNECTION ||
|
||||||
|
value === 'trailers';
|
||||||
|
}
|
||||||
|
|
||||||
|
function connectionHeaderMessageWarn() {
|
||||||
|
if (statusConnectionHeaderWarned === false) {
|
||||||
|
process.emitWarning(
|
||||||
|
'The provided connection header is not valid, ' +
|
||||||
|
'the value will be dropped from the header and ' +
|
||||||
|
'will never be in use.',
|
||||||
|
'UnsupportedWarning'
|
||||||
|
);
|
||||||
|
statusConnectionHeaderWarned = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function onStreamData(chunk) {
|
function onStreamData(chunk) {
|
||||||
const request = this[kRequest];
|
const request = this[kRequest];
|
||||||
if (request !== undefined && !request.push(chunk))
|
if (request !== undefined && !request.push(chunk))
|
||||||
@ -539,6 +559,11 @@ class Http2ServerResponse extends Stream {
|
|||||||
[kSetHeader](name, value) {
|
[kSetHeader](name, value) {
|
||||||
name = name.trim().toLowerCase();
|
name = name.trim().toLowerCase();
|
||||||
assertValidHeader(name, value);
|
assertValidHeader(name, value);
|
||||||
|
|
||||||
|
if (!isConnectionHeaderAllowed(name, value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this[kHeaders][name] = value;
|
this[kHeaders][name] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ const body =
|
|||||||
const server = http2.createServer((req, res) => {
|
const server = http2.createServer((req, res) => {
|
||||||
res.setHeader('foobar', 'baz');
|
res.setHeader('foobar', 'baz');
|
||||||
res.setHeader('X-POWERED-BY', 'node-test');
|
res.setHeader('X-POWERED-BY', 'node-test');
|
||||||
|
res.setHeader('connection', 'connection-test');
|
||||||
res.end(body);
|
res.end(body);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -34,4 +35,10 @@ server.listen(0, common.mustCall(() => {
|
|||||||
req.end();
|
req.end();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const compatMsg = 'The provided connection header is not valid, ' +
|
||||||
|
'the value will be dropped from the header and ' +
|
||||||
|
'will never be in use.';
|
||||||
|
|
||||||
|
common.expectWarning('UnsupportedWarning', compatMsg);
|
||||||
|
|
||||||
server.on('error', common.mustNotCall());
|
server.on('error', common.mustNotCall());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user