Skip to content

Commit

Permalink
fix: Proxy (HTTP_PROXY) settings are ignored for custom Repositories (#…
Browse files Browse the repository at this point in the history
…2882)

Fixes #2880

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed May 15, 2024
1 parent a3307c2 commit ba6645a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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

0 comments on commit ba6645a

Please sign in to comment.