Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: http server multi same-named headers support #6865

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ OutgoingMessage.prototype.setHeader = function(name, value) {
if (this._headers === null)
this._headers = {};

var key = name.toLowerCase();
var key = name.toLowerCase(), serial = 0, origKey = key;
while (this._headers[key] !== undefined) {
key = (origKey + serial++);
}
this._headers[key] = value;
this._headerNames[key] = name;

Expand Down Expand Up @@ -408,7 +411,18 @@ OutgoingMessage.prototype._renderHeaders = function() {

for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
headers[headerNames[key]] = headersMap[key];
if (headers[headerNames[key]] !== undefined) {
var isArray = Array.isArray(headers[headerNames[key]]);
if (isArray) {
headers[headerNames[key]].push(headersMap[key]);
} else {
var orig = headers[headerNames[key]];
headers[headerNames[key]] = [];
headers[headerNames[key]].push(orig);
}
} else {
headers[headerNames[key]] = headersMap[key];
}
}
return headers;
};
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-mutable-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function nextTest() {
break;

case 'writeHead':
assert.equal(response.headers['x-foo'], 'bar');
assert.equal(response.headers['x-foo'], 'keyboard cat');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn’t the output here be keyboard cat, bar? For incoming http messages node glues the returned header values together with ', '.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm ... I'll take a look at writeHead function, thanks.

assert.equal(response.headers['x-bar'], 'baz');
assert.equal(200, response.statusCode);
test = 'end';
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-write-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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.equal(response.headers['test'], '1, 2');
assert(response.rawHeaders.indexOf('Test') !== -1);
s.close();
});
Expand Down