connection.py
This file
Path
httpcore/connection.py
88 lines · 3.1 KiB · Python
Last changed by
AdaptersTom Christiebefore adoptionfab6fcd
History of this fileTake it
Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a
line 1why, one step back1import functoolsFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 2–6why, one step back2import typing34import h2.connection5import h116AdaptersTom Christiebefore adoptionfab6fcd
line 7why, one step back7from .adapters import AdapterFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 8–11why, one step back8from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig9from .exceptions import ConnectTimeout10from .http2 import HTTP2Connection11from .http11 import HTTP11ConnectionAdaptersTom Christiebefore adoptionfab6fcd
line 12why, one step back12from .models import Origin, Request, ResponseStream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f
line 13why, one step back13from .streams import Protocol, connectFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 14why, one step back14More specfic annotations on callbacksTom Christiebefore adoption06f6d50
lines 15–16why, one step back15# Callback signature: async def callback(conn: HTTPConnection) -> None16ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 17why, one step back17More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 18why, one step back18AdaptersTom Christiebefore adoptionfab6fcd
line 19why, one step back19class HTTPConnection(Adapter):First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 20–24why, one step back20 def __init__(21 self,22 origin: typing.Union[str, Origin],23 ssl: SSLConfig = DEFAULT_SSL_CONFIG,24 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 25why, one step back25 release_func: typing.Optional[ReleaseCallback] = None,First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 26–29why, one step back26 ):27 self.origin = Origin(origin) if isinstance(origin, str) else origin28 self.ssl = ssl29 self.timeout = timeoutMore specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 30why, one step back30 self.release_func = release_funcFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 31–33why, one step back31 self.h11_connection = None # type: typing.Optional[HTTP11Connection]32 self.h2_connection = None # type: typing.Optional[HTTP2Connection]33AdaptersTom Christiebefore adoptionfab6fcd
lines 34–37why, one step back34 def prepare_request(self, request: Request) -> None:35 pass3637 async def send(self, request: Request, **options: typing.Any) -> Response:First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 38why, one step back38 if self.h11_connection is None and self.h2_connection is None:AdaptersTom Christiebefore adoptionfab6fcd
line 39why, one step back39 await self.connect(**options)First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 40–41why, one step back4041 if self.h2_connection is not None:AdaptersTom Christiebefore adoptionfab6fcd
line 42why, one step back42 response = await self.h2_connection.send(request, **options)First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 43–44why, one step back43 else:44 assert self.h11_connection is not NoneAdaptersTom Christiebefore adoptionfab6fcd
line 45why, one step back45 response = await self.h11_connection.send(request, **options)First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 46–48why, one step back4647 return response48AdaptersTom Christiebefore adoptionfab6fcd
lines 49–53why, one step back49 async def connect(self, **options: typing.Any) -> None:50 ssl = options.get("ssl", self.ssl)51 timeout = options.get("timeout", self.timeout)52 assert isinstance(ssl, SSLConfig)53 assert isinstance(timeout, TimeoutConfig)Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 54–58why, one step back5455 hostname = self.origin.hostname56 port = self.origin.port57 ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None58More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 59why, one step back59 if self.release_func is None:Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 60–61why, one step back60 on_release = None61 else:More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 62why, one step back62 on_release = functools.partial(self.release_func, self)Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 63–65why, one step back6364 reader, writer, protocol = await connect(hostname, port, ssl_context, timeout)65 if protocol == Protocol.HTTP_2:AdaptersTom Christiebefore adoptionfab6fcd
line 66why, one step back66 self.h2_connection = HTTP2Connection(reader, writer, on_release=on_release)Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 67–68why, one step back67 else:68 self.h11_connection = HTTP11Connection(AdaptersTom Christiebefore adoptionfab6fcd
line 69why, one step back69 reader, writer, on_release=on_releaseAdd http2 connection re-useTom Christiebefore adoptionf942eb9
lines 70–71why, one step back70 )71First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 72–74why, one step back72 async def close(self) -> None:73 if self.h2_connection is not None:74 await self.h2_connection.close()Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f
line 75why, one step back75 elif self.h11_connection is not None:First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 76why, one step back76 await self.h11_connection.close()Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 77–80why, one step back7778 @property79 def is_http2(self) -> bool:80 return self.h2_connection is not NoneFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 81–88why, one step back8182 @property83 def is_closed(self) -> bool:84 if self.h2_connection is not None:85 return self.h2_connection.is_closed86 else:87 assert self.h11_connection is not None88 return self.h11_connection.is_closed