Skip to content
Myr

Add logic for determining if a URL should be proxied (#472)

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

authored by mariaS210 · committed by Seth Michael Larson · parent 2984499 · Browse files · 2 files+100 added−0 removed
This commit

State

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

Diff stats

2 files+100 added−0 removed

  • tests:+77 added−0 removed
  • other:+23 added−0 removed

2 files+100 added−0 removed

modified:httpx/utils.py+23 added
12 unmodified lines
13from urllib.request import getproxies1415if typing.TYPE_CHECKING: # pragma: no cover16 from .models import PrimitiveData17 from .models import URL181920def normalize_header_key(value: typing.AnyStr, encoding: str = None) -> bytes:21 """
188 unmodified lines
210 """211 return " ".join(f"{key}={value!r}" for key, value in kwargs.items())212213214def should_not_be_proxied(url: "URL") -> bool:215 """ Return True if url should not be proxied,216 return False otherwise.217 """218 no_proxy = getproxies().get("no")219 if not no_proxy:220 return False221 no_proxy_list = [host.strip() for host in no_proxy.split(",")]222 for name in no_proxy_list:223 if name == "*":224 return True225 if name:226 name = name.lstrip(".") # ignore leading dots227 name = re.escape(name)228 pattern = r"(.+\.)?%s$" % name229 if re.match(pattern, url.host, re.I) or re.match(230 pattern, url.authority, re.I231 ):232 return True233 return False234235236def get_environment_proxies() -> typing.Dict[str, str]:237 """Gets proxy information from the environment"""238239 # urllib.request.getproxies() falls back on System
135 unmodified lines
modified:tests/test_utils.py+77 added
12 unmodified lines
13 get_netrc,14 guess_json_utf,15 obfuscate_sensitive_headers,16 parse_header_links,17 should_not_be_proxied,18)192021@pytest.mark.parametrize(
179 unmodified lines
201 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]202 bytes_output = [(k.encode(), v.encode()) for k, v in output]203 assert list(obfuscate_sensitive_headers(headers)) == output204 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_output205206207@pytest.mark.parametrize(208 ["url", "no_proxy", "expected"],209 [210 (211 "http://127.0.0.1",212 {"NO_PROXY": ""},213 False,214 ), # everything proxied when no_proxy is empty/unset215 (216 "http://127.0.0.1",217 {"NO_PROXY": "127.0.0.1"},218 True,219 ), # no_proxy as ip case is matched220 (221 "http://127.0.0.1",222 {"NO_PROXY": "https://127.0.0.1"},223 False,224 ), # no_proxy with scheme is ignored225 (226 "http://127.0.0.1",227 {"NO_PROXY": "1.1.1.1"},228 False,229 ), # different no_proxy means its proxied230 (231 "http://courses.mit.edu",232 {"NO_PROXY": "mit.edu"},233 True,234 ), # no_proxy for sub-domain matches235 (236 "https://mit.edu.info",237 {"NO_PROXY": "mit.edu"},238 False,239 ), # domain is actually edu.info, so should be proxied240 (241 "https://mit.edu.info",242 {"NO_PROXY": "mit.edu,edu.info"},243 True,244 ), # list in no_proxy, matches second domain245 (246 "https://mit.edu.info",247 {"NO_PROXY": "mit.edu, edu.info"},248 True,249 ), # list with spaces in no_proxy250 (251 "https://mit.edu.info",252 {"NO_PROXY": "mit.edu,mit.info"},253 False,254 ), # list in no_proxy, without any domain matching255 (256 "https://foo.example.com",257 {"NO_PROXY": "www.example.com"},258 False,259 ), # different subdomains foo vs www means we still proxy260 (261 "https://www.example1.com",262 {"NO_PROXY": ".example1.com"},263 True,264 ), # no_proxy starting with dot265 (266 "https://www.example2.com",267 {"NO_PROXY": "ample2.com"},268 False,269 ), # whole-domain matching270 (271 "https://www.example3.com",272 {"NO_PROXY": "*"},273 True,274 ), # wildcard * means nothing proxied275 ],276)277def test_should_not_be_proxied(url, no_proxy, expected):278 os.environ.update(no_proxy)279 parsed_url = httpx.models.URL(url)280 assert should_not_be_proxied(parsed_url) == expected