Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Move self.proxy_headers call into get_connection of requests adapter
Browse files Browse the repository at this point in the history
This fixes a possible situation when calling get_connection with the
same arguments but different proxy_headers would reuse an already
created connection, so the new proxy_headers will be ignored.

#322 (comment)
  • Loading branch information
KostyaEsmukov committed May 10, 2017
1 parent 848edce commit bbf59ff
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions hyper/contrib.py
Expand Up @@ -31,8 +31,7 @@ def __init__(self, *args, **kwargs):
#: A mapping between HTTP netlocs and ``HTTP20Connection`` objects.
self.connections = {}

def get_connection(self, host, port, scheme, cert=None, proxy=None,
proxy_headers=None):
def get_connection(self, host, port, scheme, cert=None, proxy=None):
"""
Gets an appropriate HTTP/2 connection object based on
host/port/scheme/cert tuples.
Expand All @@ -46,6 +45,13 @@ def get_connection(self, host, port, scheme, cert=None, proxy=None,
if cert is not None:
ssl_context = init_context(cert=cert)

if proxy:
proxy_headers = self.proxy_headers(proxy)
proxy_netloc = urlparse(proxy).netloc
else:
proxy_headers = None
proxy_netloc = None

connection_key = (host, port, scheme, cert, proxy)
try:
conn = self.connections[connection_key]
Expand All @@ -55,7 +61,7 @@ def get_connection(self, host, port, scheme, cert=None, proxy=None,
port,
secure=secure,
ssl_context=ssl_context,
proxy_host=proxy,
proxy_host=proxy_netloc,
proxy_headers=proxy_headers)
self.connections[connection_key] = conn

Expand All @@ -66,20 +72,16 @@ def send(self, request, stream=False, cert=None, proxies=None, **kwargs):
Sends a HTTP message to the server.
"""
proxy = select_proxy(request.url, proxies)
proxy_headers = None
if proxy:
proxy = prepend_scheme_if_needed(proxy, 'http')
proxy_headers = self.proxy_headers(proxy)
proxy = urlparse(proxy).netloc

parsed = urlparse(request.url)
conn = self.get_connection(
parsed.hostname,
parsed.port,
parsed.scheme,
cert=cert,
proxy=proxy,
proxy_headers=proxy_headers)
proxy=proxy)

# Build the selector.
selector = parsed.path
Expand Down

0 comments on commit bbf59ff

Please sign in to comment.