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 fileTake it
Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a
line 1why, one step back1import functoolsFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 2–10why, one step back2import typing34import h2.connection5import h1167from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig8from .exceptions import ConnectTimeout9from .http2 import HTTP2Connection10from .http11 import HTTP11ConnectionAdd stream ID to HTTP2ConnectionTom Christiebefore adoption0bdcbae
line 11why, one step back11from .models import Client, Origin, Request, ResponseStream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f
line 12why, one step back12from .streams import Protocol, connectFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 13why, one step back13More specfic annotations on callbacksTom Christiebefore adoption06f6d50
lines 14–15why, one step back14# Callback signature: async def callback(conn: HTTPConnection) -> None15ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 16why, one step back16More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 17why, one step back17First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 18–23why, one step back18class 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,More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 24why, one step back24 release_func: typing.Optional[ReleaseCallback] = None,First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 25–28why, one step back25 ):26 self.origin = Origin(origin) if isinstance(origin, str) else origin27 self.ssl = ssl28 self.timeout = timeoutMore specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 29why, one step back29 self.release_func = release_funcFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 30–40why, one step back30 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:Add http2 connection re-useTom Christiebefore adoptionf942eb9
line 41why, one step back41 await self.connect(ssl, timeout)First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 42–50why, one step back4243 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 response50Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 51–64why, one step back51 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 None64More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 65why, one step back65 if self.release_func is None:Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 66–67why, one step back66 on_release = None67 else:More specfic annotations on callbacksTom Christiebefore adoption06f6d50
line 68why, one step back68 on_release = functools.partial(self.release_func, self)Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 69–87why, one step back6970 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 )87First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 88–90why, one step back88 async def close(self) -> None:89 if self.h2_connection is not None:90 await self.h2_connection.close()Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f
line 91why, one step back91 elif self.h11_connection is not None:First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
line 92why, one step back92 await self.h11_connection.close()Add http2 connection re-useTom Christiebefore adoptionf942eb9
lines 93–96why, one step back9394 @property95 def is_http2(self) -> bool:96 return self.h2_connection is not NoneFirst pass at HTTP/2 supportTom Christiebefore adoption53f3dc4
lines 97–104why, one step back9798 @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