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

Tighten up connection acquiry/releaseTom Christiebefore adoption28c505a

line 1why, one step back
1import functools

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 2–10why, one step back
2import typing
34import h2.connection5import h1167from .config import DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, SSLConfig, TimeoutConfig8from .exceptions import ConnectTimeout9from .http2 import HTTP2Connection10from .http11 import HTTP11Connection

Add stream ID to HTTP2ConnectionTom Christiebefore adoption0bdcbae

line 11why, one step back
11from .models import Client, Origin, Request, Response

Stream refactoring and HTTP/2 test caseTom Christiebefore adoptiona44190f

line 12why, one step back
12from .streams import Protocol, connect

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 13why, one step back
13

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

lines 14–15why, one step back
14# Callback signature: async def callback(conn: HTTPConnection) -> None
15ReleaseCallback = typing.Callable[["HTTPConnection"], typing.Awaitable[None]]

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 16why, one step back
16

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 17why, one step back
17

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 18–23why, one step back
18class 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 back
24 release_func: typing.Optional[ReleaseCallback] = None,

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 25–28why, one step back
25 ):
26 self.origin = Origin(origin) if isinstance(origin, str) else origin27 self.ssl = ssl28 self.timeout = timeout

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 29why, one step back
29 self.release_func = release_func

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 30–40why, one step back
30 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 back
41 await self.connect(ssl, timeout)

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 42–50why, one step back
42
43 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 response50

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 51–64why, one step back
51 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 None64

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 65why, one step back
65 if self.release_func is None:

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 66–67why, one step back
66 on_release = None
67 else:

More specfic annotations on callbacksTom Christiebefore adoption06f6d50

line 68why, one step back
68 on_release = functools.partial(self.release_func, self)

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 69–87why, one step back
69
70 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 )87

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 88–90why, one step back
88 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 back
91 elif self.h11_connection is not None:

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

line 92why, one step back
92 await self.h11_connection.close()

Add http2 connection re-useTom Christiebefore adoptionf942eb9

lines 93–96why, one step back
93
94 @property95 def is_http2(self) -> bool:96 return self.h2_connection is not None

First pass at HTTP/2 supportTom Christiebefore adoption53f3dc4

lines 97–104why, one step back
97
98 @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