Skip to content

Commit

Permalink
http2: add req and res options to server creation
Browse files Browse the repository at this point in the history
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.

Backport-PR-URL: #20456
PR-URL: #15560
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
Peter Marton authored and MylesBorins committed May 15, 2018
1 parent 6b4ec58 commit 81a4abe
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
8 changes: 8 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,14 @@ changes:
* `Http1ServerResponse` {http.ServerResponse} Specifies the ServerResponse
class to used for HTTP/1 fallback. Useful for extending the original
`http.ServerResponse`. **Default:** `http.ServerResponse`
* `Http2ServerRequest` {http2.Http2ServerRequest} Specifies the
Http2ServerRequest class to use.
Useful for extending the original `Http2ServerRequest`.
**Default:** `Http2ServerRequest`
* `Http2ServerResponse` {htt2.Http2ServerResponse} Specifies the
Http2ServerResponse class to use.
Useful for extending the original `Http2ServerResponse`.
**Default:** `Http2ServerResponse`
* `onRequestHandler` {Function} See [Compatibility API][]
* Returns: {Http2Server}

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,11 @@ class Http2ServerResponse extends Stream {
}
}

function onServerStream(stream, headers, flags, rawHeaders) {
function onServerStream(ServerRequest, ServerResponse,
stream, headers, flags, rawHeaders) {
const server = this;
const request = new Http2ServerRequest(stream, headers, undefined,
rawHeaders);
const response = new Http2ServerResponse(stream);
const request = new ServerRequest(stream, headers, undefined, rawHeaders);
const response = new ServerResponse(stream);

// Check for the CONNECT method
const method = headers[HTTP2_HEADER_METHOD];
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,10 @@ function initializeOptions(options) {
options.Http1ServerResponse = options.Http1ServerResponse ||
http.ServerResponse;

options.Http2ServerRequest = options.Http2ServerRequest ||
Http2ServerRequest;
options.Http2ServerResponse = options.Http2ServerResponse ||
Http2ServerResponse;
return options;
}

Expand Down Expand Up @@ -2604,7 +2608,11 @@ class Http2Server extends NETServer {
function setupCompat(ev) {
if (ev === 'request') {
this.removeListener('newListener', setupCompat);
this.on('stream', onServerStream);
this.on('stream', onServerStream.bind(
this,
this[kOptions].Http2ServerRequest,
this[kOptions].Http2ServerResponse)
);
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-http2-options-server-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');

class MyServerRequest extends h2.Http2ServerRequest {
getUserAgent() {
return this.headers['user-agent'] || 'unknown';
}
}

const server = h2.createServer({
Http2ServerRequest: MyServerRequest
}, (req, res) => {
assert.strictEqual(req.getUserAgent(), 'node-test');

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end();
});
server.listen(0);

server.on('listening', common.mustCall(() => {

const client = h2.connect(`http://localhost:${server.address().port}`);
const req = client.request({
':path': '/',
'User-Agent': 'node-test'
});

req.on('response', common.mustCall());

req.resume();
req.on('end', common.mustCall(() => {
server.close();
client.destroy();
}));
}));
34 changes: 34 additions & 0 deletions test/parallel/test-http2-options-server-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const h2 = require('http2');

class MyServerResponse extends h2.Http2ServerResponse {
status(code) {
return this.writeHead(code, { 'Content-Type': 'text/plain' });
}
}

const server = h2.createServer({
Http2ServerResponse: MyServerResponse
}, (req, res) => {
res.status(200);
res.end();
});
server.listen(0);

server.on('listening', common.mustCall(() => {

const client = h2.connect(`http://localhost:${server.address().port}`);
const req = client.request({ ':path': '/' });

req.on('response', common.mustCall());

req.resume();
req.on('end', common.mustCall(() => {
server.close();
client.destroy();
}));
}));

0 comments on commit 81a4abe

Please sign in to comment.