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

Add force_proto kwarg on contrib HTTP20Adapter #214

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions hyper/common/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class HTTPConnection(object):
:meth:`get_pushes() <hyper.HTTP20Connection.get_pushes>`).
:param ssl_context: (optional) A class with custom certificate settings.
If not provided then hyper's default ``SSLContext`` is used instead.
:param force_proto: (optional) An optional parameter forcing the SSL/TLS
connection to a specific protocol, circumvents use of ALPN/NPN.
:param proxy_host: (optional) The proxy to connect to. This can be an IP address
or a host name and may include a port.
:param proxy_port: (optional) The proxy port to connect to. If not provided
Expand All @@ -52,19 +54,20 @@ def __init__(self,
window_manager=None,
enable_push=False,
ssl_context=None,
force_proto=None,
proxy_host=None,
proxy_port=None,
**kwargs):

self._host = host
self._port = port
self._h1_kwargs = {
'secure': secure, 'ssl_context': ssl_context,
'secure': secure, 'ssl_context': ssl_context, 'force_proto': force_proto,
'proxy_host': proxy_host, 'proxy_port': proxy_port
}
self._h2_kwargs = {
'window_manager': window_manager, 'enable_push': enable_push,
'secure': secure, 'ssl_context': ssl_context,
'secure': secure, 'ssl_context': ssl_context, 'force_proto': force_proto,
'proxy_host': proxy_host, 'proxy_port': proxy_port
}

Expand Down
6 changes: 4 additions & 2 deletions hyper/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class HTTP20Adapter(HTTPAdapter):
HTTP/2. This implements some degree of connection pooling to maximise the
HTTP/2 gain.
"""
def __init__(self, *args, **kwargs):
def __init__(self, force_proto=None, *args, **kwargs):
#: A mapping between HTTP netlocs and ``HTTP20Connection`` objects.
self.connections = {}
self.force_proto = force_proto

def get_connection(self, host, port, scheme):
"""
Expand All @@ -41,7 +42,8 @@ def get_connection(self, host, port, scheme):
try:
conn = self.connections[(host, port, scheme)]
except KeyError:
conn = HTTPConnection(host, port, secure=secure)
conn = HTTPConnection(host, port, secure=secure,
force_proto=self.force_proto)
self.connections[(host, port, scheme)] = conn

return conn
Expand Down
5 changes: 3 additions & 2 deletions hyper/http11/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class HTTP11Connection(object):
defaults to 8080.
"""
def __init__(self, host, port=None, secure=None, ssl_context=None,
proxy_host=None, proxy_port=None, **kwargs):
force_proto=None, proxy_host=None, proxy_port=None, **kwargs):
if port is None:
self.host, self.port = to_host_port_tuple(host, default_port=80)
else:
Expand All @@ -76,6 +76,7 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
self._send_http_upgrade = not self.secure

self.ssl_context = ssl_context
self.force_proto = force_proto
self._sock = None

# Setup proxy details if applicable.
Expand Down Expand Up @@ -118,7 +119,7 @@ def connect(self):

if self.secure:
assert not self.proxy_host, "Using a proxy with HTTPS not yet supported."
sock, proto = wrap_socket(sock, host, self.ssl_context)
sock, proto = wrap_socket(sock, host, self.ssl_context, self.force_proto)

log.debug("Selected protocol: %s", proto)
sock = BufferedSocket(sock, self.network_buffer_size)
Expand Down
6 changes: 4 additions & 2 deletions test/test_abstraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class TestHTTPConnection(object):
def test_h1_kwargs(self):
c = HTTPConnection(
'test', 443, secure=False, window_manager=True, enable_push=True,
ssl_context=False, proxy_host=False, proxy_port=False, other_kwarg=True
ssl_context=False, force_proto=False, proxy_host=False, proxy_port=False, other_kwarg=True
)

assert c._h1_kwargs == {
'secure': False,
'ssl_context': False,
'force_proto': False,
'proxy_host': False,
'proxy_port': False,
'other_kwarg': True,
Expand All @@ -22,14 +23,15 @@ def test_h1_kwargs(self):
def test_h2_kwargs(self):
c = HTTPConnection(
'test', 443, secure=False, window_manager=True, enable_push=True,
ssl_context=True, proxy_host=False, proxy_port=False, other_kwarg=True
ssl_context=True, force_proto=False, proxy_host=False, proxy_port=False, other_kwarg=True
)

assert c._h2_kwargs == {
'window_manager': True,
'enable_push': True,
'secure': False,
'ssl_context': True,
'force_proto': False,
'proxy_host': False,
'proxy_port': False,
'other_kwarg': True,
Expand Down