Skip to content

Commit bdd5a7c

Browse files
committed
Basic HTTP2 working, but may need to add some HTTP2 specific tests
1 parent db490b6 commit bdd5a7c

4 files changed

Lines changed: 73 additions & 14 deletions

File tree

‎index.js‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const os = require('os');
22
const jwt = require('jsonwebtoken');
33
const http = require('http')
44
const https = require('https')
5+
const http2express = require('http2-express');
6+
const httpolyglot = require('@httptoolkit/httpolyglot');
57
const morgan = require('morgan');
68
const express = require('express')
79
const cookieParser = require('cookie-parser');
@@ -39,7 +41,7 @@ const metricsMiddleware = promBundle({
3941
metricType: PROMETHEUS_METRIC_TYPE,
4042
});
4143

42-
const app = express()
44+
const app = http2express(express);
4345
app.set('json spaces', 2);
4446
app.set('trust proxy', trustProxy);
4547

@@ -204,36 +206,65 @@ app.all('/{*splat}', (req, res) => {
204206

205207
});
206208

207-
let httpOpts = {
208-
maxHeaderSize: maxHeaderSize
209-
}
210-
211-
let httpsOpts = {
209+
// let httpOpts = {
210+
// maxHeaderSize: maxHeaderSize
211+
// }
212+
213+
// let httpsOpts = {
214+
// key: require('fs').readFileSync(process.env.HTTPS_KEY_FILE || 'testpk.pem'),
215+
// cert: require('fs').readFileSync(process.env.HTTPS_CERT_FILE || 'fullchain.pem'),
216+
// maxHeaderSize: maxHeaderSize
217+
// };
218+
219+
// //Whether to enable the client certificate feature
220+
// if(process.env.MTLS_ENABLE){
221+
// httpsOpts = {
222+
// requestCert: true,
223+
// rejectUnauthorized: false,
224+
// ...httpsOpts
225+
// }
226+
// }
227+
228+
// var httpServer = http.createServer(httpOpts, app).listen(process.env.HTTP_PORT || 8080);
229+
// var httpsServer = https.createServer(httpsOpts,app).listen(process.env.HTTPS_PORT || 8443);
230+
231+
// plain text http server, http2 server (aka "h2c")
232+
var httpServer = httpolyglot.createServer({
233+
http: { maxHeaderSize: maxHeaderSize }
234+
}, app).listen(process.env.HTTP_PORT || 8080);
235+
236+
let tlsOpts = {
212237
key: require('fs').readFileSync(process.env.HTTPS_KEY_FILE || 'testpk.pem'),
213238
cert: require('fs').readFileSync(process.env.HTTPS_CERT_FILE || 'fullchain.pem'),
214-
maxHeaderSize: maxHeaderSize
239+
ALPNProtocols: [ 'http/1.1', 'h2'],
215240
};
216241

217242
//Whether to enable the client certificate feature
218243
if(process.env.MTLS_ENABLE){
219-
httpsOpts = {
244+
tlsOpts = {
220245
requestCert: true,
221246
rejectUnauthorized: false,
222-
...httpsOpts
247+
...tlsOpts
223248
}
224249
}
225250

226-
var httpServer = http.createServer(httpOpts, app).listen(process.env.HTTP_PORT || 8080);
227-
var httpsServer = https.createServer(httpsOpts,app).listen(process.env.HTTPS_PORT || 8443);
251+
var httpsServer = httpolyglot.createServer({
252+
tls: tlsOpts,
253+
http: { maxHeaderSize: maxHeaderSize }
254+
}, app).listen(process.env.HTTPS_PORT || 8443);
255+
228256
console.log(`Listening on ports ${process.env.HTTP_PORT || 8080} for http, and ${process.env.HTTPS_PORT || 8443} for https.`);
229257

230258
let calledClose = false;
231259

232260
process.on('exit', function () {
233261
if (calledClose) return;
234262
console.log('Got exit event. Trying to stop Express server.');
235-
server.close(function() {
236-
console.log("Express server closed");
263+
httpServer.close(function() {
264+
console.log("HTTP server closed");
265+
});
266+
httpsServer.close(function() {
267+
console.log("HTTPS server closed");
237268
});
238269
});
239270

‎package-lock.json‎

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
"node": ">=16.0.0"
1818
},
1919
"dependencies": {
20+
"@httptoolkit/httpolyglot": "^3.1.0",
2021
"concat-stream": "^2.0.0",
2122
"cookie-parser": "^1.4.6",
2223
"express": "^5.2.1",
2324
"express-prom-bundle": "^8.0.0",
25+
"http2-express": "^1.1.1",
2426
"jsonwebtoken": "^9.0.0",
2527
"morgan": "^1.12.0"
2628
},

‎tests.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
6868
fi
6969
else
7070
echo " Local run. Build image "
71-
docker build -t mendhak/http-https-echo:testing .
71+
docker build --no-cache -t mendhak/http-https-echo:testing .
7272
fi
7373

7474

0 commit comments

Comments
 (0)