<s> """<STR_LIT>""" <EOL> from OpenSSL . SSL import Error , ZeroReturnError , WantReadError <EOL> from OpenSSL . SSL import TLSv1_METHOD , Context , Connection <EOL> try : <EOL> Connection ( Context ( TLSv1_METHOD ) , None ) <EOL> except TypeError , e : <EOL> if str ( e ) != "<STR_LIT>" : <EOL> raise <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> from zope . interface import implements , providedBy , directlyProvides <EOL> from twisted . python . failure import Failure <EOL> from twisted . python import log <EOL> from twisted . python . reflect import safe_str <EOL> from twisted . internet . interfaces import ISystemHandle , ISSLTransport <EOL> from twisted . internet . interfaces import IPushProducer , ILoggingContext <EOL> from twisted . internet . main import CONNECTION_LOST <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . internet . task import cooperate <EOL> from twisted . protocols . policies import ProtocolWrapper , WrappingFactory <EOL> class _PullToPush ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPushProducer ) <EOL> _finished = False <EOL> def __init__ ( self , pullProducer , consumer ) : <EOL> self . _producer = pullProducer <EOL> self . _consumer = consumer <EOL> def _pull ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> self . _producer . resumeProducing ( ) <EOL> except : <EOL> log . err ( None , "<STR_LIT>" % <EOL> ( safe_str ( self . _producer ) , ) ) <EOL> try : <EOL> self . _consumer . unregisterProducer ( ) <EOL> except : <EOL> log . err ( None , "<STR_LIT>" % <EOL> ( safe_str ( self . _consumer ) , ) ) <EOL> self . _finished = True <EOL> return <EOL> yield None <EOL> def startStreaming ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _coopTask = cooperate ( self . _pull ( ) ) <EOL> def stopStreaming ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _finished : <EOL> return <EOL> self . _finished = True <EOL> self . _coopTask . stop ( ) <EOL> def pauseProducing ( self ) : <EOL> self . _coopTask . pause ( ) <EOL> def resumeProducing ( self ) : <EOL> self . _coopTask . resume ( ) <EOL> def stopProducing ( self ) : <EOL> self . stopStreaming ( ) <EOL> self . _producer . stopProducing ( ) <EOL> class _ProducerMembrane ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPushProducer ) <EOL> _producerPaused = False <EOL> def __init__ ( self , producer ) : <EOL> self . _producer = producer <EOL> def pauseProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _producerPaused : <EOL> return <EOL> self . _producerPaused = True <EOL> self . _producer . pauseProducing ( ) <EOL> def resumeProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _producerPaused : <EOL> return <EOL> self . _producerPaused = False <EOL> self . _producer . resumeProducing ( ) <EOL> def stopProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _producer . stopProducing ( ) <EOL> class TLSMemoryBIOProtocol ( ProtocolWrapper ) : <EOL> """<STR_LIT>""" <EOL> implements ( ISystemHandle , ISSLTransport ) <EOL> _reason = None <EOL> _handshakeDone = False <EOL> _lostTLSConnection = False <EOL> _writeBlockedOnRead = False <EOL> _producer = None <EOL> def __init__ ( self , factory , wrappedProtocol , _connectWrapped = True ) : <EOL> ProtocolWrapper . __init__ ( self , factory , wrappedProtocol ) <EOL> self . _connectWrapped = _connectWrapped <EOL> def getHandle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _tlsConnection <EOL> def makeConnection ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> tlsContext = self . factory . _contextFactory . getContext ( ) <EOL> self . _tlsConnection = Connection ( tlsContext , None ) <EOL> if self . factory . _isClient : <EOL> self . _tlsConnection . set_connect_state ( ) <EOL> else : <EOL> self . _tlsConnection . set_accept_state ( ) <EOL> self . _appSendBuffer = [ ] <EOL> for interface in providedBy ( transport ) : <EOL> directlyProvides ( self , interface ) <EOL> Protocol . makeConnection ( self , transport ) <EOL> self . factory . registerProtocol ( self ) <EOL> if self . _connectWrapped : <EOL> ProtocolWrapper . makeConnection ( self , transport ) <EOL> try : <EOL> self . _tlsConnection . do_handshake ( ) <EOL> except WantReadError : <EOL> self . _flushSendBIO ( ) <EOL> def _flushSendBIO ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> bytes = self . _tlsConnection . bio_read ( <NUM_LIT:2> ** <NUM_LIT:15> ) <EOL> except WantReadError : <EOL> pass <EOL> else : <EOL> self . transport . write ( bytes ) <EOL> def _flushReceiveBIO ( self ) : <EOL> """<STR_LIT>""" <EOL> while not self . _lostTLSConnection : <EOL> try : <EOL> bytes = self . _tlsConnection . recv ( <NUM_LIT:2> ** <NUM_LIT:15> ) <EOL> except WantReadError : <EOL> break <EOL> except ZeroReturnError : <EOL> self . _shutdownTLS ( ) <EOL> self . _tlsShutdownFinished ( None ) <EOL> except Error , e : <EOL> if e . args [ <NUM_LIT:0> ] == - <NUM_LIT:1> and e . args [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> failure = Failure ( CONNECTION_LOST ) <EOL> else : <EOL> failure = Failure ( ) <EOL> self . _flushSendBIO ( ) <EOL> self . _tlsShutdownFinished ( failure ) <EOL> else : <EOL> self . _handshakeDone = True <EOL> ProtocolWrapper . dataReceived ( self , bytes ) <EOL> self . _flushSendBIO ( ) <EOL> def dataReceived ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> self . _tlsConnection . bio_write ( bytes ) <EOL> if self . _writeBlockedOnRead : <EOL> self . _writeBlockedOnRead = False <EOL> appSendBuffer = self . _appSendBuffer <EOL> self . _appSendBuffer = [ ] <EOL> for bytes in appSendBuffer : <EOL> self . _write ( bytes ) <EOL> if ( not self . _writeBlockedOnRead and self . disconnecting and <EOL> self . producer is None ) : <EOL> self . _shutdownTLS ( ) <EOL> if self . _producer is not None : <EOL> self . _producer . resumeProducing ( ) <EOL> self . _flushReceiveBIO ( ) <EOL> def _shutdownTLS ( self ) : <EOL> """<STR_LIT>""" <EOL> shutdownSuccess = self . _tlsConnection . shutdown ( ) <EOL> self . _flushSendBIO ( ) <EOL> if shutdownSuccess : <EOL> self . transport . loseConnection ( ) <EOL> def _tlsShutdownFinished ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _reason = reason <EOL> self . _lostTLSConnection = True <EOL> self . transport . loseConnection ( ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> if not self . _lostTLSConnection : <EOL> self . _tlsConnection . bio_shutdown ( ) <EOL> self . _flushReceiveBIO ( ) <EOL> self . _lostTLSConnection = True <EOL> reason = self . _reason or reason <EOL> self . _reason = None <EOL> ProtocolWrapper . connectionLost ( self , reason ) <EOL> def loseConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . disconnecting : <EOL> return <EOL> self . disconnecting = True <EOL> if not self . _writeBlockedOnRead and self . _producer is None : <EOL> self . _shutdownTLS ( ) <EOL> def write ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> if self . disconnecting and self . _producer is None : <EOL> return <EOL> self . _write ( bytes ) <EOL> def _write ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> if self . _lostTLSConnection : <EOL> return <EOL> leftToSend = bytes <EOL> while leftToSend : <EOL> try : <EOL> sent = self . _tlsConnection . send ( leftToSend ) <EOL> except WantReadError : <EOL> self . _writeBlockedOnRead = True <EOL> self . _appSendBuffer . append ( leftToSend ) <EOL> if self . _producer is not None : <EOL> self . _producer . pauseProducing ( ) <EOL> break <EOL> except Error : <EOL> self . _tlsShutdownFinished ( Failure ( ) ) <EOL> break <EOL> else : <EOL> self . _handshakeDone = True <EOL> self . _flushSendBIO ( ) <EOL> leftToSend = leftToSend [ sent : ] <EOL> def writeSequence ( self , iovec ) : <EOL> """<STR_LIT>""" <EOL> self . write ( "<STR_LIT>" . join ( iovec ) ) <EOL> def getPeerCertificate ( self ) : <EOL> return self . _tlsConnection . get_peer_certificate ( ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> if self . _lostTLSConnection : <EOL> producer . stopProducing ( ) <EOL> return <EOL> if not streaming : <EOL> producer = streamingProducer = _PullToPush ( producer , self ) <EOL> producer = _ProducerMembrane ( producer ) <EOL> self . transport . registerProducer ( producer , True ) <EOL> self . _producer = producer <EOL> if not streaming : <EOL> streamingProducer . startStreaming ( ) <EOL> def unregisterProducer ( self ) : <EOL> if isinstance ( self . _producer . _producer , _PullToPush ) : <EOL> self . _producer . _producer . stopStreaming ( ) <EOL> self . _producer = None <EOL> self . _producerPaused = False <EOL> self . transport . unregisterProducer ( ) <EOL> if self . disconnecting and not self . _writeBlockedOnRead : <EOL> self . _shutdownTLS ( ) <EOL> class TLSMemoryBIOFactory ( WrappingFactory ) : <EOL> """<STR_LIT>""" <EOL> protocol = TLSMemoryBIOProtocol <EOL> noisy = False <EOL> def __init__ ( self , contextFactory , isClient , wrappedFactory ) : <EOL> WrappingFactory . __init__ ( self , wrappedFactory ) <EOL> self . _contextFactory = contextFactory <EOL> self . _isClient = isClient <EOL> contextFactory . getContext ( ) <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> if ILoggingContext . providedBy ( self . wrappedFactory ) : <EOL> logPrefix = self . wrappedFactory . logPrefix ( ) <EOL> else : <EOL> logPrefix = self . wrappedFactory . __class__ . __name__ <EOL> return "<STR_LIT>" % ( logPrefix , ) </s>
