Skip to content

Commit

Permalink
http2: add Http2ServerRequest and Http2ServerResponse options to crea…
Browse files Browse the repository at this point in the history
…teServer
  • Loading branch information
Peter Marton authored and Peter Marton committed Feb 9, 2018
1 parent d1d9e2f commit bdca0b7
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 @@ -1714,6 +1714,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 @@ -2498,6 +2498,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 @@ -2563,7 +2567,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 bdca0b7

Please sign in to comment.