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
  • Loading branch information
marco-ippolito committed Jan 11, 2023
1 parent f6e402e commit 60f9642
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/_http_server.js
Expand Up @@ -331,7 +331,7 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() {
};

ServerResponse.prototype.writeHead = writeHead;
function writeHead(statusCode, reason, obj) {
function writeHead(statusCode, statusMessage, headers) {

if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('write');
Expand All @@ -345,47 +345,50 @@ function writeHead(statusCode, reason, obj) {
}


if (typeof reason === 'string') {
if (typeof statusMessage === 'string') {
// writeHead(statusCode, reasonPhrase[, headers])
this.statusMessage = reason;
this.statusMessage = statusMessage;
} else {
// writeHead(statusCode[, headers])
if (!this.statusMessage)
if (!this.statusMessage) {
this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
obj = reason;
}
if (!headers) {
headers = statusMessage;
}
}
this.statusCode = statusCode;

let headers;
let _headers;
if (this[kOutHeaders]) {
// Slow-case: when progressive API and header fields are passed.
let k;
if (ArrayIsArray(obj)) {
if (obj.length % 2 !== 0) {
throw new ERR_INVALID_ARG_VALUE('headers', obj);
if (ArrayIsArray(headers)) {
if (headers.length % 2 !== 0) {
throw new ERR_INVALID_ARG_VALUE('headers', headers);
}

for (let n = 0; n < obj.length; n += 2) {
k = obj[n + 0];
if (k) this.setHeader(k, obj[n + 1]);
for (let n = 0; n < headers.length; n += 2) {
k = headers[n + 0];
if (k) this.setHeader(k, headers[n + 1]);
}
} else if (obj) {
const keys = ObjectKeys(obj);
} else if (headers) {
const keys = ObjectKeys(headers);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
for (let i = 0; i < keys.length; i++) {
k = keys[i];
if (k) this.setHeader(k, obj[k]);
if (k) this.setHeader(k, headers[k]);
}
}
if (k === undefined && this._header) {
throw new ERR_HTTP_HEADERS_SENT('render');
}
// Only progressive api is used
headers = this[kOutHeaders];
_headers = this[kOutHeaders];
} else {
// Only writeHead() called
headers = obj;
_headers = headers;
}

if (checkInvalidHeaderChar(this.statusMessage))
Expand Down Expand Up @@ -414,7 +417,7 @@ function writeHead(statusCode, reason, obj) {
this.shouldKeepAlive = false;
}

this._storeHeader(statusLine, headers);
this._storeHeader(statusLine, _headers);

return this;
}
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-http-write-head-2.js
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 60f9642

Please sign in to comment.