Skip to content
Myr

Raise warning if proxy key is eg. "all" instead of "all://". (#1127)

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

* #1105 added deprecation warning, raised when we try to use proxies={"http": ...} instead of {"http://": ...}. Updated docs and added unit, which check the warning presence * Update tests/client/test_proxies.py Co-authored-by: Florimond Manca <florimond.manca@gmail.com> * Update tests/client/test_proxies.py

authored by cdeler · committed by GitHub · parent 1da46f3 · Browse files · 3 files+53 added−28 removed
This commit

State

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

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>
  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

3 files+53 added−28 removed

  • tests:+38 added−18 removed
  • other:+15 added−10 removed

3 files+53 added−28 removed

modified:docs/advanced.md+10 added-10 removed
282 unmodified lines
283For more advanced use cases, pass a proxies `dict`. For example, to route HTTP and HTTPS requests to 2 different proxies, respectively located at `http://localhost:8030`, and `http://localhost:8031`, pass a `dict` of proxy URLs:284285```python286proxies = {287 "http": "http://localhost:8030",288 "https": "http://localhost:8031",287 "http://": "http://localhost:8030",288 "https://": "http://localhost:8031",289}290291with httpx.Client(proxies=proxies) as client:292 ...
1 unmodified line
294295For detailed information about proxy routing, see the [Routing](#routing) section.296297!!! tip "Gotcha"298 In most cases, the proxy URL for the `https` key _should_ use the `http://` scheme (that's not a typo!).298 In most cases, the proxy URL for the `https://` key _should_ use the `http://` scheme (that's not a typo!).299300 This is because HTTP proxying requires initiating a connection with the proxy server. While it's possible that your proxy supports doing it via HTTPS, most proxies only support doing it via HTTP.301302 For more information, see [FORWARD vs TUNNEL](#forward-vs-tunnel).
3 unmodified lines
306Proxy credentials can be passed as the `userinfo` section of the proxy URL. For example:307308```python309proxies = {310 "http": "http://username:password@localhost:8030",310 "http://": "http://username:password@localhost:8030",311 # ...312}313```314315### Routing316317HTTPX provides fine-grained controls for deciding which requests should go through a proxy, and which shouldn't. This process is known as proxy routing.318319The `proxies` dictionary maps URL patterns ("proxy keys") to proxy URLs. HTTPX matches requested URLs against proxy keys to decide which proxy should be used, if any. Matching is done from most specific proxy keys (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https`).319The `proxies` dictionary maps URL patterns ("proxy keys") to proxy URLs. HTTPX matches requested URLs against proxy keys to decide which proxy should be used, if any. Matching is done from most specific proxy keys (e.g. `https://<domain>:<port>`) to least specific ones (e.g. `https://`).320321HTTPX supports routing proxies based on **scheme**, **domain**, **port**, or a combination of these.322323#### Wildcard routing
1 unmodified line
325Route everything through a proxy...326327```python328proxies = {329 "all": "http://localhost:8030",329 "all://": "http://localhost:8030",330}331```332333#### Scheme routing
1 unmodified line
335Route HTTP requests through one proxy, and HTTPS requests through another...336337```python338proxies = {339 "http": "http://localhost:8030",340 "https": "http://localhost:8031",339 "http://": "http://localhost:8030",340 "https://": "http://localhost:8031",341}342```343344#### Domain routing
56 unmodified lines
401402```python403proxies = {404 # Route requests through a proxy by default...405 "all": "http://localhost:8031",405 "all://": "http://localhost:8031",406 # Except those for "example.com".407 "all://example.com": None,408}409```
4 unmodified lines
414415```python416proxies = {417 # Route all traffic through a proxy by default...418 "all": "http://localhost:8030",418 "all://": "http://localhost:8030",419 # But don't use proxies for HTTPS requests to "domain.io"...420 "https://domain.io": None,421 # And use another proxy for requests to "example.com" and its subdomains...422 "all://*example.com": "http://localhost:8031",
438 unmodified lines
modified:httpx/_utils.py+5 added
476 unmodified lines
477 def __init__(self, pattern: str) -> None:478 from ._models import URL479480 if pattern and ":" not in pattern:481 warn_deprecated(482 f"Proxy keys should use proper URL forms rather "483 f"than plain scheme strings. "484 f'Instead of "{pattern}", use "{pattern}://"'485 )486 pattern += "://"487488 url = URL(pattern)489 self.pattern = pattern
58 unmodified lines
modified:tests/client/test_proxies.py+38 added-18 removed
18 unmodified lines
1920@pytest.mark.parametrize(21 ["proxies", "expected_proxies"],22 [23 ("http://127.0.0.1", [("all", "http://127.0.0.1")]),24 ({"all": "http://127.0.0.1"}, [("all", "http://127.0.0.1")]),23 ("http://127.0.0.1", [("all://", "http://127.0.0.1")]),24 ({"all://": "http://127.0.0.1"}, [("all://", "http://127.0.0.1")]),25 (26 {"http": "http://127.0.0.1", "https": "https://127.0.0.1"},27 [("http", "http://127.0.0.1"), ("https", "https://127.0.0.1")],26 {"http://": "http://127.0.0.1", "https://": "https://127.0.0.1"},27 [("http://", "http://127.0.0.1"), ("https://", "https://127.0.0.1")],28 ),29 (httpx.Proxy("http://127.0.0.1"), [("all", "http://127.0.0.1")]),29 (httpx.Proxy("http://127.0.0.1"), [("all://", "http://127.0.0.1")]),30 (31 {"https": httpx.Proxy("https://127.0.0.1"), "all": "http://127.0.0.1"},32 [("all", "http://127.0.0.1"), ("https", "https://127.0.0.1")],31 {32 "https://": httpx.Proxy("https://127.0.0.1"),33 "all://": "http://127.0.0.1",34 },35 [("all://", "http://127.0.0.1"), ("https://", "https://127.0.0.1")],36 ),37 ],38)39def test_proxies_parameter(proxies, expected_proxies):
16 unmodified lines
56 ["url", "proxies", "expected"],57 [58 ("http://example.com", None, None),59 ("http://example.com", {}, None),57 ("http://example.com", {"https": PROXY_URL}, None),60 ("http://example.com", {"https://": PROXY_URL}, None),61 ("http://example.com", {"http://example.net": PROXY_URL}, None),62 # Using "*" should match any domain name.63 ("http://example.com", {"http://*": PROXY_URL}, PROXY_URL),64 ("https://example.com", {"http://*": PROXY_URL}, None),
8 unmodified lines
73 ("http://www.example.com", {"http://*example.com": PROXY_URL}, PROXY_URL),74 ("http://wwwexample.com", {"http://*example.com": PROXY_URL}, None),75 # ...76 ("http://example.com:443", {"http://example.com": PROXY_URL}, PROXY_URL),74 ("http://example.com", {"all": PROXY_URL}, PROXY_URL),75 ("http://example.com", {"all": PROXY_URL, "http://example.com": None}, None),76 ("http://example.com", {"http": PROXY_URL}, PROXY_URL),77 ("http://example.com", {"all://": PROXY_URL}, PROXY_URL),78 ("http://example.com", {"all://": PROXY_URL, "http://example.com": None}, None),79 ("http://example.com", {"http://": PROXY_URL}, PROXY_URL),80 ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),81 ("http://example.com", {"all://example.com:80": PROXY_URL}, None),82 ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),83 ("http://example.com", {"http://example.com:80": PROXY_URL}, None),
1 unmodified line
85 ("http://example.com:8080", {"http://example.com": PROXY_URL}, PROXY_URL),86 (87 "http://example.com",88 {86 "all": PROXY_URL + ":1",87 "http": PROXY_URL + ":2",89 "all://": PROXY_URL + ":1",90 "http://": PROXY_URL + ":2",91 "all://example.com": PROXY_URL + ":3",92 "http://example.com": PROXY_URL + ":4",93 },94 PROXY_URL + ":4",95 ),96 (97 "http://example.com",98 {96 "all": PROXY_URL + ":1",97 "http": PROXY_URL + ":2",99 "all://": PROXY_URL + ":1",100 "http://": PROXY_URL + ":2",101 "all://example.com": PROXY_URL + ":3",102 },103 PROXY_URL + ":3",104 ),105 (106 "http://example.com",104 {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},107 {"all://": PROXY_URL + ":1", "http://": PROXY_URL + ":2"},108 PROXY_URL + ":2",109 ),110 ],111)
10 unmodified lines
122123124@pytest.mark.asyncio125async def test_async_proxy_close():123 client = httpx.AsyncClient(proxies={"all": PROXY_URL})126 client = httpx.AsyncClient(proxies={"all://": PROXY_URL})127 await client.aclose()128129130def test_sync_proxy_close():128 client = httpx.Client(proxies={"all": PROXY_URL})131 client = httpx.Client(proxies={"all://": PROXY_URL})132 client.close()133134135def test_unsupported_proxy_scheme():
110 unmodified lines
246 if expected is None:247 assert transport == client._transport248 else:249 assert transport.proxy_origin == url_to_origin(expected)250251252@pytest.mark.parametrize(253 ["proxies", "expected_scheme"],254 [255 ({"http": "http://127.0.0.1"}, "http://"),256 ({"https": "http://127.0.0.1"}, "https://"),257 ({"all": "http://127.0.0.1"}, "all://"),258 ],259)260def test_for_deprecated_proxy_params(proxies, expected_scheme):261 with pytest.deprecated_call() as block:262 httpx.AsyncClient(proxies=proxies)263264 warning_message = str(block.pop(DeprecationWarning))265266 assert expected_scheme in warning_message