Skip to content

Commit d12c5a7

Browse files
sebdeckersBethGriggs
authored andcommitted
http2: add 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: #24466 PR-URL: #24665 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent af58209 commit d12c5a7

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎lib/internal/http2/compat.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,16 @@ class Http2ServerResponse extends Stream {
574574
if(headers===undefined&&typeofstatusMessage==='object')
575575
headers=statusMessage;
576576

577-
if(typeofheaders==='object'){
577+
vari;
578+
if(Array.isArray(headers)){
579+
for(i=0;i<headers.length;i++){
580+
constheader=headers[i];
581+
this[kSetHeader](header[0],header[1]);
582+
}
583+
}elseif(typeofheaders==='object'){
578584
constkeys=Object.keys(headers);
579585
letkey='';
580-
for(vari=0;i<keys.length;i++){
586+
for(i=0;i<keys.length;i++){
581587
key=keys[i];
582588
this[kSetHeader](key,headers[key]);
583589
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
constassert=require('assert');
7+
consth2=require('http2');
8+
9+
// Http2ServerResponse.writeHead should support nested arrays
10+
11+
constserver=h2.createServer();
12+
server.listen(0,common.mustCall(()=>{
13+
constport=server.address().port;
14+
server.once('request',common.mustCall((request,response)=>{
15+
response.writeHead(200,[
16+
['foo','bar'],
17+
['ABC',123]
18+
]);
19+
response.end(common.mustCall(()=>{server.close();}));
20+
}));
21+
22+
consturl=`http://localhost:${port}`;
23+
constclient=h2.connect(url,common.mustCall(()=>{
24+
constheaders={
25+
':path': '/',
26+
':method': 'GET',
27+
':scheme': 'http',
28+
':authority': `localhost:${port}`
29+
};
30+
constrequest=client.request(headers);
31+
request.on('response',common.mustCall((headers)=>{
32+
assert.strictEqual(headers.foo,'bar');
33+
assert.strictEqual(headers.abc,'123');
34+
assert.strictEqual(headers[':status'],200);
35+
},1));
36+
request.on('end',common.mustCall(()=>{
37+
client.close();
38+
}));
39+
request.end();
40+
request.resume();
41+
}));
42+
}));

0 commit comments

Comments
 (0)