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

Commit

Permalink
http: cleanup setHeader()
Browse files Browse the repository at this point in the history
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 <trev.norris@gmail.com>
  • Loading branch information
trevnorris committed Sep 29, 2014
1 parent de312cf commit 979d0ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ function OutgoingMessage() {

this.finished = false;
this._hangupClose = false;
this._headerSent = false;

this.socket = null;
this.connection = null;
this._header = null;
this._headers = null;
this._headerNames = {};
}
util.inherits(OutgoingMessage, Stream);

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


OutgoingMessage.prototype.setHeader = function(name, value) {
if (arguments.length < 2) {
throw new Error('`name` and `value` are required for setHeader().');
}

if (this._header) {
if (typeof name !== 'string')
throw new TypeError('"name" should be a string');
if (value === undefined)
throw new Error('"name" and "value" are required for setHeader().');
if (this._header)
throw new Error('Can\'t set headers after they are sent.');
}

if (this._headers === null)
this._headers = {};

var key = name.toLowerCase();
this._headers = this._headers || {};
this._headerNames = this._headerNames || {};
this._headers[key] = value;
this._headerNames[key] = name;

if (automaticHeaders[key]) {
if (automaticHeaders[key])
this._removedHeader[key] = false;
}
};


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

var headers = {};
var keys = Object.keys(this._headers);

for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
headers[this._headerNames[key]] = this._headers[key];
Expand Down
11 changes: 11 additions & 0 deletions test/simple/test-http-write-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ var http = require('http');

var s = http.createServer(function(req, res) {
res.setHeader('test', '1');

// toLowerCase() is used on the name argument, so it must be a string.
var threw = false;
try {
res.setHeader(0xf00, 'bar');
} catch (e) {
assert.ok(e instanceof TypeError);
threw = true;
}
assert.ok(threw, 'Non-string names should throw');

res.writeHead(200, { Test: '2' });
res.end();
});
Expand Down

0 comments on commit 979d0ca

Please sign in to comment.