proxy bypass on Windows without DNS lookups
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by schlamar · committed by schlamar · parent 4f34446 · Browse files · 3 files·+71 added−14 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+71 added−14 removed
- tests:+16 added−11 removed
- other:+55 added−3 removed
Provenance
- Commit
1c38e1f- Tree
1ce6f8a- Author
schlamar <marc.schlaich@gmail.com>- Committer
schlamar <marc.schlaich@gmail.com>- Parent
4f34446
3 files changed+71 added−14 removed
modified:requests/compat.py+4 added-2 removed
35 unmodified lines36 # Specifics37 # ---------38 39 if is_py2:40- from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass40+ from urllib import (41+ quote, unquote, quote_plus, unquote_plus, urlencode, getproxies,42+ proxy_bypass, proxy_bypass_environment, getproxies_environment)43 from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag44 from urllib2 import parse_http_list45 import cookielib46 from Cookie import Morsel8 unmodified lines55 integer_types = (int, long)56 57 elif is_py3:58 from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag57- from urllib.request import parse_http_list, getproxies, proxy_bypass59+ from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment60 from http import cookiejar as cookielib61 from http.cookies import Morsel62 from io import StringIO63 from collections import OrderedDict7 unmodified linesmodified:requests/utils.py+51 added-1 removed
12 unmodified lines13 import collections14 import contextlib15 import io16 import os17+import platform18 import re19 import socket20 import struct21 import warnings4 unmodified lines26 from ._internal_utils import to_native_string27 from .compat import parse_http_list as _parse_list_header28 from .compat import (29 quote, urlparse, bytes, str, OrderedDict, unquote, getproxies,29- proxy_bypass, urlunparse, basestring, integer_types)30+ proxy_bypass, urlunparse, basestring, integer_types, is_py3,31+ proxy_bypass_environment, getproxies_environment)32 from .cookies import cookiejar_from_dict33 from .structures import CaseInsensitiveDict34 from .exceptions import (35 InvalidURL, InvalidHeader, FileModeWarning, UnrewindableBodyError)2 unmodified lines38 39 DEFAULT_CA_BUNDLE_PATH = certs.where()40 41 42+if platform.system() == 'Windows':43+ # provide a proxy_bypass version on Windows without DNS lookups44+45+ def proxy_bypass_registry(host):46+ if is_py3:47+ import winreg48+ else:49+ import _winreg as winreg50+ try:51+ internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER,52+ r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')53+ proxyEnable = winreg.QueryValueEx(internetSettings,54+ 'ProxyEnable')[0]55+ proxyOverride = winreg.QueryValueEx(internetSettings,56+ 'ProxyOverride')[0]57+ except OSError:58+ return False59+ if not proxyEnable or not proxyOverride:60+ return False61+62+ # make a check value list from the registry entry: replace the63+ # '<local>' string by the localhost entry and the corresponding64+ # canonical entry.65+ proxyOverride = proxyOverride.split(';')66+ # now check if we match one of the registry values.67+ for test in proxyOverride:68+ if test == '<local>':69+ if '.' not in host:70+ return True71+ test = test.replace(".", r"\.") # mask dots72+ test = test.replace("*", r".*") # change glob sequence73+ test = test.replace("?", r".") # change glob char74+ if re.match(test, host, re.I):75+ return True76+ return False77+78+ def proxy_bypass(host): # noqa79+ """Return True, if the host should be bypassed.80+81+ Checks proxy settings gathered from the environment, if specified,82+ or the registry.83+ """84+ if getproxies_environment():85+ return proxy_bypass_environment(host)86+ else:87+ return proxy_bypass_registry(host)88+89+90 def dict_to_sequence(d):91 """Returns an internal sequence dictionary update."""92 93 if hasattr(d, 'items'):810 unmodified linesmodified:tests/test_utils.py+16 added-11 removed
602 unmodified lines603 604 605 @pytest.mark.skipif(os.name != 'nt', reason='Test only on Windows')606 @pytest.mark.parametrize(607- 'url, expected', (608- ('http://192.168.0.1:5000/', True),609- ('http://192.168.0.1/', True),610- ('http://172.16.1.1/', True),611- ('http://172.16.1.1:5000/', True),612- ('http://localhost.localdomain:5000/v1.0/', True),613- ('http://172.16.1.22/', False),614- ('http://172.16.1.22:5000/', False),615- ('http://google.com:5000/v1.0/', False),607+ 'url, expected, override', (608+ ('http://192.168.0.1:5000/', True, None),609+ ('http://192.168.0.1/', True, None),610+ ('http://172.16.1.1/', True, None),611+ ('http://172.16.1.1:5000/', True, None),612+ ('http://localhost.localdomain:5000/v1.0/', True, None),613+ ('http://172.16.1.22/', False, None),614+ ('http://172.16.1.22:5000/', False, None),615+ ('http://google.com:5000/v1.0/', False, None),616+ ('http://mylocalhostname:5000/v1.0/', True, '<local>'),617+ ('http://192.168.0.1/', False, ''),618 ))617-def test_should_bypass_proxies_win_registry(url, expected, monkeypatch):619+def test_should_bypass_proxies_win_registry(url, expected, override,620+ monkeypatch):621 """Tests for function should_bypass_proxies to check if proxy622 can be bypassed or not with Windows registry settings623 """624+ if override is None:625+ override = '192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1'626 if compat.is_py3:627 import winreg628 else:629 import _winreg as winreg11 unmodified lines641 if key is ie_settings:642 if value_name == 'ProxyEnable':643 return [1]644 elif value_name == 'ProxyOverride':640- return ['192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1']645+ return [override]646 647 monkeypatch.setenv('http_proxy', '')648 monkeypatch.setenv('https_proxy', '')649 monkeypatch.setenv('ftp_proxy', '')5 unmodified lines