From cca516415ef1889bc0e707eaba93e80fa471b070 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Mon, 28 Mar 2016 16:50:25 +0300 Subject: [PATCH 1/5] Add force_proto kwarg on contrib HTTP20Adapter --- hyper/common/connection.py | 3 ++- hyper/contrib.py | 4 +++- test/test_abstraction.py | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hyper/common/connection.py b/hyper/common/connection.py index 0d6ffb69..c5050a2f 100644 --- a/hyper/common/connection.py +++ b/hyper/common/connection.py @@ -52,6 +52,7 @@ def __init__(self, window_manager=None, enable_push=False, ssl_context=None, + force_proto=None, proxy_host=None, proxy_port=None, **kwargs): @@ -64,7 +65,7 @@ def __init__(self, } 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 } diff --git a/hyper/contrib.py b/hyper/contrib.py index 17a8b0d0..01956e7c 100644 --- a/hyper/contrib.py +++ b/hyper/contrib.py @@ -27,6 +27,7 @@ class HTTP20Adapter(HTTPAdapter): def __init__(self, *args, **kwargs): #: A mapping between HTTP netlocs and ``HTTP20Connection`` objects. self.connections = {} + self.force_proto = kwargs.get('force_proto') def get_connection(self, host, port, scheme): """ @@ -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 diff --git a/test/test_abstraction.py b/test/test_abstraction.py index c0852db7..8abd1f06 100644 --- a/test/test_abstraction.py +++ b/test/test_abstraction.py @@ -22,7 +22,7 @@ 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 == { @@ -30,6 +30,7 @@ def test_h2_kwargs(self): 'enable_push': True, 'secure': False, 'ssl_context': True, + 'force_proto': False, 'proxy_host': False, 'proxy_port': False, 'other_kwarg': True, From 7632be84b2252125ebd20a676d82bbbf09c69ead Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Mon, 28 Mar 2016 17:07:09 +0300 Subject: [PATCH 2/5] Move force_proto to be an explicit HTTP20Adapter kwarg --- hyper/contrib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hyper/contrib.py b/hyper/contrib.py index 01956e7c..042aff5c 100644 --- a/hyper/contrib.py +++ b/hyper/contrib.py @@ -24,10 +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 = kwargs.get('force_proto') + self.force_proto = force_proto def get_connection(self, host, port, scheme): """ From ee9acdd504c90912297e860eefb580c9eba21679 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Mon, 28 Mar 2016 17:18:54 +0300 Subject: [PATCH 3/5] Plumb force_proto through HTTP11Connection --- hyper/common/connection.py | 2 +- hyper/http11/connection.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hyper/common/connection.py b/hyper/common/connection.py index c5050a2f..2977d3f4 100644 --- a/hyper/common/connection.py +++ b/hyper/common/connection.py @@ -60,7 +60,7 @@ def __init__(self, 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 = { diff --git a/hyper/http11/connection.py b/hyper/http11/connection.py index 9a92c002..25536daa 100644 --- a/hyper/http11/connection.py +++ b/hyper/http11/connection.py @@ -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: @@ -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. @@ -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) From a34e41362f936e14476309a4dee120af2b7152f2 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Mon, 28 Mar 2016 17:22:31 +0300 Subject: [PATCH 4/5] Add force_proto param documentation on HTTPConnection --- hyper/common/connection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hyper/common/connection.py b/hyper/common/connection.py index 2977d3f4..c8671133 100644 --- a/hyper/common/connection.py +++ b/hyper/common/connection.py @@ -40,6 +40,8 @@ class HTTPConnection(object): :meth:`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 From 6ed0da828a3191b3f649d830213547f5545f0a0d Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Mon, 28 Mar 2016 17:23:58 +0300 Subject: [PATCH 5/5] Fix TestHTTPConnection --- test/test_abstraction.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_abstraction.py b/test/test_abstraction.py index 8abd1f06..133efa9a 100644 --- a/test/test_abstraction.py +++ b/test/test_abstraction.py @@ -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,