Skip to content
Myr

Detect credentials in proxy URLs and create Proxy-authorization header (#780)

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

* Detect auth in proxy URLs and create Proxy-authorization header * Add credentials and SOCKS details to proxy documentation * Use URL.copy_with to remove credentials from URL

authored by Yeray Diaz Diaz · committed by GitHub · parent b128bfa · Browse files · 3 files+42 added−4 removed
This commit

State

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

Diff stats

3 files+42 added−4 removed

  • tests:+21 added−3 removed
  • other:+21 added−1 removed

3 files+42 added−4 removed

modified:docs/advanced.md+6 added-1 removed
174 unmodified lines
175```176177## HTTP Proxying178179HTTPX supports setting up proxies the same way that Requests does via the `proxies` parameter.179HTTPX supports setting up HTTP proxies the same way that Requests does via the `proxies` parameter.180For example to forward all HTTP traffic to `http://127.0.0.1:3080` and all HTTPS traffic181to `http://127.0.0.1:3081` your `proxies` config would look like this:182183```python
4 unmodified lines
188>>> with httpx.Client(proxies=proxies) as client:189... ...190```191192Credentials may be passed in as part of the URL in the standard way, i.e.193`http://username:password@127.0.0.1:3080`.194195Proxies can be configured for a specific scheme and host, all schemes of a host,196all hosts for a scheme, or for all requests. When determining which proxy configuration197to use for a given request this same order is used.198
37 unmodified lines
236237 To use proxies you must pass the proxy information at `Client` initialization,238 rather than on the `.get(...)` call or other request methods.239240SOCKS proxies are *not* supported yet.241242## Timeout Configuration243244HTTPX is careful to enforce timeouts everywhere by default.245
228 unmodified lines
modified:httpx/config.py+15 added
1import os2import ssl3import typing4from base64 import b64encode5from pathlib import Path67import certifi8
316 unmodified lines
325 raise ValueError(f"Unknown scheme for proxy URL {url!r}")326 if mode not in ("DEFAULT", "CONNECT_ONLY", "TUNNEL_ONLY"):327 raise ValueError(f"Unknown proxy mode {mode!r}")328329 if url.username or url.password:330 headers.setdefault(331 "Proxy-Authorization",332 self.build_auth_header(url.username, url.password),333 )334 # Remove userinfo from the URL authority, e.g.:335 # 'username:password@proxy_host:proxy_port' -> 'proxy_host:proxy_port'336 url = url.copy_with(username=None, password=None)337338 self.url = url339 self.headers = headers340 self.mode = mode341342 def build_auth_header(self, username: str, password: str) -> str:343 userpass = (username.encode("utf-8"), password.encode("utf-8"))344 token = b64encode(b":".join(userpass)).decode().strip()345 return f"Basic {token}"346347 def __repr__(self) -> str:348 return (349 f"Proxy(url={str(self.url)!r}, "350 f"headers={dict(self.headers)!r}, "
7 unmodified lines
modified:tests/test_config.py+21 added-3 removed
206 unmodified lines
207208 assert ssl_config.ssl_context.keylog_filename is None209210211def test_proxy_from_url():212 proxy = httpx.Proxy("https://example.com")213 assert repr(proxy) == "Proxy(url='https://example.com', headers={}, mode='DEFAULT')"211@pytest.mark.parametrize(212 "url,expected_url,expected_headers,expected_mode",213 [214 ("https://example.com", "https://example.com", {}, "DEFAULT"),215 (216 "https://user:pass@example.com",217 "https://example.com:443",218 {"proxy-authorization": "Basic dXNlcjpwYXNz"},219 "DEFAULT",220 ),221 ],222)223def test_proxy_from_url(url, expected_url, expected_headers, expected_mode):224 proxy = httpx.Proxy(url)225226 assert str(proxy.url) == expected_url227 assert dict(proxy.headers) == expected_headers228 assert proxy.mode == expected_mode229 assert repr(proxy) == "Proxy(url='{}', headers={}, mode='{}')".format(230 expected_url, str(expected_headers), expected_mode231 )232233234def test_invalid_proxy_scheme():235 with pytest.raises(ValueError):236 httpx.Proxy("invalid://example.com")237
4 unmodified lines