Skip to content
Myr

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

2 files+69 added−63 removed

modified:httpx/dispatch/connection.py+61 added-1 removed
1import functools2import ssl3import typing45import h1167from ..concurrency.base import ConcurrencyBackend, lookup_backend8from ..config import CertTypes, SSLConfig, Timeout, VerifyTypes7from ..models import Origin, Request, Response9from ..models import URL, Origin, Request, Response10from ..utils import get_logger11from .base import Dispatcher12from .http2 import HTTP2Connection13from .http11 import HTTP11Connection
85 unmodified lines
99 else:100 assert http_version == "HTTP/1.1"101 self.h11_connection = HTTP11Connection(stream, on_release=on_release)102103 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 timeout115116 # 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_PROTOCOL121122 # 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_release129130 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 None136137 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 pass152153 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)160161 async def get_ssl_context(self, ssl: SSLConfig) -> typing.Optional[ssl.SSLContext]:162 if not self.origin.is_ssl:163 return None164
32 unmodified lines
modified:httpx/dispatch/proxy_http.py+8 added-62 removed
1import enum2import typing3from base64 import b64encode45import h1165from ..concurrency.base import ConcurrencyBackend8from ..config import (9 DEFAULT_POOL_LIMITS,10 CertTypes,11 PoolLimits,12 SSLConfig,13 Timeout,14 VerifyTypes,15)6from ..config import DEFAULT_POOL_LIMITS, CertTypes, PoolLimits, Timeout, VerifyTypes7from ..exceptions import ProxyError8from ..models import URL, Headers, HeaderTypes, Origin, Request, Response, URLTypes9from ..utils import get_logger10from .connection import HTTPConnection11from .connection_pool import ConnectionPool21from .http2 import HTTP2Connection22from .http11 import HTTP11Connection1213logger = get_logger(__name__)1415
82 unmodified lines
98 self.active_connections.remove(connection)99 connection.origin = origin100 self.active_connections.add(connection)101113 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)111112 return connection
41 unmodified lines
154 await proxy_response.read()155156 return connection157164 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 timeout169170 # 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.socket179180 # 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 None187188 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 )203204 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)211158 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 and
34 unmodified lines