1111
1212
1313NPN_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' ]
1515SUPPORTED_NPN_PROTOCOLS = H2_NPN_PROTOCOLS + ['http/1.1' ]
1616
1717
2222# Work out where our certificates are.
2323cert_loc = path .join (path .dirname (__file__ ), 'certs.pem' )
2424
25+
2526def wrap_socket (sock , server_hostname ):
2627 """
2728 A vastly simplified SSL wrapping function. We'll probably extend this to
@@ -35,17 +36,24 @@ def wrap_socket(sock, server_hostname):
3536 # the spec requires SNI support
3637 ssl_sock = _context .wrap_socket (sock , server_hostname = server_hostname )
3738 # 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.
4041 if _context .check_hostname : # pragma: no cover
4142 try :
4243 ssl .match_hostname (ssl_sock .getpeercert (), server_hostname )
4344 except AttributeError :
4445 ssl .verify_hostname (ssl_sock , server_hostname ) # pyopenssl
4546
4647 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+
4754 with ignore_missing ():
48- proto = ssl_sock .selected_npn_protocol ()
55+ if proto is None :
56+ proto = ssl_sock .selected_npn_protocol ()
4957
5058 return (ssl_sock , proto )
5159
@@ -63,6 +71,9 @@ def _init_context():
6371 with ignore_missing ():
6472 context .set_npn_protocols (SUPPORTED_NPN_PROTOCOLS )
6573
74+ with ignore_missing ():
75+ context .set_alpn_protocols (SUPPORTED_NPN_PROTOCOLS )
76+
6677 # required by the spec
6778 context .options |= ssl .OP_NO_COMPRESSION
6879
0 commit comments