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

Commit d156855

Browse files
committed
Update connection constructors and connect methods for proxy support
1 parent 2b2a56c commit d156855

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

hyper/common/connection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class HTTPConnection(object):
4040
:meth:`get_pushes() <hyper.HTTP20Connection.get_pushes>`).
4141
:param ssl_context: (optional) A class with custom certificate settings.
4242
If not provided then hyper's default ``SSLContext`` is used instead.
43-
:param proxies: (optional) A dictionary whose keys are the scheme used
44-
(http or https) and the value being the url of the proxy).
43+
:param proxy: (optional) The proxy to connect to. This can be an IP address
44+
or a host name and may include a port.
4545
"""
4646
def __init__(self,
4747
host,
@@ -50,15 +50,15 @@ def __init__(self,
5050
window_manager=None,
5151
enable_push=False,
5252
ssl_context=None,
53-
proxies=None,
53+
proxy=None,
5454
**kwargs):
5555

5656
self._host = host
5757
self._port = port
58-
self._h1_kwargs = {'secure': secure, 'ssl_context': ssl_context, 'proxies': proxies }
58+
self._h1_kwargs = {'secure': secure, 'ssl_context': ssl_context, 'proxy': proxy }
5959
self._h2_kwargs = {
6060
'window_manager': window_manager, 'enable_push': enable_push,
61-
'secure': secure, 'ssl_context': ssl_context, 'proxies': proxies
61+
'secure': secure, 'ssl_context': ssl_context, 'proxy': proxy
6262
}
6363

6464
# Add any unexpected kwargs to both dictionaries.

hyper/http11/connection.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class HTTP11Connection(object):
4747
port 443.
4848
:param ssl_context: (optional) A class with custom certificate settings.
4949
If not provided then hyper's default ``SSLContext`` is used instead.
50-
:param proxies: (optional) A dictionary whose keys are the scheme used
51-
(http or https) and the value being the url of the proxy).
50+
:param proxy: (optional) The proxy to connect to. This can be an IP address
51+
or a host name and may include a port.
5252
"""
5353
def __init__(self, host, port=None, secure=None, ssl_context=None,
54-
proxies=None, **kwargs):
54+
proxy=None, **kwargs):
5555
if port is None:
5656
try:
5757
self.host, self.port = host.split(':')
@@ -75,8 +75,17 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
7575
self._send_http_upgrade = not self.secure
7676

7777
self.ssl_context = ssl_context
78-
self.proxies = proxies
7978
self._sock = None
79+
80+
if proxy:
81+
self.proxy_host, self.proxy_port = proxy.split(':')
82+
if(self.proxy_port):
83+
self.proxy_port = int(self.proxy_port)
84+
else:
85+
self.proxy_port = 8080
86+
else:
87+
self.proxy_host = None
88+
self.proxy_port = None
8089

8190
#: The size of the in-memory buffer used to store data from the
8291
#: network. This is used as a performance optimisation. Increase buffer
@@ -96,11 +105,16 @@ def connect(self):
96105
:returns: Nothing.
97106
"""
98107
if self._sock is None:
99-
sock = socket.create_connection((self.host, self.port), 5)
108+
if not self.proxy_host:
109+
host = self.host
110+
else:
111+
port = self.proxy_host
112+
113+
sock = socket.create_connection((host, port), 5)
100114
proto = None
101115

102116
if self.secure:
103-
sock, proto = wrap_socket(sock, self.host, self.ssl_context)
117+
sock, proto = wrap_socket(sock, host, self.ssl_context)
104118

105119
log.debug("Selected protocol: %s", proto)
106120
sock = BufferedSocket(sock, self.network_buffer_size)

hyper/http20/connection.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class HTTP20Connection(object):
5656
:meth:`get_pushes() <hyper.HTTP20Connection.get_pushes>`).
5757
:param ssl_context: (optional) A class with custom certificate settings.
5858
If not provided then hyper's default ``SSLContext`` is used instead.
59-
:param proxies: (optional) A dictionary whose keys are the scheme used
59+
:param proxy: (optional) A dictionary whose keys are the scheme used
6060
(http or https) and the value being the url of the proxy).
6161
"""
6262
def __init__(self, host, port=None, secure=None, window_manager=None, enable_push=False,
63-
ssl_context=None, proxies=None, **kwargs):
63+
ssl_context=None, proxy=None, **kwargs):
6464
"""
6565
Creates an HTTP/2 connection to a specific server.
6666
"""
@@ -82,7 +82,16 @@ def __init__(self, host, port=None, secure=None, window_manager=None, enable_pus
8282

8383
self._enable_push = enable_push
8484
self.ssl_context = ssl_context
85-
self.proxies = proxies
85+
86+
if proxy:
87+
self.proxy_host, self.proxy_port = proxy.split(':')
88+
if(self.proxy_port):
89+
self.proxy_port = int(self.proxy_port)
90+
else:
91+
self.proxy_port = 8080
92+
else:
93+
self.proxy_host = None
94+
self.proxy_port = None
8695

8796
#: The size of the in-memory buffer used to store data from the
8897
#: network. This is used as a performance optimisation. Increase buffer
@@ -225,10 +234,15 @@ def connect(self):
225234
:returns: Nothing.
226235
"""
227236
if self._sock is None:
228-
sock = socket.create_connection((self.host, self.port), 5)
237+
if not self.proxy_host:
238+
host = self.host
239+
else:
240+
port = self.proxy_host
241+
242+
sock = socket.create_connection((host, port), 5)
229243

230244
if self.secure:
231-
sock, proto = wrap_socket(sock, self.host, self.ssl_context)
245+
sock, proto = wrap_socket(sock, host, self.ssl_context)
232246
else:
233247
proto = H2C_PROTOCOL
234248

0 commit comments

Comments
 (0)