From c92863fdc4e4e439caeadf7712684c25499c40d0 Mon Sep 17 00:00:00 2001 From: Sebastiaan Deckers Date: Tue, 27 Nov 2018 10:45:51 +0800 Subject: [PATCH] http2: compat support for nested array headers writeHead supports an array of arrays containing header name and values. Compatibility between http2 & http1 even though this is not documented. Fixes: https://github.com/nodejs/node/issues/24466 --- lib/internal/http2/compat.js | 10 ++++- ...2-compat-serverresponse-writehead-array.js | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-http2-compat-serverresponse-writehead-array.js diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 145e77410494b6..464f1141066c3a 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -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]); } diff --git a/test/parallel/test-http2-compat-serverresponse-writehead-array.js b/test/parallel/test-http2-compat-serverresponse-writehead-array.js new file mode 100644 index 00000000000000..e024f8ab3952f0 --- /dev/null +++ b/test/parallel/test-http2-compat-serverresponse-writehead-array.js @@ -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(); + })); +}));