Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 979d0ca

Browse files
committed
http: cleanup setHeader()
Several fields on OutgoingMessage were set after instantiation. These have been included in the constructor to prevent mutation of the object map after instantiation. "name" is now explicitly checked to be a string. Where before if a non-string was passed the following cryptic error was thrown: _http_outgoing.js:334 var key = name.toLowerCase(); ^ TypeError: undefined is not a function Signed-off-by: Trevor Norris <[email protected]>
1 parent de312cf commit 979d0ca

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/_http_outgoing.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ function OutgoingMessage() {
8282

8383
this.finished = false;
8484
this._hangupClose = false;
85+
this._headerSent = false;
8586

8687
this.socket = null;
8788
this.connection = null;
89+
this._header = null;
90+
this._headers = null;
91+
this._headerNames = {};
8892
}
8993
util.inherits(OutgoingMessage, Stream);
9094

@@ -323,23 +327,22 @@ function storeHeader(self, state, field, value) {
323327

324328

325329
OutgoingMessage.prototype.setHeader = function(name, value) {
326-
if (arguments.length < 2) {
327-
throw new Error('`name` and `value` are required for setHeader().');
328-
}
329-
330-
if (this._header) {
330+
if (typeof name !== 'string')
331+
throw new TypeError('"name" should be a string');
332+
if (value === undefined)
333+
throw new Error('"name" and "value" are required for setHeader().');
334+
if (this._header)
331335
throw new Error('Can\'t set headers after they are sent.');
332-
}
336+
337+
if (this._headers === null)
338+
this._headers = {};
333339

334340
var key = name.toLowerCase();
335-
this._headers = this._headers || {};
336-
this._headerNames = this._headerNames || {};
337341
this._headers[key] = value;
338342
this._headerNames[key] = name;
339343

340-
if (automaticHeaders[key]) {
344+
if (automaticHeaders[key])
341345
this._removedHeader[key] = false;
342-
}
343346
};
344347

345348

@@ -387,6 +390,7 @@ OutgoingMessage.prototype._renderHeaders = function() {
387390

388391
var headers = {};
389392
var keys = Object.keys(this._headers);
393+
390394
for (var i = 0, l = keys.length; i < l; i++) {
391395
var key = keys[i];
392396
headers[this._headerNames[key]] = this._headers[key];

test/simple/test-http-write-head.js

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ var http = require('http');
2828

2929
var s = http.createServer(function(req, res) {
3030
res.setHeader('test', '1');
31+
32+
// toLowerCase() is used on the name argument, so it must be a string.
33+
var threw = false;
34+
try {
35+
res.setHeader(0xf00, 'bar');
36+
} catch (e) {
37+
assert.ok(e instanceof TypeError);
38+
threw = true;
39+
}
40+
assert.ok(threw, 'Non-string names should throw');
41+
3142
res.writeHead(200, { Test: '2' });
3243
res.end();
3344
});

0 commit comments

Comments
 (0)