Skip to content

Commit c0d6945

Browse files
Peter MartonMylesBorins
authored andcommitted
http2: add req and res options to server creation
Add optional Http2ServerRequest and Http2ServerResponse options to createServer and createSecureServer. Allows custom req & res classes that extend the default ones to be used without overriding the prototype. PR-URL: #15560 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent af977db commit c0d6945

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

‎doc/api/http2.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,14 @@ changes:
17141714
*`Http1ServerResponse`{http.ServerResponse} Specifies the ServerResponse
17151715
class to used for HTTP/1 fallback. Useful for extending the original
17161716
`http.ServerResponse`. **Default:**`http.ServerResponse`
1717+
*`Http2ServerRequest`{http2.Http2ServerRequest} Specifies the
1718+
Http2ServerRequest class to use.
1719+
Useful for extending the original `Http2ServerRequest`.
1720+
**Default:**`Http2ServerRequest`
1721+
*`Http2ServerResponse`{htt2.Http2ServerResponse} Specifies the
1722+
Http2ServerResponse class to use.
1723+
Useful for extending the original `Http2ServerResponse`.
1724+
**Default:**`Http2ServerResponse`
17171725
*`onRequestHandler`{Function} See [Compatibility API][]
17181726
* Returns:{Http2Server}
17191727

‎lib/internal/http2/compat.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,11 @@ class Http2ServerResponse extends Stream{
661661
}
662662
}
663663

664-
functiononServerStream(stream,headers,flags,rawHeaders){
664+
functiononServerStream(ServerRequest,ServerResponse,
665+
stream,headers,flags,rawHeaders){
665666
constserver=this;
666-
constrequest=newHttp2ServerRequest(stream,headers,undefined,
667-
rawHeaders);
668-
constresponse=newHttp2ServerResponse(stream);
667+
constrequest=newServerRequest(stream,headers,undefined,rawHeaders);
668+
constresponse=newServerResponse(stream);
669669

670670
// Check for the CONNECT method
671671
constmethod=headers[HTTP2_HEADER_METHOD];

‎lib/internal/http2/core.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,10 @@ function initializeOptions(options){
24872487
options.Http1ServerResponse=options.Http1ServerResponse||
24882488
http.ServerResponse;
24892489

2490+
options.Http2ServerRequest=options.Http2ServerRequest||
2491+
Http2ServerRequest;
2492+
options.Http2ServerResponse=options.Http2ServerResponse||
2493+
Http2ServerResponse;
24902494
returnoptions;
24912495
}
24922496

@@ -2552,7 +2556,11 @@ class Http2Server extends NETServer{
25522556
functionsetupCompat(ev){
25532557
if(ev==='request'){
25542558
this.removeListener('newListener',setupCompat);
2555-
this.on('stream',onServerStream);
2559+
this.on('stream',onServerStream.bind(
2560+
this,
2561+
this[kOptions].Http2ServerRequest,
2562+
this[kOptions].Http2ServerResponse)
2563+
);
25562564
}
25572565
}
25582566

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
classMyServerRequestextendsh2.Http2ServerRequest{
10+
getUserAgent(){
11+
returnthis.headers['user-agent']||'unknown';
12+
}
13+
}
14+
15+
constserver=h2.createServer({
16+
Http2ServerRequest: MyServerRequest
17+
},(req,res)=>{
18+
assert.strictEqual(req.getUserAgent(),'node-test');
19+
20+
res.writeHead(200,{'Content-Type': 'text/plain'});
21+
res.end();
22+
});
23+
server.listen(0);
24+
25+
server.on('listening',common.mustCall(()=>{
26+
27+
constclient=h2.connect(`http://localhost:${server.address().port}`);
28+
constreq=client.request({
29+
':path': '/',
30+
'User-Agent': 'node-test'
31+
});
32+
33+
req.on('response',common.mustCall());
34+
35+
req.resume();
36+
req.on('end',common.mustCall(()=>{
37+
server.close();
38+
client.destroy();
39+
}));
40+
}));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
consth2=require('http2');
7+
8+
classMyServerResponseextendsh2.Http2ServerResponse{
9+
status(code){
10+
returnthis.writeHead(code,{'Content-Type': 'text/plain'});
11+
}
12+
}
13+
14+
constserver=h2.createServer({
15+
Http2ServerResponse: MyServerResponse
16+
},(req,res)=>{
17+
res.status(200);
18+
res.end();
19+
});
20+
server.listen(0);
21+
22+
server.on('listening',common.mustCall(()=>{
23+
24+
constclient=h2.connect(`http://localhost:${server.address().port}`);
25+
constreq=client.request({':path': '/'});
26+
27+
req.on('response',common.mustCall());
28+
29+
req.resume();
30+
req.on('end',common.mustCall(()=>{
31+
server.close();
32+
client.destroy();
33+
}));
34+
}));

0 commit comments

Comments
(0)