
    Mhh                        d dl mZmZmZmZmZmZmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z(m)Z)m*Z* dd	l+m,Z,m-Z- g d
Z. G d de*e*          Z/ G d de*e*          Z0dZ1de
eef         de2fdZ3de4de
eef         dee5e
ed         ee6         f         f         fdZ7 G d d          Z8dS )    )
AnyCallablecastDictListOptionaloverload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                       e Zd ZdS )r+   N__name__
__module____qualname__     O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/h11/_connection.pyr+   r+   3           Dr3   r+   )	metaclassc                       e Zd ZdS )r,   Nr.   r2   r3   r4   r,   r,   7   r5   r3   r,   i @  eventreturnc                 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)r8   
connections     r4   _keep_aliverB   T   sD    !%-??J:uunf--66u4r3   request_methodr2   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)r      transfer-encoding   chunked)chunkedr2      content-lengthrJ   r   )http/1.0r2   )typer   r   status_coder   r?   int)rC   r8   transfer_encodingscontent_lengthss       r4   _body_framingrU   ]   s"    ;;7H----- E{{h++((*,,8I1O1O1O1OC1O1O1O1O1O++  C'''' *%-9MNN !j\1111 'u}6GHHO > 3q'9#:#:"<== E{{g''r3   c                      e Zd ZdZefdee         deddfdZe	de
ee         ee         f         fd            Ze	dee         fd            Ze	dee         fd	            Ze	defd
            Zd#dZdee         ddfdZdedeee                  fdZdee         deddfdZdee         dee         deeef         deedef                  fdZ	 d$de
ee         ee         f         dee         ddfdZe	deeef         fd            ZdeddfdZ deeee!         ee"         f         fdZ#deeee!         ee"         f         fdZ$e%de&ddfd            Z'e%dee(e)e*e+e,f         defd            Z'e%dedee         fd            Z'dedee         fdZ'dedee-e                  fdZ.d#d Z/d!e*de*fd"Z0d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`.

    our_rolemax_incomplete_event_sizer9   Nc                    || _         |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 F)_max_incomplete_event_sizer   r#   
ValueErrorrW   
their_roler   _cstate_get_io_objectr(   _writerr   _readerr   _receive_buffer_receive_buffer_closedtheir_http_version_request_method"client_is_waiting_for_100_continue)selfrW   rX   s      r4   __init__zConnection.__init__   s    
 +D'FF+++KxKKLLL 'v$DOO$DO&(( **4=$HH**4?D'JJ  - ',# 4804 38///r3   c                 4    t          | j        j                  S )zA dictionary like::

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

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

        )dictr]   statesrf   s    r4   rj   zConnection.states   s     DL'(((r3   c                 0    | j         j        | j                 S )zjThe current state of whichever role we are playing. See
        :ref:`state-machine` for details.
        )r]   rj   rW   rk   s    r4   	our_statezConnection.our_state   s    
 |"4=11r3   c                 0    | j         j        | j                 S )znThe current state of whichever role we are NOT playing. See
        :ref:`state-machine` for details.
        )r]   rj   r\   rk   s    r4   their_statezConnection.their_state   s    
 |"4?33r3   c                 ,    | j         t          u o| j        S N)r\   r   re   rk   s    r4   !they_are_waiting_for_100_continuez,Connection.they_are_waiting_for_100_continue   s    &(TT-TTr3   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)ri   r]   rj   start_next_cyclerd   re   _respond_to_state_changes)rf   
old_statess     r4   rt   zConnection.start_next_cycle   s]     $,-..
%%'''# ::::&&z22222r3   rolec                     t          | j        j                  }| j                            |           |                     |           d S rq   )ri   r]   rj   process_errorru   )rf   rw   rv   s      r4   _process_errorzConnection._process_error   sD    $,-..
""4(((&&z22222r3   r8   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   rH   rI   )rP   r   rQ   r   r   r   r]   pending_switch_proposalsrf   r8   s     r4   _server_switch_eventzConnection._server_switch_event  sx    ;;///E4E4L4L"";;(""4<#HHH5,2222s22222&&tr3   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 r9t'          t(          t
          t"          t$          f         |          }|j        | _        t	          |          t
          t"          fv rMt/          t'          t(          t
          t"          f         |                    s| j                                         t	          |          t
          u rt3          |          rd| _        t	          |          t$          t"          fv rd| _        |t          u r$t	          |          t6          t8          fv rd| _        |                     ||           d S )NrG   s   upgradeTF)ri   r]   rj   r   rP   r   methodprocess_client_switch_proposalr   r   r?   r   r#   r   process_eventrd   r\   r   r   r   r   r=   rc   rB   process_keep_alive_disabledr   re   r   r   ru   )rf   rw   r8   rv   server_switch_events        r4   _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
 (
 (

 w2GGH%PPE&+&8D# ;;7H---kw()5117
 7
- 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9r3   io_dict.c                 2   | j         j        |         }|t          u rft          t	          t
          | j                  t	          t          t          t          f         |                    \  }} |t                   |         | S |
                    ||f          S rq   )r]   rj   r"   rU   r   bytesrd   r   r   r   get)rf   rw   r8   r   stateframing_typeargss          r4   r^   zConnection._get_io_object@  s     #D)I "/UD0114gx>O8PRW3X3X" "L$ 479%l3T:: ;;e}---r3   rv   c                     | j         || j                 k    r&|                     | j        |t                    | _        | j        || j                 k    r(|                     | j        |t                    | _        d S d S rq   )	rm   rW   r^   r(   r_   ro   r\   r   r`   )rf   rv   r8   s      r4   ru   z$Connection._respond_to_state_changesV  so     >Z666..t}eWMMDLz$/:::..twOODLLL ;:r3   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.
        )r   ra   rb   rk   s    r4   trailing_datazConnection.trailing_dataa  s     d*++T-HIIr3   datac                 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)rb   RuntimeErrorra   )rf   r   s     r4   receive_datazConnection.receive_datal  sP    N  	/* O"#MNNN  D(    *.D'''r3   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)ro   r   ra   r,   r!   r$   r`   rb   hasattrr   r   r+   )rf   r   r8   s      r4   _extract_next_receive_eventz&Connection._extract_next_receive_event  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r3   c                 :   | j         t          u rt          d          	 |                                 }|t          t
          fvr.|                     | j        t          t          |                     |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)ro   r    r&   r   r+   r,   r   r\   r   r   lenra   rZ   rb   BaseExceptionrz   
isinstancer%   !_reraise_as_remote_protocol_error)rf   r8   excs      r4   
next_eventzConnection.next_event  sD   P u$$%&STTT	4466EY///##DOT%5G5GHHH	!!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 C   
D
ADDc                     d S rq   r2   r~   s     r4   sendzConnection.send      r3   c                     d S rq   r2   r~   s     r4   r   zConnection.send  s	     	r3   c                     d S rq   r2   r~   s     r4   r   zConnection.send  r   r3   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.

        Nr3   )send_with_data_passthroughjoin)rf   r8   	data_lists      r4   r   zConnection.send  s4    * 33E::	488I&&&r3   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)rm   r    r%   rP   r   &_clean_up_response_headers_for_sendingr_   r   rW   r   appendrz   )rf   r8   writerr   s       r4   r   z%Connection.send_with_data_passthrough   s     >U""$%NOOO	E{{h&&CCEJJ \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)rz   rW   rk   s    r4   send_failedzConnection.send_failedA  s      	DM*****r3   responsec                    t          |          t          u sJ |j        }d}t          t          | 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          |                    }t          ||j        |j        |j                  S )NFrF   s   GET)rM   rO   rN   r>   rK   TrL   r;   s
   keep-aliver<   )r?   rQ   r=   reason)rP   r   r?   r   r   rd   rU   r   rc   r]   
keep_alivesetr   discardaddsortedrQ   r=   r   )rf   r   r?   
need_closemethod_for_choosing_headersr   _rA   s           r4   r   z1Connection._clean_up_response_headers_for_sendingZ  sl   H~~))))"
 '+5$2F&G&G#&'11*0''(CXNNa222 'w0A2FFG&.$2IF2R2R
 +74H"MM
 '722!%J*74H:,WW|& 	S* 	S-g}EEFFJ}---NN8$$$&wvj?Q?QRRG ,!.?	
 
 
 	
r3   )r9   Nrq   )1r/   r0   r1   __doc__!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr   r'   rR   rg   propertyr   rj   rm   ro   boolrr   rt   rz   r   r   r   r   r   r   r)   r   r   r^   ru   r
   r   r   r   r+   r,   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r2   r3   r4   r*   r*      s@        $ *K%8 %8x.%8 $'%8 
	%8 %8 %8 %8N )T(^T(^;< ) ) ) X) 24> 2 2 2 X2 4T(^ 4 4 4 X4 U4 U U U XU3 3 3 3(34> 3d 3 3 3 3
	% 	HT(^4L 	 	 	 	.:4> .:% .:D .: .: .: .:`.8n. . {K/0	.
 
(38$	%. . . .2 "&	P 	Phh78	P 	P 
		P 	P 	P 	P JuUD[1 J J J XJ,/ ,/4 ,/ ,/ ,/ ,/\	ud9otF|3	4   8?E%i$v,"FG ? ? ? ?B * t    X 7$98T<WX	   X
 % HUO    X'% 'HUO ' ' ' '6 (4;:O    B+ + + +29
x 9
H 9
 9
 9
 9
 9
 9
r3   r*   N)9typingr   r   r   r   r   r   r	   r
   r   r   _eventsr   r   r   r   r   r   r   _headersr   r   r   _readersr   r   _receivebufferr   _stater   r   r   r   r   r    r!   r"   r#   r$   _utilr%   r&   r'   _writersr(   r)   __all__r+   r,   r   r   rB   r   strrR   rU   r*   r2   r3   r4   <module>r      s                                           R Q Q Q Q Q Q Q Q Q * * * * * * * * ) ) ) ) ) )                                
 + * * * * * * * 0
/
/	 	 	 	 	H 	 	 	 		 	 	 	 	X 	 	 	 	 %. !uWh./ D    0 0 "'(9":0 
3eBis+,,-0  0  0  0 t|
 |
 |
 |
 |
 |
 |
 |
 |
 |
r3   