Skip to content

Commit c0d6945

Browse files
Peter MartonMylesBorins
Peter Marton
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

+8
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,11 @@ class Http2ServerResponse extends Stream {
661661
}
662662
}
663663

664-
function onServerStream(stream, headers, flags, rawHeaders) {
664+
function onServerStream(ServerRequest, ServerResponse,
665+
stream, headers, flags, rawHeaders) {
665666
const server = this;
666-
const request = new Http2ServerRequest(stream, headers, undefined,
667-
rawHeaders);
668-
const response = new Http2ServerResponse(stream);
667+
const request = new ServerRequest(stream, headers, undefined, rawHeaders);
668+
const response = new ServerResponse(stream);
669669

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

lib/internal/http2/core.js

+9-1
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
return options;
24912495
}
24922496

@@ -2552,7 +2556,11 @@ class Http2Server extends NETServer {
25522556
function setupCompat(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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const assert = require('assert');
7+
const h2 = require('http2');
8+
9+
class MyServerRequest extends h2.Http2ServerRequest {
10+
getUserAgent() {
11+
return this.headers['user-agent'] || 'unknown';
12+
}
13+
}
14+
15+
const server = 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+
const client = h2.connect(`http://localhost:${server.address().port}`);
28+
const req = 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+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const h2 = require('http2');
7+
8+
class MyServerResponse extends h2.Http2ServerResponse {
9+
status(code) {
10+
return this.writeHead(code, { 'Content-Type': 'text/plain' });
11+
}
12+
}
13+
14+
const server = 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+
const client = h2.connect(`http://localhost:${server.address().port}`);
25+
const req = 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)