1414
1515import decorator
1616import urllib3
17+ from urllib3 .connection import match_hostname as urllib3_match_hostname
1718from urllib3 .util .ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket
1819from urllib3 .util .ssl_ import wrap_socket as urllib3_wrap_socket
1920
2021from .compat import basestring , byte_type , decode_from_bytes , encode_to_bytes , text_type
21- from .utils import SSL_PROTOCOL , MocketSocketCore , hexdump , hexload , wrap_ssl_socket
22+ from .utils import SSL_PROTOCOL , MocketSocketCore , hexdump , hexload
2223
2324xxh32 = None
2425try :
4950true_inet_pton = socket .inet_pton
5051true_urllib3_wrap_socket = urllib3_wrap_socket
5152true_urllib3_ssl_wrap_socket = urllib3_ssl_wrap_socket
53+ true_urllib3_match_hostname = urllib3_match_hostname
5254
5355
5456class SuperFakeSSLContext (object ):
55- """ For Python 3.6 """
57+ """For Python 3.6"""
5658
5759 class FakeSetter (int ):
5860 def __set__ (self , * args ):
5961 pass
6062
6163 options = FakeSetter ()
62- verify_mode = FakeSetter (ssl .CERT_OPTIONAL )
64+ verify_mode = FakeSetter (ssl .CERT_NONE )
6365
6466
6567class FakeSSLContext (SuperFakeSSLContext ):
6668 sock = None
6769 post_handshake_auth = None
70+ _check_hostname = False
71+
72+ @property
73+ def check_hostname (self ):
74+ return self ._check_hostname
75+
76+ @check_hostname .setter
77+ def check_hostname (self , * args ):
78+ self ._check_hostname = False
6879
6980 def __init__ (self , sock = None , server_hostname = None , _context = None , * args , ** kwargs ):
7081 if isinstance (sock , MocketSocket ):
@@ -141,16 +152,6 @@ def __init__(
141152 self ._truesocket_recording_dir = None
142153 self .kwargs = kwargs
143154
144- sock = kwargs .get ("sock" )
145- if sock is not None :
146- self .__dict__ = dict (sock .__dict__ )
147-
148- self .true_socket = wrap_ssl_socket (
149- true_ssl_socket ,
150- self .true_socket ,
151- true_ssl_context (protocol = SSL_PROTOCOL ),
152- )
153-
154155 def __unicode__ (self ): # pragma: no cover
155156 return str (self )
156157
@@ -323,16 +324,10 @@ def true_sendall(self, data, *args, **kwargs):
323324 host = true_gethostbyname (host )
324325
325326 if isinstance (self .true_socket , true_socket ) and self ._secure_socket :
326- try :
327- self = MocketSocket (sock = self )
328- except TypeError :
329- ssl_context = self .kwargs .get ("ssl_context" )
330- server_hostname = self .kwargs .get ("server_hostname" )
331- self .true_socket = true_ssl_context .wrap_socket (
332- self = ssl_context ,
333- sock = self .true_socket ,
334- server_hostname = server_hostname ,
335- )
327+ self .true_socket = true_urllib3_ssl_wrap_socket (
328+ self .true_socket ,
329+ ** self .kwargs ,
330+ )
336331
337332 try :
338333 self .true_socket .connect ((host , port ))
@@ -388,7 +383,7 @@ def close(self):
388383 self ._fd = None
389384
390385 def __getattr__ (self , name ):
391- """ Do nothing catchall function, for methods like close() and shutdown() """
386+ """Do nothing catchall function, for methods like close() and shutdown()"""
392387
393388 def do_nothing (* args , ** kwargs ):
394389 pass
@@ -479,6 +474,9 @@ def enable(namespace=None, truesocket_recording_dir=None):
479474 urllib3 .connection .ssl_wrap_socket = urllib3 .connection .__dict__ [
480475 "ssl_wrap_socket"
481476 ] = FakeSSLContext .wrap_socket
477+ urllib3 .connection .match_hostname = urllib3 .connection .__dict__ [
478+ "match_hostname"
479+ ] = lambda cert , hostname : None
482480 if pyopenssl_override : # pragma: no cover
483481 # Take out the pyopenssl version - use the default implementation
484482 extract_from_urllib3 ()
@@ -506,6 +504,9 @@ def disable():
506504 urllib3 .connection .ssl_wrap_socket = urllib3 .connection .__dict__ [
507505 "ssl_wrap_socket"
508506 ] = true_urllib3_ssl_wrap_socket
507+ urllib3 .connection .match_hostname = urllib3 .connection .__dict__ [
508+ "match_hostname"
509+ ] = true_urllib3_match_hostname
509510 Mocket .reset ()
510511 if pyopenssl_override : # pragma: no cover
511512 # Put the pyopenssl version back in place
@@ -521,7 +522,7 @@ def get_truesocket_recording_dir(cls):
521522
522523 @classmethod
523524 def assert_fail_if_entries_not_served (cls ):
524- """ Mocket checks that all entries have been served at least once. """
525+ """Mocket checks that all entries have been served at least once."""
525526 assert all (
526527 entry ._served for entry in itertools .chain (* cls ._entries .values ())
527528 ), "Some Mocket entries have not been served"
0 commit comments