Skip to content
Myr

fix: NO_PROXY should support IPv4, IPv6 and localhost (#2659)

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

* fix: NO_PROXY supports IPv4, IPv6 and localhost * add more tests for test_get_environment_proxies ---------

authored by Jiayun Shen · committed by GitHub · parent 7d7c4f1 · Browse files · 2 files+33 added−1 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>

Diff stats

2 files+33 added−1 removed

  • tests:+6 added−0 removed
  • other:+27 added−1 removed

2 files+33 added−1 removed

modified:httpx/_utils.py+27 added-1 removed
1import codecs2import email.message3import ipaddress4import mimetypes5import os6import re7import time
251 unmodified lines
259 # which disables "www.google.com" but not "google.com"260 # NO_PROXY=google.com is marked as "all://*google.com,261 # which disables "www.google.com" and "google.com".262 # (But not "wwwgoogle.com")263 # NO_PROXY can include domains, IPv6, IPv4 addresses and "localhost"264 # NO_PROXY=example.com,::1,localhost,192.168.0.0/16265 if is_ipv4_hostname(hostname):266 mounts[f"all://{hostname}"] = None267 elif is_ipv6_hostname(hostname):268 mounts[f"all://[{hostname}]"] = None269 elif hostname.lower() == "localhost":270 mounts[f"all://{hostname}"] = None271 else:262 mounts[f"all://*{hostname}"] = None272 mounts[f"all://*{hostname}"] = None273274 return mounts275276
181 unmodified lines
458 return self.priority < other.priority459460 def __eq__(self, other: typing.Any) -> bool:461 return isinstance(other, URLPattern) and self.pattern == other.pattern462463464def is_ipv4_hostname(hostname: str) -> bool:465 try:466 ipaddress.IPv4Address(hostname.split("/")[0])467 except:468 return False469 return True470471472def is_ipv6_hostname(hostname: str) -> bool:473 try:474 ipaddress.IPv6Address(hostname.split("/")[0])475 except:476 return False477 return True
modified:tests/test_utils.py+6 added
183 unmodified lines
184 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},185 ),186 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),187 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),188 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),189 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),190 ({"no_proxy": "::1"}, {"all://[::1]": None}),191 ({"no_proxy": "localhost"}, {"all://localhost": None}),192 ({"no_proxy": "github.com"}, {"all://*github.com": None}),193 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),194 ],195)196def test_get_environment_proxies(environment, proxies):197 os.environ.update(environment)
85 unmodified lines