<s> __ssl = __import__ ( '<STR_LIT>' ) <EOL> for attr in dir ( __ssl ) : <EOL> exec "<STR_LIT>" % ( attr , attr ) <EOL> import errno <EOL> time = __import__ ( '<STR_LIT:time>' ) <EOL> from eventlet . support import get_errno <EOL> from eventlet . hubs import trampoline <EOL> from eventlet . greenio import set_nonblocking , GreenSocket , SOCKET_CLOSED , CONNECT_ERR , CONNECT_SUCCESS <EOL> orig_socket = __import__ ( '<STR_LIT>' ) <EOL> socket = orig_socket . socket <EOL> timeout_exc = orig_socket . timeout <EOL> __patched__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class GreenSSLSocket ( __ssl . SSLSocket ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sock , * args , ** kw ) : <EOL> if not isinstance ( sock , GreenSocket ) : <EOL> sock = GreenSocket ( sock ) <EOL> self . act_non_blocking = sock . act_non_blocking <EOL> self . _timeout = sock . gettimeout ( ) <EOL> super ( GreenSSLSocket , self ) . __init__ ( sock . fd , * args , ** kw ) <EOL> for fn in orig_socket . _delegate_methods : <EOL> delattr ( self , fn ) <EOL> def settimeout ( self , timeout ) : <EOL> self . _timeout = timeout <EOL> def gettimeout ( self ) : <EOL> return self . _timeout <EOL> def setblocking ( self , flag ) : <EOL> if flag : <EOL> self . act_non_blocking = False <EOL> self . _timeout = None <EOL> else : <EOL> self . act_non_blocking = True <EOL> self . _timeout = <NUM_LIT:0.0> <EOL> def _call_trampolining ( self , func , * a , ** kw ) : <EOL> if self . act_non_blocking : <EOL> return func ( * a , ** kw ) <EOL> else : <EOL> while True : <EOL> try : <EOL> return func ( * a , ** kw ) <EOL> except SSLError , exc : <EOL> if get_errno ( exc ) == SSL_ERROR_WANT_READ : <EOL> trampoline ( self , <EOL> read = True , <EOL> timeout = self . gettimeout ( ) , <EOL> timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> elif get_errno ( exc ) == SSL_ERROR_WANT_WRITE : <EOL> trampoline ( self , <EOL> write = True , <EOL> timeout = self . gettimeout ( ) , <EOL> timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> else : <EOL> raise <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return self . _call_trampolining ( <EOL> super ( GreenSSLSocket , self ) . write , data ) <EOL> def read ( self , len = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return self . _call_trampolining ( <EOL> super ( GreenSSLSocket , self ) . read , len ) <EOL> def send ( self , data , flags = <NUM_LIT:0> ) : <EOL> if self . _sslobj : <EOL> if flags != <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> self . __class__ ) <EOL> while True : <EOL> try : <EOL> v = self . _sslobj . write ( data ) <EOL> except SSLError , e : <EOL> if get_errno ( e ) == SSL_ERROR_WANT_READ : <EOL> return <NUM_LIT:0> <EOL> elif get_errno ( e ) == SSL_ERROR_WANT_WRITE : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> raise <EOL> else : <EOL> return v <EOL> else : <EOL> while True : <EOL> try : <EOL> return socket . send ( self , data , flags ) <EOL> except orig_socket . error , e : <EOL> if self . act_non_blocking : <EOL> raise <EOL> if get_errno ( e ) == errno . EWOULDBLOCK or get_errno ( e ) == errno . ENOTCONN : <EOL> return <NUM_LIT:0> <EOL> raise <EOL> def sendto ( self , data , addr , flags = <NUM_LIT:0> ) : <EOL> if self . _sslobj : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . __class__ ) <EOL> else : <EOL> trampoline ( self , write = True , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> return socket . sendto ( self , data , addr , flags ) <EOL> def sendall ( self , data , flags = <NUM_LIT:0> ) : <EOL> if self . _sslobj : <EOL> if flags != <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> self . __class__ ) <EOL> amount = len ( data ) <EOL> count = <NUM_LIT:0> <EOL> while ( count < amount ) : <EOL> v = self . send ( data [ count : ] ) <EOL> count += v <EOL> return amount <EOL> else : <EOL> while True : <EOL> try : <EOL> return socket . sendall ( self , buflen , flags ) <EOL> except orig_socket . error , e : <EOL> if self . act_non_blocking : <EOL> raise <EOL> if get_errno ( e ) == errno . EWOULDBLOCK : <EOL> trampoline ( self , write = True , <EOL> timeout = self . gettimeout ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> if get_errno ( e ) in SOCKET_CLOSED : <EOL> return '<STR_LIT>' <EOL> raise <EOL> def recv ( self , buflen = <NUM_LIT> , flags = <NUM_LIT:0> ) : <EOL> if self . _sslobj : <EOL> if flags != <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> self . __class__ ) <EOL> read = self . read ( buflen ) <EOL> return read <EOL> else : <EOL> while True : <EOL> try : <EOL> return socket . recv ( self , buflen , flags ) <EOL> except orig_socket . error , e : <EOL> if self . act_non_blocking : <EOL> raise <EOL> if get_errno ( e ) == errno . EWOULDBLOCK : <EOL> trampoline ( self , read = True , <EOL> timeout = self . gettimeout ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> if get_errno ( e ) in SOCKET_CLOSED : <EOL> return '<STR_LIT>' <EOL> raise <EOL> def recv_into ( self , buffer , nbytes = None , flags = <NUM_LIT:0> ) : <EOL> if not self . act_non_blocking : <EOL> trampoline ( self , read = True , timeout = self . gettimeout ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> return super ( GreenSSLSocket , self ) . recv_into ( buffer , nbytes , flags ) <EOL> def recvfrom ( self , addr , buflen = <NUM_LIT> , flags = <NUM_LIT:0> ) : <EOL> if not self . act_non_blocking : <EOL> trampoline ( self , read = True , timeout = self . gettimeout ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> return super ( GreenSSLSocket , self ) . recvfrom ( addr , buflen , flags ) <EOL> def recvfrom_into ( self , buffer , nbytes = None , flags = <NUM_LIT:0> ) : <EOL> if not self . act_non_blocking : <EOL> trampoline ( self , read = True , timeout = self . gettimeout ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> return super ( GreenSSLSocket , self ) . recvfrom_into ( buffer , nbytes , flags ) <EOL> def unwrap ( self ) : <EOL> return GreenSocket ( super ( GreenSSLSocket , self ) . unwrap ( ) ) <EOL> def do_handshake ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _call_trampolining ( <EOL> super ( GreenSSLSocket , self ) . do_handshake ) <EOL> def _socket_connect ( self , addr ) : <EOL> real_connect = socket . connect <EOL> if self . act_non_blocking : <EOL> return real_connect ( self , addr ) <EOL> else : <EOL> if self . gettimeout ( ) is None : <EOL> while True : <EOL> try : <EOL> return real_connect ( self , addr ) <EOL> except orig_socket . error , exc : <EOL> if get_errno ( exc ) in CONNECT_ERR : <EOL> trampoline ( self , write = True ) <EOL> elif get_errno ( exc ) in CONNECT_SUCCESS : <EOL> return <EOL> else : <EOL> raise <EOL> else : <EOL> end = time . time ( ) + self . gettimeout ( ) <EOL> while True : <EOL> try : <EOL> real_connect ( self , addr ) <EOL> except orig_socket . error , exc : <EOL> if get_errno ( exc ) in CONNECT_ERR : <EOL> trampoline ( self , write = True , <EOL> timeout = end - time . time ( ) , timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> elif get_errno ( exc ) in CONNECT_SUCCESS : <EOL> return <EOL> else : <EOL> raise <EOL> if time . time ( ) >= end : <EOL> raise timeout_exc ( '<STR_LIT>' ) <EOL> def connect ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> if self . _sslobj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _socket_connect ( addr ) <EOL> self . _sslobj = _ssl . sslwrap ( self . _sock , False , self . keyfile , self . certfile , <EOL> self . cert_reqs , self . ssl_version , <EOL> self . ca_certs ) <EOL> if self . do_handshake_on_connect : <EOL> self . do_handshake ( ) <EOL> def accept ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . act_non_blocking : <EOL> newsock , addr = socket . accept ( self ) <EOL> else : <EOL> while True : <EOL> try : <EOL> newsock , addr = socket . accept ( self ) <EOL> set_nonblocking ( newsock ) <EOL> break <EOL> except orig_socket . error , e : <EOL> if get_errno ( e ) != errno . EWOULDBLOCK : <EOL> raise <EOL> trampoline ( self , read = True , timeout = self . gettimeout ( ) , <EOL> timeout_exc = timeout_exc ( '<STR_LIT>' ) ) <EOL> new_ssl = type ( self ) ( newsock , <EOL> keyfile = self . keyfile , <EOL> certfile = self . certfile , <EOL> server_side = True , <EOL> cert_reqs = self . cert_reqs , <EOL> ssl_version = self . ssl_version , <EOL> ca_certs = self . ca_certs , <EOL> do_handshake_on_connect = self . do_handshake_on_connect , <EOL> suppress_ragged_eofs = self . suppress_ragged_eofs ) <EOL> return ( new_ssl , addr ) <EOL> SSLSocket = GreenSSLSocket <EOL> def wrap_socket ( sock , keyfile = None , certfile = None , <EOL> server_side = False , cert_reqs = CERT_NONE , <EOL> ssl_version = PROTOCOL_SSLv23 , ca_certs = None , <EOL> do_handshake_on_connect = True , <EOL> suppress_ragged_eofs = True ) : <EOL> return GreenSSLSocket ( sock , keyfile = keyfile , certfile = certfile , <EOL> server_side = server_side , cert_reqs = cert_reqs , <EOL> ssl_version = ssl_version , ca_certs = ca_certs , <EOL> do_handshake_on_connect = do_handshake_on_connect , <EOL> suppress_ragged_eofs = suppress_ragged_eofs ) <EOL> if hasattr ( __ssl , '<STR_LIT>' ) : <EOL> def sslwrap_simple ( sock , keyfile = None , certfile = None ) : <EOL> """<STR_LIT>""" <EOL> ssl_sock = GreenSSLSocket ( sock , keyfile = keyfile , certfile = certfile , <EOL> server_side = False , <EOL> cert_reqs = CERT_NONE , <EOL> ssl_version = PROTOCOL_SSLv23 , <EOL> ca_certs = None ) <EOL> return ssl_sock </s>
