Skip to content

Commit e9ebd1d

Browse files
florimondmancalovelydinosaur
authored andcommitted
Drop per-request cert, verify, and trust_env (#617)
* 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
1 parent f3b7999 commit e9ebd1d

7 files changed

Lines changed: 36 additions & 126 deletions

File tree

httpx/client.py

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,26 @@ async def request(
223223
trust_env: bool = None,
224224
) -> Response:
225225
if cert is not None:
226-
warnings.warn(
226+
raise RuntimeError(
227227
"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, "
229229
"passing any 'cert' arguments to the client itself."
230230
)
231+
231232
if verify is not None:
232-
warnings.warn(
233+
raise RuntimeError(
233234
"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, "
235236
"passing any 'verify' arguments to the client itself."
236237
)
238+
237239
if trust_env is not None:
238-
warnings.warn(
240+
raise RuntimeError(
239241
"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, "
241243
"passing any 'trust_env' argument to the client itself."
242244
)
245+
243246
if stream:
244247
warnings.warn(
245248
"The 'stream=True' argument is due to be deprecated. "
@@ -261,10 +264,7 @@ async def request(
261264
stream=stream,
262265
auth=auth,
263266
allow_redirects=allow_redirects,
264-
verify=verify,
265-
cert=cert,
266267
timeout=timeout,
267-
trust_env=trust_env,
268268
)
269269
return response
270270

@@ -388,25 +388,17 @@ async def send(
388388
stream: bool = False,
389389
auth: AuthTypes = None,
390390
allow_redirects: bool = True,
391-
verify: VerifyTypes = None,
392-
cert: CertTypes = None,
393391
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
394-
trust_env: bool = None,
395392
) -> Response:
396393
if request.url.scheme not in ("http", "https"):
397394
raise InvalidURL('URL scheme must be "http" or "https".')
398395

399396
timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)
400397

401-
auth = self.setup_auth(request, trust_env, auth)
398+
auth = self.setup_auth(request, auth)
402399

403400
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,
410402
)
411403

412404
if not stream:
@@ -417,11 +409,8 @@ async def send(
417409

418410
return response
419411

420-
def setup_auth(
421-
self, request: Request, trust_env: bool = None, auth: AuthTypes = None
422-
) -> Auth:
412+
def setup_auth(self, request: Request, auth: AuthTypes = None) -> Auth:
423413
auth = self.auth if auth is None else auth
424-
trust_env = self.trust_env if trust_env is None else trust_env
425414

426415
if auth is not None:
427416
if isinstance(auth, tuple):
@@ -436,7 +425,7 @@ def setup_auth(
436425
if username or password:
437426
return BasicAuth(username=username, password=password)
438427

439-
if trust_env and "Authorization" not in request.headers:
428+
if self.trust_env and "Authorization" not in request.headers:
440429
credentials = self.netrc.get_credentials(request.url.authority)
441430
if credentials is not None:
442431
return BasicAuth(username=credentials[0], password=credentials[1])
@@ -448,8 +437,6 @@ async def send_handling_redirects(
448437
request: Request,
449438
auth: Auth,
450439
timeout: Timeout,
451-
verify: VerifyTypes = None,
452-
cert: CertTypes = None,
453440
allow_redirects: bool = True,
454441
history: typing.List[Response] = None,
455442
) -> Response:
@@ -463,7 +450,7 @@ async def send_handling_redirects(
463450
raise RedirectLoop()
464451

465452
response = await self.send_handling_auth(
466-
request, auth=auth, timeout=timeout, verify=verify, cert=cert
453+
request, auth=auth, timeout=timeout,
467454
)
468455
response.history = list(history)
469456

@@ -479,8 +466,6 @@ async def send_handling_redirects(
479466
self.send_handling_redirects,
480467
request=request,
481468
auth=auth,
482-
verify=verify,
483-
cert=cert,
484469
timeout=timeout,
485470
allow_redirects=False,
486471
history=history,
@@ -580,17 +565,12 @@ def redirect_stream(
580565
return request.stream
581566

582567
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,
589569
) -> Response:
590570
auth_flow = auth(request)
591571
request = next(auth_flow)
592572
while True:
593-
response = await self.send_single_request(request, timeout, verify, cert)
573+
response = await self.send_single_request(request, timeout)
594574
try:
595575
next_request = auth_flow.send(response)
596576
except StopIteration:
@@ -603,11 +583,7 @@ async def send_handling_auth(
603583
await response.close()
604584

605585
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,
611587
) -> Response:
612588
"""
613589
Sends a single request, without handling any redirections.
@@ -617,9 +593,7 @@ async def send_single_request(
617593

618594
try:
619595
with ElapsedTimer() as timer:
620-
response = await dispatcher.send(
621-
request, verify=verify, cert=cert, timeout=timeout
622-
)
596+
response = await dispatcher.send(request, timeout=timeout)
623597
response.elapsed = timer.elapsed
624598
response.request = request
625599
except HTTPError as exc:

httpx/dispatch/asgi.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import typing
22

3-
from ..config import CertTypes, TimeoutTypes, VerifyTypes
3+
from ..config import TimeoutTypes
44
from ..content_streams import ByteStream
55
from ..models import Request, Response
66
from .base import Dispatcher
@@ -54,14 +54,7 @@ def __init__(
5454
self.root_path = root_path
5555
self.client = client
5656

57-
async def send(
58-
self,
59-
request: Request,
60-
verify: VerifyTypes = None,
61-
cert: CertTypes = None,
62-
timeout: TimeoutTypes = None,
63-
) -> Response:
64-
57+
async def send(self, request: Request, timeout: TimeoutTypes = None) -> Response:
6558
scope = {
6659
"type": "http",
6760
"asgi": {"version": "3.0"},

httpx/dispatch/base.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing
22
from types import TracebackType
33

4-
from ..config import CertTypes, Timeout, VerifyTypes
4+
from ..config import Timeout
55
from ..models import (
66
HeaderTypes,
77
QueryParamTypes,
@@ -29,20 +29,12 @@ async def request(
2929
data: RequestData = b"",
3030
params: QueryParamTypes = None,
3131
headers: HeaderTypes = None,
32-
verify: VerifyTypes = None,
33-
cert: CertTypes = None,
3432
timeout: Timeout = None,
3533
) -> Response:
3634
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)
3836

39-
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:
4638
raise NotImplementedError() # pragma: nocover
4739

4840
async def close(self) -> None:

httpx/dispatch/connection.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,21 @@ def __init__(
4040
self.open_connection: typing.Optional[OpenConnection] = None
4141
self.expires_at: typing.Optional[float] = None
4242

43-
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:
5044
timeout = Timeout() if timeout is None else timeout
5145

5246
if self.open_connection is None:
53-
await self.connect(verify=verify, cert=cert, timeout=timeout)
47+
await self.connect(timeout=timeout)
5448

5549
assert self.open_connection is not None
5650
response = await self.open_connection.send(request, timeout=timeout)
5751

5852
return response
5953

60-
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)
64-
54+
async def connect(self, timeout: Timeout) -> None:
6555
host = self.origin.host
6656
port = self.origin.port
67-
ssl_context = await self.get_ssl_context(ssl)
57+
ssl_context = await self.get_ssl_context(self.ssl)
6858

6959
if self.release_func is None:
7060
on_release = None
@@ -92,12 +82,7 @@ async def connect(
9282
self.set_open_connection(http_version, socket=stream, on_release=on_release)
9383

9484
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,
10186
) -> None:
10287
"""
10388
Upgrade this connection to use TLS, assuming it represents a TCP tunnel.
@@ -122,8 +107,7 @@ async def tunnel_start_tls(
122107
if origin.is_ssl:
123108
# Pull the socket stream off the internal HTTP connection object,
124109
# 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)
127111
assert ssl_context is not None
128112

129113
logger.trace(f"tunnel_start_tls proxy_url={proxy_url!r} origin={origin!r}")

httpx/dispatch/connection_pool.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,13 @@ async def check_keepalive_expiry(self) -> None:
140140
self.max_connections.release()
141141
await connection.close()
142142

143-
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:
150144
await self.check_keepalive_expiry()
151145
connection = await self.acquire_connection(
152146
origin=request.url.origin, timeout=timeout
153147
)
154148
try:
155-
response = await connection.send(
156-
request, verify=verify, cert=cert, timeout=timeout
157-
)
149+
response = await connection.send(request, timeout=timeout)
158150
except BaseException as exc:
159151
self.active_connections.remove(connection)
160152
self.max_connections.release()

httpx/dispatch/proxy_http.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ async def tunnel_connection(
117117
self.active_connections.add(connection)
118118

119119
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,
125121
)
126122
else:
127123
self.active_connections.add(connection)
@@ -183,14 +179,7 @@ def should_forward_origin(self, origin: Origin) -> bool:
183179
self.proxy_mode == DEFAULT_MODE and not origin.is_ssl
184180
) or self.proxy_mode == FORWARD_ONLY
185181

186-
async def send(
187-
self,
188-
request: Request,
189-
verify: VerifyTypes = None,
190-
cert: CertTypes = None,
191-
timeout: Timeout = None,
192-
) -> Response:
193-
182+
async def send(self, request: Request, timeout: Timeout = None) -> Response:
194183
if self.should_forward_origin(request.url.origin):
195184
# Change the request to have the target URL
196185
# as its full_path and switch the proxy URL
@@ -201,9 +190,7 @@ async def send(
201190
for name, value in self.proxy_headers.items():
202191
request.headers.setdefault(name, value)
203192

204-
return await super().send(
205-
request=request, verify=verify, cert=cert, timeout=timeout
206-
)
193+
return await super().send(request=request, timeout=timeout)
207194

208195
def __repr__(self) -> str:
209196
return (

tests/dispatch/test_connections.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,12 @@ async def test_premature_close(server):
3131

3232

3333
@pytest.mark.usefixtures("async_environment")
34-
async def test_https_get_with_ssl_defaults(https_server, ca_cert_pem_file):
34+
async def test_https_get_with_ssl(https_server, ca_cert_pem_file):
3535
"""
36-
An HTTPS request, with default SSL configuration set on the client.
36+
An HTTPS request, with SSL configuration set on the client.
3737
"""
3838
async with HTTPConnection(origin=https_server.url, verify=ca_cert_pem_file) as conn:
3939
response = await conn.request("GET", https_server.url)
4040
await response.read()
4141
assert response.status_code == 200
4242
assert response.content == b"Hello, world!"
43-
44-
45-
@pytest.mark.usefixtures("async_environment")
46-
async 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 == 200
54-
assert response.content == b"Hello, world!"

0 commit comments

Comments
 (0)