Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.
8 changes: 5 additions & 3 deletions hyper/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, *args, **kwargs):
self.connections = {}

def get_connection(self, host, port, scheme, cert=None, verify=True,
proxy=None):
proxy=None, **kwargs):
"""
Gets an appropriate HTTP/2 connection object based on
host/port/scheme/cert tuples.
Expand Down Expand Up @@ -77,7 +77,8 @@ def get_connection(self, host, port, scheme, cert=None, verify=True,
secure=secure,
ssl_context=ssl_context,
proxy_host=proxy_netloc,
proxy_headers=proxy_headers)
proxy_headers=proxy_headers,
**kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be sending arbitrary kwargs from Requests to hyper: there's no reason to assume that those kwargs will match in any sensible way. We should aim to be explicit here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I had change to explicit timeout and add more timeout tests

self.connections[connection_key] = conn

return conn
Expand All @@ -98,7 +99,8 @@ def send(self, request, stream=False, cert=None, verify=True, proxies=None,
parsed.scheme,
cert=cert,
verify=verify,
proxy=proxy)
proxy=proxy,
**kwargs)

# Build the selector.
selector = parsed.path
Expand Down
17 changes: 15 additions & 2 deletions hyper/http11/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
#: the standard hyper parsing interface.
self.parser = Parser()

# timeout
timeout = kwargs.get('timeout')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout should be optional. I think it should be None - that means no timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, timeout is optional and default None, is equivalent to socket.setblocking(1)

if isinstance(timeout, tuple):
self._connect_timeout = timeout[0]
self._read_timeout = timeout[1]
else:
self._connect_timeout = timeout
self._read_timeout = timeout

def connect(self):
"""
Connect to the server specified when the object was created. This is a
Expand All @@ -172,10 +181,11 @@ def connect(self):
# Simple http proxy
sock = socket.create_connection(
(self.proxy_host, self.proxy_port),
5
timeout=self._connect_timeout
)
else:
sock = socket.create_connection((self.host, self.port), 5)
sock = socket.create_connection((self.host, self.port),
timeout=self._connect_timeout)
proto = None

if self.secure:
Expand All @@ -184,6 +194,9 @@ def connect(self):
log.debug("Selected protocol: %s", proto)
sock = BufferedSocket(sock, self.network_buffer_size)

# Set read timeout
sock.settimeout(self._read_timeout)

if proto not in ('http/1.1', None):
raise TLSUpgrade(proto, sock)

Expand Down
18 changes: 16 additions & 2 deletions hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ def __init__(self, host, port=None, secure=None, window_manager=None,
self.__wm_class = window_manager or FlowControlManager
self.__init_state()

# timeout
timeout = kwargs.get('timeout')
if isinstance(timeout, tuple):
self._connect_timeout = timeout[0]
self._read_timeout = timeout[1]
else:
self._connect_timeout = timeout
self._read_timeout = timeout

return

def __init_state(self):
Expand Down Expand Up @@ -355,10 +364,12 @@ def connect(self):
elif self.proxy_host:
# Simple http proxy
sock = socket.create_connection(
(self.proxy_host, self.proxy_port)
(self.proxy_host, self.proxy_port),
timeout=self._connect_timeout
)
else:
sock = socket.create_connection((self.host, self.port))
sock = socket.create_connection((self.host, self.port),
timeout=self._connect_timeout)

if self.secure:
sock, proto = wrap_socket(sock, self.host, self.ssl_context,
Expand All @@ -374,6 +385,9 @@ def connect(self):

self._sock = BufferedSocket(sock, self.network_buffer_size)

# Set read timeout
self._sock.settimeout(self._read_timeout)

self._send_preamble()

def _connect_upgrade(self, sock):
Expand Down
2 changes: 1 addition & 1 deletion test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ def socket_handler(listener):

s = requests.Session()
s.mount('https://%s' % self.host, HTTP20Adapter())
r = s.get('https://%s:%s/some/path' % (self.host, self.port))
r = s.get('https://%s:%s/some/path' % (self.host, self.port), timeout=(10, 30))

# Assert about the received values.
assert r.status_code == 200
Expand Down