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

Commit 8539cb6

Browse files
committed
Add proxy port keyword parameter and update tests accordingly
1 parent fe16703 commit 8539cb6

File tree

7 files changed

+74
-36
lines changed

7 files changed

+74
-36
lines changed

hyper/common/connection.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ 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 proxy: (optional) The proxy to connect to. This can be an IP address
43+
:param proxy_host: (optional) The proxy to connect to. This can be an IP address
4444
or a host name and may include a port.
45+
:param proxy_port: (optional) The proxy port to connect to. If not provided
46+
and one also isn't provided in the ``proxy`` parameter, defaults to 8080.
4547
"""
4648
def __init__(self,
4749
host,
@@ -50,15 +52,16 @@ def __init__(self,
5052
window_manager=None,
5153
enable_push=False,
5254
ssl_context=None,
53-
proxy=None,
55+
proxy_host=None,
56+
proxy_port=None,
5457
**kwargs):
5558

5659
self._host = host
5760
self._port = port
58-
self._h1_kwargs = {'secure': secure, 'ssl_context': ssl_context, 'proxy': proxy }
61+
self._h1_kwargs = {'secure': secure, 'ssl_context': ssl_context, 'proxy_host': proxy_host, 'proxy_port': proxy_port }
5962
self._h2_kwargs = {
6063
'window_manager': window_manager, 'enable_push': enable_push,
61-
'secure': secure, 'ssl_context': ssl_context, 'proxy': proxy
64+
'secure': secure, 'ssl_context': ssl_context, 'proxy_host': proxy_host, 'proxy_port': proxy_port
6265
}
6366

6467
# Add any unexpected kwargs to both dictionaries.

hyper/http11/connection.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ 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 proxy: (optional) The proxy to connect to. This can be an IP address
50+
:param proxy_host: (optional) The proxy to connect to. This can be an IP address
5151
or a host name and may include a port.
52+
:param proxy_port: (optional) The proxy port to connect to. If not provided
53+
and one also isn't provided in the ``proxy`` parameter, defaults to 8080.
5254
"""
5355
def __init__(self, host, port=None, secure=None, ssl_context=None,
54-
proxy=None, **kwargs):
56+
proxy_host=None, proxy_port=None, **kwargs):
5557
if port is None:
5658
try:
5759
self.host, self.port = host.split(':')
@@ -76,17 +78,19 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
7678

7779
self.ssl_context = ssl_context
7880
self._sock = None
79-
80-
if proxy:
81-
if(':' in proxy):
82-
self.proxy_host, self.proxy_port = proxy.split(':')
83-
self.proxy_port = int(self.proxy_port)
81+
82+
# Setup proxy details if applicable.
83+
if proxy_host:
84+
if proxy_port is None:
85+
try:
86+
self.proxy_host, self.proxy_port = proxy_host.split(':')
87+
self.proxy_port = int(self.proxy_port)
88+
except ValueError:
89+
self.proxy_host, self.proxy_port = proxy_host, 8080
8490
else:
85-
self.proxy_host = proxy
86-
self.proxy_port = 8080
91+
self.proxy_host, self.proxy_port = proxy_host, proxy_port
8792
else:
8893
self.proxy_host = None
89-
self.proxy_port = None
9094

9195
#: The size of the in-memory buffer used to store data from the
9296
#: network. This is used as a performance optimisation. Increase buffer

hyper/http20/connection.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ 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 proxy: (optional) A dictionary whose keys are the scheme used
60-
(http or https) and the value being the url of the proxy).
59+
:param proxy_host: (optional) The proxy to connect to. This can be an IP address
60+
or a host name and may include a port.
61+
:param proxy_port: (optional) The proxy port to connect to. If not provided
62+
and one also isn't provided in the ``proxy`` parameter, defaults to 8080.
6163
"""
6264
def __init__(self, host, port=None, secure=None, window_manager=None, enable_push=False,
63-
ssl_context=None, proxy=None, **kwargs):
65+
ssl_context=None, proxy_host=None, proxy_port=None, **kwargs):
6466
"""
6567
Creates an HTTP/2 connection to a specific server.
6668
"""
@@ -83,15 +85,18 @@ def __init__(self, host, port=None, secure=None, window_manager=None, enable_pus
8385
self._enable_push = enable_push
8486
self.ssl_context = ssl_context
8587

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)
88+
# Setup proxy details if applicable.
89+
if proxy_host:
90+
if proxy_port is None:
91+
try:
92+
self.proxy_host, self.proxy_port = proxy_host.split(':')
93+
self.proxy_port = int(self.proxy_port)
94+
except ValueError:
95+
self.proxy_host, self.proxy_port = proxy_host, 8080
9096
else:
91-
self.proxy_port = 8080
97+
self.proxy_host, self.proxy_port = proxy_host, proxy_port
9298
else:
9399
self.proxy_host = None
94-
self.proxy_port = None
95100

96101
#: The size of the in-memory buffer used to store data from the
97102
#: network. This is used as a performance optimisation. Increase buffer

test/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ class SocketLevelTest(object):
137137
def set_up(self, secure=True, proxy=False):
138138
self.host = None
139139
self.port = None
140+
140141
self.proxy = proxy
142+
self.proxy_host = None
143+
self.proxy_port = None
144+
141145
self.secure = secure if not proxy else False
142146

143147
self.server_thread = None
@@ -185,9 +189,9 @@ def _proxy_socket_handler(tx_sock, rx_sock):
185189

186190
def get_connection(self):
187191
if self.h2:
188-
return HTTP20Connection(self.host, self.port, self.secure, proxy=':'.join([self.proxy_host, self.proxy_port]) if self.proxy else None)
192+
return HTTP20Connection(self.host, self.port, self.secure, proxy_host=self.proxy_host, proxy_port=self.proxy_port)
189193
else:
190-
return HTTP11Connection(self.host, self.port, self.secure, proxy=':'.join([self.proxy_host, self.proxy_port]) if self.proxy else None)
194+
return HTTP11Connection(self.host, self.port, self.secure, proxy_host=self.proxy_host, proxy_port=self.proxy_port)
191195

192196
def get_encoder(self):
193197
"""

test/test_abstraction.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ class TestHTTPConnection(object):
88
def test_h1_kwargs(self):
99
c = HTTPConnection(
1010
'test', 443, secure=False, window_manager=True, enable_push=True,
11-
ssl_context=False, proxy=False, other_kwarg=True
11+
ssl_context=False, proxy_host=False, proxy_port=False, other_kwarg=True
1212
)
1313

1414
assert c._h1_kwargs == {
1515
'secure': False,
1616
'ssl_context': False,
17-
'proxy': False,
17+
'proxy_host': False,
18+
'proxy_port': False,
1819
'other_kwarg': True,
1920
}
2021

2122
def test_h2_kwargs(self):
2223
c = HTTPConnection(
2324
'test', 443, secure=False, window_manager=True, enable_push=True,
24-
ssl_context=True, proxy=False, other_kwarg=True
25+
ssl_context=True, proxy_host=False, proxy_port=False, other_kwarg=True
2526
)
2627

2728
assert c._h2_kwargs == {
2829
'window_manager': True,
2930
'enable_push': True,
3031
'secure': False,
3132
'ssl_context': True,
32-
'proxy': False,
33+
'proxy_host': False,
34+
'proxy_port': False,
3335
'other_kwarg': True,
3436
}
3537

test/test_http11.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def test_initialization_no_port(self):
4444
assert c.port == 80
4545
assert not c.secure
4646
assert not c.proxy_host
47-
assert not c.proxy_port
4847

4948
def test_initialization_inline_port(self):
5049
c = HTTP11Connection('httpbin.org:443')
@@ -53,7 +52,6 @@ def test_initialization_inline_port(self):
5352
assert c.port == 443
5453
assert c.secure
5554
assert not c.proxy_host
56-
assert not c.proxy_port
5755

5856
def test_initialization_separate_port(self):
5957
c = HTTP11Connection('localhost', 8080)
@@ -62,7 +60,6 @@ def test_initialization_separate_port(self):
6260
assert c.port == 8080
6361
assert not c.secure
6462
assert not c.proxy_host
65-
assert not c.proxy_port
6663

6764
def test_can_override_security(self):
6865
c = HTTP11Connection('localhost', 443, secure=False)
@@ -71,26 +68,35 @@ def test_can_override_security(self):
7168
assert c.port == 443
7269
assert not c.secure
7370
assert not c.proxy_host
74-
assert not c.proxy_port
7571

7672
def test_initialization_proxy(self):
77-
c = HTTP11Connection('httpbin.org', proxy='localhost')
73+
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
7874

7975
assert c.host == 'httpbin.org'
8076
assert c.port == 80
8177
assert not c.secure
8278
assert c.proxy_host == 'localhost'
8379
assert c.proxy_port == 8080
8480

85-
def test_initialization_proxy_with_port(self):
86-
c = HTTP11Connection('httpbin.org', proxy='localhost:8443')
81+
def test_initialization_proxy_with_inline_port(self):
82+
c = HTTP11Connection('httpbin.org', proxy_host='localhost:8443')
8783

8884
assert c.host == 'httpbin.org'
8985
assert c.port == 80
9086
assert not c.secure
9187
assert c.proxy_host == 'localhost'
9288
assert c.proxy_port == 8443
9389

90+
def test_initialization_proxy_with_separate_port(self):
91+
c = HTTP11Connection('httpbin.org', proxy_host='localhost', proxy_port=8443)
92+
93+
assert c.host == 'httpbin.org'
94+
assert c.port == 80
95+
assert not c.secure
96+
assert c.proxy_host == 'localhost'
97+
assert c.proxy_port == 8443
98+
99+
94100
def test_basic_request(self):
95101
c = HTTP11Connection('httpbin.org')
96102
c._sock = sock = DummySocket()

test/test_hyper.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,25 @@ def test_connections_accept_hosts_and_ports(self):
4141
c = HTTP20Connection(host='www.google.com', port=8080)
4242
assert c.host =='www.google.com'
4343
assert c.port == 8080
44+
assert c.proxy_host == None
4445

4546
def test_connections_can_parse_hosts_and_ports(self):
4647
c = HTTP20Connection('www.google.com:8080')
4748
assert c.host == 'www.google.com'
4849
assert c.port == 8080
50+
assert c.proxy_host == None
51+
52+
def test_connections_accept_proxy_hosts_and_ports(self):
53+
c = HTTP20Connection('www.google.com', proxy_host='localhost:8443')
54+
assert c.host == 'www.google.com'
55+
assert c.proxy_host == 'localhost'
56+
assert c.proxy_port == 8443
57+
58+
def test_connections_can_parse_proxy_hosts_and_ports(self):
59+
c = HTTP20Connection('www.google.com', proxy_host='localhost', proxy_port=8443)
60+
assert c.host == 'www.google.com'
61+
assert c.proxy_host == 'localhost'
62+
assert c.proxy_port == 8443
4963

5064
def test_putrequest_establishes_new_stream(self):
5165
c = HTTP20Connection("www.google.com")

0 commit comments

Comments
 (0)