<s> """<STR_LIT>""" <EOL> import socket <EOL> import threading <EOL> import time <EOL> from cherrypy import wsgiserver <EOL> try : <EOL> from OpenSSL import SSL <EOL> from OpenSSL import crypto <EOL> except ImportError : <EOL> SSL = None <EOL> class SSL_fileobject ( wsgiserver . CP_fileobject ) : <EOL> """<STR_LIT>""" <EOL> ssl_timeout = <NUM_LIT:3> <EOL> ssl_retry = <NUM_LIT> <EOL> def _safe_call ( self , is_reader , call , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> start = time . time ( ) <EOL> while True : <EOL> try : <EOL> return call ( * args , ** kwargs ) <EOL> except SSL . WantReadError : <EOL> time . sleep ( self . ssl_retry ) <EOL> except SSL . WantWriteError : <EOL> time . sleep ( self . ssl_retry ) <EOL> except SSL . SysCallError as e : <EOL> if is_reader and e . args == ( - <NUM_LIT:1> , '<STR_LIT>' ) : <EOL> return "<STR_LIT>" <EOL> errnum = e . args [ <NUM_LIT:0> ] <EOL> if is_reader and errnum in wsgiserver . socket_errors_to_ignore : <EOL> return "<STR_LIT>" <EOL> raise socket . error ( errnum ) <EOL> except SSL . Error as e : <EOL> if is_reader and e . args == ( - <NUM_LIT:1> , '<STR_LIT>' ) : <EOL> return "<STR_LIT>" <EOL> thirdarg = None <EOL> try : <EOL> thirdarg = e . args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> except IndexError : <EOL> pass <EOL> if thirdarg == '<STR_LIT>' : <EOL> raise wsgiserver . NoSSLError ( ) <EOL> raise wsgiserver . FatalSSLAlert ( * e . args ) <EOL> except : <EOL> raise <EOL> if time . time ( ) - start > self . ssl_timeout : <EOL> raise socket . timeout ( "<STR_LIT>" ) <EOL> def recv ( self , * args , ** kwargs ) : <EOL> buf = [ ] <EOL> r = super ( SSL_fileobject , self ) . recv <EOL> while True : <EOL> data = self . _safe_call ( True , r , * args , ** kwargs ) <EOL> buf . append ( data ) <EOL> p = self . _sock . pending ( ) <EOL> if not p : <EOL> return "<STR_LIT>" . join ( buf ) <EOL> def sendall ( self , * args , ** kwargs ) : <EOL> return self . _safe_call ( False , super ( SSL_fileobject , self ) . sendall , <EOL> * args , ** kwargs ) <EOL> def send ( self , * args , ** kwargs ) : <EOL> return self . _safe_call ( False , super ( SSL_fileobject , self ) . send , <EOL> * args , ** kwargs ) <EOL> class SSLConnection : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> self . _ssl_conn = SSL . Connection ( * args ) <EOL> self . _lock = threading . RLock ( ) <EOL> for f in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> exec ( """<STR_LIT>""" % ( f , f ) ) <EOL> def shutdown ( self , * args ) : <EOL> self . _lock . acquire ( ) <EOL> try : <EOL> return self . _ssl_conn . shutdown ( ) <EOL> finally : <EOL> self . _lock . release ( ) <EOL> class pyOpenSSLAdapter ( wsgiserver . SSLAdapter ) : <EOL> """<STR_LIT>""" <EOL> context = None <EOL> """<STR_LIT>""" <EOL> certificate = None <EOL> """<STR_LIT>""" <EOL> private_key = None <EOL> """<STR_LIT>""" <EOL> certificate_chain = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , certificate , private_key , certificate_chain = None ) : <EOL> if SSL is None : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> self . context = None <EOL> self . certificate = certificate <EOL> self . private_key = private_key <EOL> self . certificate_chain = certificate_chain <EOL> self . _environ = None <EOL> def bind ( self , sock ) : <EOL> """<STR_LIT>""" <EOL> if self . context is None : <EOL> self . context = self . get_context ( ) <EOL> conn = SSLConnection ( self . context , sock ) <EOL> self . _environ = self . get_environ ( ) <EOL> return conn <EOL> def wrap ( self , sock ) : <EOL> """<STR_LIT>""" <EOL> return sock , self . _environ . copy ( ) <EOL> def get_context ( self ) : <EOL> """<STR_LIT>""" <EOL> c = SSL . Context ( SSL . SSLv23_METHOD ) <EOL> c . use_privatekey_file ( self . private_key ) <EOL> if self . certificate_chain : <EOL> c . load_verify_locations ( self . certificate_chain ) <EOL> c . use_certificate_file ( self . certificate ) <EOL> return c <EOL> def get_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> ssl_environ = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> if self . certificate : <EOL> cert = open ( self . certificate , '<STR_LIT:rb>' ) . read ( ) <EOL> cert = crypto . load_certificate ( crypto . FILETYPE_PEM , cert ) <EOL> ssl_environ . update ( { <EOL> '<STR_LIT>' : cert . get_version ( ) , <EOL> '<STR_LIT>' : cert . get_serial_number ( ) , <EOL> } ) <EOL> for prefix , dn in [ ( "<STR_LIT:I>" , cert . get_issuer ( ) ) , <EOL> ( "<STR_LIT:S>" , cert . get_subject ( ) ) ] : <EOL> dnstr = str ( dn ) [ <NUM_LIT> : - <NUM_LIT:2> ] <EOL> wsgikey = '<STR_LIT>' % prefix <EOL> ssl_environ [ wsgikey ] = dnstr <EOL> while dnstr : <EOL> pos = dnstr . rfind ( "<STR_LIT:=>" ) <EOL> dnstr , value = dnstr [ : pos ] , dnstr [ pos + <NUM_LIT:1> : ] <EOL> pos = dnstr . rfind ( "<STR_LIT:/>" ) <EOL> dnstr , key = dnstr [ : pos ] , dnstr [ pos + <NUM_LIT:1> : ] <EOL> if key and value : <EOL> wsgikey = '<STR_LIT>' % ( prefix , key ) <EOL> ssl_environ [ wsgikey ] = value <EOL> return ssl_environ <EOL> def makefile ( self , sock , mode = '<STR_LIT:r>' , bufsize = - <NUM_LIT:1> ) : <EOL> if SSL and isinstance ( sock , SSL . ConnectionType ) : <EOL> timeout = sock . gettimeout ( ) <EOL> f = SSL_fileobject ( sock , mode , bufsize ) <EOL> f . ssl_timeout = timeout <EOL> return f <EOL> else : <EOL> return wsgiserver . CP_fileobject ( sock , mode , bufsize ) </s>
