<s> """<STR_LIT>""" <EOL> __metaclass__ = type <EOL> from zope . interface import implements <EOL> from twisted . python import log <EOL> from twisted . python . reflect import fullyQualifiedName <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet . interfaces import IConsumer , IPushProducer <EOL> from twisted . internet . error import ConnectionDone <EOL> from twisted . internet . defer import Deferred , succeed , fail , maybeDeferred <EOL> from twisted . internet . defer import CancelledError <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . protocols . basic import LineReceiver <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . web . http import NO_CONTENT , NOT_MODIFIED <EOL> from twisted . web . http import _DataLoss , PotentialDataLoss <EOL> from twisted . web . http import _IdentityTransferDecoder , _ChunkedTransferDecoder <EOL> from . iweb import IResponse , UNKNOWN_LENGTH <EOL> STATUS = '<STR_LIT>' <EOL> HEADER = '<STR_LIT>' <EOL> BODY = '<STR_LIT>' <EOL> DONE = '<STR_LIT>' <EOL> class BadHeaders ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ExcessWrite ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ParseError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reason , data ) : <EOL> Exception . __init__ ( self , reason , data ) <EOL> self . data = data <EOL> class BadResponseVersion ( ParseError ) : <EOL> """<STR_LIT>""" <EOL> class _WrapperException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reasons ) : <EOL> Exception . __init__ ( self , reasons ) <EOL> self . reasons = reasons <EOL> class RequestGenerationFailed ( _WrapperException ) : <EOL> """<STR_LIT>""" <EOL> class RequestTransmissionFailed ( _WrapperException ) : <EOL> """<STR_LIT>""" <EOL> class ConnectionAborted ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class WrongBodyLength ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ResponseDone ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ResponseFailed ( _WrapperException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reasons , response = None ) : <EOL> _WrapperException . __init__ ( self , reasons ) <EOL> self . response = response <EOL> class ResponseNeverReceived ( ResponseFailed ) : <EOL> """<STR_LIT>""" <EOL> class RequestNotSent ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def _callAppFunction ( function ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> function ( ) <EOL> except : <EOL> log . err ( None , "<STR_LIT>" % ( <EOL> fullyQualifiedName ( function ) , ) ) <EOL> class HTTPParser ( LineReceiver ) : <EOL> """<STR_LIT>""" <EOL> delimiter = '<STR_LIT:\n>' <EOL> CONNECTION_CONTROL_HEADERS = set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def connectionMade ( self ) : <EOL> self . headers = Headers ( ) <EOL> self . connHeaders = Headers ( ) <EOL> self . state = STATUS <EOL> self . _partialHeader = None <EOL> def switchToBodyMode ( self , decoder ) : <EOL> """<STR_LIT>""" <EOL> if self . state == BODY : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . bodyDecoder = decoder <EOL> self . state = BODY <EOL> self . setRawMode ( ) <EOL> def lineReceived ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line [ - <NUM_LIT:1> : ] == '<STR_LIT:\r>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> if self . state == STATUS : <EOL> self . statusReceived ( line ) <EOL> self . state = HEADER <EOL> elif self . state == HEADER : <EOL> if not line or line [ <NUM_LIT:0> ] not in '<STR_LIT>' : <EOL> if self . _partialHeader is not None : <EOL> header = '<STR_LIT>' . join ( self . _partialHeader ) <EOL> name , value = header . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> value = value . strip ( ) <EOL> self . headerReceived ( name , value ) <EOL> if not line : <EOL> self . allHeadersReceived ( ) <EOL> else : <EOL> self . _partialHeader = [ line ] <EOL> else : <EOL> self . _partialHeader . append ( line ) <EOL> def rawDataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . bodyDecoder . dataReceived ( data ) <EOL> def isConnectionControlHeader ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name in self . CONNECTION_CONTROL_HEADERS <EOL> def statusReceived ( self , status ) : <EOL> """<STR_LIT>""" <EOL> def headerReceived ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> name = name . lower ( ) <EOL> if self . isConnectionControlHeader ( name ) : <EOL> headers = self . connHeaders <EOL> else : <EOL> headers = self . headers <EOL> headers . addRawHeader ( name , value ) <EOL> def allHeadersReceived ( self ) : <EOL> """<STR_LIT>""" <EOL> self . switchToBodyMode ( None ) <EOL> class HTTPClientParser ( HTTPParser ) : <EOL> """<STR_LIT>""" <EOL> NO_BODY_CODES = set ( [ NO_CONTENT , NOT_MODIFIED ] ) <EOL> _transferDecoders = { <EOL> '<STR_LIT>' : _ChunkedTransferDecoder , <EOL> } <EOL> bodyDecoder = None <EOL> def __init__ ( self , request , finisher ) : <EOL> self . request = request <EOL> self . finisher = finisher <EOL> self . _responseDeferred = Deferred ( ) <EOL> self . _everReceivedData = False <EOL> def dataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . _everReceivedData = True <EOL> HTTPParser . dataReceived ( self , data ) <EOL> def parseVersion ( self , strversion ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> proto , strnumber = strversion . split ( '<STR_LIT:/>' ) <EOL> major , minor = strnumber . split ( '<STR_LIT:.>' ) <EOL> major , minor = int ( major ) , int ( minor ) <EOL> except ValueError as e : <EOL> raise BadResponseVersion ( str ( e ) , strversion ) <EOL> if major < <NUM_LIT:0> or minor < <NUM_LIT:0> : <EOL> raise BadResponseVersion ( "<STR_LIT>" , strversion ) <EOL> return ( proto , major , minor ) <EOL> def statusReceived ( self , status ) : <EOL> """<STR_LIT>""" <EOL> parts = status . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> if len ( parts ) != <NUM_LIT:3> : <EOL> raise ParseError ( "<STR_LIT>" , status ) <EOL> try : <EOL> statusCode = int ( parts [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise ParseError ( "<STR_LIT>" , status ) <EOL> self . response = Response ( <EOL> self . parseVersion ( parts [ <NUM_LIT:0> ] ) , <EOL> statusCode , <EOL> parts [ <NUM_LIT:2> ] , <EOL> self . headers , <EOL> self . transport ) <EOL> def _finished ( self , rest ) : <EOL> """<STR_LIT>""" <EOL> self . state = DONE <EOL> self . finisher ( rest ) <EOL> def isConnectionControlHeader ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if self . request . method == '<STR_LIT>' and name == '<STR_LIT>' : <EOL> return False <EOL> return HTTPParser . isConnectionControlHeader ( self , name ) <EOL> def allHeadersReceived ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . response . code in self . NO_BODY_CODES <EOL> or self . request . method == '<STR_LIT>' ) : <EOL> self . response . length = <NUM_LIT:0> <EOL> self . _finished ( self . clearLineBuffer ( ) ) <EOL> else : <EOL> transferEncodingHeaders = self . connHeaders . getRawHeaders ( <EOL> '<STR_LIT>' ) <EOL> if transferEncodingHeaders : <EOL> transferDecoder = self . _transferDecoders [ transferEncodingHeaders [ <NUM_LIT:0> ] . lower ( ) ] <EOL> else : <EOL> contentLengthHeaders = self . connHeaders . getRawHeaders ( '<STR_LIT>' ) <EOL> if contentLengthHeaders is None : <EOL> contentLength = None <EOL> elif len ( contentLengthHeaders ) == <NUM_LIT:1> : <EOL> contentLength = int ( contentLengthHeaders [ <NUM_LIT:0> ] ) <EOL> self . response . length = contentLength <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if contentLength == <NUM_LIT:0> : <EOL> self . _finished ( self . clearLineBuffer ( ) ) <EOL> transferDecoder = None <EOL> else : <EOL> transferDecoder = lambda x , y : _IdentityTransferDecoder ( <EOL> contentLength , x , y ) <EOL> if transferDecoder is None : <EOL> self . response . _bodyDataFinished ( ) <EOL> else : <EOL> self . transport . pauseProducing ( ) <EOL> self . switchToBodyMode ( transferDecoder ( <EOL> self . response . _bodyDataReceived , <EOL> self . _finished ) ) <EOL> self . _responseDeferred . callback ( self . response ) <EOL> del self . _responseDeferred <EOL> def connectionLost ( self , reason ) : <EOL> if self . bodyDecoder is not None : <EOL> try : <EOL> try : <EOL> self . bodyDecoder . noMoreData ( ) <EOL> except PotentialDataLoss : <EOL> self . response . _bodyDataFinished ( Failure ( ) ) <EOL> except _DataLoss : <EOL> self . response . _bodyDataFinished ( <EOL> Failure ( ResponseFailed ( [ reason , Failure ( ) ] , <EOL> self . response ) ) ) <EOL> else : <EOL> self . response . _bodyDataFinished ( ) <EOL> except : <EOL> log . err ( ) <EOL> elif self . state != DONE : <EOL> if self . _everReceivedData : <EOL> exceptionClass = ResponseFailed <EOL> else : <EOL> exceptionClass = ResponseNeverReceived <EOL> self . _responseDeferred . errback ( Failure ( exceptionClass ( [ reason ] ) ) ) <EOL> del self . _responseDeferred <EOL> class Request : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , uri , headers , bodyProducer , persistent = False ) : <EOL> self . method = method <EOL> self . uri = uri <EOL> self . headers = headers <EOL> self . bodyProducer = bodyProducer <EOL> self . persistent = persistent <EOL> def _writeHeaders ( self , transport , TEorCL ) : <EOL> hosts = self . headers . getRawHeaders ( '<STR_LIT:host>' , ( ) ) <EOL> if len ( hosts ) != <NUM_LIT:1> : <EOL> raise BadHeaders ( "<STR_LIT>" ) <EOL> requestLines = [ ] <EOL> requestLines . append ( <EOL> '<STR_LIT>' % ( self . method , self . uri ) ) <EOL> if not self . persistent : <EOL> requestLines . append ( '<STR_LIT>' ) <EOL> if TEorCL is not None : <EOL> requestLines . append ( TEorCL ) <EOL> for name , values in self . headers . getAllRawHeaders ( ) : <EOL> requestLines . extend ( [ '<STR_LIT>' % ( name , v ) for v in values ] ) <EOL> requestLines . append ( '<STR_LIT:\r\n>' ) <EOL> transport . writeSequence ( requestLines ) <EOL> def _writeToChunked ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> self . _writeHeaders ( transport , '<STR_LIT>' ) <EOL> encoder = ChunkedEncoder ( transport ) <EOL> encoder . registerProducer ( self . bodyProducer , True ) <EOL> d = self . bodyProducer . startProducing ( encoder ) <EOL> def cbProduced ( ignored ) : <EOL> encoder . unregisterProducer ( ) <EOL> def ebProduced ( err ) : <EOL> encoder . _allowNoMoreWrites ( ) <EOL> transport . unregisterProducer ( ) <EOL> return err <EOL> d . addCallbacks ( cbProduced , ebProduced ) <EOL> return d <EOL> def _writeToContentLength ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> self . _writeHeaders ( <EOL> transport , <EOL> '<STR_LIT>' % ( self . bodyProducer . length , ) ) <EOL> finishedConsuming = Deferred ( ) <EOL> encoder = LengthEnforcingConsumer ( <EOL> self . bodyProducer , transport , finishedConsuming ) <EOL> transport . registerProducer ( self . bodyProducer , True ) <EOL> finishedProducing = self . bodyProducer . startProducing ( encoder ) <EOL> def combine ( consuming , producing ) : <EOL> def cancelConsuming ( ign ) : <EOL> finishedProducing . cancel ( ) <EOL> ultimate = Deferred ( cancelConsuming ) <EOL> state = [ None ] <EOL> def ebConsuming ( err ) : <EOL> if state == [ None ] : <EOL> state [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> ultimate . errback ( err ) <EOL> else : <EOL> log . err ( <EOL> err , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self , state [ <NUM_LIT:0> ] ) ) <EOL> def cbProducing ( result ) : <EOL> if state == [ None ] : <EOL> state [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> try : <EOL> encoder . _noMoreWritesExpected ( ) <EOL> except : <EOL> ultimate . errback ( ) <EOL> else : <EOL> ultimate . callback ( None ) <EOL> def ebProducing ( err ) : <EOL> if state == [ None ] : <EOL> state [ <NUM_LIT:0> ] = <NUM_LIT:3> <EOL> encoder . _allowNoMoreWrites ( ) <EOL> ultimate . errback ( err ) <EOL> else : <EOL> log . err ( err , "<STR_LIT>" ) <EOL> consuming . addErrback ( ebConsuming ) <EOL> producing . addCallbacks ( cbProducing , ebProducing ) <EOL> return ultimate <EOL> d = combine ( finishedConsuming , finishedProducing ) <EOL> def f ( passthrough ) : <EOL> transport . unregisterProducer ( ) <EOL> return passthrough <EOL> d . addBoth ( f ) <EOL> return d <EOL> def writeTo ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> if self . bodyProducer is not None : <EOL> if self . bodyProducer . length is UNKNOWN_LENGTH : <EOL> return self . _writeToChunked ( transport ) <EOL> else : <EOL> return self . _writeToContentLength ( transport ) <EOL> else : <EOL> self . _writeHeaders ( transport , None ) <EOL> return succeed ( None ) <EOL> def stopWriting ( self ) : <EOL> """<STR_LIT>""" <EOL> _callAppFunction ( self . bodyProducer . stopProducing ) <EOL> class LengthEnforcingConsumer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , producer , consumer , finished ) : <EOL> self . _length = producer . length <EOL> self . _producer = producer <EOL> self . _consumer = consumer <EOL> self . _finished = finished <EOL> def _allowNoMoreWrites ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _finished = None <EOL> def write ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> if self . _finished is None : <EOL> self . _producer . stopProducing ( ) <EOL> raise ExcessWrite ( ) <EOL> if len ( bytes ) <= self . _length : <EOL> self . _length -= len ( bytes ) <EOL> self . _consumer . write ( bytes ) <EOL> else : <EOL> _callAppFunction ( self . _producer . stopProducing ) <EOL> self . _finished . errback ( WrongBodyLength ( "<STR_LIT>" ) ) <EOL> self . _allowNoMoreWrites ( ) <EOL> def _noMoreWritesExpected ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _finished is not None : <EOL> self . _allowNoMoreWrites ( ) <EOL> if self . _length : <EOL> raise WrongBodyLength ( "<STR_LIT>" ) <EOL> def makeStatefulDispatcher ( name , template ) : <EOL> """<STR_LIT>""" <EOL> def dispatcher ( self , * args , ** kwargs ) : <EOL> func = getattr ( self , '<STR_LIT:_>' + name + '<STR_LIT:_>' + self . _state , None ) <EOL> if func is None : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % ( self , name , self . _state ) ) <EOL> return func ( * args , ** kwargs ) <EOL> dispatcher . __doc__ = template . __doc__ <EOL> return dispatcher <EOL> class Response : <EOL> """<STR_LIT>""" <EOL> implements ( IResponse ) <EOL> length = UNKNOWN_LENGTH <EOL> _bodyProtocol = None <EOL> _bodyFinished = False <EOL> def __init__ ( self , version , code , phrase , headers , _transport ) : <EOL> self . version = version <EOL> self . code = code <EOL> self . phrase = phrase <EOL> self . headers = headers <EOL> self . _transport = _transport <EOL> self . _bodyBuffer = [ ] <EOL> self . _state = '<STR_LIT>' <EOL> def deliverBody ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> deliverBody = makeStatefulDispatcher ( '<STR_LIT>' , deliverBody ) <EOL> def _deliverBody_INITIAL ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> self . _transport . resumeProducing ( ) <EOL> protocol . makeConnection ( self . _transport ) <EOL> self . _bodyProtocol = protocol <EOL> for data in self . _bodyBuffer : <EOL> self . _bodyProtocol . dataReceived ( data ) <EOL> self . _bodyBuffer = None <EOL> self . _state = '<STR_LIT>' <EOL> def _deliverBody_CONNECTED ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . _bodyProtocol , ) ) <EOL> def _deliverBody_DEFERRED_CLOSE ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> protocol . makeConnection ( self . _transport ) <EOL> for data in self . _bodyBuffer : <EOL> protocol . dataReceived ( data ) <EOL> self . _bodyBuffer = None <EOL> protocol . connectionLost ( self . _reason ) <EOL> self . _state = '<STR_LIT>' <EOL> def _deliverBody_FINISHED ( self , protocol ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" ) <EOL> def _bodyDataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> _bodyDataReceived = makeStatefulDispatcher ( '<STR_LIT>' , <EOL> _bodyDataReceived ) <EOL> def _bodyDataReceived_INITIAL ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . _bodyBuffer . append ( data ) <EOL> def _bodyDataReceived_CONNECTED ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . _bodyProtocol . dataReceived ( data ) <EOL> def _bodyDataReceived_DEFERRED_CLOSE ( self , data ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _bodyDataReceived_FINISHED ( self , data ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _bodyDataFinished ( self , reason = None ) : <EOL> """<STR_LIT>""" <EOL> _bodyDataFinished = makeStatefulDispatcher ( '<STR_LIT>' , <EOL> _bodyDataFinished ) <EOL> def _bodyDataFinished_INITIAL ( self , reason = None ) : <EOL> """<STR_LIT>""" <EOL> self . _state = '<STR_LIT>' <EOL> if reason is None : <EOL> reason = Failure ( ResponseDone ( "<STR_LIT>" ) ) <EOL> self . _reason = reason <EOL> def _bodyDataFinished_CONNECTED ( self , reason = None ) : <EOL> """<STR_LIT>""" <EOL> if reason is None : <EOL> reason = Failure ( ResponseDone ( "<STR_LIT>" ) ) <EOL> self . _bodyProtocol . connectionLost ( reason ) <EOL> self . _bodyProtocol = None <EOL> self . _state = '<STR_LIT>' <EOL> def _bodyDataFinished_DEFERRED_CLOSE ( self ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _bodyDataFinished_FINISHED ( self ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> class ChunkedEncoder : <EOL> """<STR_LIT>""" <EOL> implements ( IConsumer ) <EOL> def __init__ ( self , transport ) : <EOL> self . transport = transport <EOL> def _allowNoMoreWrites ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transport = None <EOL> def registerProducer ( self , producer , streaming ) : <EOL> """<STR_LIT>""" <EOL> self . transport . registerProducer ( producer , streaming ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . transport is None : <EOL> raise ExcessWrite ( ) <EOL> self . transport . writeSequence ( ( "<STR_LIT>" % len ( data ) , data , "<STR_LIT:\r\n>" ) ) <EOL> def unregisterProducer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . write ( '<STR_LIT>' ) <EOL> self . transport . unregisterProducer ( ) <EOL> self . _allowNoMoreWrites ( ) <EOL> class TransportProxyProducer : <EOL> """<STR_LIT>""" <EOL> implements ( IPushProducer ) <EOL> disconnecting = False <EOL> def __init__ ( self , producer ) : <EOL> self . _producer = producer <EOL> def _stopProxying ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _producer = None <EOL> def stopProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _producer is not None : <EOL> self . _producer . stopProducing ( ) <EOL> def resumeProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _producer is not None : <EOL> self . _producer . resumeProducing ( ) <EOL> def pauseProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _producer is not None : <EOL> self . _producer . pauseProducing ( ) <EOL> class HTTP11ClientProtocol ( Protocol ) : <EOL> """<STR_LIT>""" <EOL> _state = '<STR_LIT>' <EOL> _parser = None <EOL> _finishedRequest = None <EOL> _currentRequest = None <EOL> _transportProxy = None <EOL> _responseDeferred = None <EOL> def __init__ ( self , quiescentCallback = lambda c : None ) : <EOL> self . _quiescentCallback = quiescentCallback <EOL> self . _abortDeferreds = [ ] <EOL> @ property <EOL> def state ( self ) : <EOL> return self . _state <EOL> def request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if self . _state != '<STR_LIT>' : <EOL> return fail ( RequestNotSent ( ) ) <EOL> self . _state = '<STR_LIT>' <EOL> _requestDeferred = maybeDeferred ( request . writeTo , self . transport ) <EOL> def cancelRequest ( ign ) : <EOL> if self . _state in ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> _requestDeferred . cancel ( ) <EOL> else : <EOL> self . transport . abortConnection ( ) <EOL> self . _disconnectParser ( Failure ( CancelledError ( ) ) ) <EOL> self . _finishedRequest = Deferred ( cancelRequest ) <EOL> self . _currentRequest = request <EOL> self . _transportProxy = TransportProxyProducer ( self . transport ) <EOL> self . _parser = HTTPClientParser ( request , self . _finishResponse ) <EOL> self . _parser . makeConnection ( self . _transportProxy ) <EOL> self . _responseDeferred = self . _parser . _responseDeferred <EOL> def cbRequestWrotten ( ignored ) : <EOL> if self . _state == '<STR_LIT>' : <EOL> self . _state = '<STR_LIT>' <EOL> self . _responseDeferred . chainDeferred ( self . _finishedRequest ) <EOL> def ebRequestWriting ( err ) : <EOL> if self . _state == '<STR_LIT>' : <EOL> self . _state = '<STR_LIT>' <EOL> self . transport . abortConnection ( ) <EOL> self . _finishedRequest . errback ( <EOL> Failure ( RequestGenerationFailed ( [ err ] ) ) ) <EOL> else : <EOL> log . err ( err , '<STR_LIT>' <EOL> '<STR_LIT>' % self . _state ) <EOL> _requestDeferred . addCallbacks ( cbRequestWrotten , ebRequestWriting ) <EOL> return self . _finishedRequest <EOL> def _finishResponse ( self , rest ) : <EOL> """<STR_LIT>""" <EOL> _finishResponse = makeStatefulDispatcher ( '<STR_LIT>' , _finishResponse ) <EOL> def _finishResponse_WAITING ( self , rest ) : <EOL> if self . _state == '<STR_LIT>' : <EOL> self . _state = '<STR_LIT>' <EOL> else : <EOL> self . _state = '<STR_LIT>' <EOL> self . _responseDeferred . chainDeferred ( self . _finishedRequest ) <EOL> if self . _parser is None : <EOL> return <EOL> reason = ConnectionDone ( "<STR_LIT>" ) <EOL> connHeaders = self . _parser . connHeaders . getRawHeaders ( '<STR_LIT>' , ( ) ) <EOL> if ( ( '<STR_LIT>' in connHeaders ) or self . _state != "<STR_LIT>" or <EOL> not self . _currentRequest . persistent ) : <EOL> self . _giveUp ( Failure ( reason ) ) <EOL> else : <EOL> try : <EOL> self . _quiescentCallback ( self ) <EOL> except : <EOL> log . err ( ) <EOL> self . transport . loseConnection ( ) <EOL> self . _disconnectParser ( reason ) <EOL> _finishResponse_TRANSMITTING = _finishResponse_WAITING <EOL> def _disconnectParser ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> if self . _parser is not None : <EOL> parser = self . _parser <EOL> self . _parser = None <EOL> self . _currentRequest = None <EOL> self . _finishedRequest = None <EOL> self . _responseDeferred = None <EOL> self . _transportProxy . _stopProxying ( ) <EOL> self . _transportProxy = None <EOL> parser . connectionLost ( reason ) <EOL> def _giveUp ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . transport . loseConnection ( ) <EOL> self . _disconnectParser ( reason ) <EOL> def dataReceived ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _parser . dataReceived ( bytes ) <EOL> except : <EOL> self . _giveUp ( Failure ( ) ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> connectionLost = makeStatefulDispatcher ( '<STR_LIT>' , connectionLost ) <EOL> def _connectionLost_QUIESCENT ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _state = '<STR_LIT>' <EOL> def _connectionLost_GENERATION_FAILED ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _state = '<STR_LIT>' <EOL> def _connectionLost_TRANSMITTING ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _state = '<STR_LIT>' <EOL> self . _finishedRequest . errback ( <EOL> Failure ( RequestTransmissionFailed ( [ reason ] ) ) ) <EOL> del self . _finishedRequest <EOL> self . _currentRequest . stopWriting ( ) <EOL> def _connectionLost_TRANSMITTING_AFTER_RECEIVING_RESPONSE ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _state = '<STR_LIT>' <EOL> def _connectionLost_WAITING ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _disconnectParser ( reason ) <EOL> self . _state = '<STR_LIT>' <EOL> def _connectionLost_ABORTING ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _disconnectParser ( Failure ( ConnectionAborted ( ) ) ) <EOL> self . _state = '<STR_LIT>' <EOL> for d in self . _abortDeferreds : <EOL> d . callback ( None ) <EOL> self . _abortDeferreds = [ ] <EOL> def abort ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _state == "<STR_LIT>" : <EOL> return succeed ( None ) <EOL> self . transport . loseConnection ( ) <EOL> self . _state = '<STR_LIT>' <EOL> d = Deferred ( ) <EOL> self . _abortDeferreds . append ( d ) <EOL> return d </s>
