Skip to content

Commit 1cdf93e

Browse files
FlarnaBethGriggs
authored andcommitted
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. Backport-PR-URL: #22850 PR-URL: #21360 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 4228141 commit 1cdf93e

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

doc/api/http2.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2132,8 +2132,7 @@ For incoming headers:
21322132
`proxy-authorization`, `range`, `referer`,`retry-after`, `tk`,
21332133
`upgrade-insecure-requests`, `user-agent` or `x-content-type-options` are
21342134
discarded.
2135-
* `set-cookie` is a string if present once or an array in case duplicates
2136-
are present.
2135+
* `set-cookie` is always an array. Duplicates are added to the array.
21372136
* `cookie`: the values are joined together with '; '.
21382137
* For all other headers, the values are joined together with ', '.
21392138

lib/internal/http2/util.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ function toHeaderObject(headers) {
504504
value |= 0;
505505
var existing = obj[name];
506506
if (existing === undefined) {
507-
obj[name] = value;
507+
obj[name] = name === HTTP2_HEADER_SET_COOKIE ? [value] : value;
508508
} else if (!kSingleValueHeaders.has(name)) {
509509
switch (name) {
510510
case HTTP2_HEADER_COOKIE:
@@ -523,10 +523,7 @@ function toHeaderObject(headers) {
523523
// fields with the same name. Since it cannot be combined into a
524524
// single field-value, recipients ought to handle "Set-Cookie" as a
525525
// special case while processing header fields."
526-
if (Array.isArray(existing))
527-
existing.push(value);
528-
else
529-
obj[name] = [existing, value];
526+
existing.push(value);
530527
break;
531528
default:
532529
// https://tools.ietf.org/html/rfc7230#section-3.2.2

test/parallel/test-http2-util-headers-list.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Flags: --expose-internals
22
'use strict';
33

4-
// Tests the internal utility function that is used to prepare headers
5-
// to pass to the internal binding layer.
4+
// Tests the internal utility functions that are used to prepare headers
5+
// to pass to the internal binding layer and to build a header object.
66

77
const common = require('../common');
88
if (!common.hasCrypto)
99
common.skip('missing crypto');
1010
const assert = require('assert');
11-
const { mapToHeaders } = require('internal/http2/util');
11+
const { mapToHeaders, toHeaderObject } = require('internal/http2/util');
1212

1313
const {
1414
HTTP2_HEADER_STATUS,
@@ -302,3 +302,41 @@ common.expectsError({
302302

303303
assert(!(mapToHeaders({ te: 'trailers' }) instanceof Error));
304304
assert(!(mapToHeaders({ te: ['trailers'] }) instanceof Error));
305+
306+
307+
{
308+
const rawHeaders = [
309+
':status', '200',
310+
'cookie', 'foo',
311+
'set-cookie', 'sc1',
312+
'age', '10',
313+
'x-multi', 'first'
314+
];
315+
const headers = toHeaderObject(rawHeaders);
316+
assert.strictEqual(headers[':status'], 200);
317+
assert.strictEqual(headers.cookie, 'foo');
318+
assert.deepStrictEqual(headers['set-cookie'], ['sc1']);
319+
assert.strictEqual(headers.age, '10');
320+
assert.strictEqual(headers['x-multi'], 'first');
321+
}
322+
323+
{
324+
const rawHeaders = [
325+
':status', '200',
326+
':status', '400',
327+
'cookie', 'foo',
328+
'cookie', 'bar',
329+
'set-cookie', 'sc1',
330+
'set-cookie', 'sc2',
331+
'age', '10',
332+
'age', '20',
333+
'x-multi', 'first',
334+
'x-multi', 'second'
335+
];
336+
const headers = toHeaderObject(rawHeaders);
337+
assert.strictEqual(headers[':status'], 200);
338+
assert.strictEqual(headers.cookie, 'foo; bar');
339+
assert.deepStrictEqual(headers['set-cookie'], ['sc1', 'sc2']);
340+
assert.strictEqual(headers.age, '10');
341+
assert.strictEqual(headers['x-multi'], 'first, second');
342+
}

0 commit comments

Comments
 (0)