
    Wha                         d dl T d dlmZmZmZ d dlmZ d dlmZ d dl	T d dl	m
Z
mZmZ d dlmZmZmZ d dlmZ g dZ ed	          Z ed
          ZdZd Zd Z G d d          ZdS )   )*)get_comma_headerhas_expect_100_continueset_comma_header)READERS)ReceiveBuffer)_SWITCH_CONNECT_SWITCH_UPGRADEConnectionState)LocalProtocolErrormake_sentinelRemoteProtocolError)WRITERS)
Connection	NEED_DATAPAUSEDr   r   i @  c                 j    t          | j        d          }d|v rdS t          | dd          dk     rdS dS )N
   connection   closeFhttp_version   1.1T)r   headersgetattr)event
connections     U/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/h11/_connection.py_keep_aliver   /   sD    !%-??J:uunf--66u4    c                    t          |          t          t          fv sJ t          |          t          u r9|j        dv s!| dk    s| dk    rd|j        cxk    rdk     rn ndS |j        dk    sJ t	          |j        d          }|r|dgk    sJ d	S t	          |j        d
          }|rdt          |d                   ffS t          |          t          u rdS dS )N)   i0     HEAD   CONNECT   ,  )content-length)       transfer-encodings   chunked)chunked    content-lengthr%   r&   )http/1.0r)   )typeRequestResponsestatus_coder   r   int)request_methodr   transfer_encodingscontent_lengthss       r   _body_framingr4   8   s"   
 ;;7H----- E{{h++((*,,8I1O1O1O1OC1O1O1O1O1O++  C'''' *%-9MNN !j\1111 'u}6GHHO > 3q'9#:#:"<== E{{g''r   c                       e Zd ZdZefdZed             Zed             Zed             Z	ed             Z
d Zd Zd	 Zd
 Zd ZddZed             Zd Zd Zd Zd Zd Zd Zd ZdS )r   aB  An 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`.

    c                    || _         |t          t          fvr"t          d                    |                    || _        |t          u rt          | _        nt          | _        t                      | _        | 	                    | j        d t                    | _        | 	                    | j        d t                    | _        t                      | _        d| _        d | _        d | _        d| _        d S )Nz#expected CLIENT or SERVER, not {!r}F)_max_incomplete_event_sizeCLIENTSERVER
ValueErrorformatour_role
their_roler   _cstate_get_io_objectr   _writerr   _readerr   _receive_buffer_receive_buffer_closedtheir_http_version_request_method"client_is_waiting_for_100_continue)selfr<   max_incomplete_event_sizes      r   __init__zConnection.__init__   s     +D'FF+++BII(SSTTT v$DOO$DO&(( **4=$HH**4?D'JJ  - ',# #'# 38///r   c                 4    t          | j        j                  S )zA dictionary like::

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

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

        )dictr>   statesrG   s    r   rL   zConnection.states   s     DL'(((r   c                 0    | j         j        | j                 S )zjThe current state of whichever role we are playing. See
        :ref:`state-machine` for details.
        )r>   rL   r<   rM   s    r   	our_statezConnection.our_state   s    
 |"4=11r   c                 0    | j         j        | j                 S )znThe current state of whichever role we are NOT playing. See
        :ref:`state-machine` for details.
        )r>   rL   r=   rM   s    r   their_statezConnection.their_state   s    
 |"4?33r   c                 ,    | j         t          u o| j        S N)r=   r8   rF   rM   s    r   !they_are_waiting_for_100_continuez,Connection.they_are_waiting_for_100_continue   s    &(TT-TTr   c                     t          | j        j                  }| j                                         d| _        | j        rJ |                     |           dS )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)rK   r>   rL   start_next_cyclerE   rF   _respond_to_state_changes)rG   
old_statess     r   rV   zConnection.start_next_cycle   s]     $,-..
%%'''# ::::&&z22222r   c                     t          | j        j                  }| j                            |           |                     |           d S rS   )rK   r>   rL   process_errorrW   )rG   rolerX   s      r   _process_errorzConnection._process_error   sD    $,-..
""4(((&&z22222r   c                     t          |          t          u r|j        dk    rt          S t          |          t          u r/t
          | j        j        v rd|j        cxk    rdk     r
n nt
          S d S )Ne   r#   r$   )r,   InformationalResponser/   r
   r.   r	   r>   pending_switch_proposals)rG   r   s     r   _server_switch_eventzConnection._server_switch_event   sx    ;;///E4E4L4L"";;(""4<#HHH5,2222s22222&&tr   c                 (   t          | j        j                  }|t          u rtt	          |          t
          u r^|j        dk    r| j                            t                     t          |j
        d          r| j                            t                     d }|t          u r|                     |          }| j                            |t	          |          |           t	          |          t
          u r|j        | _        || j        u r/t	          |          t
          t"          t$          fv r|j        | _        t	          |          t
          t"          fv r(t+          |          s| j                                         t	          |          t
          u rt/          |          rd| _        t	          |          t$          t"          fv rd| _        |t          u r$t	          |          t2          t4          fv rd| _        |                     ||           d S )Nr"   s   upgradeTF)rK   r>   rL   r8   r,   r-   methodprocess_client_switch_proposalr	   r   r   r
   r9   ra   process_eventrE   r=   r.   r_   r   rD   r   process_keep_alive_disabledr   rF   DataEndOfMessagerW   )rG   r[   r   rX   server_switch_events        r   _process_eventzConnection._process_event   s    $,-..
6>>d5kkW44|z));;OLLLz:: M;;OLLL"6>>"&";";E"B"B""4e6IJJJ
 ;;'!!#(<D  4?""tE{{!7
 (
 (

 ',&8D# ;;7H---k%6H6H-L44666 ;;'!!&=e&D&D!6:D3;;0(;;;6;D36>>d5kkdL-AAA6;D3&&z599999r   c                     | j         j        |         }|t          u r.t          | j        |          \  }} |t                   |         | S |                    ||f          S rS   )r>   rL   	SEND_BODYr4   rE   get)rG   r[   r   io_dictstateframing_typeargss          r   r?   zConnection._get_io_object  sd    #D)I "/t/CU!K!KL$379%l3T:: ;;e}---r   Nc                     | j         || j                 k    r&|                     | j        |t                    | _        | j        || j                 k    r(|                     | j        |t                    | _        d S d S rS   )	rO   r<   r?   r   r@   rQ   r=   r   rA   )rG   rX   r   s      r   rW   z$Connection._respond_to_state_changes$  sm    >Z666..t}eWMMDLz$/:::..twOODLLL ;:r   c                 8    t          | j                  | j        fS )ab  Data 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.
        )bytesrB   rC   rM   s    r   trailing_datazConnection.trailing_data+  s     d*++T-HIIr   c                 h    |r(| j         rt          d          | xj        |z  c_        dS d| _         dS )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)rC   RuntimeErrorrB   )rG   datas     r   receive_datazConnection.receive_data6  sP    N  	/* O"#MNNN  D(    *.D'''r   c                 f   | j         }|t          u r| j        rt          S |t          u s	|t
          u rt          S | j        J |                     | j                  }|K| j        sD| j        r=t          | j        d          r| j        	                                }nt                      }|t          }|S )Nread_eof)rQ   DONErB   r   MIGHT_SWITCH_PROTOCOLSWITCHED_PROTOCOLrA   rC   hasattrr{   ConnectionClosedr   )rG   ro   r   s      r   _extract_next_receive_eventz&Connection._extract_next_receive_eventd  s     
 D==T1=M)))U6G-G-GM|'''T122=' /D,G /
 4<44 / L1133EE,..E=Er   c                    | j         t          u rt          d          	 |                                 }|t          t
          fvr|                     | j        |           |t          u rDt          | j	                  | j
        k    rt          dd          | j        rt          d          |S # t          $ rP}|                     | j                   t          |t                    r|                                 n Y d}~dS d}~ww xY w)aF  Parse 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 ERRORzReceive buffer too longi  )error_status_hintz#peer unexpectedly closed connectionN)rQ   ERRORr   r   r   r   rj   r=   lenrB   r7   rC   BaseExceptionr\   
isinstancer   !_reraise_as_remote_protocol_error)rG   r   excs      r   
next_eventzConnection.next_event~  s:   P u$$%&STTT	4466EY///##DOU;;;	!!t+,,t/NNN .1S    . U ..STTTL 	 	 	000#122 557777 877777	s   BB- -
D7ADDc                 ^    |                      |          }|dS d                    |          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.

        Nr   )send_with_data_passthroughjoin)rG   r   	data_lists      r   sendzConnection.send  s4    * 33E::	488I&&&r   c                 z   | j         t          u rt          d          	 t          |          t          u r|                     |           | j        }|                     | j        |           t          |          t          u rdS |J g } |||j
                   |S #  |                     | j                    xY w)ah  Identical 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)rO   r   r   r,   r.   &_clean_up_response_headers_for_sendingr@   rj   r<   r   appendr\   )rG   r   writerr   s       r   r   z%Connection.send_with_data_passthrough  s     >U""$%NOOO	E{{h&&;;EBBB \Fu555E{{...t )))	ui.///  	...s   A#B B B:c                 :    |                      | j                   dS )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)r\   r<   rM   s    r   send_failedzConnection.send_failed  s      	DM*****r   c                 0   t          |          t          u sJ |j        }d}| j        }|dk    rd}t	          ||          \  }}|dv rTt          |dg           }| j        | j        dk     rt          |dg           }| j        dk    rd}nt          |dd	g          }| j        j        r|ret          t          |d
                    }|                    d           |                    d           t          |d
t          |                    }||_        d S )NFr!   s   GET)r(   r+   r*   r   r'   Tr(   r   s
   keep-aliver   )r,   r.   r   rE   r4   r   rD   r>   
keep_alivesetr   discardaddsorted)rG   responser   
need_closemethod_for_choosing_headersrp   _r   s           r   r   z1Connection._clean_up_response_headers_for_sending  sE   H~~))))"
 '+&:#&'11*0''(CXNNa222 'w0A2FFG&.$2IF2R2R
 +74H"MM
 '722!%J*74H9+VV|& 	S* 	S-g}EEFFJ}---NN8$$$&wvj?Q?QRRG"r   rS   )__name__
__module____qualname____doc__!DEFAULT_MAX_INCOMPLETE_EVENT_SIZErI   propertyrL   rO   rQ   rT   rV   r\   ra   rj   r?   rW   ru   ry   r   r   r   r   r   r   r)   r   r   r   r   p   s          3T"8 "8 "8 "8H ) ) X) 2 2 X2 4 4 X4 U U XU3 3 3(3 3 3
	 	 	-: -: -:^. . .P P P P J J XJ,/ ,/ ,/\  4? ? ?B' ' '6  B+ + +44# 4# 4# 4# 4#r   r   N)_events_headersr   r   r   _readersr   _receivebufferr   _stater	   r
   r   _utilr   r   r   _writersr   __all__r   r   r   r   r4   r   r)   r   r   <module>r      sS       Q Q Q Q Q Q Q Q Q Q       ) ) ) ) ) )     E E E E E E E E E E         
       0
/
/M+&&		x	 	  %. !  .  .  . pY# Y# Y# Y# Y# Y# Y# Y# Y# Y#r   