提交 fb08e956 编写于 作者: T Tyler Brown 提交者: Rubén Norte

Duplicate header handling (#874)

* Update parseHeaders to match node http behavior

Node ignores duplicate entries for certain HTTP headers.

It also always converts the `set-cookie` header into an array.

* add tests for new duplicate header handling

* clarify comment
上级 2b856269
......@@ -2,6 +2,15 @@
var utils = require('./../utils');
// Headers whose duplicates are ignored by node
// c.f. https://nodejs.org/api/http.html#http_message_headers
var ignoreDuplicateOf = [
'age', 'authorization', 'content-length', 'content-type', 'etag',
'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
'last-modified', 'location', 'max-forwards', 'proxy-authorization',
'referer', 'retry-after', 'user-agent'
];
/**
* Parse headers into an object
*
......@@ -29,7 +38,14 @@ module.exports = function parseHeaders(headers) {
val = utils.trim(line.substr(i + 1));
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
if (parsed[key] && ignoreDuplicateOf.indexOf(key) >= 0) {
return;
}
if (key === 'set-cookie') {
parsed[key] = (parsed[key] ? parsed[key] : []).concat([val]);
} else {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
}
});
......
......@@ -15,5 +15,31 @@ describe('helpers::parseHeaders', function () {
expect(parsed['connection']).toEqual('keep-alive');
expect(parsed['transfer-encoding']).toEqual('chunked');
});
});
it('should use array for set-cookie', function() {
var parsedZero = parseHeaders('');
var parsedSingle = parseHeaders(
'Set-Cookie: key=val;'
);
var parsedMulti = parseHeaders(
'Set-Cookie: key=val;\n' +
'Set-Cookie: key2=val2;\n'
);
expect(parsedZero['set-cookie']).toBeUndefined();
expect(parsedSingle['set-cookie']).toEqual(['key=val;']);
expect(parsedMulti['set-cookie']).toEqual(['key=val;', 'key2=val2;']);
});
it('should handle duplicates', function() {
var parsed = parseHeaders(
'Age: age-a\n' + // age is in ignore duplicates blacklist
'Age: age-b\n' +
'Foo: foo-a\n' +
'Foo: foo-b\n'
);
expect(parsed['age']).toEqual('age-a');
expect(parsed['foo']).toEqual('foo-a, foo-b');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册