Skip to content
Myr

Add ConcurrencyBackend.start_tls() (#263)

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

authored by Seth Michael Larson · committed by GitHub · parent a4b93b9 · Browse files · 8 files+85 added−7 removed
This commit

State

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

Diff stats

8 files+85 added−7 removed

  • tests:+31 added−0 removed
  • other:+54 added−7 removed

8 files+85 added−7 removed

modified:httpx/concurrency/asyncio.py+39 added-1 removed
16 unmodified lines
17from ..config import PoolLimits, TimeoutConfig18from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout19from .base import (20 BaseBackgroundManager,21 BasePoolSemaphore,21 BaseEvent,22 BasePoolSemaphore,23 BaseQueue,24 BaseStream,25 ConcurrencyBackend,26 TimeoutFlag,
166 unmodified lines
193 return Stream(194 stream_reader=stream_reader, stream_writer=stream_writer, timeout=timeout195 )196197 async def start_tls(198 self,199 stream: BaseStream,200 hostname: str,201 ssl_context: ssl.SSLContext,202 timeout: TimeoutConfig,203 ) -> BaseStream:204205 loop = self.loop206 if not hasattr(loop, "start_tls"): # pragma: no cover207 raise NotImplementedError(208 "asyncio.AbstractEventLoop.start_tls() is only available in Python 3.7+"209 )210211 assert isinstance(stream, Stream)212213 stream_reader = asyncio.StreamReader()214 protocol = asyncio.StreamReaderProtocol(stream_reader)215 transport = stream.stream_writer.transport216217 loop_start_tls = loop.start_tls # type: ignore218 transport = await asyncio.wait_for(219 loop_start_tls(220 transport=transport,221 protocol=protocol,222 sslcontext=ssl_context,223 server_hostname=hostname,224 ),225 timeout=timeout.connect_timeout,226 )227228 stream_reader.set_transport(transport)229 stream.stream_reader = stream_reader230 stream.stream_writer = asyncio.StreamWriter(231 transport=transport, protocol=protocol, reader=stream_reader, loop=loop232 )233 return stream234235 async def run_in_threadpool(236 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any237 ) -> typing.Any:238 if kwargs:
49 unmodified lines
modified:httpx/concurrency/base.py+9 added
114 unmodified lines
115 timeout: TimeoutConfig,116 ) -> BaseStream:117 raise NotImplementedError() # pragma: no cover118119 async def start_tls(120 self,121 stream: BaseStream,122 hostname: str,123 ssl_context: ssl.SSLContext,124 timeout: TimeoutConfig,125 ) -> BaseStream:126 raise NotImplementedError() # pragma: no cover127128 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:129 raise NotImplementedError() # pragma: no cover130131 async def run_in_threadpool(
55 unmodified lines
modified:httpx/dispatch/asgi.py+2 added-2 removed
1import typing23from .base import AsyncDispatcher4from ..concurrency.base import ConcurrencyBackend3from ..concurrency.asyncio import AsyncioBackend4from ..concurrency.base import ConcurrencyBackend5from ..config import CertTypes, TimeoutTypes, VerifyTypes6from ..models import AsyncRequest, AsyncResponse7from .base import AsyncDispatcher8910class ASGIDispatch(AsyncDispatcher):11 """
183 unmodified lines
modified:httpx/dispatch/connection_pool.py+1 added-1 removed
1import typing23from .base import AsyncDispatcher3from ..concurrency.asyncio import AsyncioBackend4from ..concurrency.base import ConcurrencyBackend5from ..config import (6 DEFAULT_POOL_LIMITS,
4 unmodified lines
11 TimeoutTypes,12 VerifyTypes,13)14from ..models import AsyncRequest, AsyncResponse, Origin15from .base import AsyncDispatcher16from .connection import HTTPConnection1718CONNECTIONS_DICT = typing.Dict[Origin, typing.List[HTTPConnection]]19
150 unmodified lines
modified:httpx/dispatch/connection.py+1 added-1 removed
1import functools2import ssl3import typing45from .base import AsyncDispatcher5from ..concurrency.asyncio import AsyncioBackend6from ..concurrency.base import ConcurrencyBackend7from ..config import (8 DEFAULT_TIMEOUT_CONFIG,
5 unmodified lines
14 TimeoutTypes,15 VerifyTypes,16)17from ..models import AsyncRequest, AsyncResponse, Origin18from .base import AsyncDispatcher19from .http2 import HTTP2Connection20from .http11 import HTTP11Connection2122# Callback signature: async def callback(conn: HTTPConnection) -> None
105 unmodified lines
modified:httpx/dispatch/threaded.py+1 added-1 removed
1from .base import AsyncDispatcher, Dispatcher1from ..concurrency.base import ConcurrencyBackend2from ..config import CertTypes, TimeoutTypes, VerifyTypes3from ..models import (4 AsyncRequest,
4 unmodified lines
9 RequestData,10 Response,11 ResponseContent,12)13from .base import AsyncDispatcher, Dispatcher141516class ThreadedDispatcher(AsyncDispatcher):17 """
80 unmodified lines
modified:httpx/dispatch/wsgi.py+1 added-1 removed
1import io2import typing34from .base import Dispatcher4from ..config import CertTypes, TimeoutTypes, VerifyTypes5from ..models import Request, Response6from .base import Dispatcher789class WSGIDispatch(Dispatcher):10 """
154 unmodified lines
added:tests/test_concurrency.py+31 added
1import sys23import pytest45from httpx import AsyncioBackend, HTTPVersionConfig, SSLConfig, TimeoutConfig678@pytest.mark.xfail(9 sys.version_info < (3, 7),10 reason="Requires Python 3.7+ for AbstractEventLoop.start_tls()",11)12@pytest.mark.asyncio13async def test_start_tls_on_socket_stream(https_server):14 """15 See that the backend can make a connection without TLS then16 start TLS on an existing connection.17 """18 backend = AsyncioBackend()19 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())20 timeout = TimeoutConfig(5)2122 stream = await backend.connect("127.0.0.1", 8001, None, timeout)23 assert stream.is_connection_dropped() is False24 assert stream.stream_writer.get_extra_info("cipher", default=None) is None2526 stream = await backend.start_tls(stream, "127.0.0.1", ctx, timeout)27 assert stream.is_connection_dropped() is False28 assert stream.stream_writer.get_extra_info("cipher", default=None) is not None2930 await stream.write(b"GET / HTTP/1.1\r\n\r\n")31 assert (await stream.read(8192, timeout)).startswith(b"HTTP/1.1 200 OK\r\n")