<s> """<STR_LIT>""" <EOL> from twisted . protocols . policies import ProtocolWrapper , WrappingFactory <EOL> from twisted . python . failure import Failure <EOL> from . AsyncStateMachine import AsyncStateMachine <EOL> from gdata . tlslite . TLSConnection import TLSConnection <EOL> from gdata . tlslite . errors import * <EOL> import socket <EOL> import errno <EOL> class _FakeSocket : <EOL> def __init__ ( self , wrapper ) : <EOL> self . wrapper = wrapper <EOL> self . data = "<STR_LIT>" <EOL> def send ( self , data ) : <EOL> ProtocolWrapper . write ( self . wrapper , data ) <EOL> return len ( data ) <EOL> def recv ( self , numBytes ) : <EOL> if self . data == "<STR_LIT>" : <EOL> raise socket . error ( errno . EWOULDBLOCK , "<STR_LIT>" ) <EOL> returnData = self . data [ : numBytes ] <EOL> self . data = self . data [ numBytes : ] <EOL> return returnData <EOL> class TLSTwistedProtocolWrapper ( ProtocolWrapper , AsyncStateMachine ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , factory , wrappedProtocol ) : <EOL> ProtocolWrapper . __init__ ( self , factory , wrappedProtocol ) <EOL> AsyncStateMachine . __init__ ( self ) <EOL> self . fakeSocket = _FakeSocket ( self ) <EOL> self . tlsConnection = TLSConnection ( self . fakeSocket ) <EOL> self . tlsStarted = False <EOL> self . connectionLostCalled = False <EOL> def connectionMade ( self ) : <EOL> try : <EOL> ProtocolWrapper . connectionMade ( self ) <EOL> except TLSError as e : <EOL> self . connectionLost ( Failure ( e ) ) <EOL> ProtocolWrapper . loseConnection ( self ) <EOL> def dataReceived ( self , data ) : <EOL> try : <EOL> if not self . tlsStarted : <EOL> ProtocolWrapper . dataReceived ( self , data ) <EOL> else : <EOL> self . fakeSocket . data += data <EOL> while self . fakeSocket . data : <EOL> AsyncStateMachine . inReadEvent ( self ) <EOL> except TLSError as e : <EOL> self . connectionLost ( Failure ( e ) ) <EOL> ProtocolWrapper . loseConnection ( self ) <EOL> def connectionLost ( self , reason ) : <EOL> if not self . connectionLostCalled : <EOL> ProtocolWrapper . connectionLost ( self , reason ) <EOL> self . connectionLostCalled = True <EOL> def outConnectEvent ( self ) : <EOL> ProtocolWrapper . connectionMade ( self ) <EOL> def outCloseEvent ( self ) : <EOL> ProtocolWrapper . loseConnection ( self ) <EOL> def outReadEvent ( self , data ) : <EOL> if data == "<STR_LIT>" : <EOL> ProtocolWrapper . loseConnection ( self ) <EOL> else : <EOL> ProtocolWrapper . dataReceived ( self , data ) <EOL> def setServerHandshakeOp ( self , ** args ) : <EOL> self . tlsStarted = True <EOL> AsyncStateMachine . setServerHandshakeOp ( self , ** args ) <EOL> def loseConnection ( self ) : <EOL> if not self . tlsStarted : <EOL> ProtocolWrapper . loseConnection ( self ) <EOL> else : <EOL> AsyncStateMachine . setCloseOp ( self ) <EOL> def write ( self , data ) : <EOL> if not self . tlsStarted : <EOL> ProtocolWrapper . write ( self , data ) <EOL> else : <EOL> AsyncStateMachine . setWriteOp ( self , data ) <EOL> def writeSequence ( self , seq ) : <EOL> if not self . tlsStarted : <EOL> ProtocolWrapper . writeSequence ( self , seq ) <EOL> else : <EOL> AsyncStateMachine . setWriteOp ( self , "<STR_LIT>" . join ( seq ) ) </s>
