use global urllib Pools

This commit is contained in:
Jens Langhammer 2019-04-11 15:29:35 +02:00
parent f69f959bdb
commit 61478db94e
1 changed files with 9 additions and 6 deletions

View File

@ -27,6 +27,11 @@ ERRORS_MESSAGES = {
'upstream-no-scheme': ("Upstream URL scheme must be either " 'upstream-no-scheme': ("Upstream URL scheme must be either "
"'http' or 'https' (%s).") "'http' or 'https' (%s).")
} }
HTTP_NO_VERIFY = urllib3.PoolManager()
HTTP = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
class ApplicationGatewayMiddleware: class ApplicationGatewayMiddleware:
@ -45,10 +50,6 @@ class ApplicationGatewayMiddleware:
def __init__(self, get_response): def __init__(self, get_response):
self.get_response = get_response self.get_response = get_response
self.ignored_hosts = cache.get(IGNORED_HOSTNAMES_KEY, []) self.ignored_hosts = cache.get(IGNORED_HOSTNAMES_KEY, [])
self.http_no_verify = urllib3.PoolManager()
self.http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
def precheck(self, request): def precheck(self, request):
"""Check if a request should be proxied or forwarded to passbook""" """Check if a request should be proxied or forwarded to passbook"""
@ -137,6 +138,8 @@ class ApplicationGatewayMiddleware:
.. versionadded:: 0.9.8 .. versionadded:: 0.9.8
""" """
request_headers = self.get_proxy_request_headers(self.request) request_headers = self.get_proxy_request_headers(self.request)
if not self.app_gw.authentication_header:
return request_headers
request_headers[self.app_gw.authentication_header] = self.request.user.get_username() request_headers[self.app_gw.authentication_header] = self.request.user.get_username()
LOGGER.info("%s set", self.app_gw.authentication_header) LOGGER.info("%s set", self.app_gw.authentication_header)
@ -171,9 +174,9 @@ class ApplicationGatewayMiddleware:
request_url += '?' + self.get_encoded_query_params() request_url += '?' + self.get_encoded_query_params()
LOGGER.debug("Request URL: %s", request_url) LOGGER.debug("Request URL: %s", request_url)
http = self.http http = HTTP
if not self.app_gw.upstream_ssl_verification: if not self.app_gw.upstream_ssl_verification:
http = self.http_no_verify http = HTTP_NO_VERIFY
try: try:
proxy_response = http.urlopen(request.method, proxy_response = http.urlopen(request.method,