From ae5141cb8ac12b0c7e21236c34109b686ff8cc96 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 17 Jan 2023 10:05:53 +0100 Subject: [PATCH] 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 Reviewed-By: Paolo Insogna Reviewed-By: James M Snell Reviewed-By: Matteo Collina --- lib/_http_server.js | 2 +- test/parallel/test-http-write-head-2.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 52e25977769d31..bf1cd91b288491 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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; diff --git a/test/parallel/test-http-write-head-2.js b/test/parallel/test-http-write-head-2.js index a47d0d72e3be56..d64b8259f01215 100644 --- a/test/parallel/test-http-write-head-2.js +++ b/test/parallel/test-http-write-head-2.js @@ -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(); + })); + })); + })); +}