Skip to content
Myr

Drop OpenConnection (#700)

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

authored by Tom Christie · committed by GitHub · parent 56afde1 · Browse files · 4 files+26 added−62 removed
This commit

State

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

Diff stats

4 files+26 added−62 removed

  • other:+26 added−62 removed

4 files+26 added−62 removed

modified:httpx/dispatch/base.py-23 removed
48 unmodified lines
49 exc_value: BaseException = None,50 traceback: TracebackType = None,51 ) -> None:52 await self.close()535455class OpenConnection:56 """57 Base class for connection classes that interact with a host via HTTP.58 """5960 @property61 def is_http2(self) -> bool:62 raise NotImplementedError() # pragma: no cover6364 async def send(self, request: Request, timeout: Timeout = None,) -> Response:65 raise NotImplementedError() # pragma: no cover6667 @property68 def is_closed(self) -> bool:69 raise NotImplementedError() # pragma: no cover7071 def is_connection_dropped(self) -> bool:72 raise NotImplementedError() # pragma: no cover7374 async def close(self) -> None:75 raise NotImplementedError() # pragma: no cover
modified:httpx/dispatch/connection.py+24 added-35 removed
2 unmodified lines
3import typing45import h1167from ..backends.base import BaseSocketStream, ConcurrencyBackend, lookup_backend7from ..backends.base import ConcurrencyBackend, lookup_backend8from ..config import CertTypes, SSLConfig, Timeout, VerifyTypes9from ..models import URL, Origin, Request, Response10from ..utils import get_logger11from .base import Dispatcher, OpenConnection11from .base import Dispatcher12from .http2 import HTTP2Connection13from .http11 import HTTP11Connection1415# Callback signature: async def callback(conn: HTTPConnection) -> None
20 unmodified lines
36 self.http2 = http237 self.backend = lookup_backend(backend)38 self.release_func = release_func39 self.uds = uds40 self.open_connection: typing.Optional[OpenConnection] = None40 self.connection: typing.Union[None, HTTP11Connection, HTTP2Connection] = None41 self.expires_at: typing.Optional[float] = None4243 async def send(self, request: Request, timeout: Timeout = None) -> Response:44 timeout = Timeout() if timeout is None else timeout4546 if self.open_connection is None:47 await self.connect(timeout=timeout)46 if self.connection is None:47 self.connection = await self.connect(timeout=timeout)4849 assert self.open_connection is not None50 response = await self.open_connection.send(request, timeout=timeout)49 return await self.connection.send(request, timeout=timeout)5052 return response5354 async def connect(self, timeout: Timeout) -> None:51 async def connect(52 self, timeout: Timeout53 ) -> typing.Union[HTTP11Connection, HTTP2Connection]:54 host = self.origin.host55 port = self.origin.port56 ssl_context = await self.get_ssl_context(self.ssl)57
5 unmodified lines
63 if self.uds is None:64 logger.trace(65 f"start_connect tcp host={host!r} port={port!r} timeout={timeout!r}"66 )68 stream = await self.backend.open_tcp_stream(67 socket = await self.backend.open_tcp_stream(68 host, port, ssl_context, timeout69 )70 else:71 logger.trace(72 f"start_connect uds path={self.uds!r} host={host!r} timeout={timeout!r}"73 )75 stream = await self.backend.open_uds_stream(74 socket = await self.backend.open_uds_stream(75 self.uds, host, ssl_context, timeout76 )7779 http_version = stream.get_http_version()78 http_version = socket.get_http_version()79 logger.trace(f"connected http_version={http_version!r}")8082 self.set_open_connection(http_version, socket=stream, on_release=on_release)81 if http_version == "HTTP/2":82 return HTTP2Connection(socket, self.backend, on_release=on_release)83 return HTTP11Connection(socket, on_release=on_release)8485 async def tunnel_start_tls(86 self, origin: Origin, proxy_url: URL, timeout: Timeout = None,87 ) -> None:
3 unmodified lines
91 timeout = Timeout() if timeout is None else timeout9293 # First, check that we are in the correct state to start TLS, i.e. we've94 # just agreed to switch protocols with the server via HTTP/1.1.94 assert isinstance(self.open_connection, HTTP11Connection)95 h11_connection = self.open_connection95 assert isinstance(self.connection, HTTP11Connection)96 h11_connection = self.connection97 assert h11_connection is not None98 assert h11_connection.h11_state.our_state == h11.SWITCHED_PROTOCOL99100 # Store this information here so that we can transfer
25 unmodified lines
126 # User requested the use of a tunnel, but they're performing a plain-text127 # HTTP request. Don't try to upgrade to TLS in this case.128 pass129129 self.set_open_connection(http_version, socket=socket, on_release=on_release)130131 def set_open_connection(132 self,133 http_version: str,134 socket: BaseSocketStream,135 on_release: typing.Optional[typing.Callable],136 ) -> None:130 if http_version == "HTTP/2":138 self.open_connection = HTTP2Connection(131 self.connection = HTTP2Connection(132 socket, self.backend, on_release=on_release133 )134 else:142 assert http_version == "HTTP/1.1"143 self.open_connection = HTTP11Connection(socket, on_release=on_release)135 self.connection = HTTP11Connection(socket, on_release=on_release)136137 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:138 if not self.origin.is_ssl:139 return None
2 unmodified lines
142 return await self.backend.run_in_threadpool(ssl.load_ssl_context, self.http2)143144 async def close(self) -> None:145 logger.trace("close_connection")154 if self.open_connection is not None:155 await self.open_connection.close()146 if self.connection is not None:147 await self.connection.close()148149 @property150 def is_http2(self) -> bool:159 assert self.open_connection is not None160 return self.open_connection.is_http2151 return self.connection is not None and self.connection.is_http2152153 @property154 def is_closed(self) -> bool:164 assert self.open_connection is not None165 return self.open_connection.is_closed155 return self.connection is not None and self.connection.is_closed156157 def is_connection_dropped(self) -> bool:168 assert self.open_connection is not None169 return self.open_connection.is_connection_dropped()158 return self.connection is not None and self.connection.is_connection_dropped()159160 def __repr__(self) -> str:161 class_name = self.__class__.__name__162 return f"{class_name}(origin={self.origin!r})"
modified:httpx/dispatch/http11.py+1 added-2 removed
6 unmodified lines
7from ..content_streams import AsyncIteratorStream8from ..exceptions import ConnectionClosed, ProtocolError9from ..models import Request, Response10from ..utils import get_logger11from .base import OpenConnection1112H11Event = typing.Union[13 h11.Request,14 h11.Response,
12 unmodified lines
2728logger = get_logger(__name__)293032class HTTP11Connection(OpenConnection):31class HTTP11Connection:32 READ_NUM_BYTES = 40963334 def __init__(35 self,
171 unmodified lines
modified:httpx/dispatch/http2.py+1 added-2 removed
14 unmodified lines
15from ..content_streams import AsyncIteratorStream16from ..exceptions import ProtocolError17from ..models import Request, Response18from ..utils import get_logger19from .base import OpenConnection1920logger = get_logger(__name__)212224class HTTP2Connection(OpenConnection):23class HTTP2Connection:24 READ_NUM_BYTES = 409625 CONFIG = H2Configuration(validate_inbound_headers=False)2627 def __init__(
271 unmodified lines