From b105997193374ce1802e91d51ae0fd6e479e86a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Bjo=CC=88rklund?= Date: Mon, 10 Feb 2014 19:56:09 +0100 Subject: [PATCH] http: avoid duplicate keys in writeHead Use setHeader in writeHead to avoid sending duplicate headers Fixes #5036 --- lib/_http_server.js | 7 ++--- test/simple/test-http-write-head.js | 46 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/simple/test-http-write-head.js diff --git a/lib/_http_server.js b/lib/_http_server.js index 4cb0c4b2f3..eab7d64ff4 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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 diff --git a/test/simple/test-http-write-head.js b/test/simple/test-http-write-head.js new file mode 100644 index 0000000000..2de59fe2ed --- /dev/null +++ b/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(); + }); +}