L ih ddlmZmZmZmZmZmZmZmZm Z m Z ddl m Z m Z mZmZmZmZmZddlmZmZmZddlmZmZddlmZddlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*dd l+m,Z,m-Z-gd Z.Gd d e*e* Z/Gdde*e* Z0dZ1de eefde2fdZ3de4de eefdee5e edee6fffdZ7GddZ8y)) AnyCallablecastDictListOptionaloverloadTupleTypeUnion)ConnectionClosedData EndOfMessageEventInformationalResponseRequestResponse)get_comma_headerhas_expect_100_continueset_comma_header)READERS ReadersType) ReceiveBuffer) _SWITCH_CONNECT_SWITCH_UPGRADECLIENTConnectionStateDONEERRORMIGHT_SWITCH_PROTOCOL SEND_BODYSERVERSWITCHED_PROTOCOL)LocalProtocolErrorRemoteProtocolErrorSentinel)WRITERS WritersType) Connection NEED_DATAPAUSEDc eZdZy)r+N__name__ __module__ __qualname__U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/h11/_connection.pyr+r+3r3r+) metaclassc eZdZy)r,Nr.r2r3r4r,r,7r5r3r,i@eventreturnc\t|jd}d|vryt|dddkryy)N connectioncloseF http_version1.1T)rheadersgetattr)r8 connections r4 _keep_aliverBTs4!%--?J:unf-6 r3request_methodr2ct|ttfvsJt|turB|jdvs"|dk(s|dk(rd|jcxkrdkryny|jdk\sJt |j d}|r |dgk(sJy t |j d }|rd t |d ffSt|turyy )N)i0HEADCONNECT,)content-length)rtransfer-encodingchunked)chunkedr2content-lengthrJr)http/1.0r2)typerr status_coderr?int)rCr8transfer_encodingscontent_lengthss r4 _body_framingrU]s ;7H- -- - E{h    +(*,8I8I1OC1O+2P+  C'''*%--9MN!j\111'u}}6GHO 3q'9#:"<== E{g'r3c eZdZdZefdeededdfdZe de eeeeffdZ e deefdZ e deefd Z e defd Zd#d Zd eeddfd ZdedeeefdZd eededdfdZd eedeedeeefdeedeffdZ d$de eeeefdeeddfdZe deeeffdZdeddfdZ deeee!ee"ffdZ#deeee!ee"ffdZ$e%de&ddfdZ'e%dee(e)e*e+e,fdefdZ'e%dedeefdZ'dedeefdZ'dedee-efdZ.d#d Z/d!e*de*fd"Z0y)%r*aBAn object encapsulating the state of an HTTP connection. Args: our_role: If you're implementing a client, pass :data:`h11.CLIENT`. If you're implementing a server, pass :data:`h11.SERVER`. max_incomplete_event_size (int): The maximum number of bytes we're willing to buffer of an incomplete event. In practice this mostly sets a limit on the maximum size of the request/response line + headers. If this is exceeded, then :meth:`next_event` will raise :exc:`RemoteProtocolError`. our_rolemax_incomplete_event_sizer9Nc||_|ttfvrtd|||_||tur t|_n t|_t |_|j|jdt|_ |j|j dt|_ t|_d|_d|_d|_d|_y)Nzexpected CLIENT or SERVER, not F)_max_incomplete_event_sizerr# ValueErrorrW their_roler_cstate_get_io_objectr(_writerr_readerr_receive_buffer_receive_buffer_closedtheir_http_version_request_method"client_is_waiting_for_100_continue)selfrWrXs r4__init__zConnection.__init__s +D' FF+ +>xlKL L   v $DO$DO&( **4==$H **4??D'J  -',#480438/r3c@t|jjS)zA dictionary like:: {CLIENT: , SERVER: } See :ref:`state-machine` for details. )dictr]statesrfs r4rjzConnection.statessDLL''((r3cH|jj|jS)zjThe current state of whichever role we are playing. See :ref:`state-machine` for details. )r]rjrWrks r4 our_statezConnection.our_states ||""4==11r3cH|jj|jS)znThe current state of whichever role we are NOT playing. See :ref:`state-machine` for details. )r]rjr\rks r4 their_statezConnection.their_states ||""4??33r3cB|jtuxr |jSN)r\rrerks r4!they_are_waiting_for_100_continuez,Connection.they_are_waiting_for_100_continues&(TT-T-TTr3ct|jj}|jjd|_|j rJ|j |y)aAttempt to reset our connection state for a new request/response cycle. If both client and server are in :data:`DONE` state, then resets them both to :data:`IDLE` state in preparation for a new request/response cycle on this same connection. Otherwise, raises a :exc:`LocalProtocolError`. See :ref:`keepalive-and-pipelining`. N)rir]rjstart_next_cyclerdre_respond_to_state_changes)rf old_statess r4rtzConnection.start_next_cyclesO$,,--.  %%'#:::: &&z2r3rolect|jj}|jj||j |yrq)rir]rj process_errorru)rfrwrvs r4_process_errorzConnection._process_errors6$,,--.  ""4( &&z2r3r8ct|tur|jdk(rtSt|tur9t |j jvrd|jcxkr dkrt Syy)NerHrI)rPrrQrrrr]pending_switch_proposalsrfr8s r4_server_switch_eventzConnection._server_switch_eventsc ;/ /E4E4E4L" " ;( "4<<#H#HH5,,2s2&&3r3cZt|jj}|turtt |t urc|j dk(r|jjtt|jdr|jjtd}|tur|j|}|jj|t ||t |t ur|j |_||j urOt |t t"t$fvr3t't(t t"t$f|}|j*|_t |t t"fvr@t/t't(t t"f|s|jj1t |t urt3|rd|_t |t$t"fvrd|_|turt |t6t8fvrd|_|j;||y)NrGsupgradeTF)rir]rjrrPrmethodprocess_client_switch_proposalrrr?rr#r process_eventrdr\rrrr r=rcrBprocess_keep_alive_disabledrrerrru)rfrwr8rvserver_switch_events r4_process_eventzConnection._process_events$,,--. 6>d5kW4||z) ;;OL z: ;;OL" 6>"&";";E"B  ""4e6IJ ;' !#(<d5kdL-AA6;D 3 &&z59r3io_dict.c |jj|}|turQtt t |j t tttf|\}}|t||S|j||fSrq) r]rjr"rUrbytesrdr rrget)rfrwr8rstate framing_typeargss r4r^zConnection._get_io_object@s ##D) I "/UD0014gx>O8PRW3X" L$479%l3T: :;;e}- -r3rvc|j||jk7r&|j|j|t|_|j ||j k7r'|j|j |t|_yyrq) rmrWr^r(r_ror\rr`)rfrvr8s r4ruz$Connection._respond_to_state_changesVsi >>Z 6 6..t}}eWMDL   z$//: :..twODL ;r3cDt|j|jfS)abData that has been received, but not yet processed, represented as a tuple with two elements, where the first is a byte-string containing the unprocessed data itself, and the second is a bool that is True if the receive connection was closed. See :ref:`switching-protocols` for discussion of why you'd want this. )rrarbrks r4 trailing_datazConnection.trailing_dataas d**+T-H-HIIr3datacp|r-|jr td|xj|z c_yd|_y)aAdd data to our internal receive buffer. This does not actually do any processing on the data, just stores it. To trigger processing, you have to call :meth:`next_event`. Args: data (:term:`bytes-like object`): The new data that was just received. Special case: If *data* is an empty byte-string like ``b""``, then this indicates that the remote side has closed the connection (end of file). Normally this is convenient, because standard Python APIs like :meth:`file.read` or :meth:`socket.recv` use ``b""`` to indicate end-of-file, while other failures to read are indicated using other mechanisms like raising :exc:`TimeoutError`. When using such an API you can just blindly pass through whatever you get from ``read`` to :meth:`receive_data`, and everything will work. But, if you have an API where reading an empty string is a valid non-EOF condition, then you need to be aware of this and make sure to check for such strings and avoid passing them to :meth:`receive_data`. Returns: Nothing, but after calling this you should call :meth:`next_event` to parse the newly received data. Raises: RuntimeError: Raised if you pass an empty *data*, indicating EOF, and then pass a non-empty *data*, indicating more data that somehow arrived after the EOF. (Calling ``receive_data(b"")`` multiple times is fine, and equivalent to calling it once.) z(received close, then received more data?TN)rb RuntimeErrorra)rfrs r4 receive_datazConnection.receive_datals6N **"#MNN  D ( *.D 'r3c|j}|tur|jrtS|tus|t urtS|j J|j |j}|S|jsG|jr;t|j dr|j j}n t}|t}|S)Nread_eof) rorrar,r!r$r`rbhasattrrrr+)rfrr8s r4_extract_next_receive_eventz&Connection._extract_next_receive_events   D=T11M ) )U6G-GM||''' T112 =''D,G,G 4<<4 LL113E,.E =E r3c|jtur td |j}|tt fvr*|j |jtt||turFt|j|jkDr tdd|jr td|S#t$rG}|j|jt!|t"r|j%nYd}~yd}~wwxYw)aFParse the next event out of our receive buffer, update our internal state, and return it. This is a mutating operation -- think of it like calling :func:`next` on an iterator. Returns: : One of three things: 1) An event object -- see :ref:`events`. 2) The special constant :data:`NEED_DATA`, which indicates that you need to read more data from your socket and pass it to :meth:`receive_data` before this method will be able to return any more events. 3) The special constant :data:`PAUSED`, which indicates that we are not in a state where we can process incoming data (usually because the peer has finished their part of the current request/response cycle, and you have not yet called :meth:`start_next_cycle`). See :ref:`flow-control` for details. Raises: RemoteProtocolError: The peer has misbehaved. You should close the connection (possibly after sending some kind of 4xx response). Once this method returns :class:`ConnectionClosed` once, then all subsequent calls will also return :class:`ConnectionClosed`. If this method raises any exception besides :exc:`RemoteProtocolError` then that's a bug -- if it happens please file a bug report! If this method raises any exception then it also sets :attr:`Connection.their_state` to :data:`ERROR` -- see :ref:`error-handling` for discussion. z+Can't receive data when peer state is ERRORzReceive buffer too longi)error_status_hintz#peer unexpectedly closed connectionN)ror r&rr+r,rr\rrlenrarZrb BaseExceptionrz isinstancer%!_reraise_as_remote_protocol_error)rfr8excs r4 next_eventzConnection.next_eventsP   u $%&ST T 446EY//##DOOT%5GH !t++,t/N/NN.1S....STTL     0#125578 sBB77 D=DDcyrqr2r~s r4sendzConnection.send r3cyrqr2r~s r4rzConnection.sends r3cyrqr2r~s r4rzConnection.sendrr3cL|j|}|ydj|S)aConvert a high-level event into bytes that can be sent to the peer, while updating our internal state machine. Args: event: The :ref:`event ` to send. Returns: If ``type(event) is ConnectionClosed``, then returns ``None``. Otherwise, returns a :term:`bytes-like object`. Raises: LocalProtocolError: Sending this event at this time would violate our understanding of the HTTP/1.1 protocol. If this method raises any exception then it also sets :attr:`Connection.our_state` to :data:`ERROR` -- see :ref:`error-handling` for discussion. Nr3)send_with_data_passthroughjoin)rfr8 data_lists r4rzConnection.sends,*33E:  88I& &r3cn|jtur td t|tur|j |}|j }|j|j|t|tury|Jg}|||j|S#|j|jxYw)ahIdentical to :meth:`send`, except that in situations where :meth:`send` returns a single :term:`bytes-like object`, this instead returns a list of them -- and when sending a :class:`Data` event, this list is guaranteed to contain the exact object you passed in as :attr:`Data.data`. See :ref:`sendfile` for discussion. z'Can't send data when our state is ERRORN) rmr r%rPr&_clean_up_response_headers_for_sendingr_rrWrappendrz)rfr8writerrs r4rz%Connection.send_with_data_passthrough s >>U "$%NO O E{h&CCEJ \\F    u 5E{..))))+ ui../       . sAB;BB4c:|j|jy)zNotify the state machine that we failed to send the data it gave us. This causes :attr:`Connection.our_state` to immediately become :data:`ERROR` -- see :ref:`error-handling` for discussion. N)rzrWrks r4 send_failedzConnection.send_failedAs DMM*r3responseczt|tusJ|j}d}tt|j }|dk(rd}t ||\}}|dvrUt|dg}|j|jdkrt|dg}|j dk7rd}nt|dd g}|jjr|rMtt|d }|jd |jd t|d t|}t||j |j"|j$ S)NFrFsGET)rMrOrNr>rKTrLr;s keep-aliver<)r?rQr=reason)rPrr?rrrdrUrrcr] keep_alivesetrdiscardaddsortedrQr=r)rfrr? need_closemethod_for_choosing_headersr_rAs r4rz1Connection._clean_up_response_headers_for_sendingZs9H~)))"" '+5$2F2F&G# &' 1*0 ''(CXN a 2 2'w0A2FG&&.$2I2IF2R +74H"M ''72!%J*74H:,W||&&*-g}EFJ   } - NN8 $&w vj?QRG ,,!..??   r3)r9Nrq)1r/r0r1__doc__!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr r'rRrgpropertyrrjrmroboolrrrtrzrrrrr rr)rrr^rur rrrr+r,rrr rrrrrrrrrrrr2r3r4r*r*s $*K%8x.%8$'%8  %8N)T(^T(^;<))24>22 4T(^44 U4UU3(34>3d3 % HT(^4L .:4>.:%.:D.:`.8n..{K/0 . (38$ % .2"& Phh78 P P  PJuUD[1JJ,/,/4,/\ ud9otF|3 48?E%i$v,"FG?B * t   7$98T<WX    % HUO  '%'HUO'6(4;:OB+29 x9 H9 r3r*N)9typingrrrrrrr r r r _eventsrrrrrrr_headersrrr_readersrr_receivebufferr_staterrrrrr r!r"r#r$_utilr%r&r'_writersr(r)__all__r+r,rrrBrstrrRrUr*r2r3r4rs   RQ*)    + 0 H  X %.!uWh./D0 0 "'(9":0  3eBis+, ,-0 t| | r3