http: avoid duplicate keys in writeHead

Use setHeader in writeHead to avoid sending duplicate headers

Fixes #5036
This commit is contained in:
David Björklund 2014-02-10 19:56:09 +01:00 committed by Timothy J Fontaine
parent 845e5d3458
commit b105997193
2 changed files with 49 additions and 4 deletions

View File

@ -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
@ -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

View File

@ -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();
});
}