Skip to content
Myr

connection.py

This file

Path

httpcore/connection.py

104 lines · 3.4 KiB · Python

Last changed by

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

History of this file

Take it

1import functools2import typing34import h2.connection5import h1167from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig8from .exceptions import ConnectTimeout9from .http2 import HTTP2Connection10from .http11 import HTTP11Connection11from .models import Client, Origin, Request, Response12from .streams import Protocol, connect1314# Callback signature: async def callback(conn: HTTPConnection) -> None15ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]161718class HTTPConnection(Client):19    def __init__(20        self,21        origin: typing.Union[str, Origin],22        ssl: SSLConfig = DEFAULT_SSL_CONFIG,23        timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,24        release_func: typing.Optional[ReleaseCallback] = None,25    ):26        self.origin = Origin(origin) if isinstance(origin, str) else origin27        self.ssl = ssl28        self.timeout = timeout29        self.release_func = release_func30        self.h11_connection = None  # type: typing.Optional[HTTP11Connection]31        self.h2_connection = None  # type: typing.Optional[HTTP2Connection]3233    async def send(34        self,35        request: Request,36        *,37        ssl: typing.Optional[SSLConfig] = None,38        timeout: typing.Optional[TimeoutConfig] = None,39    ) -> Response:40        if self.h11_connection is None and self.h2_connection is None:41            await self.connect(ssl, timeout)4243        if self.h2_connection is not None:44            response = await self.h2_connection.send(request, ssl=ssl, timeout=timeout)45        else:46            assert self.h11_connection is not None47            response = await self.h11_connection.send(request, ssl=ssl, timeout=timeout)4849        return response5051    async def connect(52        self,53        ssl: typing.Optional[SSLConfig] = None,54        timeout: typing.Optional[TimeoutConfig] = None,55    ) -> None:56        if ssl is None:57            ssl = self.ssl58        if timeout is None:59            timeout = self.timeout6061        hostname = self.origin.hostname62        port = self.origin.port63        ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None6465        if self.release_func is None:66            on_release = None67        else:68            on_release = functools.partial(self.release_func, self)6970        reader, writer, protocol = await connect(hostname, port, ssl_context, timeout)71        if protocol == Protocol.HTTP_2:72            self.h2_connection = HTTP2Connection(73                reader,74                writer,75                origin=self.origin,76                timeout=self.timeout,77                on_release=on_release,78            )79        else:80            self.h11_connection = HTTP11Connection(81                reader,82                writer,83                origin=self.origin,84                timeout=self.timeout,85                on_release=on_release,86            )8788    async def close(self) -> None:89        if self.h2_connection is not None:90            await self.h2_connection.close()91        elif self.h11_connection is not None:92            await self.h11_connection.close()9394    @property95    def is_http2(self) -> bool:96        return self.h2_connection is not None9798    @property99    def is_closed(self) -> bool:100        if self.h2_connection is not None:101            return self.h2_connection.is_closed102        else:103            assert self.h11_connection is not None104            return self.h11_connection.is_closed