Skip to content

Commit c88af23

Browse files
Flarnatargos
authored andcommittedJul 6, 2018
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: #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 ceec23e commit c88af23

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

‎lib/internal/http2/util.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ function toHeaderObject(headers) {
509509
value |= 0;
510510
var existing = obj[name];
511511
if (existing === undefined) {
512-
obj[name] = value;
512+
obj[name] = name === HTTP2_HEADER_SET_COOKIE ? [value] : value;
513513
} else if (!kSingleValueHeaders.has(name)) {
514514
switch (name) {
515515
case HTTP2_HEADER_COOKIE:
@@ -528,10 +528,7 @@ function toHeaderObject(headers) {
528528
// fields with the same name. Since it cannot be combined into a
529529
// single field-value, recipients ought to handle "Set-Cookie" as a
530530
// special case while processing header fields."
531-
if (Array.isArray(existing))
532-
existing.push(value);
533-
else
534-
obj[name] = [existing, value];
531+
existing.push(value);
535532
break;
536533
default:
537534
// 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,
@@ -305,3 +305,41 @@ common.expectsError({
305305

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

0 commit comments

Comments
 (0)
Please sign in to comment.