http: writeHead if statusmessage is undefined dont override headers

PR-URL: https://github.com/nodejs/node/pull/46173
Fixes: https://github.com/nodejs/node/issues/32395
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Marco Ippolito 2023-01-17 10:05:53 +01:00 committed by GitHub
parent 3ef38c4bd7
commit 6e375b389b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -352,7 +352,7 @@ function writeHead(statusCode, reason, obj) {
// writeHead(statusCode[, headers])
if (!this.statusMessage)
this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
obj = reason;
obj ??= reason;
}
this.statusCode = statusCode;

View File

@ -59,3 +59,21 @@ const http = require('http');
}));
}));
}
{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, undefined, [ 'foo', 'bar' ]);
res.end();
}));
server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall((res) => {
assert.strictEqual(res.statusMessage, 'OK');
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.headers.foo, 'bar');
res.resume().on('end', common.mustCall(() => {
server.close();
}));
}));
}));
}