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
Provenance
- Commit
2c4a1dd- Tree
1243a29- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
56afde1
4 files changed+26 added−62 removed
modified:httpx/dispatch/base.py-23 removed
48 unmodified lines49 exc_value: BaseException = None,50 traceback: TracebackType = None,51 ) -> None:52 await self.close()53-54-55-class OpenConnection:56- """57- Base class for connection classes that interact with a host via HTTP.58- """59-60- @property61- def is_http2(self) -> bool:62- raise NotImplementedError() # pragma: no cover63-64- async def send(self, request: Request, timeout: Timeout = None,) -> Response:65- raise NotImplementedError() # pragma: no cover66-67- @property68- def is_closed(self) -> bool:69- raise NotImplementedError() # pragma: no cover70-71- def is_connection_dropped(self) -> bool:72- raise NotImplementedError() # pragma: no cover73-74- async def close(self) -> None:75- raise NotImplementedError() # pragma: no covermodified:httpx/dispatch/connection.py+24 added-35 removed
2 unmodified lines3 import typing4 5 import h116 7-from ..backends.base import BaseSocketStream, ConcurrencyBackend, lookup_backend7+from ..backends.base import ConcurrencyBackend, lookup_backend8 from ..config import CertTypes, SSLConfig, Timeout, VerifyTypes9 from ..models import URL, Origin, Request, Response10 from ..utils import get_logger11-from .base import Dispatcher, OpenConnection11+from .base import Dispatcher12 from .http2 import HTTP2Connection13 from .http11 import HTTP11Connection14 15 # Callback signature: async def callback(conn: HTTPConnection) -> None20 unmodified lines36 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] = None42 43 async def send(self, request: Request, timeout: Timeout = None) -> Response:44 timeout = Timeout() if timeout is None else timeout45 46- 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)48 49- 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)50 52- return response53-54- 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 lines63 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 )77 79- http_version = stream.get_http_version()78+ http_version = socket.get_http_version()79 logger.trace(f"connected http_version={http_version!r}")80 82- 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)84 85 async def tunnel_start_tls(86 self, origin: Origin, proxy_url: URL, timeout: Timeout = None,87 ) -> None:3 unmodified lines91 timeout = Timeout() if timeout is None else timeout92 93 # 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_PROTOCOL99 100 # Store this information here so that we can transfer25 unmodified lines126 # 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 pass129 129- self.set_open_connection(http_version, socket=socket, on_release=on_release)130-131- 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)136 137 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:138 if not self.origin.is_ssl:139 return None2 unmodified lines142 return await self.backend.run_in_threadpool(ssl.load_ssl_context, self.http2)143 144 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()148 149 @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_http2152 153 @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_closed156 157 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()159 160 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 lines7 from ..content_streams import AsyncIteratorStream8 from ..exceptions import ConnectionClosed, ProtocolError9 from ..models import Request, Response10 from ..utils import get_logger11-from .base import OpenConnection11 12 H11Event = typing.Union[13 h11.Request,14 h11.Response,12 unmodified lines27 28 logger = get_logger(__name__)29 30 32-class HTTP11Connection(OpenConnection):31+class HTTP11Connection:32 READ_NUM_BYTES = 409633 34 def __init__(35 self,171 unmodified linesmodified:httpx/dispatch/http2.py+1 added-2 removed
14 unmodified lines15 from ..content_streams import AsyncIteratorStream16 from ..exceptions import ProtocolError17 from ..models import Request, Response18 from ..utils import get_logger19-from .base import OpenConnection19 20 logger = get_logger(__name__)21 22 24-class HTTP2Connection(OpenConnection):23+class HTTP2Connection:24 READ_NUM_BYTES = 409625 CONFIG = H2Configuration(validate_inbound_headers=False)26 27 def __init__(271 unmodified lines