Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update default proxy construction in HTTPTransport to include SSL context #3506

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions httpx/_transports/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ def map_httpcore_exceptions() -> typing.Iterator[None]:
raise mapped_exc(message) from exc


def create_proxy(proxy: ProxyTypes | None, ssl_context: ssl.SSLContext) -> Proxy | None:
if isinstance(proxy, (str, URL)):
proxy_url = proxy if isinstance(proxy, URL) else URL(proxy)
if proxy_url.scheme == "https":
return Proxy(url=proxy_url, ssl_context=ssl_context)
return Proxy(url=proxy_url)
return proxy


class ResponseStream(SyncByteStream):
def __init__(self, httpcore_stream: typing.Iterable[bytes]) -> None:
self._httpcore_stream = httpcore_stream
Expand Down Expand Up @@ -149,8 +158,8 @@ def __init__(
) -> None:
import httpcore

proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
proxy = create_proxy(proxy, ssl_context)

if proxy is None:
self._pool = httpcore.ConnectionPool(
Expand Down Expand Up @@ -293,8 +302,8 @@ def __init__(
) -> None:
import httpcore

proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
proxy = create_proxy(proxy, ssl_context)

if proxy is None:
self._pool = httpcore.AsyncConnectionPool(
Expand Down
8 changes: 8 additions & 0 deletions tests/client/test_proxies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ssl

import httpcore
import pytest

Expand Down Expand Up @@ -263,3 +265,9 @@ def test_proxy_with_mounts():

transport = client._transport_for_url(httpx.URL("http://example.com"))
assert transport == proxy_transport


def test_proxy_with_ssl_context():
ssl_context = ssl.create_default_context()
proxy_transport = httpx.HTTPTransport(proxy="https://127.0.0.1", verify=ssl_context)
assert proxy_transport._pool._proxy_ssl_context == ssl_context # type: ignore[attr-defined]