From ad2c0f6f3e192cf06c9623ea4a4370130a3f5b94 Mon Sep 17 00:00:00 2001 From: Mehmet Fidanboylu Date: Thu, 27 Aug 2020 15:06:54 -0700 Subject: [PATCH] Use SecureSocket.connect directly if there's no authority in Credentials (#343) --- lib/src/client/http2_connection.dart | 37 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/src/client/http2_connection.dart b/lib/src/client/http2_connection.dart index a98ec87..1905cdf 100644 --- a/lib/src/client/http2_connection.dart +++ b/lib/src/client/http2_connection.dart @@ -75,22 +75,33 @@ class Http2ClientConnection implements connection.ClientConnection { static const _estimatedRoundTripTime = const Duration(milliseconds: 20); - Future connectTransport() async { + Future _createSocket() async { final securityContext = credentials.securityContext; - Socket socket = await Socket.connect(host, port); + if (securityContext == null) { + return Socket.connect(host, port); + } else { + if (options.credentials.authority == null) { + return SecureSocket.connect(host, port, + context: securityContext, + onBadCertificate: _validateBadCertificate); + } else { + // Todo(sigurdm): We want to pass supportedProtocols: ['h2']. http://dartbug.com/37950 + return SecureSocket.secure(await Socket.connect(host, port), + // This is not really the host, but the authority to verify the TLC + // connection against. + // + // We don't use `this.authority` here, as that includes the port. + host: options.credentials.authority, + context: securityContext, + onBadCertificate: _validateBadCertificate); + } + } + } + + Future connectTransport() async { + final Socket socket = await _createSocket(); // Don't wait for io buffers to fill up before sending requests. socket.setOption(SocketOption.tcpNoDelay, true); - if (securityContext != null) { - // Todo(sigurdm): We want to pass supportedProtocols: ['h2']. http://dartbug.com/37950 - socket = await SecureSocket.secure(socket, - // This is not really the host, but the authority to verify the TLC - // connection against. - // - // We don't use `this.authority` here, as that includes the port. - host: options.credentials.authority ?? host, - context: securityContext, - onBadCertificate: _validateBadCertificate); - } final connection = ClientTransportConnection.viaSocket(socket); socket.done.then((_) => _abandonConnection());