Skip to content

Commit

Permalink
http2: add compat support for nested array headers
Browse files Browse the repository at this point in the history
writeHead supports an array of arrays containing header name and values.
Compatibility between http2 & http1 even though this is not documented.

Fixes: #24466

PR-URL: #24665
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
sebdeckers authored and targos committed Nov 29, 2018
1 parent d5bf736 commit 61e0103
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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++) {
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++) {
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();
}));
}));

0 comments on commit 61e0103

Please sign in to comment.