Skip to content
This repository was archived by the owner on Jul 6, 2018. It is now read-only.

Commit 4038ea0

Browse files
jmukjasnell
authored andcommitted
http2: specify default TLS options for http2 client connection.
fixes: #59 Also, add a testcase for http2/TLS secure connection. This verifies to send the server name and ALPN protocols by default. PR-URL: #61 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 83b3ac0 commit 4038ea0

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

lib/internal/http2/core.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,13 @@ function initializeOptions(options) {
10911091
return options;
10921092
}
10931093

1094-
function initializeTLSOptions(options) {
1094+
function initializeTLSOptions(options, servername) {
10951095
options = initializeOptions(options);
10961096
options.ALPNProtocols = ['hc', 'h2'];
10971097
options.NPNProtocols = ['hc', 'h2'];
1098+
if (servername !== undefined) {
1099+
options.servername = servername;
1100+
}
10981101
return options;
10991102
}
11001103

@@ -1242,7 +1245,7 @@ function connect(authority, options, listener) {
12421245
socket = net.connect(port, host);
12431246
break;
12441247
case 'https:':
1245-
socket = tls.connect(port, host, options);
1248+
socket = tls.connect(port, host, initializeTLSOptions(options, host));
12461249
break;
12471250
default:
12481251
throw new TypeError(`protocol "${protocol}" in unsupported.`);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
const fs = require('fs');
7+
const tls = require('tls');
8+
const h2 = require('http2');
9+
const body =
10+
'<html><head></head><body><h1>this is some data</h2></body></html>';
11+
12+
const key = loadKey('agent8-key.pem');
13+
const cert = loadKey('agent8-cert.pem');
14+
const ca = loadKey('fake-startcom-root-cert.pem');
15+
16+
function loadKey(keyname) {
17+
return fs.readFileSync(path.join(common.fixturesDir, 'keys', keyname), 'binary');
18+
}
19+
20+
const server = h2.createSecureServer({cert, key});
21+
22+
// we use the lower-level API here
23+
server.on('stream', common.mustCall(onStream));
24+
25+
function onStream(stream) {
26+
stream.respond({
27+
'content-type': 'text/html',
28+
':status': 200
29+
});
30+
const socket = stream.session.socket;
31+
stream.end(JSON.stringify({
32+
servername: socket.servername,
33+
alpnProtocol: socket.alpnProtocol
34+
}));
35+
}
36+
37+
server.listen(0);
38+
39+
server.on('listening', common.mustCall(function() {
40+
41+
const headers = { ':path': '/' };
42+
43+
const clientOptions = {secureContext: tls.createSecureContext({ca})};
44+
const client = h2.connect(`https://localhost:${this.address().port}`, clientOptions, function() {
45+
const req = client.request(headers);
46+
47+
req.on('response', common.mustCall(function(headers) {
48+
assert.strictEqual(headers[':status'], '200', 'status code is set');
49+
assert.strictEqual(headers['content-type'], 'text/html',
50+
'content type is set');
51+
assert(headers['date'], 'there is a date');
52+
}));
53+
54+
let data = '';
55+
req.setEncoding('utf8');
56+
req.on('data', (d) => data += d);
57+
req.on('end', common.mustCall(() => {
58+
const jsonData = JSON.parse(data);
59+
assert.strictEqual(jsonData.servername, 'localhost');
60+
assert(jsonData.alpnProtocol === 'h2' || jsonData.alpnProtocol === 'hc');
61+
server.close();
62+
client.socket.destroy();
63+
}));
64+
req.end();
65+
});
66+
}));

0 commit comments

Comments
 (0)