Move tunnel_start_tls() to HTTPConnection (#609)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by Tom Christie · parent df9dc6d · Browse files · 2 files·+69 added−63 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−63 removed
- other:+69 added−63 removed
Provenance
- Commit
e1f5b8b- Tree
592aed1- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
df9dc6d
2 files changed+69 added−63 removed
modified:httpx/dispatch/connection.py+61 added-1 removed
1 import functools2 import ssl3 import typing4 5+import h116+7 from ..concurrency.base import ConcurrencyBackend, lookup_backend8 from ..config import CertTypes, SSLConfig, Timeout, VerifyTypes7-from ..models import Origin, Request, Response9+from ..models import URL, Origin, Request, Response10 from ..utils import get_logger11 from .base import Dispatcher12 from .http2 import HTTP2Connection13 from .http11 import HTTP11Connection85 unmodified lines99 else:100 assert http_version == "HTTP/1.1"101 self.h11_connection = HTTP11Connection(stream, on_release=on_release)102 103+ async def tunnel_start_tls(104+ self,105+ origin: Origin,106+ proxy_url: URL,107+ timeout: Timeout = None,108+ cert: CertTypes = None,109+ verify: VerifyTypes = True,110+ ) -> None:111+ """112+ Upgrade this connection to use TLS, assuming it represents a TCP tunnel.113+ """114+ timeout = Timeout() if timeout is None else timeout115+116+ # First, check that we are in the correct state to start TLS, i.e. we've117+ # just agreed to switch protocols with the server via HTTP/1.1.118+ h11_connection = self.h11_connection119+ assert h11_connection is not None120+ assert h11_connection.h11_state.our_state == h11.SWITCHED_PROTOCOL121+122+ # Store this information here so that we can transfer123+ # it to the new internal connection object after124+ # the old one goes to 'SWITCHED_PROTOCOL'.125+ # Note that the negotiated 'http_version' may change after the TLS upgrade.126+ http_version = "HTTP/1.1"127+ socket = h11_connection.socket128+ on_release = h11_connection.on_release129+130+ if origin.is_ssl:131+ # Pull the socket stream off the internal HTTP connection object,132+ # and run start_tls().133+ ssl_config = SSLConfig(cert=cert, verify=verify)134+ ssl_context = await self.get_ssl_context(ssl_config)135+ assert ssl_context is not None136+137+ logger.trace(f"tunnel_start_tls proxy_url={proxy_url!r} origin={origin!r}")138+ socket = await socket.start_tls(139+ hostname=origin.host, ssl_context=ssl_context, timeout=timeout140+ )141+ http_version = socket.get_http_version()142+ logger.trace(143+ f"tunnel_tls_complete "144+ f"proxy_url={proxy_url!r} "145+ f"origin={origin!r} "146+ f"http_version={http_version!r}"147+ )148+ else:149+ # User requested the use of a tunnel, but they're performing a plain-text150+ # HTTP request. Don't try to upgrade to TLS in this case.151+ pass152+153+ if http_version == "HTTP/2":154+ self.h2_connection = HTTP2Connection(155+ socket, self.backend, on_release=on_release156+ )157+ else:158+ assert http_version == "HTTP/1.1"159+ self.h11_connection = HTTP11Connection(socket, on_release=on_release)160+161 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:162 if not self.origin.is_ssl:163 return None164 32 unmodified linesmodified:httpx/dispatch/proxy_http.py+8 added-62 removed
1 import enum2 import typing3 from base64 import b64encode4 5-import h116-5 from ..concurrency.base import ConcurrencyBackend8-from ..config import (9- DEFAULT_POOL_LIMITS,10- CertTypes,11- PoolLimits,12- SSLConfig,13- Timeout,14- VerifyTypes,15-)6+from ..config import DEFAULT_POOL_LIMITS, CertTypes, PoolLimits, Timeout, VerifyTypes7 from ..exceptions import ProxyError8 from ..models import URL, Headers, HeaderTypes, Origin, Request, Response, URLTypes9 from ..utils import get_logger10 from .connection import HTTPConnection11 from .connection_pool import ConnectionPool21-from .http2 import HTTP2Connection22-from .http11 import HTTP11Connection12 13 logger = get_logger(__name__)14 15 82 unmodified lines98 self.active_connections.remove(connection)99 connection.origin = origin100 self.active_connections.add(connection)101 113- timeout = Timeout() if timeout is None else timeout114- await self.tunnel_start_tls(origin, connection, timeout)102+ await connection.tunnel_start_tls(103+ origin=origin,104+ proxy_url=self.proxy_url,105+ timeout=timeout,106+ cert=self.cert,107+ verify=self.verify,108+ )109 else:110 self.active_connections.add(connection)111 112 return connection41 unmodified lines154 await proxy_response.read()155 156 return connection157 164- async def tunnel_start_tls(165- self, origin: Origin, connection: HTTPConnection, timeout: Timeout = None166- ) -> None:167- """Runs start_tls() on a TCP-tunneled connection"""168- timeout = Timeout() if timeout is None else timeout169-170- # Store this information here so that we can transfer171- # it to the new internal connection object after172- # the old one goes to 'SWITCHED_PROTOCOL'.173- http_version = "HTTP/1.1"174- http_connection = connection.h11_connection175- assert http_connection is not None176- assert http_connection.h11_state.our_state == h11.SWITCHED_PROTOCOL177- on_release = http_connection.on_release178- socket = http_connection.socket179-180- # If we need to start TLS again for the target server181- # we need to pull the socket stream off the internal182- # HTTP connection object and run start_tls()183- if origin.is_ssl:184- ssl_config = SSLConfig(cert=self.cert, verify=self.verify)185- ssl_context = await connection.get_ssl_context(ssl_config)186- assert ssl_context is not None187-188- logger.trace(189- f"tunnel_start_tls "190- f"proxy_url={self.proxy_url!r} "191- f"origin={origin!r}"192- )193- socket = await socket.start_tls(194- hostname=origin.host, ssl_context=ssl_context, timeout=timeout195- )196- http_version = socket.get_http_version()197- logger.trace(198- f"tunnel_tls_complete "199- f"proxy_url={self.proxy_url!r} "200- f"origin={origin!r} "201- f"http_version={http_version!r}"202- )203-204- if http_version == "HTTP/2":205- connection.h2_connection = HTTP2Connection(206- socket, self.backend, on_release=on_release207- )208- else:209- assert http_version == "HTTP/1.1"210- connection.h11_connection = HTTP11Connection(socket, on_release=on_release)211-158 def should_forward_origin(self, origin: Origin) -> bool:159 """Determines if the given origin should160 be forwarded or tunneled. If 'proxy_mode' is 'DEFAULT'161 then the proxy will forward all 'HTTP' requests and34 unmodified lines