<s> """<STR_LIT>""" <EOL> import logging <EOL> import socket <EOL> import certutils <EOL> def _SetUpUsingDummyCert ( handler ) : <EOL> """<STR_LIT>""" <EOL> context = certutils . get_ssl_context ( ) <EOL> def handle_servername ( connection ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> host = connection . get_servername ( ) <EOL> if host : <EOL> cert_str = ( <EOL> handler . server . get_certificate ( host ) ) <EOL> new_context = certutils . get_ssl_context ( ) <EOL> cert = certutils . load_cert ( cert_str ) <EOL> new_context . use_certificate ( cert ) <EOL> new_context . use_privatekey_file ( handler . server . ca_cert_path ) <EOL> connection . set_context ( new_context ) <EOL> return new_context <EOL> except Exception , e : <EOL> logging . error ( '<STR_LIT>' , e ) <EOL> context . set_tlsext_servername_callback ( handle_servername ) <EOL> handler . connection = certutils . get_ssl_connection ( context , handler . connection ) <EOL> handler . connection . set_accept_state ( ) <EOL> try : <EOL> handler . connection . do_handshake ( ) <EOL> except certutils . Error , v : <EOL> host = handler . connection . get_servername ( ) <EOL> if not host : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> raise certutils . Error ( '<STR_LIT>' % ( host , str ( v ) ) ) <EOL> handler . rfile = socket . _fileobject ( handler . connection , '<STR_LIT:rb>' , handler . rbufsize , <EOL> close = False ) <EOL> handler . wfile = socket . _fileobject ( handler . connection , '<STR_LIT:wb>' , handler . wbufsize , <EOL> close = False ) <EOL> def wrap_handler ( handler_class ) : <EOL> """<STR_LIT>""" <EOL> if certutils . openssl_import_error : <EOL> raise certutils . openssl_import_error <EOL> class WrappedHandler ( handler_class ) : <EOL> def setup ( self ) : <EOL> handler_class . setup ( self ) <EOL> _SetUpUsingDummyCert ( self ) <EOL> def finish ( self ) : <EOL> handler_class . finish ( self ) <EOL> self . connection . shutdown ( ) <EOL> self . connection . close ( ) <EOL> return WrappedHandler </s>
