Merge remote-tracking branch 'origin/5.4.x' into 6.0.x

This commit is contained in:
WILLIAM CHENG
2021-11-01 00:54:44 +08:00
288 changed files with 4107 additions and 2113 deletions

View File

@@ -262,6 +262,9 @@ conf = petstore_api.Configuration(
self.proxy = None
"""Proxy URL
"""
self.no_proxy = None
"""bypass proxy for host in the no_proxy list.
"""
self.proxy_headers = None
"""Proxy headers
"""

View File

@@ -14,8 +14,10 @@ import logging
import re
import ssl
from urllib.parse import urlencode
from urllib.parse import urlparse
from urllib.request import proxy_bypass_environment
import urllib3
import ipaddress
from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError
@@ -72,7 +74,7 @@ class RESTClientObject(object):
maxsize = 4
# https pool manager
if configuration.proxy:
if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''):
self.pool_manager = urllib3.ProxyManager(
num_pools=pools_size,
maxsize=maxsize,
@@ -290,3 +292,55 @@ class RESTClientObject(object):
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)
# end of class RESTClientObject
def is_ipv4(target):
""" Test if IPv4 address or not
"""
try:
chk = ipaddress.IPv4Address(target)
return True
except ipaddress.AddressValueError:
return False
def in_ipv4net(target, net):
""" Test if target belongs to given IPv4 network
"""
try:
nw = ipaddress.IPv4Network(net)
ip = ipaddress.IPv4Address(target)
if ip in nw:
return True
return False
except ipaddress.AddressValueError:
return False
except ipaddress.NetmaskValueError:
return False
def should_bypass_proxies(url, no_proxy=None):
""" Yet another requests.should_bypass_proxies
Test if proxies should not be used for a particular url.
"""
parsed = urlparse(url)
# special cases
if parsed.hostname in [None, '']:
return True
# special cases
if no_proxy in [None , '']:
return False
if no_proxy == '*':
return True
no_proxy = no_proxy.lower().replace(' ','');
entries = (
host for host in no_proxy.split(',') if host
)
if is_ipv4(parsed.hostname):
for item in entries:
if in_ipv4net(parsed.hostname, item):
return True
return proxy_bypass_environment(parsed.hostname, {'no': no_proxy} )

View File

@@ -48,9 +48,9 @@ class TestExtraOptionsForPools(unittest.TestCase):
socket_options = ["extra", "socket", "options"]
config = petstore_api.Configuration(host="HOST")
config = petstore_api.Configuration(host="http://somehost.local:8080")
config.socket_options = socket_options
config.proxy = True
config.proxy = "http://proxy.local:8080/"
with patch("petstore_api.rest.urllib3.ProxyManager", StubProxyManager):
api_client = petstore_api.ApiClient(config)