Skip to content

Commit 58b88f1

Browse files
committed
Initial support proposal for http2
1 parent a4bd437 commit 58b88f1

File tree

7 files changed

+1408
-1330
lines changed

7 files changed

+1408
-1330
lines changed

lib/application.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var debug = require('debug')('express:application');
2222
var View = require('./view');
2323
var http = require('http');
2424
var compileETag = require('./utils').compileETag;
25+
var isHttp2Suported = require('./utils').isHttp2Supported;
2526
var compileQueryParser = require('./utils').compileQueryParser;
2627
var compileTrust = require('./utils').compileTrust;
2728
var deprecate = require('depd')('express');
@@ -99,6 +100,13 @@ app.defaultConfiguration = function defaultConfiguration() {
99100
setPrototypeOf(this.response, parent.response)
100101
setPrototypeOf(this.engines, parent.engines)
101102
setPrototypeOf(this.settings, parent.settings)
103+
104+
//set prototype for http2 requests/response
105+
if (isHttp2Suported) {
106+
setPrototypeOf(this.http2Request, parent.http2Request)
107+
setPrototypeOf(this.http2Response, parent.http2Response)
108+
}
109+
102110
});
103111

104112
// setup locals

lib/express.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var Route = require('./router/route');
1919
var Router = require('./router');
2020
var req = require('./request');
2121
var res = require('./response');
22-
22+
var isHttp2Supported = require('./utils').isHttp2Supported;
2323
/**
2424
* Expose `createApplication()`.
2525
*/
@@ -34,23 +34,36 @@ exports = module.exports = createApplication;
3434
*/
3535

3636
function createApplication() {
37-
var app = function(req, res, next) {
37+
var app = function (req, res, next) {
3838
app.handle(req, res, next);
3939
};
4040

4141
mixin(app, EventEmitter.prototype, false);
4242
mixin(app, proto, false);
4343

4444
// expose the prototype that will get set on requests
45-
app.request = Object.create(req, {
45+
app.request = Object.create(req.req, {
4646
app: { configurable: true, enumerable: true, writable: true, value: app }
4747
})
4848

4949
// expose the prototype that will get set on responses
50-
app.response = Object.create(res, {
50+
app.response = Object.create(res.res, {
5151
app: { configurable: true, enumerable: true, writable: true, value: app }
5252
})
5353

54+
if (isHttp2Supported) {
55+
app.http2Request = Object.create(req.http2Req, {
56+
app: { configurable: true, enumerable: true, writable: true, value: app }
57+
});
58+
59+
app.http2Response = Object.create(res.http2Res, {
60+
app: { configurable: true, enumerable: true, writable: true, value: app }
61+
});
62+
}
63+
64+
65+
66+
5467
app.init();
5568
return app;
5669
}

lib/middleware/init.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
*/
88

99
'use strict';
10-
1110
/**
1211
* Module dependencies.
1312
* @private
1413
*/
1514

16-
var setPrototypeOf = require('setprototypeof')
15+
var setPrototypeOf = require('setprototypeof');
16+
var isHttp2Supported = require('../utils').isHttp2Supported;
17+
var http2Request = null;
1718

19+
if (isHttp2Supported) {
20+
http2Request = require('http2').Http2ServerRequest;
21+
}
1822
/**
1923
* Initialization middleware, exposing the
2024
* request and response to each other, as well
@@ -31,9 +35,14 @@ exports.init = function(app){
3135
req.res = res;
3236
res.req = req;
3337
req.next = next;
38+
if (isHttp2Supported && req instanceof http2Request) {
39+
setPrototypeOf(req, app.http2Request)
40+
setPrototypeOf(res, app.http2Response)
41+
} else {
42+
setPrototypeOf(req, app.request)
43+
setPrototypeOf(res, app.response)
44+
}
3445

35-
setPrototypeOf(req, app.request)
36-
setPrototypeOf(res, app.response)
3746

3847
res.locals = res.locals || Object.create(null);
3948

0 commit comments

Comments
 (0)