
     fg                        d dl 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)          Z/dZ0de	eef         de1fdZ2de3de	eef         dee4e	ed         ee5         f         f         fdZ6 G d d          Z7dS )    )	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                       e Zd ZdS )r*   N__name__
__module____qualname__     R/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/h11/_connection.pyr*   r*   (           Dr2   r*   )	metaclassc                       e Zd ZdS )r+   Nr-   r1   r2   r3   r+   r+   ,   r4   r2   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)r7   
connections     r3   _keep_aliverA   H   sD    !%-??J:uunf--66u4r2   request_methodr1   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)chunkedr1      content-lengthrI   r   )http/1.0r1   )typer   r   status_coder   r>   int)rB   r7   transfer_encodingscontent_lengthss       r3   _body_framingrT   Q   s"    ;;7H----- E{{h++((*,,8I1O1O1O1OC1O1O1O1O1O++  C'''' *%-9MNN !j\1111 'u}6GHHO > 3q'9#:#:"<== E{{g''r2   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$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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`.

    our_rolemax_incomplete_event_sizer8   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 {!r}F)_max_incomplete_event_sizer   r"   
ValueErrorformatrV   
their_roler   _cstate_get_io_objectr'   _writerr   _readerr   _receive_buffer_receive_buffer_closedtheir_http_version_request_method"client_is_waiting_for_100_continue)selfrV   rW   s      r3   __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  - ',# 4804 38///r2   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    r3   rj   zConnection.states   s     DL'(((r2   c                 0    | j         j        | j                 S )zjThe current state of whichever role we are playing. See
        :ref:`state-machine` for details.
        )r]   rj   rV   rk   s    r3   	our_statezConnection.our_state   s    
 |"4=11r2   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    r3   their_statezConnection.their_state   s    
 |"4?33r2   c                 ,    | j         t          u o| j        S N)r\   r   re   rk   s    r3   !they_are_waiting_for_100_continuez,Connection.they_are_waiting_for_100_continue   s    &(TT-TTr2   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     r3   rt   zConnection.start_next_cycle   s]     $,-..
%%'''# ::::&&z22222r2   rolec                     t          | j        j                  }| j                            |           |                     |           d S rq   )ri   r]   rj   process_errorru   )rf   rw   rv   s      r3   _process_errorzConnection._process_error   sD    $,-..
""4(((&&z22222r2   r7   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   rG   rH   )rO   r   rP   r   r   r   r]   pending_switch_proposals)rf   r7   s     r3   _server_switch_eventzConnection._server_switch_event   sx    ;;///E4E4L4L"";;(""4<#HHH5,2222s22222&&tr2   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 )NrF   s   upgradeTF)ri   r]   rj   r   rO   r   methodprocess_client_switch_proposalr   r   r>   r   r"   r~   process_eventrd   r\   r   r   r   r   r<   rc   rA   process_keep_alive_disabledr   re   r   r   ru   )rf   rw   r7   rv   server_switch_events        r3   _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9r2   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!   rT   r   bytesrd   r   r   r   get)rf   rw   r7   r   stateframing_typeargss          r3   r^   zConnection._get_io_object4  s     #D)I "/UD0114gx>O8PRW3X3X" "L$ 479%l3T:: ;;e}---r2   rv   c                     | j         || j                 k    r&|                     | j        |t                    | _        | j        || j                 k    r(|                     | j        |t                    | _        d S d S rq   )	rm   rV   r^   r'   r_   ro   r\   r   r`   )rf   rv   r7   s      r3   ru   z$Connection._respond_to_state_changesJ  so     >Z666..t}eWMMDLz$/:::..twOODLLL ;:r2   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    r3   trailing_datazConnection.trailing_dataU  s     d*++T-HIIr2   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     r3   receive_datazConnection.receive_data`  sP    N  	/* O"#MNNN  D(    *.D'''r2   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   r7   s      r3   _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r2   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   rY   rb   BaseExceptionrz   
isinstancer$   !_reraise_as_remote_protocol_error)rf   r7   excs      r3   
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 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.

        Nr2   )send_with_data_passthroughjoin)rf   r7   	data_lists      r3   sendzConnection.send  s4    * 33E::	488I&&&r2   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$   rO   r   &_clean_up_response_headers_for_sendingr_   r   rV   r   appendrz   )rf   r7   writerr   s       r3   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   rV   rk   s    r3   send_failedzConnection.send_failed'  s      	DM*****r2   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 )NFrE   s   GET)rL   rN   rM   r=   rJ   TrK   r:   s
   keep-aliver;   )r>   rP   r<   reason)rO   r   r>   r   r   rd   rT   r   rc   r]   
keep_alivesetr   discardaddsortedrP   r<   r   )rf   r   r>   
need_closemethod_for_choosing_headersr   _r@   s           r3   r   z1Connection._clean_up_response_headers_for_sending@  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 ,!.?	
 
 
 	
r2   )r8   Nrq   )+r.   r/   r0   __doc__!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr
   r&   rQ   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   r1   r2   r3   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'% 'HUO ' ' ' '6 (4;:O    B+ + + +29
x 9
H 9
 9
 9
 9
 9
 9
r2   r)   N)8typingr   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   rA   r   strrQ   rT   r)   r1   r2   r3   <module>r      s   Q P P P P P P P P P P P P P P P P P P P P P                  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n
 n
 n
 n
 n
 n
 n
 n
 n
 n
r2   