Skip to content

Commit d883024

Browse files
bcoetargos
authored andcommitted
http2: wait for secureConnect before initializing
PR-URL: #32958 Fixes: #32922 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 1811a10 commit d883024

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

lib/_tls_wrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ function TLSSocket(socket, opts) {
466466
this._securePending = false;
467467
this._newSessionPending = false;
468468
this._controlReleased = false;
469+
this.secureConnecting = true;
469470
this._SNICallback = null;
470471
this.servername = null;
471472
this.alpnProtocol = null;
@@ -1035,6 +1036,7 @@ function onServerSocketSecure() {
10351036

10361037
if (!this.destroyed && this._releaseControl()) {
10371038
debug('server emit secureConnection');
1039+
this.secureConnecting = false;
10381040
this._tlsOptions.server.emit('secureConnection', this);
10391041
}
10401042
}

lib/internal/http2/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ class Http2Session extends EventEmitter {
11151115
socket.disableRenegotiation();
11161116

11171117
const setupFn = setupHandle.bind(this, socket, type, options);
1118-
if (socket.connecting) {
1118+
if (socket.connecting || socket.secureConnecting) {
11191119
const connectEvent =
11201120
socket instanceof tls.TLSSocket ? 'secureConnect' : 'connect';
11211121
socket.once(connectEvent, () => {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
8+
const http2 = require('http2');
9+
const net = require('net');
10+
11+
const {
12+
HTTP2_HEADER_PATH,
13+
} = http2.constants;
14+
15+
// Create a normal session, as a control case
16+
function normalSession(cb) {
17+
http2.connect('https://google.com', (clientSession) => {
18+
let error = null;
19+
const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
20+
req.on('error', (err) => {
21+
error = err;
22+
});
23+
req.on('response', (_headers) => {
24+
req.on('data', (_chunk) => { });
25+
req.on('end', () => {
26+
clientSession.close();
27+
return cb(error);
28+
});
29+
});
30+
});
31+
}
32+
normalSession(common.mustCall(function(err) {
33+
assert.ifError(err);
34+
}));
35+
36+
// Create a session using a socket that has not yet finished connecting
37+
function socketNotFinished(done) {
38+
const socket2 = net.connect(443, 'google.com');
39+
http2.connect('https://google.com', { socket2 }, (clientSession) => {
40+
let error = null;
41+
const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
42+
req.on('error', (err) => {
43+
error = err;
44+
});
45+
req.on('response', (_headers) => {
46+
req.on('data', (_chunk) => { });
47+
req.on('end', () => {
48+
clientSession.close();
49+
socket2.destroy();
50+
return done(error);
51+
});
52+
});
53+
});
54+
}
55+
socketNotFinished(common.mustCall(function(err) {
56+
assert.ifError(err);
57+
}));
58+
59+
// Create a session using a socket that has finished connecting
60+
function socketFinished(done) {
61+
const socket = net.connect(443, 'google.com', () => {
62+
http2.connect('https://google.com', { socket }, (clientSession) => {
63+
let error = null;
64+
const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
65+
req.on('error', (err) => {
66+
error = err;
67+
});
68+
req.on('response', (_headers) => {
69+
req.on('data', (_chunk) => { });
70+
req.on('end', () => {
71+
clientSession.close();
72+
return done(error);
73+
});
74+
});
75+
});
76+
});
77+
}
78+
socketFinished(common.mustCall(function(err) {
79+
assert.ifError(err);
80+
}));

0 commit comments

Comments
 (0)