^cXddlZddlZddlmZddlmZddlmZddlm Z m Z mZm Z m Z ddl mZmZdZd ZdZd Zd Zeeed ZdZd Zd ZdZdZdZdZdZeeeeeeeedZdZd Z d Z!dZ"ee e!e"dZ#dZ$d Z%e$e%dZ&dZ'd Z(e'e(dZ)dZ*d Z+e*e+dZ,dZ-d Z.e-e.dZ/ee#ee&ee)eieieiee,ee/iZ0ej1dZ2dZ3ej4dGddZ5ej4ddGddZ6dZ7d Z8d!Z9d"Z:dedfd#Z;d$ZdLd'Z?d(Z@efd)ZAGd*d+ZBGd,d-ZCGd.d/ZDGd0d1ZEGd2d3ZFGd4d5ZGGd6d7eGZHGd8d9ZIGd:d;eIZJiZKgZLd<ZMGd=d>ZNGd?d@eNZOGdAdBeNZPdCZQdDdEgiZRGdFdGZSGdHdIZTGdJdKZUdS)MN)_)getattr)attr)encodingerrorpycompatutilwireprototypes)cborutil stringutili)s stream-begins stream-endsencoded )scommand-requests command-datascommand-responseserror-responses text-outputsprogressssender-protocol-settingssstream-settings)snew continuationsmores have-data)reoszzhumanflags..}s0001q!000r|)itemsappendgetjoin)mappingvaluenamemapflagsvals r humanflagsr,{s00 000G E C 3,, 3; F LLS*=*CDD E E E   3,, 99U  r T)slotsceZdZdZejZejZejZejZ ejZ ejZ dS) frameheaderz&Represents the data in a frame header.N) __name__ __module__ __qualname____doc__riblength requestidstreamid streamflagstypeidr*rr rr/r/sb00 TWYYF ItwyyH$'))K TWYYF DGIIEEEr r/F)r-reprceZdZdZejZejZejZejZ ejZ ejZ e j dZdS)framezRepresents a parsed frame.c Rd|jz}tD]\}}||jkr|}ndt|j|j|jtt|j |tt |ji|j fzS)Nr!sHframe(size=%d; request=%d; stream=%d; streamflags=%s; type=%s; flags=%s)) r9 FRAME_TYPESr#lenpayloadr6r7r, STREAM_FLAGSr8FRAME_TYPE_FLAGSr%r*)selftypenamenamer(s r__repr__zframe.__repr__s&4&,,..  KD% ##$  "DL!! <)9::+// R@@$*MM   r N)r0r1r2r3rr4r6r7r8r9r*r@r strmethodrFrr rr<r<s$$ ItwyyH$'))K TWYYF DGIIEdgiiG      r r<cttt|z}tjdt|}|dd|dd<tjd|d||||dz|z|d<||dd<|S) z#Assemble a frame into a byte array.z This can be used by user-facing applications and tests for creating frames easily without having to type out a bunch of constants. Request ID and stream IDs are integers. Stream flags, frame type, and flags can be specified by integer or named constant. Flags can be delimited by `|` to bitwise OR them together. If the payload begins with ``cbor:``, the following string will be evaluated as Python literal and the resulting object will be fed into a CBOR encoder. Otherwise, the payload is interpreted as a Python byte string literal.  rrr"scbor:r N)r6r7r8r9r*r@) splitintrAr>rB startswithr&r streamencoder evalpythonliteral unescapestrrP) sfieldsr6r7r8 frametype frameflagsr@finalstreamflagsflag finalflags validflagss rmakeframefromhumanstringras,WWT1  FGMDIxiWII8}}H!!$''** <    T 2 2   D )  K *  NN J!),J  &&$$ :   *T* *JJ #d)) #JJ(##2((  !*">wqrr{"K"K L L   (11 $    r c|dd|dzzd|dzz}tjd|d\}}}|d}|d zd z }|d z}t||||||S) zParse a unified framing protocol frame header from a buffer. The header is expected to be in the buffer at offset 0 and the buffer is expected to be large enough to hold a full header. rri@rrIrrr)rL unpack_fromr/)data framelengthr6r7r8 typeflagsr[r\s r parseheaderrj sq'C$q'M)EDGO;K'-'9&$'J'J$IxQIT!a'IT!J Y+y*  r ctt}||}|dkrdS|tkr&tjt d||fzt |}||j}t||jkr8tjt d|jt|fzt|j |j |j |j|j|S)zRead a unified framing protocol frame from a file object. Returns a 3-tuple of (type, flags, payload) for the decoded frame or None if no frame is available. May raise if a malformed frame is seen. rNs+received incomplete frame: got %d bytes: %ss'frame length error: expected %d; got %d)rJrKreadintorAbortrrjreadr5r?r<r6r7r8r9r*)fhheader readcounthr@s r readframers%s( ) )F F##IA~~t%%%k < = =&! "   FAggahG 7||qxk 8 9 9xW& '    QZ!'7  r c#Kd|i}|r||d<|r||d<dtj|}d} d} |s | tz} n | tz} |r | t z} ||||z} |t | z }t | |kr|t |kr | tz} ||t| | V| tzsn|r | t}d} t |tkrt} n$t} | d dksJd} ||t| |V| rd Sd S) zCreate frames necessary to transmit a request to run a command. This is a generator of bytearrays. Each item represents a frame ready to be sent over the wire to a peer. nameargsredirectr rTr6r9r*r@FrN)r&r rVFLAG_COMMAND_REQUEST_NEW!FLAG_COMMAND_REQUEST_CONTINUATION FLAG_COMMAND_REQUEST_EXPECT_DATAr? FLAG_COMMAND_REQUEST_MORE_FRAMESrPFRAME_TYPE_COMMAND_REQUESTrnDEFAULT_MAX_FRAME_SIZEFLAG_COMMAND_DATA_CONTINUATIONFLAG_COMMAND_DATA_EOSFRAME_TYPE_COMMAND_DATA) streamr6cmdargsdatafh maxframesizeredirectrgoffsetr*r@dones rcreatecommandframesrGs S>D W %$[ 88H)$// 0 0D F 7 - -EE 6 6E  6 5 5Ev 556#g,, w<<< ' 'FSYY,>,> 5 5E-      88  7: ;;566DD4yy2226-{{1~~,,,,""#. #    ' r cdtjddi}|jr||}d}|sdSnd}||t t||S)Nr statussokTFr6r9r*r@encoded)r&r rVstreamsettingssentencoderPFRAME_TYPE_COMMAND_RESPONSE"FLAG_COMMAND_RESPONSE_CONTINUATION)rr6overallrs rcreatecommandresponseokframershhx,i-?@@AAG  --(( 4    *0   r c#K|}d} ||||z}|t|z }|t|k}|rt}nt}||t |||dkV|rdSm)zACreate an empty payload frame representing command end-of-stream.rTr rN)flushr?FLAG_COMMAND_RESPONSE_EOSrrPr)rr6rr@rchunkrr*s rcreatecommandresponseeosframesrsllnnG F,!667#e**W%  7-EE6E.sN         E)r cfd|j|jdd}dD]1}t||}|||dtj|<2dt j|}|jr| |}d}nd}| |tt|| S) Nrw)surls mediatype)rlocation)size fullhashes fullhashseedserverdercertsservercadercertsrr TFr) url mediatyperr bytestrr&r rVrrrPrr)rr6locationrgar(r@rs r$createalternatelocationresponseframersL",    D ; ;!$$  5:D h.q11 2hhx,T2233G  --((   *0   r c#Kdd|id}|r ||dd<dtj|}||tt |VdS)Nerrormessage)rrrvr rx)r&r rVrPrr)rr6messagermrs rcreatecommanderrorresponsers    A $#( Ghhx,Q//00G   *'   r c#Kt|tksJdtj|d|igd}||t d|VdS)Nr msg)typerrrx)r?r~r&r rVrPFRAME_TYPE_ERROR_RESPONSE)rr6msgerrtyper@s rcreateerrorframers s88- - - - -hh $c]O    G   (   r c#Kg}|D]\}}}t|tstd|D]&}t|tstd'|D]&} t| tstd'|ddd}d|D}d|D}d|i} |r|| d <|r|| d <|| d tj|} t| |krtd | |td | VdS)aCreate a text output frame to render text to people. ``atoms`` is a 3-tuple of (formatting string, args, labels). The formatting string contains ``%s`` tokens to be replaced by the corresponding indexed entry in ``args``. ``labels`` is an iterable of formatters to be applied at rendering time. In terms of the ``ui`` class, each atom corresponds to a ``ui.write()``. s!must use bytes formatting stringssmust use bytes for argumentssmust use bytes for labelsasciireplacec`g|]+}|ddd,S)zutf-8rdecoder)rrs r z)createtextoutputframe..6s4KKK),,33G<<KKKr c`g|]+}|ddd,S)rstrictr)rrOs rrz)createtextoutputframe..9s4NNN!!((7H--44W==NNNr rrvslabelsr s$cannot encode data in a single framerrxN) isinstancebytes ValueErrorrrr$r&r rVr?rPFRAME_TYPE_TEXT_OUTPUT) rr6atomsr atomdicts formattingrlabelsarglabelatomr@s rcreatetextoutputframersI&+"T6*e,, CABB B B BCc5)) B !@AAA B ? ?EeU++ ? !=>>> ? &&w ::AA'JJ LKdKKKONvNNN #  ! DM  %$DOhhx,Y7788G 7||l""@AAA   %   r c(eZdZdZefdZdZdZdS)bufferingcommandresponseemitteraHelper object to emit command response frames intelligently. Raw command response data is likely emitted in chunks much smaller than what can fit in a single frame. This class exists to buffer chunks until enough data is available to fit in a single frame. TODO we'll need something like this when compression is supported. So it might make sense to implement this functionality at the stream level. cL||_||_||_g|_d|_dSNr)_stream _requestid_maxsize_chunks _chunkssize)rCrr6rs r__init__z(bufferingcommandresponseemitter.__init__\s, #$  r c#K||D]}|VdS|j|}|sdSt||jkr|D]}|Vd} ||||jz}|t|z }|j|jtt|dV|t|krdSjt||j z|jkr9|j ||xj t|z c_ dS|D]}|V|j |t||_ dS)zSend new data for emission. Is a generator of new frames that were derived from the new input. If the special input ``None`` is received, flushes all buffered data to frames. NrTr9r*r@r) _flushrrr?rrPrrrrrr$)rCrgr<rrs rsendz$bufferingcommandresponseemitter.sendcs <   F|""4((  F t99t} $ $   F Vft}&<<=#e**$l,,O6<! -SYY&&F " t99t' '$- 7 7 L   % % %   D )   F [[]]  EKKKK D!!!t99r c#Kd|j}t||jksJg|jdd<d|_|sdS|j|jtt|dVdS)Nr rTr) r&rr?rrrrPrrr)rCr@s rrz&bufferingcommandresponseemitter._flushs((4<((7||t},,,, QQQ  Fl$$ O.4 %       r N)r0r1r2r3r~rrrrr rrrPsZ  8NB%B%B%H     r rc*eZdZdZdZdZdZdZdS)identityencoderz3Encoder for the "identity" stream encoding profile.cdSNr)rCuis rrzidentityencoder.__init__s r c|SrrrCrgs rrzidentityencoder.encode r cdSNr rrCs rrzidentityencoder.flushsr cdSrrrs rfinishzidentityencoder.finishrr N)r0r1r2r3rrrrrr rrrsV==   r rceZdZdZdZdZdS)identitydecoderz3Decoder for the "identity" stream encoding profile.cL|r!tjtddS)Ns6identity decoder received unexpected additional values)rrmr)rCr extraobjss rrzidentitydecoder.__init__s4  +KLL   r c|Srrrs rrzidentitydecoder.decoderr N)r0r1r2r3rrrr rrrs8== r rc&eZdZdZdZdZdZdS) zlibencodercNddl}||_||_dSr)zlib_zlib compressobj _compressor)rCrrs rrzzlibencoder.__init__s,  ++--r c6|j|Srrcompressrs rrzzlibencoder.encode((...r cJ|j|jjSr)rrr Z_SYNC_FLUSHrs rrzzlibencoder.flushs %%dj&=>>>r c\|j|jj}d|_|Sr)rrrZ_FINISHrCress rrzzlibencoder.finishs*$$TZ%899 r Nr0r1r2rrrrrr rrrsP... ///??? r rceZdZdZdZdS) zlibdecodercddl}|r!tjtd||_dS)Nrs2zlib decoder received unexpected additional values)rrrmr decompressobj _decompressor)rCrrrs rrzzlibdecoder.__init__sO  +GHH "//11r c6|j|Srr decompressrs rrzzlibdecoder.decode!,,T222r Nr0r1r2rrrr rrrs222233333r rc&eZdZdZdZdZdZdS)zstdbaseencoderc~ddlm}||_||}||_dS)Nrzstd)level)r _zstdZstdCompressorrr)rCr r cctxs rrzzstdbaseencoder.__init__sK """//++--r c6|j|Srrrs rrzzstdbaseencoder.encoderr cJ|j|jjSr)rrrCOMPRESSOBJ_FLUSH_BLOCKrs rrzzstdbaseencoder.flush s %%dj&HIIIr c\|j|jj}d|_|Sr)rrrCOMPRESSOBJ_FLUSH_FINISHrs rrzzstdbaseencoder.finishs*$$TZ%HII r Nrrr rrrsS...///JJJ r rceZdZfdZxZS)zstd8mbencodercXtt|ddS)Nr)superrr)rCr __class__s rrzzstd8mbencoder.__init__s' nd##,,Q/////r r0r1r2r __classcell__rs@rrrs8000000000r rceZdZdZdZdS)zstdbasedecodercpddlm}||}||_dS)Nrr )max_window_size)r r ZstdDecompressorrr)rC maxwindowsizer dctxs rrzzstdbasedecoder.__init__sD$$]$CC!//11r c6|j|Srrrs rrzzstdbasedecoder.decode"rr Nrrr rrrs2222 33333r rceZdZfdZxZS)zstd8mbdecoderc|r!tjtdtt|ddS)Ns5zstd8mb decoder received unexpected additional valuesi)r#)rrmrrr'r)rCrrrs rrzzstd8mbdecoder.__init__'sT  +JKK  nd##,,;,GGGGGr rrs@rr'r'&sAHHHHHHHHHr r'c~trdS ddlm}|jn#t$rd}YnwxYw|r0t t ftd<tdttftd<tdttftd<tddS)Nrr szstd-8mbszlibidentity) STREAM_ENCODERSr r  __version__ ImportErrorrr'STREAM_ENCODERS_ORDERr$rrrrr s rpopulatestreamencodersr/6s   2(6'G $$$[111 +[9OG  )))$3_#EOK   -----s  ((c eZdZdZddZdZdS)rz5Represents a logical unidirectional series of frames.Fc"||_||_dSr)r7_active)rCr7actives rrzstream.__init__Ps   r chd}|js|tz}d|_t||j||||S)}Create a frame to be sent out over this stream. Only returns the frame instance. Does not actually send it. rT)r2STREAM_FLAG_BEGIN_STREAMrPr7)rCr6r9r*r@r8s rrPzstream.makeframeTsF  | 3 3KDL t}k65'   r NF)r0r1r2r3rrPrr rrrMs=??      r rc6eZdZdZdfd ZdZdZdZxZS) inputstreamz,Represents a stream used for receiving data.Fcjtt|||d|_dS)Nr3)rr9r_decoderrCr7r3rs rrzinputstream.__init__fs1 k4  ))(6)BBB r c|tvr$tjtd|zt|d|||_dS)zSet the decoder for this stream. Receives the stream profile name and any additional CBOR objects decoded from the stream encoding settings frame payloads. sunknown stream decoder: %srN)r+rrmrr<)rCrrErs r setdecoderzinputstream.setdecoderjsL  & &+a =>>EFF F'-a0Y?? r cH|js|S|j|Sr)r<rrs rrzinputstream.decodeus)} K}##D)))r cF|jsdS|jSr)r<rrs rrzinputstream.flush}%} 3}""$$$r r7) r0r1r2r3rr?rrrrs@rr9r9css66 @ @ @***%%%%%%%r r9cJeZdZdZd fd ZdZdZdZdZd dZ d Z xZ S) outputstreamz*Represents a stream used for sending data.Fctt|||d|_d|_d|_dS)Nr;F)rrDrr_encoder _encodernamer=s rrzoutputstream.__init__sC lD!!**8F*CCC"'  r c|tvr$tjtd|zt|d||_||_dS)zTSet the encoder for this stream. Receives the stream profile name. sunknown stream encoder: %srN)r+rrmrrFrG)rCrrEs r setencoderzoutputstream.setencodersT  & &+a =>>EFF F'-a044  r cH|js|S|j|Sr)rFrrs rrzoutputstream.encodes'} K}##D)))r cF|jsdS|jSr)rFrrs rrzoutputstream.flushrBr cJ|jsdS|jdSr)rFrrs rrzoutputstream.finishs+} 3 r cd}|js|tz}d|_|r%|jstjd|t z}|t kr|tzrd|_t||j ||||S)r5rTs@attempting to send encoded frame without sending stream settings) r2r6rrProgrammingErrorSTREAM_FLAG_ENCODING_APPLIEDFRAME_TYPE_STREAM_SETTINGS!FLAG_STREAM_ENCODING_SETTINGS_EOSrPr7)rCr6r9r*r@rr8s rrPzoutputstream.makeframes  | 3 3KDL  8* ,  7 7K 0 0 099 1'+D # t}k65'   r c|jr|jrdSdtj|j}||tt|S)zCreate a stream settings frame for this stream. Returns frame data or None if no stream settings frame is needed or has already been sent. Nr ) rFrr&r rVrGrPrPrQ)rCr6r@s rmakestreamsettingsframez$outputstream.makestreamsettingsframes^ }  7 4((801BCCDD~~  & -     r r7) r0r1r2r3rrIrrrrPrSrrs@rrDrDs44!!!!!! ! ! !*** %%%      :       r rDcR|jdzrtjd|jzdS)NrsAserver should only write to even numbered streams; %d is not even)r7rrN)rs rensureserverstreamrUs@  $ 028/ B     r contentencodingsr*ceZdZdZddZdZdZdZdZdZ dd Z d Z d Z d Z dZdZdZdZdZdZdZdZd S) serverreactora> Holds state of a server handling frame-based protocol requests. This class is the "brain" of the unified frame-based protocol server component. While the protocol is stateless from the perspective of requests/commands, something needs to track which frames have been received, what frames to expect, etc. This class is that thing. Instances are modeled as a state machine of sorts. Instances are also reactionary to external events. The point of this class is to encapsulate the state of the connection and the exchange of frames, not to perform work. Instead, callers tell this class when something occurs, like a frame arriving. If that activity is worthy of a follow-up action (say *run a command*), the return value of that handler will say so. I/O and CPU intensive operations are purposefully delegated outside of this class. Consumers are expected to tell instances when events occur. They do so by calling the various ``on*`` methods. These methods return a 2-tuple describing any follow-up action(s) to take. The first element is the name of an action to perform. The second is a data structure (usually a dict) specific to that action that contains more information. e.g. if the server wants to send frames back to the client, the data structure will contain a reference to those frames. Valid actions that consumers can be instructed to take are: sendframes Indicates that frames should be sent to the client. The ``framegen`` key contains a generator of frames that should be sent. The server assumes that all frames are sent to the client. error Indicates that an error occurred. Consumer should probably abort. runcommand Indicates that the consumer should run a wire protocol command. Details of the command to run are given in the data structure. wantframe Indicates that nothing of interest happened and the server is waiting on more frames from the client before anything interesting can be done. noop Indicates no additional action is required. Known Issues ------------ There are no limits to the number of partially received commands or their size. A malicious client could stream command request data and exhaust the server's memory. Partially received commands are not acted upon when end of input is reached. Should the server error if it receives a partial request? Should the client send a message to abort a partially transmitted request to facilitate graceful shutdown? Active requests that haven't been responded to aren't tracked. This means that if we receive a command and instruct its dispatch, another command with its request ID can come in over the wire and there will be a race between who responds to what. Fc||_||_d|_d|_g|_i|_i|_i|_t|_ d|_ tt|_ tdS)aConstruct a new server reactor. ``deferoutput`` can be used to indicate that no output frames should be instructed to be sent until input has been exhausted. In this mode, events that would normally generate output frames (such as a command response being ready) will instead defer instructing the consumer to send those frames. This is useful for half-duplex transports where the sender cannot receive until all data has been transmitted. initialrN)_ui _deferoutput_state_nextoutgoingstreamid_bufferedframegens_incomingstreams_outgoingstreams_receivingcommandsset_activecommands_protocolsettingsdecoderdictDEFAULT_PROTOCOL_SETTINGS_sendersettingsr/)rCr deferoutputs rrzserverreactor.__init__'s}'  %&""$ " ""$ #uu(,% $$=>>     r c|jdzs1d|_|td|jzS|j|jvrY|jt zs)d|_|tdSt|j|j|j<|jtzrd|_tj d|jtzr |j|j=|j |j |j|j|jd}||j}|stj d|jz||S)zProcess a frame that has been received off the wire. Returns a dict with an ``action`` key that details what action, if any, the consumer should take next. rerroreds/received frame with even numbered stream ID: %dsNreceived frame on unknown inactive stream without beginning of stream flag sets8support for decoding stream payloads not yet implemented)rZprotocol-settings-receivingidlecommand-receivingrksunhandled state: %s)r7r]_makeerrorresultrr`r8r6r9rOrrNSTREAM_FLAG_END_STREAM_onframeinitial_onframeprotocolsettings _onframeidle_onframecommandreceiving_onframeerroredr%)rCr<handlersmeths r onframerecvzserverreactor.onframerecvGsy ~! $DK((DEE.!  >!6 6 6$'?? ( ,,85@4O4OD !%. 1  ; ; $DK(K   5 5 6%en5,,0,I&"&"?,   ||DK(( O()?$+)MNN NtE{{r ctttrtfd}|S)zSignal that objects are ready to be sent to the client. ``objs`` is an iterable of objects (typically a generator) that will be encoded via CBOR and added to frames, which will be sent to the client. c 3dKd}d}t } t}n#t$r9|dD]}|V|rt D]}|VYn*t j$r.}t |j|j D]}|VYd}~nd}~wt$r:}t dtj |zdD]}|VYd}~nd}~wwxYw t|tjrJ|rt jd }|r|Vt' |Vd}d}:|rt jd|s3 }|r|Vt) }|r|Vd}t|tjr"||jD]}|Vnt|tjr9t1j|jD]}||D]}|Vn3t1j|D]}||D]}|Vn4#t$r'}t d|zdD]}|VYd}~n d}~wwxYw} jdS)NFTs%sserverrs:alternatelocationresponse seen after initial output objects(object follows alternatelocationresponse)rnext StopIterationrrrWireprotoCommandErrorrr messageargs Exceptionrr forcebytestrrr alternatelocationresponserNrSrrencodedresponsergindefinitebytestringresponser streamencodebytestringfromiterchunksrVrdremove) emittedalternatelocationsentemitteror<erobjsr6rCrs r sendframesz?serverreactor.oncommandresponsereadyobjects..sendframessG$) !5fiHHGi T AA$   !(d!3!3$$# (%C"I&&((E#(KKKKE2!; 19am""$$$ EEEE   !1! 7 : :: ) """$$$ EEEE H"!^%MNN!""'"8!1## !' > >y I I ("'KKKB"Iq15-"& ,#4G# ' & > >y I I ("'KKK!=VY O O ("'KKK"&"!^%CDD,%,\\!&%9%9((E"'KKKK($>F,&.%LH&&,,E*1e)<)<,,&+ , ,&.%:1%=%=,,E)0e)<)<,,&+ ,!!1 519i"""$$$ EEEE Gi V  ' ' 2 2 2 2 2sG*?C*,C*:#B## C*0/C%%C*.A"I D I J*J  J)rUrlistiter_handlesendframes)rCrr6rrs```` roncommandresponsereadyobjectsz+serverreactor.oncommandresponsereadyobjectsxs 6""" dD ! ! ::Do 3o 3o 3o 3o 3o 3o 3o 3b%%jjll333r cNjrjsdifSfd}dd|ifS)zSignals that end of input has been received. No more frames will be received. All pending activity should be completed. noopc32KjD] }|D]}|V dSr)r_)genr<rCs rmakegenz)serverreactor.oninputeof..makegensB.     EKKKK   r sendframesframegen)r\r_)rCrs` r oninputeofzserverreactor.oninputeofs\  (? B;          r cX|jr|j|difSdd|ifS)Nrrr)r\r_r$)rCframegens rrzserverreactor._handlesendframessE     # * *8 4 4 4B;  X# r crtfd}|S)Nc3tKtdD]}|VjdS)Nr{r|)rrdr)r<rr6rCrs rrz/serverreactor.onservererror..sendframes&sX) 3     ' ' 2 2 2 2 2r rUr)rCrr6rrs```` r onservererrorzserverreactor.onservererror#sX6""" 3 3 3 3 3 3 3 3%%jjll333r Ncvtfd}|S)zACalled when a command encountered an error before sending output.c3rKtD]}|VjdSr)rrdr)r<rrr6rCrs rrz0serverreactor.oncommanderror..sendframes4sS3 7D    ' ' 2 2 2 2 2r r)rCrr6rrrs````` roncommanderrorzserverreactor.oncommanderror0s^6""" 3 3 3 3 3 3 3 3 3%%jjll333r c|j}|xjdz c_t|}||j|<tD].}||jdvr||j|n/|S)zCreate a stream to be used for sending data to the client. If this is called before protocol settings frames are received, we don't know what stream encodings are supported by the client and we will default to identity. rrV)r^rDrar.rhrIr[)rCr7rYrEs rmakeoutputstreamzserverreactor.makeoutputstream>s- ""a'""  " "*+h'*  Dt+,?@@@ TXt,,,Ar cdd|ifS)Nrrr)rCrs rrozserverreactor._makeerrorresultTs    r c|j|}|dsd|_tjd|j|=|jrd|_nd|_|ddt j|dd}d|vr)d|_|td Sd |vri|d <||j vsJ|j |d ||d|d | d |d r|d nddfS)N requestdonerks,should not be called without requestdone setrnrmpayloadrrus$command request missing "name" fieldrvs runcommandrwdata)s requestidscommandrvrwr) rbr]rrNseekr decodeallgetvaluerorrdaddr%)rCr6entryrequests r_makeruncommandresultz#serverreactor._makeruncommandresultYsx' 2^$ $DK(?   #I .  " ".DKK!DK jq!!!$U:%6%?%?%A%ABB1E ' ! !$DK((9::  ' ! !!GG  44444   +++ '#G, )$[[556;GnNw00222$     r cdd|jifS)Ns wantframesstate)r]rs r_makewantframeresultz"serverreactor._makewantframeresults dk   r c|jtz}|jtz}|r+|r)d|_|t dS|s+|s+d|_|t dSdSdS)NrksGreceived command request frame with both new and continuation flags setsJreceived command request frame with neither new nor continuation flags set)r*ryrzr]ror)rCr<new continuations r_validatecommandrequestframez*serverreactor._validatecommandrequestframesk44{%FF  < $DK((.  < $DK((.     r cD|jtkr4d|_tj|_||S|jtkrd|_||Sd|_| td|jzS)NrlrmrksBexpected sender protocol settings or command request frame; got %d) r9#FRAME_TYPE_SENDER_PROTOCOL_SETTINGSr]r bufferingdecoderrerrr}rsrorrCr<s rrqzserverreactor._onframeinitials <> > >8DK,4,E,G,GD )0077 7 \7 7 7!DK$$U++ +%DK((%,  r c<|jdksJ|jJ|jtkr1d|_|t d|jzS|jtz}|jtz}|r+|r)d|_|t dS|s+|s)d|_|t dS |j |j nU#t$rH}d|_|t dtj |zcYd}~Sd}~wwxYw|r|S|sJ|j}d|_|s)d|_|t dSt!|dkr)d|_|t d S|d }d |vr|d |jd <d |_|S) Nrlrks/expected sender protocol settings frame; got %dsXsender protocol settings frame cannot have both continuation and end of stream flags setsOsender protocol settings frame must have continuation or end of stream flag sets;error decoding CBOR from sender protocol settings frame: %ss8sender protocol settings frame did not contain CBOR datars=sender protocol settings frame contained multiple CBOR valuesrrVrm)r]rer9rrorr**FLAG_SENDER_PROTOCOL_SETTINGS_CONTINUATION!FLAG_SENDER_PROTOCOL_SETTINGS_EOSrr@rr rr getavailabler?rh)rCr<moreeosrdecodedds rrrz&serverreactor._onframeprotocolsettingss{<<<<<,888 <> > >$DK((DEE,  {GGk==  C $DK((@  C $DK((.    ) 0 0 ? ? ? ?   $DK((R)!,,-         /,,.. . /<<>>(,% $DK((MNN \\A  $DK((  AJ ! # #89:M8ND !4 5 ((***sC11 E;=D>8E>Ec~|jtkr1d|_|t d|jzS||}|r|S|j|jvr1d|_|t d|jzS|j|jvr1d|_|t d|jzS|j tz}|j tz}|j tz}|s)d|_|t dStj}||j|d| t#|d|j|j<|s|s||jS|s|sJd|_|S)Nrks&expected command request frame; got %ds#request with ID %d already receiveds$request with ID %d is already actives3received command request frame without new flag set)rrr expectingdatarn)r9r}r]rorrr6rbrdr*ryr|r{r bytesiowriter@boolrr)rCr<rr moreframes expectingdatar@s rrszserverreactor._onframeidles <5 5 5$DK((;<> >*]**** ((***r c |jtkr=|jtzr||S||}|r|S|j|jvr1d|_| td|jzS|j|j vr1d|_| td|jzS|j |j}|jtkr|jtz}t|jtz}|dr)d|_| tdS||dkr)d|_| tdS|d|j|sd |d<|s|s||jS|S|jt&krm|ds1d|_| td |jzS|d t)j|d <|||Sd|_| td |jzS) Nrks3received frame for request that is still active: %ds4received frame for request that is not receiving: %drsGreceived command request frame when request frames were supposedly doners4mismatch between expect data flag and previous framerTsFreceived command data frame for request that is not expecting data: %drs"received unexpected frame type: %d)r9r}r*ryrsrr6rdr]rorrbr|rr{rr@rrrr r_handlecommanddataframe)rCr<rrrrs rrtz&serverreactor._onframecommandreceiving5s <5 5 5{55 0((///33E::C   ?d2 2 2$DK((HII/"  ?$"9 9 9$DK((IJJ/"  '8 <5 5 5'GGJ /O!OPPM^$ ( ,,0&6 777( ,,MNN *  # #EM 2 2 2 -(,n% Cm C11%/BBB,,.. . \4 4 4)* ( ,,.o &W~%!%g//u== =$DK((7885<G r c|jtksJ|d|j|jt zr|S|jtzr5|dd| |j Sd|_ | tdS)Nrrrks command data frame without flags)r9rrr@r*rrrrrr6r]ror)rCr<rs rrz%serverreactor._handlecommanddataframes|66666 gU]+++ ;7 7 Q,,.. . [0 0 Q 'N   " " "--eo>> >$DK((+N)O)OPP Pr cF|tdS)Nsserver already errored)rorrs rruzserverreactor._onframeerroreds$$Q'@%A%ABBBr r7r)r0r1r2r3rrxrrrrrrrorrrrqrrrsrtrrurr rrXrXsO>>@!!!!@///bK4K4K4Z   * 4 4 4 4 4 4 4,   ) ) ) V   ,*J+J+J+X3+3+3+jLLL\ Q Q QCCCCCr rXceZdZdZddZdS)commandrequestz&Represents a request to run a command.NcZ||_||_||_||_||_d|_dS)Nspending)r6rErrrstate)rCr6rErrrs rrzcommandrequest.__init__s0"      r NN)r0r1r2r3rrr rrrs.00      r rcLeZdZdZ d dZddZdZdZd Zd Z d Z d Z dS) clientreactoraHolds state of a client issuing frame-based protocol requests. This is like ``serverreactor`` but for client-side state. Each instance is bound to the lifetime of a connection. For persistent connection transports using e.g. TCP sockets and speaking the raw framing protocol, there will be a single instance for the lifetime of the TCP socket. For transports where there are multiple discrete interactions (say tunneled within in HTTP request), there will be a separate instance for each distinct interaction. Consumers are expected to tell instances when events occur by calling various methods. These methods return a 2-tuple describing any follow-up action(s) to take. The first element is the name of an action to perform. The second is a data structure (usually a dict) specific to that action that contains more information. e.g. if the reactor wants to send frames to the server, the data structure will contain a reference to those frames. Valid actions that consumers can be instructed to take are: noop Indicates no additional action is required. sendframes Indicates that frames should be sent to the server. The ``framegen`` key contains a generator of frames that should be sent. The reactor assumes that all frames in this generator are sent to the server. error Indicates that an error occurred. The ``message`` key contains an error message describing the failure. responsedata Indicates a response to a previously-issued command was received. The ``request`` key contains the ``commandrequest`` instance that represents the request this data is for. The ``data`` key contains the decoded data from the server. ``expectmore`` and ``eos`` evaluate to True when more response data is expected to follow or we're at the end of the response stream, respectively. FTNc||_||_||_||_d|_d|_d|_d|_td|_ tj |_ i|_ i|_i|_t!dS)aCreate a new instance. ``hasmultiplesend`` indicates whether multiple sends are supported by the transport. When True, it is possible to send commands immediately instead of buffering until the caller signals an intent to finish a send operation. ``buffercommands`` indicates whether sends should be buffered until the last request has been issued. ``clientcontentencoders`` is an iterable of content encoders the client will advertise to the server and that the server can use for encoding data. If not defined, the client will not advertise content encoders to the server. TFrN)r[_hasmultiplesend _buffersends_clientcontentencoders_canissuecommands_cansend_protocolsettingssent_nextrequestidrD_outgoingstream collectionsdeque_pendingrequests_activerequestsr`_streamsettingsdecodersr/)rCrhasmultiplesend buffersendsclientcontentencoderss rrzclientreactor.__init__s, /'&;#!% %*"+A + 1 3 3! "')$     r cn|jstjd|j}|xjdz c_t |||||}|jr|j||difS|jstjd|j sd|_d|_|dd| |ifS) aRequest that a command be executed. Receives the command name, a dict of arguments to pass to the command, and an optional file object containing the raw data for the command. Returns a 3-tuple of (request, action, action data). scannot issue new commandsrrrr*sends cannot be performed on this instanceFrr) rrrNrrrrr$rr_makecommandframes)rCrErrrr6rs r callcommandzclientreactor.callcommands% G()EFF F'  q  tT&8        ! ( ( 1 1 1GR' '= ,A( / % ).&!8!8!A!A r cjsdifSjstjdjsd_d_fd}dd|ifS)a_Request that all queued commands be sent. If any commands are buffered, this will instruct the caller to send them over the wire. If no commands are buffered it instructs the client to no-op. If instances aren't configured for multiple sends, no new command requests are allowed after this is called. rrFc3Kjr>j}|D]}|Vj.makeframes9so' /7799!44W==  EKKKK'     r rr)rrrrNrr)rCrs` r flushcommandszclientreactor.flushcommands!s$ B; } (=  $ "%*D "!DM         r c#K||j|j<d|_|jsk|jrdd|_dt jd|ji}|j |jtt|Vt|j|j|j |j|j|j}|D]}|Vd|_dS) zEmit frames to issue a command request. As a side-effect, update request accounting to reflect its changed state. ssendingTr rVrxrssentN)rr6rrrr&r rVrrPrrrrErrr)rCrr@rr<s rrz clientreactor._makecommandframesCs 3:W./" ) d.I )-D &hh%+T-HG&00!+:7 1   "    L L>%      EKKKK r c0|jdzrddtd|jzifS|j|jvrC|jtzsddtdifSt |j|j|j<|j|j}|jt zr||j|_|jtzr |j|j=|j tkr| |S|j |jvrddtd|j zifS|j|j }d|_t |jt$|ji}||j }|st+jd|j z|||S) zProcess a frame that has been received off the wire. Returns a 2-tuple of (action, meta) describing further action the caller needs to take as a result of receiving this frame. rrrs.received frame with odd numbered stream ID: %dsEreceived frame on unknown stream without beginning of stream flag sets*received frame for inactive request ID: %ds receivingsunhandled frame type: %d)r7rr`r8r6r9rOrr@rpr9rP_onstreamsettingsframer6rrr_oncommandresponseframer_onerrorresponseframer%rrN)rCr<rrrvrws rrxzclientreactor.onframerecvls >A  KLL.)  >!6 6 6$'?? "AD%%5@4O4OD !%. 1&u~6  ; ; 9"MM%-88EM  5 5 6%en5 <5 5 5..u55 5 ?$"6 6 6GHH/* &u7$  ()E %t'A  ||EL)) (+el: tGU###r cv|jtksJ|jtz}|jtz}|r|rddt difS|s|sddt difS|j|jvr"tj }||j|j<|j|j} | |j n?#t$r2}ddt dtj|zifcYd}~Sd}~wwxYw|rdifS|sJ|}|j|j=|sddt difS |j|j|j|d|d dn?#t$r2}ddt d tj|zifcYd}~Sd}~wwxYwdifS) NrrsXstream encoding settings frame cannot have both continuation and end of stream flags setsOstream encoding settings frame must have continuation or end of stream flag sets;error decoding CBOR from stream encoding settings frame: %srs8stream encoding settings frame did not contain CBOR datarrs error setting stream decoder: %s)r9rPr**FLAG_STREAM_ENCODING_SETTINGS_CONTINUATIONrQrr7rr rrr@rr rrr`r?r[)rCr<rrdecoderrrs rrz$clientreactor._onstreamsettingsframes|99999{GGk==  C H   C B!!  >!= = =/11G;BD ( 8.u~>  NN5= ) ) ) )   2%1!44 5          B;  &&((  ( 8 %!!    !%. 1 < <'!*gabbk       =>>$1!445       {s<"B== C9'C4.C94C9=:E88 F4'F/)F4/F4c|jtzrd|_|j|j=d||jt z|jtz|jdfS)Nsreceiveds responsedata)requests expectmorerr)r*rrrr6rr@)rCrr<s rrz%clientreactor._oncommandresponseframe s^ ;2 2 8'GM$W%67 #${-OO &??     r cd|_|j|j=tj|jd}d||d|ddfS)Nrkrrrr)rrr)rrr6r rr@)rCrr<rs rrz#clientreactor._onerrorresponseframesZ"  !2 3  u} - -a 0 #7jM    r )FTNr) r0r1r2r3rrrrrxrrrrr rrrs,,b" '!'!'!'!R%%%%N    D' ' ' RF$F$F$PVVVp         r rr)VrrLi18nrr r thirdpartyrr rrr r utilsr r rKr~r6rprOrAr}rrrrFRAME_TYPE_PROGRESSrrPr>ryrzr|r{FLAGS_COMMAND_REQUESTrrFLAGS_COMMAND_DATArrFLAGS_COMMAND_RESPONSErrFLAGS_SENDER_PROTOCOL_SETTINGSrrQFLAGS_STREAM_ENCODING_SETTINGSrBStructARGUMENT_RECORD_HEADERr,rYr/r<rPrarjrsrrrrrrrrrrrrrrrr'r+r.r/rr9rDrUrgrXrrrr rrs  #.), "" &*#!3,40*$!D2    $(!#' #'  %6 -2 "&4 ! &*" 8 % .2*$(!@ -"" .2*$(!@ -"" 5/!7rB')G > 'u--   dd         @*???D2N ' JJJJZ,%;<"""J.,,B5555pg g g g g g g g \                * 3 3 3 3 3 3 3 3.00000_000 33333333HHHHH_HHH....        ,%%%%%&%%%BR R R R R 6R R R j   + m Cm Cm Cm Cm Cm Cm Cm C`         G G G G G G G G G G r