2727
2828log = logging .getLogger (__name__ )
2929
30+ H2_CLEARTEXT_PROTOCOL = 'h2c'
3031
3132class 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