Skip to content
Myr

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

3 files+71 added−14 removed

modified:requests/compat.py+4 added-2 removed
35 unmodified lines
36# Specifics37# ---------3839if 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 Morsel
8 unmodified lines
55 integer_types = (int, long)5657elif 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 OrderedDict
7 unmodified lines
modified:requests/utils.py+51 added-1 removed
12 unmodified lines
13import collections14import contextlib15import io16import os17import platform18import re19import socket20import struct21import warnings
4 unmodified lines
26from ._internal_utils import to_native_string27from .compat import parse_http_list as _parse_list_header28from .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)32from .cookies import cookiejar_from_dict33from .structures import CaseInsensitiveDict34from .exceptions import (35 InvalidURL, InvalidHeader, FileModeWarning, UnrewindableBodyError)
2 unmodified lines
3839DEFAULT_CA_BUNDLE_PATH = certs.where()404142if platform.system() == 'Windows':43 # provide a proxy_bypass version on Windows without DNS lookups4445 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 False6162 # 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 False7778 def proxy_bypass(host): # noqa79 """Return True, if the host should be bypassed.8081 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)888990def dict_to_sequence(d):91 """Returns an internal sequence dictionary update."""9293 if hasattr(d, 'items'):
810 unmodified lines
modified:tests/test_utils.py+16 added-11 removed
602 unmodified lines
603604605@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 ))617def test_should_bypass_proxies_win_registry(url, expected, monkeypatch):619def 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 winreg
11 unmodified lines
641 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]646647 monkeypatch.setenv('http_proxy', '')648 monkeypatch.setenv('https_proxy', '')649 monkeypatch.setenv('ftp_proxy', '')
5 unmodified lines