MCPcopy Index your code
hub / github.com/RustPython/RustPython / _proxy_bypass_macosx_sysconf

Function _proxy_bypass_macosx_sysconf

Lib/urllib/request.py:1946–2006  ·  view source on GitHub ↗

Return True iff this host shouldn't be accessed using a proxy This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: { 'exclude_simple': bool, 'exceptions': ['foo

(host, proxy_settings)

Source from the content-addressed store, hash-verified

1944# This code tests an OSX specific data structure but is testable on all
1945# platforms
1946def _proxy_bypass_macosx_sysconf(host, proxy_settings):
1947 """
1948 Return True iff this host shouldn't be accessed using a proxy
1949
1950 This function uses the MacOSX framework SystemConfiguration
1951 to fetch the proxy information.
1952
1953 proxy_settings come from _scproxy._get_proxy_settings or get mocked ie:
1954 { 'exclude_simple': bool,
1955 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16']
1956 }
1957 """
1958 from fnmatch import fnmatch
1959 from ipaddress import AddressValueError, IPv4Address
1960
1961 hostonly, port = _splitport(host)
1962
1963 def ip2num(ipAddr):
1964 parts = ipAddr.split('.')
1965 parts = list(map(int, parts))
1966 if len(parts) != 4:
1967 parts = (parts + [0, 0, 0, 0])[:4]
1968 return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]
1969
1970 # Check for simple host names:
1971 if '.' not in host:
1972 if proxy_settings['exclude_simple']:
1973 return True
1974
1975 hostIP = None
1976 try:
1977 hostIP = int(IPv4Address(hostonly))
1978 except AddressValueError:
1979 pass
1980
1981 for value in proxy_settings.get('exceptions', ()):
1982 # Items in the list are strings like these: *.local, 169.254/16
1983 if not value: continue
1984
1985 m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
1986 if m is not None and hostIP is not None:
1987 base = ip2num(m.group(1))
1988 mask = m.group(2)
1989 if mask is None:
1990 mask = 8 * (m.group(1).count('.') + 1)
1991 else:
1992 mask = int(mask[1:])
1993
1994 if mask < 0 or mask > 32:
1995 # System libraries ignore invalid prefix lengths
1996 continue
1997
1998 mask = 32 - mask
1999
2000 if (hostIP >> mask) == (base >> mask):
2001 return True
2002
2003 elif fnmatch(host, value):

Callers 2

test_osx_proxy_bypassMethod · 0.90

Calls 8

_splitportFunction · 0.90
IPv4AddressClass · 0.90
fnmatchFunction · 0.90
ip2numFunction · 0.85
getMethod · 0.45
matchMethod · 0.45
groupMethod · 0.45
countMethod · 0.45

Tested by 1

test_osx_proxy_bypassMethod · 0.72