Skip to content

Commit dacbcef

Browse files
committed
fix(proxy): fix host no proxy check
1 parent d6d7897 commit dacbcef

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

volcenginesdkcore/rest.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import re
1010
import ssl
11+
import threading
1112

1213
import certifi
1314
# python 2 and python 3 compatibility library
@@ -61,6 +62,19 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
6162
# maxsize is the number of requests to host that are allowed in parallel # noqa: E501
6263
# Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501
6364

65+
self._configuration = configuration
66+
self._last_pm_proxy_url = None
67+
self._pools_size = pools_size
68+
self._maxsize = maxsize
69+
self._lock = threading.Lock()
70+
self.pool_manager = None
71+
72+
def __init_pool_manager(self, configuration, host, pools_size, maxsize):
73+
74+
proxy_url = self.__get_proxy(host)
75+
if proxy_url == self._last_pm_proxy_url and self.pool_manager:
76+
return
77+
6478
# cert_reqs
6579
if configuration.verify_ssl:
6680
cert_reqs = ssl.CERT_REQUIRED
@@ -92,26 +106,6 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
92106
read=configuration.read_timeout,
93107
)
94108

95-
proxy_url = configuration.proxy
96-
if configuration.host in _get_no_proxy(configuration):
97-
proxy_url = None
98-
elif not configuration.proxy:
99-
if configuration.scheme == 'http':
100-
if configuration.http_proxy:
101-
proxy_url = configuration.http_proxy
102-
elif os.getenv('HTTP_PROXY'):
103-
proxy_url = os.getenv('HTTP_PROXY')
104-
elif os.getenv('http_proxy'):
105-
proxy_url = os.getenv('http_proxy')
106-
elif configuration.scheme == 'https':
107-
if configuration.https_proxy:
108-
proxy_url = configuration.https_proxy
109-
elif os.getenv('HTTPS_PROXY'):
110-
proxy_url = os.getenv('HTTPS_PROXY')
111-
elif os.getenv('https_proxy'):
112-
proxy_url = os.getenv('https_proxy')
113-
114-
# https pool manager
115109
if proxy_url:
116110
self.pool_manager = urllib3.ProxyManager(
117111
num_pools=pools_size,
@@ -138,6 +132,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
138132
**addition_pool_args
139133
)
140134

135+
self._last_pm_proxy_url = proxy_url
136+
137+
def __get_proxy(self, host):
138+
configuration = self._configuration
139+
proxy_url = configuration.proxy
140+
if host in _get_no_proxy(configuration):
141+
proxy_url = None
142+
elif not configuration.proxy:
143+
if configuration.scheme == 'http':
144+
if configuration.http_proxy:
145+
proxy_url = configuration.http_proxy
146+
elif os.getenv('HTTP_PROXY'):
147+
proxy_url = os.getenv('HTTP_PROXY')
148+
elif os.getenv('http_proxy'):
149+
proxy_url = os.getenv('http_proxy')
150+
elif configuration.scheme == 'https':
151+
if configuration.https_proxy:
152+
proxy_url = configuration.https_proxy
153+
elif os.getenv('HTTPS_PROXY'):
154+
proxy_url = os.getenv('HTTPS_PROXY')
155+
elif os.getenv('https_proxy'):
156+
proxy_url = os.getenv('https_proxy')
157+
return proxy_url
158+
141159
def request(self, method, url, query_params=None, headers=None,
142160
body=None, post_params=None, _preload_content=True,
143161
_request_timeout=None):
@@ -187,6 +205,10 @@ def request(self, method, url, query_params=None, headers=None,
187205
if 'Content-Type' not in headers:
188206
headers['Content-Type'] = 'application/json'
189207

208+
host = headers.get('Host')
209+
with self._lock:
210+
self.__init_pool_manager(self._configuration, host, self._pools_size, self._maxsize)
211+
190212
try:
191213
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
192214
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:

0 commit comments

Comments
 (0)