11
11
12
12
13
13
NPN_PROTOCOL = 'h2'
14
- H2_NPN_PROTOCOLS = [NPN_PROTOCOL , 'h2-16' , 'h2-15' , 'h2-14' ] # All h2s we support.
14
+ H2_NPN_PROTOCOLS = [NPN_PROTOCOL , 'h2-16' , 'h2-15' , 'h2-14' ]
15
15
SUPPORTED_NPN_PROTOCOLS = H2_NPN_PROTOCOLS + ['http/1.1' ]
16
16
17
17
22
22
# Work out where our certificates are.
23
23
cert_loc = path .join (path .dirname (__file__ ), 'certs.pem' )
24
24
25
+
25
26
def wrap_socket (sock , server_hostname ):
26
27
"""
27
28
A vastly simplified SSL wrapping function. We'll probably extend this to
@@ -35,17 +36,24 @@ def wrap_socket(sock, server_hostname):
35
36
# the spec requires SNI support
36
37
ssl_sock = _context .wrap_socket (sock , server_hostname = server_hostname )
37
38
# Setting SSLContext.check_hostname to True only verifies that the
38
- # post-handshake servername matches that of the certificate. We also need to
39
- # check that it matches the requested one.
39
+ # post-handshake servername matches that of the certificate. We also need
40
+ # to check that it matches the requested one.
40
41
if _context .check_hostname : # pragma: no cover
41
42
try :
42
43
ssl .match_hostname (ssl_sock .getpeercert (), server_hostname )
43
44
except AttributeError :
44
45
ssl .verify_hostname (ssl_sock , server_hostname ) # pyopenssl
45
46
46
47
proto = None
48
+
49
+ # ALPN is newer, so we prefer it over NPN. The odds of us getting different
50
+ # answers is pretty low, but let's be sure.
51
+ with ignore_missing ():
52
+ proto = ssl_sock .selected_alpn_protocol ()
53
+
47
54
with ignore_missing ():
48
- proto = ssl_sock .selected_npn_protocol ()
55
+ if proto is None :
56
+ proto = ssl_sock .selected_npn_protocol ()
49
57
50
58
return (ssl_sock , proto )
51
59
@@ -63,6 +71,9 @@ def _init_context():
63
71
with ignore_missing ():
64
72
context .set_npn_protocols (SUPPORTED_NPN_PROTOCOLS )
65
73
74
+ with ignore_missing ():
75
+ context .set_alpn_protocols (SUPPORTED_NPN_PROTOCOLS )
76
+
66
77
# required by the spec
67
78
context .options |= ssl .OP_NO_COMPRESSION
68
79
0 commit comments