http2: pass incoming set-cookie header as array

Incoming set-cookie headers should be passed to user as array like in
http module.

Besides improving compatibility between http and http2 it avoids the
need to check if the type is an array or not in user code.

PR-URL: https://github.com/nodejs/node/pull/21360
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Gerhard Stoebich 2018-06-15 23:37:46 +02:00 committed by Matteo Collina
parent e021ca28ea
commit d4966a15cb
3 changed files with 44 additions and 10 deletions

View File

@ -2130,8 +2130,7 @@ For incoming headers:
`proxy-authorization`, `range`, `referer`,`retry-after`, `tk`, `proxy-authorization`, `range`, `referer`,`retry-after`, `tk`,
`upgrade-insecure-requests`, `user-agent` or `x-content-type-options` are `upgrade-insecure-requests`, `user-agent` or `x-content-type-options` are
discarded. discarded.
* `set-cookie` is a string if present once or an array in case duplicates * `set-cookie` is always an array. Duplicates are added to the array.
are present.
* `cookie`: the values are joined together with '; '. * `cookie`: the values are joined together with '; '.
* For all other headers, the values are joined together with ', '. * For all other headers, the values are joined together with ', '.

View File

@ -509,7 +509,7 @@ function toHeaderObject(headers) {
value |= 0; value |= 0;
var existing = obj[name]; var existing = obj[name];
if (existing === undefined) { if (existing === undefined) {
obj[name] = value; obj[name] = name === HTTP2_HEADER_SET_COOKIE ? [value] : value;
} else if (!kSingleValueHeaders.has(name)) { } else if (!kSingleValueHeaders.has(name)) {
switch (name) { switch (name) {
case HTTP2_HEADER_COOKIE: case HTTP2_HEADER_COOKIE:
@ -528,10 +528,7 @@ function toHeaderObject(headers) {
// fields with the same name. Since it cannot be combined into a // fields with the same name. Since it cannot be combined into a
// single field-value, recipients ought to handle "Set-Cookie" as a // single field-value, recipients ought to handle "Set-Cookie" as a
// special case while processing header fields." // special case while processing header fields."
if (Array.isArray(existing)) existing.push(value);
existing.push(value);
else
obj[name] = [existing, value];
break; break;
default: default:
// https://tools.ietf.org/html/rfc7230#section-3.2.2 // https://tools.ietf.org/html/rfc7230#section-3.2.2

View File

@ -1,14 +1,14 @@
// Flags: --expose-internals // Flags: --expose-internals
'use strict'; 'use strict';
// Tests the internal utility function that is used to prepare headers // Tests the internal utility functions that are used to prepare headers
// to pass to the internal binding layer. // to pass to the internal binding layer and to build a header object.
const common = require('../common'); const common = require('../common');
if (!common.hasCrypto) if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
const assert = require('assert'); const assert = require('assert');
const { mapToHeaders } = require('internal/http2/util'); const { mapToHeaders, toHeaderObject } = require('internal/http2/util');
const { const {
HTTP2_HEADER_STATUS, HTTP2_HEADER_STATUS,
@ -305,3 +305,41 @@ common.expectsError({
assert(!(mapToHeaders({ te: 'trailers' }) instanceof Error)); assert(!(mapToHeaders({ te: 'trailers' }) instanceof Error));
assert(!(mapToHeaders({ te: ['trailers'] }) instanceof Error)); assert(!(mapToHeaders({ te: ['trailers'] }) instanceof Error));
{
const rawHeaders = [
':status', '200',
'cookie', 'foo',
'set-cookie', 'sc1',
'age', '10',
'x-multi', 'first'
];
const headers = toHeaderObject(rawHeaders);
assert.strictEqual(headers[':status'], 200);
assert.strictEqual(headers.cookie, 'foo');
assert.deepStrictEqual(headers['set-cookie'], ['sc1']);
assert.strictEqual(headers.age, '10');
assert.strictEqual(headers['x-multi'], 'first');
}
{
const rawHeaders = [
':status', '200',
':status', '400',
'cookie', 'foo',
'cookie', 'bar',
'set-cookie', 'sc1',
'set-cookie', 'sc2',
'age', '10',
'age', '20',
'x-multi', 'first',
'x-multi', 'second'
];
const headers = toHeaderObject(rawHeaders);
assert.strictEqual(headers[':status'], 200);
assert.strictEqual(headers.cookie, 'foo; bar');
assert.deepStrictEqual(headers['set-cookie'], ['sc1', 'sc2']);
assert.strictEqual(headers.age, '10');
assert.strictEqual(headers['x-multi'], 'first, second');
}