27
27
28
28
log = logging .getLogger (__name__ )
29
29
30
+ H2_CLEARTEXT_PROTOCOL = 'h2c'
30
31
31
32
class HTTP20Connection (object ):
32
33
"""
@@ -43,6 +44,9 @@ class HTTP20Connection(object):
43
44
``'http2bin.org'``, ``'http2bin.org:443'`` or ``'127.0.0.1'``.
44
45
:param port: (optional) The port to connect to. If not provided and one also
45
46
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.
46
50
:param window_manager: (optional) The class to use to manage flow control
47
51
windows. This needs to be a subclass of the
48
52
:class:`BaseFlowControlManager <hyper.http20.window.BaseFlowControlManager>`.
@@ -55,7 +59,7 @@ class HTTP20Connection(object):
55
59
:param ssl_context: (optional) A class with custom certificate settings.
56
60
If not provided then hyper's default ``SSLContext`` is used instead.
57
61
"""
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 ,
59
63
ssl_context = None , ** kwargs ):
60
64
"""
61
65
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,
69
73
else :
70
74
self .host , self .port = host , port
71
75
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
+
72
83
self ._enable_push = enable_push
73
84
self .ssl_context = ssl_context
74
85
@@ -210,9 +221,13 @@ def connect(self):
210
221
if self ._sock is None :
211
222
sock = socket .create_connection ((self .host , self .port ), 5 )
212
223
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
+
214
229
log .debug ("Selected NPN protocol: %s" , proto )
215
- assert proto in H2_NPN_PROTOCOLS
230
+ assert proto in ( H2_NPN_PROTOCOLS , H2_CLEARTEXT_PROTOCOL )
216
231
217
232
self ._sock = BufferedSocket (sock , self .network_buffer_size )
218
233
0 commit comments