Skip to content
Myr

Drop per-request `cert`, `verify`, and `trust_env` (#617)

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

* Drop per-request cert/verify/trust_env * Remove cert/verify from the dispatcher API * Apply lint * Reintroduce cert/verify/trust_env on client methods, with errors

authored by Florimond Manca · committed by Tom Christie · parent f3b7999 · Browse files · 7 files+36 added−126 removed
This commit

State

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

Diff stats

7 files+36 added−126 removed

  • tests:+2 added−14 removed
  • other:+34 added−112 removed

7 files+36 added−126 removed

modified:httpx/dispatch/asgi.py+2 added-9 removed
1import typing23from ..config import CertTypes, TimeoutTypes, VerifyTypes3from ..config import TimeoutTypes4from ..content_streams import ByteStream5from ..models import Request, Response6from .base import Dispatcher7
45 unmodified lines
53 self.raise_app_exceptions = raise_app_exceptions54 self.root_path = root_path55 self.client = client5657 async def send(58 self,59 request: Request,60 verify: VerifyTypes = None,61 cert: CertTypes = None,62 timeout: TimeoutTypes = None,63 ) -> Response:6457 async def send(self, request: Request, timeout: TimeoutTypes = None) -> Response:58 scope = {59 "type": "http",60 "asgi": {"version": "3.0"},61 "http_version": "1.1",
65 unmodified lines
modified:httpx/dispatch/base.py+3 added-11 removed
1import typing2from types import TracebackType34from ..config import CertTypes, Timeout, VerifyTypes4from ..config import Timeout5from ..models import (6 HeaderTypes,7 QueryParamTypes,8 Request,
19 unmodified lines
28 *,29 data: RequestData = b"",30 params: QueryParamTypes = None,31 headers: HeaderTypes = None,32 verify: VerifyTypes = None,33 cert: CertTypes = None,32 timeout: Timeout = None,33 ) -> Response:34 request = Request(method, url, data=data, params=params, headers=headers)37 return await self.send(request, verify=verify, cert=cert, timeout=timeout)35 return await self.send(request, timeout=timeout)3639 async def send(40 self,41 request: Request,42 verify: VerifyTypes = None,43 cert: CertTypes = None,44 timeout: Timeout = None,45 ) -> Response:37 async def send(self, request: Request, timeout: Timeout = None) -> Response:38 raise NotImplementedError() # pragma: nocover3940 async def close(self) -> None:41 pass # pragma: nocover
34 unmodified lines
modified:httpx/dispatch/connection_pool.py+2 added-10 removed
138 unmodified lines
139 self.keepalive_connections.remove(connection)140 self.max_connections.release()141 await connection.close()142143 async def send(144 self,145 request: Request,146 verify: VerifyTypes = None,147 cert: CertTypes = None,148 timeout: Timeout = None,149 ) -> Response:143 async def send(self, request: Request, timeout: Timeout = None) -> Response:144 await self.check_keepalive_expiry()145 connection = await self.acquire_connection(146 origin=request.url.origin, timeout=timeout147 )148 try:155 response = await connection.send(156 request, verify=verify, cert=cert, timeout=timeout157 )149 response = await connection.send(request, timeout=timeout)150 except BaseException as exc:151 self.active_connections.remove(connection)152 self.max_connections.release()153 raise exc
66 unmodified lines
modified:httpx/dispatch/connection.py+6 added-22 removed
38 unmodified lines
39 self.uds = uds40 self.open_connection: typing.Optional[OpenConnection] = None41 self.expires_at: typing.Optional[float] = None4243 async def send(44 self,45 request: Request,46 verify: VerifyTypes = None,47 cert: CertTypes = None,48 timeout: Timeout = None,49 ) -> Response:43 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:53 await self.connect(verify=verify, cert=cert, timeout=timeout)47 await self.connect(timeout=timeout)4849 assert self.open_connection is not None50 response = await self.open_connection.send(request, timeout=timeout)5152 return response5360 async def connect(61 self, timeout: Timeout, verify: VerifyTypes = None, cert: CertTypes = None,62 ) -> None:63 ssl = self.ssl.with_overrides(verify=verify, cert=cert)6454 async def connect(self, timeout: Timeout) -> None:55 host = self.origin.host56 port = self.origin.port67 ssl_context = await self.get_ssl_context(ssl)57 ssl_context = await self.get_ssl_context(self.ssl)5859 if self.release_func is None:60 on_release = None61 else:
19 unmodified lines
8182 self.set_open_connection(http_version, socket=stream, on_release=on_release)8384 async def tunnel_start_tls(95 self,96 origin: Origin,97 proxy_url: URL,98 timeout: Timeout = None,99 cert: CertTypes = None,100 verify: VerifyTypes = True,85 self, origin: Origin, proxy_url: URL, timeout: Timeout = None,86 ) -> None:87 """88 Upgrade this connection to use TLS, assuming it represents a TCP tunnel.89 """
16 unmodified lines
106107 if origin.is_ssl:108 # Pull the socket stream off the internal HTTP connection object,109 # and run start_tls().125 ssl_config = SSLConfig(cert=cert, verify=verify)126 ssl_context = await self.get_ssl_context(ssl_config)110 ssl_context = await self.get_ssl_context(self.ssl)111 assert ssl_context is not None112113 logger.trace(f"tunnel_start_tls proxy_url={proxy_url!r} origin={origin!r}")114 socket = await socket.start_tls(
59 unmodified lines
modified:httpx/dispatch/proxy_http.py+3 added-16 removed
115 unmodified lines
116 connection.origin = origin117 self.active_connections.add(connection)118119 await connection.tunnel_start_tls(120 origin=origin,121 proxy_url=self.proxy_url,122 timeout=timeout,123 cert=self.cert,124 verify=self.verify,120 origin=origin, proxy_url=self.proxy_url, timeout=timeout,121 )122 else:123 self.active_connections.add(connection)124
53 unmodified lines
178 return (179 self.proxy_mode == DEFAULT_MODE and not origin.is_ssl180 ) or self.proxy_mode == FORWARD_ONLY181186 async def send(187 self,188 request: Request,189 verify: VerifyTypes = None,190 cert: CertTypes = None,191 timeout: Timeout = None,192 ) -> Response:193182 async def send(self, request: Request, timeout: Timeout = None) -> Response:183 if self.should_forward_origin(request.url.origin):184 # Change the request to have the target URL185 # as its full_path and switch the proxy URL186 # for where the request will be sent.
2 unmodified lines
189 request.url.full_path = target_url190 for name, value in self.proxy_headers.items():191 request.headers.setdefault(name, value)192204 return await super().send(205 request=request, verify=verify, cert=cert, timeout=timeout206 )193 return await super().send(request=request, timeout=timeout)194195 def __repr__(self) -> str:196 return (197 f"HTTPProxy(proxy_url={self.proxy_url!r} "
3 unmodified lines
modified:httpx/client.py+18 added-44 removed
221 unmodified lines
222 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,223 trust_env: bool = None,224 ) -> Response:225 if cert is not None:226 warnings.warn(226 raise RuntimeError(227 "Passing a 'cert' argument when making a request on a client "228 "is due to be deprecated. Instantiate a new client instead, "228 "is not supported anymore. Instantiate a new client instead, "229 "passing any 'cert' arguments to the client itself."230 )231232 if verify is not None:232 warnings.warn(233 raise RuntimeError(234 "Passing a 'verify' argument when making a request on a client "234 "is due to be deprecated. Instantiate a new client instead, "235 "is not supported anymore. Instantiate a new client instead, "236 "passing any 'verify' arguments to the client itself."237 )238239 if trust_env is not None:238 warnings.warn(240 raise RuntimeError(241 "Passing a 'trust_env' argument when making a request on a client "240 "is due to be deprecated. Instantiate a new client instead, "242 "is not supported anymore. Instantiate a new client instead, "243 "passing any 'trust_env' argument to the client itself."244 )245246 if stream:247 warnings.warn(248 "The 'stream=True' argument is due to be deprecated. "249 "Use 'async with client.stream(method, url, ...) as response' instead."
13 unmodified lines
263 request,264 stream=stream,265 auth=auth,266 allow_redirects=allow_redirects,264 verify=verify,265 cert=cert,267 timeout=timeout,267 trust_env=trust_env,268 )269 return response270271 def stream(
115 unmodified lines
387 *,388 stream: bool = False,389 auth: AuthTypes = None,390 allow_redirects: bool = True,391 verify: VerifyTypes = None,392 cert: CertTypes = None,391 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,394 trust_env: bool = None,392 ) -> Response:393 if request.url.scheme not in ("http", "https"):394 raise InvalidURL('URL scheme must be "http" or "https".')395396 timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)397401 auth = self.setup_auth(request, trust_env, auth)398 auth = self.setup_auth(request, auth)399400 response = await self.send_handling_redirects(404 request,405 auth=auth,406 verify=verify,407 cert=cert,408 timeout=timeout,409 allow_redirects=allow_redirects,401 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,402 )403404 if not stream:405 try:
2 unmodified lines
408 await response.close()409410 return response411420 def setup_auth(421 self, request: Request, trust_env: bool = None, auth: AuthTypes = None422 ) -> Auth:412 def setup_auth(self, request: Request, auth: AuthTypes = None) -> Auth:413 auth = self.auth if auth is None else auth424 trust_env = self.trust_env if trust_env is None else trust_env414415 if auth is not None:416 if isinstance(auth, tuple):417 return BasicAuth(username=auth[0], password=auth[1])
6 unmodified lines
424 username, password = request.url.username, request.url.password425 if username or password:426 return BasicAuth(username=username, password=password)427439 if trust_env and "Authorization" not in request.headers:428 if self.trust_env and "Authorization" not in request.headers:429 credentials = self.netrc.get_credentials(request.url.authority)430 if credentials is not None:431 return BasicAuth(username=credentials[0], password=credentials[1])432
3 unmodified lines
436 self,437 request: Request,438 auth: Auth,439 timeout: Timeout,451 verify: VerifyTypes = None,452 cert: CertTypes = None,440 allow_redirects: bool = True,441 history: typing.List[Response] = None,442 ) -> Response:443 if history is None:
5 unmodified lines
449 if request.url in (response.url for response in history):450 raise RedirectLoop()451452 response = await self.send_handling_auth(466 request, auth=auth, timeout=timeout, verify=verify, cert=cert453 request, auth=auth, timeout=timeout,454 )455 response.history = list(history)456457 if not response.is_redirect:
7 unmodified lines
465 response.call_next = functools.partial(466 self.send_handling_redirects,467 request=request,468 auth=auth,482 verify=verify,483 cert=cert,469 timeout=timeout,470 allow_redirects=False,471 history=history,472 )
91 unmodified lines
564 raise RedirectBodyUnavailable()565 return request.stream566567 async def send_handling_auth(583 self,584 request: Request,585 auth: Auth,586 timeout: Timeout,587 verify: VerifyTypes = None,588 cert: CertTypes = None,568 self, request: Request, auth: Auth, timeout: Timeout,569 ) -> Response:570 auth_flow = auth(request)571 request = next(auth_flow)572 while True:593 response = await self.send_single_request(request, timeout, verify, cert)573 response = await self.send_single_request(request, timeout)574 try:575 next_request = auth_flow.send(response)576 except StopIteration:577 return response
4 unmodified lines
582 request = next_request583 await response.close()584585 async def send_single_request(606 self,607 request: Request,608 timeout: Timeout,609 verify: VerifyTypes = None,610 cert: CertTypes = None,586 self, request: Request, timeout: Timeout,587 ) -> Response:588 """589 Sends a single request, without handling any redirections.590 """
1 unmodified line
592 dispatcher = self.dispatcher_for_url(request.url)593594 try:595 with ElapsedTimer() as timer:620 response = await dispatcher.send(621 request, verify=verify, cert=cert, timeout=timeout622 )596 response = await dispatcher.send(request, timeout=timeout)597 response.elapsed = timer.elapsed598 response.request = request599 except HTTPError as exc:600 # Add the original request to any HTTPError unless
359 unmodified lines
modified:tests/dispatch/test_connections.py+2 added-14 removed
29 unmodified lines
30 await response.read()313233@pytest.mark.usefixtures("async_environment")34async def test_https_get_with_ssl_defaults(https_server, ca_cert_pem_file):34async def test_https_get_with_ssl(https_server, ca_cert_pem_file):35 """36 An HTTPS request, with default SSL configuration set on the client.36 An HTTPS request, with SSL configuration set on the client.37 """38 async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:39 response = await conn.request("GET", https_server.url)40 await response.read()41 assert response.status_code == 20042 assert response.content == b"Hello, world!"434445@pytest.mark.usefixtures("async_environment")46async def test_https_get_with_sll_overrides(https_server, ca_cert_pem_file):47 """48 An HTTPS request, with SSL configuration set on the request.49 """50 async with HTTPConnection(origin=https_server.url) as conn:51 response = await conn.request("GET", https_server.url, verify=ca_cert_pem_file)52 await response.read()53 assert response.status_code == 20054 assert response.content == b"Hello, world!"