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

fix: Proxy (HTTP_PROXY) settings are ignored for custom Repositories #2882

Merged
merged 1 commit into from
May 15, 2024
Merged
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
1 change: 1 addition & 0 deletions news/2880.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Proxy (`HTTP_PROXY` env vars) settings are ignored for custom indexes.
19 changes: 12 additions & 7 deletions src/pdm/models/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def _create_truststore_ssl_context() -> SSLContext | None:

@lru_cache(maxsize=None)
def _get_transport(
verify: bool | SSLContext | str = True, cert: tuple[str, str | None] | None = None
verify: bool | SSLContext | str = True,
cert: tuple[str, str | None] | None = None,
proxy: httpx.Proxy | None = None,
) -> httpx.BaseTransport:
return httpx.HTTPTransport(verify=verify, cert=cert, trust_env=True)
return httpx.HTTPTransport(verify=verify, cert=cert, trust_env=True, proxy=proxy)


class MsgPackSerializer(hishel.BaseSerializer):
Expand Down Expand Up @@ -127,13 +129,17 @@ def is_binary(self) -> bool:

class PDMPyPIClient(PyPIClient):
def __init__(self, *, sources: list[RepositoryConfig], cache_dir: Path, **kwargs: Any) -> None:
from httpx._utils import URLPattern
from unearth.fetchers.sync import LocalFSTransport

storage = hishel.FileStorage(serializer=MsgPackSerializer(), base_path=cache_dir, ttl=CACHES_TTL)
controller = hishel.Controller()

mounts: dict[str, httpx.BaseTransport] = {"file://": LocalFSTransport()}
self._trusted_host_ports: set[tuple[str, int | None]] = set()
self._proxy_map = {
URLPattern(key): proxy for key, proxy in sorted(self._get_proxy_map(None, allow_env_proxies=True).items())
}
for s in sources:
assert s.url is not None
url = httpx.URL(s.url)
Expand All @@ -146,10 +152,7 @@ def __init__(self, *, sources: list[RepositoryConfig], cache_dir: Path, **kwargs
self._transport_for(s), storage, controller
)
mounts.update(kwargs.pop("mounts", None) or {})
kwargs.update(
proxies=self._get_proxy_map(None, allow_env_proxies=True),
follow_redirects=True,
)
kwargs.update(follow_redirects=True)

httpx.Client.__init__(self, mounts=mounts, **kwargs)

Expand All @@ -166,7 +169,9 @@ def _transport_for(self, source: RepositoryConfig) -> httpx.BaseTransport:
cert = (source.client_cert, source.client_key)
else:
cert = None
return _get_transport(verify=verify, cert=cert)
source_url = httpx.URL(cast(str, source.url))
proxy = next((proxy for pattern, proxy in self._proxy_map.items() if pattern.matches(source_url)), None)
return _get_transport(verify=verify, cert=cert, proxy=proxy)

def _make_user_agent(self) -> str:
import platform
Expand Down
Loading