Skip to content
Myr

Add support for proxy tunnels for Python 3.6 + asyncio. (#521)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Backport start_tls() support for Python 3.6 * Remove version check in start_tls test

authored by Seth Michael Larson · committed by Florimond Manca · parent 6045ee2 · Browse files · 2 files+38 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−10 removed

  • tests:+0 added−5 removed
  • other:+38 added−5 removed

2 files+38 added−10 removed

modified:httpx/concurrency/asyncio.py+38 added-5 removed
56 unmodified lines
57 async def start_tls(58 self, hostname: str, ssl_context: ssl.SSLContext, timeout: TimeoutConfig59 ) -> "SocketStream":60 loop = asyncio.get_event_loop()61 if not hasattr(loop, "start_tls"): # pragma: no cover62 raise NotImplementedError(63 "asyncio.AbstractEventLoop.start_tls() is only available in Python 3.7+"64 )6162 stream_reader = asyncio.StreamReader()63 protocol = asyncio.StreamReaderProtocol(stream_reader)64 transport = self.stream_writer.transport6566 if hasattr(loop, "start_tls"):70 loop_start_tls = loop.start_tls # type: ignore67 loop_start_tls = loop.start_tls # type: ignore68 else:6970 async def loop_start_tls(71 transport: asyncio.BaseTransport,72 protocol: asyncio.BaseProtocol,73 sslcontext: ssl.SSLContext = None,74 *,75 server_side: bool = False,76 server_hostname: str = None,77 ssl_handshake_timeout: float = None,78 ) -> asyncio.Transport:79 """Python 3.6 asyncio doesn't have a start_tls() method on the loop80 so we use this function in place of the loop's start_tls() method.81 Adapted from this comment:82 https://github.com/urllib3/urllib3/issues/1323#issuecomment-36249483983 """84 import asyncio.sslproto8586 waiter = loop.create_future()87 ssl_protocol = asyncio.sslproto.SSLProtocol(88 loop,89 protocol,90 sslcontext,91 waiter,92 server_side=False,93 server_hostname=server_hostname,94 call_connection_made=False,95 )9697 transport.set_protocol(ssl_protocol)98 loop.call_soon(ssl_protocol.connection_made, transport)99 loop.call_soon(transport.resume_reading) # type: ignore100101 await waiter102 return ssl_protocol._app_transport103104 transport = await asyncio.wait_for(105 loop_start_tls(106 transport=transport,107 protocol=protocol,
223 unmodified lines
modified:tests/test_concurrency.py-5 removed
1import sys21import pytest2import trio34from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig
23 unmodified lines
28 """29 See that the concurrency backend can make a connection without TLS then30 start TLS on an existing connection.31 """34 if isinstance(backend, AsyncioBackend) and sys.version_info < (3, 7):35 pytest.xfail(reason="Requires Python 3.7+ for AbstractEventLoop.start_tls()")3632 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())33 timeout = TimeoutConfig(5)3435 stream = await backend.open_tcp_stream(
30 unmodified lines