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

http2: compat support for nested array headers #24665

Closed
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
10 changes: 8 additions & 2 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,16 @@ class Http2ServerResponse extends Stream {
if (headers === undefined && typeof statusMessage === 'object')
headers = statusMessage;

if (typeof headers === 'object') {
var i;
if (Array.isArray(headers)) {
for (i = 0; i < headers.length; i++) {
Trott marked this conversation as resolved.
Show resolved Hide resolved
const header = headers[i];
this[kSetHeader](header[0], header[1]);
}
} else if (typeof headers === 'object') {
const keys = Object.keys(headers);
let key = '';
for (var i = 0; i < keys.length; i++) {
for (i = 0; i < keys.length; i++) {
Trott marked this conversation as resolved.
Show resolved Hide resolved
key = keys[i];
this[kSetHeader](key, headers[key]);
}
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-writehead-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');

// Http2ServerResponse.writeHead should support nested arrays

const server = h2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
server.once('request', common.mustCall((request, response) => {
response.writeHead(200, [
['foo', 'bar'],
['ABC', 123]
]);
response.end(common.mustCall(() => { server.close(); }));
}));

const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(() => {
const headers = {
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.on('response', common.mustCall((headers) => {
assert.strictEqual(headers.foo, 'bar');
assert.strictEqual(headers.abc, '123');
assert.strictEqual(headers[':status'], 200);
}, 1));
request.on('end', common.mustCall(() => {
client.close();
}));
request.end();
request.resume();
}));
}));