Browse Source

http: avoid duplicate keys in writeHead

Use setHeader in writeHead to avoid sending duplicate headers

Fixes #5036
v0.11.12-release
David Björklund 11 years ago
committed by Timothy J Fontaine
parent
commit
b105997193
  1. 7
      lib/_http_server.js
  2. 46
      test/simple/test-http-write-head.js

7
lib/_http_server.js

@ -187,9 +187,6 @@ ServerResponse.prototype.writeHead = function(statusCode) {
var obj = arguments[headerIndex]; var obj = arguments[headerIndex];
if (obj && this._headers) { if (obj && this._headers) {
// Slow-case: when progressive API and header fields are passed.
headers = this._renderHeaders();
if (util.isArray(obj)) { if (util.isArray(obj)) {
// handle array case // handle array case
// TODO: remove when array is no longer accepted // TODO: remove when array is no longer accepted
@ -207,8 +204,10 @@ ServerResponse.prototype.writeHead = function(statusCode) {
var keys = Object.keys(obj); var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
var k = keys[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) { } else if (this._headers) {
// only progressive api is used // only progressive api is used

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

@ -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();
});
}
Loading…
Cancel
Save