Skip to content

Commit

Permalink
http: writeHead if statusmessage is undefined dont override headers
Browse files Browse the repository at this point in the history
PR-URL: #46173
Fixes: #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>
  • Loading branch information
marco-ippolito authored and RafaelGSS committed Jan 20, 2023
1 parent 3f9fb37 commit ae5141c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,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;

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-http-write-head-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}));
}));
}

0 comments on commit ae5141c

Please sign in to comment.