HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/h11/__pycache__/_connection.cpython-310.pyc
o

���g�g�@sjddlmZmZmZmZmZmZmZmZm	Z	ddl
mZmZm
Z
mZmZmZmZddlmZmZmZddlmZmZddlmZddlmZmZmZmZm 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-Gdd�de)e)d
�Z.Gdd�de)e)d
�Z/dZ0de	eefde1fdd�Z2de3de	eefdee4e	edee5fffdd�Z6Gdd�d�Z7dS)�)	�Any�Callable�cast�Dict�List�Optional�Tuple�Type�Union�)�ConnectionClosed�Data�EndOfMessage�Event�InformationalResponse�Request�Response)�get_comma_header�has_expect_100_continue�set_comma_header)�READERS�ReadersType)�
ReceiveBuffer)
�_SWITCH_CONNECT�_SWITCH_UPGRADE�CLIENT�ConnectionState�DONE�ERROR�MIGHT_SWITCH_PROTOCOL�	SEND_BODY�SERVER�SWITCHED_PROTOCOL)�LocalProtocolError�RemoteProtocolError�Sentinel)�WRITERS�WritersType)�
Connection�	NEED_DATA�PAUSEDc@�eZdZdS)r)N��__name__�
__module__�__qualname__�r0r0�:/usr/local/lib/python3.10/dist-packages/h11/_connection.pyr)(�r))�	metaclassc@r+)r*Nr,r0r0r0r1r*,r2r*i@�event�returncCs0t|jd�}d|vrdSt|dd�dkrdSdS)N�
connection�closeF�http_version�1.1T)r�headers�getattr)r4�
connectionr0r0r1�_keep_aliveHsr=�request_methodr0cCs�t|�ttfvs
J�t|�tur4|jdvs+|dks+|dkr-d|jkr)dkr-dSndS|jdks4J�t|jd�}|rE|dgksCJ�d	St|jd
�}|rVdt|d�ffSt|�tur^dSd
S)N)��i0�HEAD�CONNECT���,)�content-length)r�transfer-encoding�chunked)�chunkedr0�content-lengthrDr)�http/1.0r0)�typerr�status_coderr:�int)r>r4�transfer_encodings�content_lengthsr0r0r1�
_body_framingQs&
�rOc@s�eZdZdZefdeededdfdd�Ze	de
eeeeffdd	��Ze	deefd
d��Ze	deefdd
��Z
e	defdd��Zd3dd�Zdeeddfdd�Zdedeeefdd�Zdeededdfdd�Zdeedeedeeefdeedeffdd�Z	d4de
eeeefdeeddfdd �Ze	deeeffd!d"��Zd#eddfd$d%�Z deeee!ee"ffd&d'�Z#deeee!ee"ffd(d)�Z$dedeefd*d+�Z%dedee&efd,d-�Z'd3d.d/�Z(d0e)de)fd1d2�Z*dS)5r(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_role�max_incomplete_event_sizer5NcCs�||_|ttfvrtd�|���||_||turt|_nt|_t�|_|�	|jdt
�|_|�	|jdt�|_
t�|_d|_d|_d|_d|_dS)Nz#expected CLIENT or SERVER, not {!r}F)�_max_incomplete_event_sizerr!�
ValueError�formatrP�
their_roler�_cstate�_get_io_objectr&�_writerr�_readerr�_receive_buffer�_receive_buffer_closed�their_http_version�_request_method�"client_is_waiting_for_100_continue)�selfrPrQr0r0r1�__init__�s 
zConnection.__init__cCst|jj�S)z�A dictionary like::

           {CLIENT: <client state>, SERVER: <server state>}

        See :ref:`state-machine` for details.

        )�dictrV�states�r_r0r0r1rb�s	zConnection.statescC�|jj|jS)zjThe current state of whichever role we are playing. See
        :ref:`state-machine` for details.
        )rVrbrPrcr0r0r1�	our_state��zConnection.our_statecCrd)znThe current state of whichever role we are NOT playing. See
        :ref:`state-machine` for details.
        )rVrbrUrcr0r0r1�their_state�rfzConnection.their_statecCs|jtuo|jS�N)rUrr^rcr0r0r1�!they_are_waiting_for_100_continue�sz,Connection.they_are_waiting_for_100_continuecCs4t|jj�}|j��d|_|jrJ�|�|�dS)a�Attempt 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)rarVrb�start_next_cycler]r^�_respond_to_state_changes)r_�
old_statesr0r0r1rj�s


zConnection.start_next_cycle�rolecCs&t|jj�}|j�|�|�|�dSrh)rarVrb�
process_errorrk)r_rmrlr0r0r1�_process_error�szConnection._process_errorr4cCsTt|�tur
|jdkr
tSt|�tur(t|jjvr(d|jkr%dkr(tSdSdS)N�erBrC)rJrrKrrrrV�pending_switch_proposals)r_r4r0r0r1�_server_switch_event�s�zConnection._server_switch_eventcCsRt|jj�}|tur't|�tur'|jdkr|j�t�t	|j
d�r'|j�t�d}|tur2|�
|�}|j�|t|�|�t|�turF|j|_||jurbt|�tttfvrbtttttf|�}|j|_t|�ttfvrztttttf|��sz|j��t|�tur�t|�r�d|_t|�ttfvr�d|_|tur�t|�ttfvr�d|_|�||�dS)NrAsupgradeTF)rarVrbrrJr�method�process_client_switch_proposalrrr:rr!rr�
process_eventr]rUrrrr
r8r\r=�process_keep_alive_disabledrr^r
rrk)r_rmr4rl�server_switch_eventr0r0r1�_process_events<

��
zConnection._process_event�io_dict.cCsV|jj|}|tur$ttt|j�tttt	f|��\}}|t||�S|�
||f�Srh)rVrbr rOr�bytesr]r
rr�get)r_rmr4ry�state�framing_type�argsr0r0r1rW4s�zConnection._get_io_objectrlcCsL|j||jkr|�|j|t�|_|j||jkr$|�|j|t�|_dSdSrh)	rerPrWr&rXrgrUrrY)r_rlr4r0r0r1rkJs
�z$Connection._respond_to_state_changescCst|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.
        )rzrZr[rcr0r0r1�
trailing_dataUs	zConnection.trailing_data�datacCs.|r|jr	td��|j|7_dSd|_dS)a�Add 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)r[�RuntimeErrorrZ)r_r�r0r0r1�receive_data`s
'
zConnection.receive_datacCs�|j}|tur|jrtS|tus|turtS|jdusJ�|�|j�}|dur<|js<|jr<t|jd�r9|j�	�}nt
�}|durBt}|S)N�read_eof)rgrrZr*rr"rYr[�hasattrr�rr))r_r|r4r0r0r1�_extract_next_receive_event�sz&Connection._extract_next_receive_eventc
Cs�|jtur	td��z0|��}|ttfvr|�|jtt	|��|tur7t
|j�|jkr0tddd��|j
r7td��|WSty]}z|�|j�t|t�rQ|��n�WYd}~dSd}~ww)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)rgrr$r�r)r*rxrUrr�lenrZrRr[�
BaseExceptionro�
isinstancer#�!_reraise_as_remote_protocol_error)r_r4�excr0r0r1�
next_event�s,
(�

���zConnection.next_eventcCs |�|�}|durdSd�|�S)a�Convert a high-level event into bytes that can be sent to the peer,
        while updating our internal state machine.

        Args:
            event: The :ref:`event <events>` 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.

        N�)�send_with_data_passthrough�join)r_r4�	data_listr0r0r1�send�s

zConnection.sendcCs�|jtur	td��z/t|�tur|�|�}|j}|�|j|�t|�t	ur(WdS|dus.J�g}|||j
�|WS|�|j��)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)rerr#rJr�&_clean_up_response_headers_for_sendingrXrxrPr�appendro)r_r4�writerr�r0r0r1r�s 

z%Connection.send_with_data_passthroughcCs|�|j�dS)z�Notify 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)rorPrcr0r0r1�send_failed'szConnection.send_failed�responsecCs�t|�tusJ�|j}d}tt|j�}|dkrd}t||�\}}|dvrIt|dg�}|jdus4|jdkrBt|dg�}|jdkrAd}nt|dd	g�}|j	j
rO|rhtt|d
��}|�
d�|�d�t|d
t|��}t||j|j|jd
�S)NFr@sGET)rGrIrHr9rETrFr6s
keep-aliver7)r:rKr8�reason)rJrr:rrzr]rOrr\rV�
keep_alive�setr�discard�add�sortedrKr8r�)r_r�r:�
need_close�method_for_choosing_headersr}�_r<r0r0r1r�@s4	
�

�z1Connection._clean_up_response_headers_for_sending)r5Nrh)+r-r.r/�__doc__�!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr	r%rLr`�propertyrrbrerg�boolrirjrorrrrrxr
rr'rrrWrkrrzrr�r)r*r�r�r�rr�r�rr�r0r0r0r1r(�sd���
�' 

0��
�
����
�
.
� A
!r(N)8�typingrrrrrrrr	r
�_eventsrr
rrrrr�_headersrrr�_readersrr�_receivebufferr�_staterrrrrrrr r!r"�_utilr#r$r%�_writersr&r'�__all__r)r*r�r�r=rz�strrLrOr(r0r0r0r1�<module>s*,$	0	�
�
�: