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

Commit 147142f

Browse files
committed
Allow protocol version to be specified, useful is ALPN is unavailable but is the only means conveyed
1 parent 89d4227 commit 147142f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

hyper/http20/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class HTTP20Connection(object):
6666
and one also isn't provided in the ``proxy`` parameter, defaults to 8080.
6767
"""
6868
def __init__(self, host, port=None, secure=None, window_manager=None, enable_push=False,
69-
ssl_context=None, proxy_host=None, proxy_port=None, **kwargs):
69+
ssl_context=None, proxy_host=None, proxy_port=None,
70+
force_proto=None, **kwargs):
7071
"""
7172
Creates an HTTP/2 connection to a specific server.
7273
"""
@@ -101,6 +102,8 @@ def __init__(self, host, port=None, secure=None, window_manager=None, enable_pus
101102
#: Defaults to 64kB.
102103
self.network_buffer_size = 65536
103104

105+
self.force_proto = force_proto
106+
104107
# Create the mutable state.
105108
self.__wm_class = window_manager or FlowControlManager
106109
self.__init_state()
@@ -249,7 +252,8 @@ def connect(self):
249252

250253
if self.secure:
251254
assert not self.proxy_host, "Using a proxy with HTTPS not yet supported."
252-
sock, proto = wrap_socket(sock, host, self.ssl_context)
255+
sock, proto = wrap_socket(sock, host, self.ssl_context,
256+
force_proto=self.force_proto)
253257
else:
254258
proto = H2C_PROTOCOL
255259

hyper/tls.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
cert_loc = path.join(path.dirname(__file__), 'certs.pem')
2525

2626

27-
def wrap_socket(sock, server_hostname, ssl_context=None):
27+
def wrap_socket(sock, server_hostname, ssl_context=None, force_proto=None):
2828
"""
2929
A vastly simplified SSL wrapping function. We'll probably extend this to
3030
do more things later.
@@ -49,16 +49,19 @@ def wrap_socket(sock, server_hostname, ssl_context=None):
4949
except AttributeError:
5050
ssl.verify_hostname(ssl_sock, server_hostname) # pyopenssl
5151

52-
proto = None
52+
if force_proto != None:
53+
proto = force_proto
54+
else:
55+
proto = None
5356

54-
# ALPN is newer, so we prefer it over NPN. The odds of us getting different
55-
# answers is pretty low, but let's be sure.
56-
with ignore_missing():
57-
proto = ssl_sock.selected_alpn_protocol()
57+
# ALPN is newer, so we prefer it over NPN. The odds of us getting
58+
# different answers is pretty low, but let's be sure.
59+
with ignore_missing():
60+
proto = ssl_sock.selected_alpn_protocol()
5861

59-
with ignore_missing():
60-
if proto is None:
61-
proto = ssl_sock.selected_npn_protocol()
62+
with ignore_missing():
63+
if proto is None:
64+
proto = ssl_sock.selected_npn_protocol()
6265

6366
return (ssl_sock, proto)
6467

0 commit comments

Comments
 (0)