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

Commit 8416072

Browse files
committed
Added support for plaintext http20
1 parent 7a4d51e commit 8416072

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

hyper/common/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class HTTPConnection(object):
2727
``'http2bin.org'``, ``'http2bin.org:443'`` or ``'127.0.0.1'``.
2828
:param port: (optional) The port to connect to. If not provided and one also
2929
isn't provided in the ``host`` parameter, defaults to 443.
30-
:param secure: (optional, HTTP/1.1 only) Whether the request should use
31-
TLS. Defaults to ``False`` for most requests, but to ``True`` for any
30+
:param secure: (optional) Whether the request should use TLS.
31+
Defaults to ``False`` for most requests, but to ``True`` for any
3232
request issued to port 443.
3333
:param window_manager: (optional) The class to use to manage flow control
3434
windows. This needs to be a subclass of the

hyper/http20/connection.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
log = logging.getLogger(__name__)
2929

30+
H2_CLEARTEXT_PROTOCOL = 'h2c'
3031

3132
class HTTP20Connection(object):
3233
"""
@@ -43,6 +44,9 @@ class HTTP20Connection(object):
4344
``'http2bin.org'``, ``'http2bin.org:443'`` or ``'127.0.0.1'``.
4445
:param port: (optional) The port to connect to. If not provided and one also
4546
isn't provided in the ``host`` parameter, defaults to 443.
47+
:param secure: (optional) Whether the request should use TLS. Defaults to
48+
``False`` for most requests, but to ``True`` for any request issued to
49+
port 443.
4650
:param window_manager: (optional) The class to use to manage flow control
4751
windows. This needs to be a subclass of the
4852
:class:`BaseFlowControlManager <hyper.http20.window.BaseFlowControlManager>`.
@@ -55,7 +59,7 @@ class HTTP20Connection(object):
5559
:param ssl_context: (optional) A class with custom certificate settings.
5660
If not provided then hyper's default ``SSLContext`` is used instead.
5761
"""
58-
def __init__(self, host, port=None, window_manager=None, enable_push=False,
62+
def __init__(self, host, port=None, secure=None, window_manager=None, enable_push=False,
5963
ssl_context=None, **kwargs):
6064
"""
6165
Creates an HTTP/2 connection to a specific server.
@@ -69,6 +73,13 @@ def __init__(self, host, port=None, window_manager=None, enable_push=False,
6973
else:
7074
self.host, self.port = host, port
7175

76+
if secure is not None:
77+
self.secure = secure
78+
elif self.port == 443:
79+
self.secure = True
80+
else:
81+
self.secure = False
82+
7283
self._enable_push = enable_push
7384
self.ssl_context = ssl_context
7485

@@ -210,9 +221,13 @@ def connect(self):
210221
if self._sock is None:
211222
sock = socket.create_connection((self.host, self.port), 5)
212223

213-
sock, proto = wrap_socket(sock, self.host, self.ssl_context)
224+
if self.secure:
225+
sock, proto = wrap_socket(sock, self.host, self.ssl_context)
226+
else:
227+
proto = H2_CLEARTEXT_PROTOCOL
228+
214229
log.debug("Selected NPN protocol: %s", proto)
215-
assert proto in H2_NPN_PROTOCOLS
230+
assert proto in (H2_NPN_PROTOCOLS, H2_CLEARTEXT_PROTOCOL)
216231

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

0 commit comments

Comments
 (0)