]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
TCPClient: connect without SSL and wrap the connection later.
authorBen Darnell <ben@bendarnell.com>
Sat, 24 May 2014 21:00:53 +0000 (17:00 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 24 May 2014 21:00:53 +0000 (17:00 -0400)
This prevents us from potentially starting two SSL handshakes
at the same time.

tornado/tcpclient.py

index 1bb1253f1f35dd94d1c840d5770cc673957e3807..f29c29c1c68c8bd703c9e70b17056a4dc6ee3833 100644 (file)
@@ -159,24 +159,20 @@ class TCPClient(object):
         addrinfo = yield self.resolver.resolve(host, port, af)
         connector = _Connector(
             addrinfo, self.io_loop,
-            functools.partial(self._create_stream,
-                              host, ssl_options, max_buffer_size))
+            functools.partial(self._create_stream, max_buffer_size))
         af, addr, stream = yield connector.start()
         # TODO: For better performance we could cache the (af, addr)
         # information here and re-use it on sbusequent connections to
         # the same host. (http://tools.ietf.org/html/rfc6555#section-4.2)
+        if ssl_options is not None:
+            stream = yield stream.start_tls(False, ssl_options=ssl_options,
+                                            server_hostname=host)
         raise gen.Return(stream)
 
-    def _create_stream(self, host, ssl_options, max_buffer_size, af, addr):
-        # TODO: we should connect in plaintext mode and start the
-        # ssl handshake only after stopping the _Connector.
-        if ssl_options is None:
-            stream = IOStream(socket.socket(af),
-                              io_loop=self.io_loop,
-                              max_buffer_size=max_buffer_size)
-        else:
-            stream = SSLIOStream(socket.socket(af),
-                                 io_loop=self.io_loop,
-                                 ssl_options=ssl_options,
-                                 max_buffer_size=max_buffer_size)
-        return stream.connect(addr, server_hostname=host)
+    def _create_stream(self, max_buffer_size, af, addr):
+        # Always connect in plaintext; we'll convert to ssl if necessary
+        # after one connection has completed.
+        stream = IOStream(socket.socket(af),
+                          io_loop=self.io_loop,
+                          max_buffer_size=max_buffer_size)
+        return stream.connect(addr)