Skip to content

Commit

Permalink
http: avoid duplicate keys in writeHead
Browse files Browse the repository at this point in the history
Use setHeader in writeHead to avoid sending duplicate headers

Fixes #5036
  • Loading branch information
kesla authored and tjfontaine committed Feb 19, 2014
1 parent 845e5d3 commit b105997
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ ServerResponse.prototype.writeHead = function(statusCode) {
var obj = arguments[headerIndex];

if (obj && this._headers) {
// Slow-case: when progressive API and header fields are passed.
headers = this._renderHeaders();

if (util.isArray(obj)) {
// handle array case
// TODO: remove when array is no longer accepted
Expand All @@ -207,8 +204,10 @@ ServerResponse.prototype.writeHead = function(statusCode) {
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
if (k) headers[k] = obj[k];
if (k) this.setHeader(k, obj[k]);
}
// Slow-case: when progressive API and header fields are passed.
headers = this._renderHeaders();
}
} else if (this._headers) {
// only progressive api is used
Expand Down
46 changes: 46 additions & 0 deletions test/simple/test-http-write-head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var http = require('http');

// Verify that ServerResponse.writeHead() works as setHeader.
// Issue 5036 on github.

var s = http.createServer(function(req, res) {
res.setHeader('test', '1');
res.writeHead(200, { Test: '2' });
res.end();
});

s.listen(common.PORT, runTest);

function runTest() {
http.get({ port: common.PORT }, function(response) {
response.on('end', function() {
assert.equal(response.headers['test'], '2');
assert(response.rawHeaders.indexOf('Test') !== -1);
s.close();
});
response.resume();
});
}

0 comments on commit b105997

Please sign in to comment.