
    ,hh                        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      Z8y)    )
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y)r+   N__name__
__module____qualname__     Q/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/h11/_connection.pyr+   r+   3       r3   r+   )	metaclassc                       e Zd Zy)r,   Nr.   r2   r3   r4   r,   r,   7   r5   r3   r,   i @  eventreturnc                 \    t        | j                  d      }d|v ryt        | dd      dk  ryy)N
   connection   closeFhttp_version   1.1T)r   headersgetattr)r8   
connections     r4   _keep_aliverB   T   s4    !%--?J:unf-6r3   request_methodr2   c                    t        |      t        t        fv sJ t        |      t        u rB|j                  dv s"| dk(  s| dk(  rd|j                  cxk  rdk  ry ny|j                  dk\  sJ t	        |j
                  d      }|r	|dgk(  sJ y	t	        |j
                  d
      }|rdt        |d         ffS t        |      t        u ryy)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+(*,8I8I1OC1O+ 2P+   C'' *%--9MN!j\11 'u}}6G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"Z0y)%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                  | j                  d t              | _
        | j                  | j
                  d t              | _        t               | _        d| _        d | _        d | _        d| _        y )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++>xlKLL v$DO$DO&( **4==$H**4??D'J  - ',# 4804 38/r3   c                 @    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     DLL''((r3   c                 H    | 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                 H    | 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                 B    | j                   t        u xr | 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-TTr3   c                     t        | j                  j                        }| j                  j                          d| _        | j
                  rJ | j                  |       y)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   sL     $,,--.
%%'# :::&&z2r3   rolec                     t        | j                  j                        }| j                  j                  |       | j	                  |       y rq   )ri   r]   rj   process_errorru   )rf   rw   rv   s      r4   _process_errorzConnection._process_error   s6    $,,--.
""4(&&z2r3   r8   c                     t        |      t        u r|j                  dk(  rt        S t        |      t        u r9t
        | j                  j                  v rd|j                  cxk  r
dk  rt
        S  y y )Ne   rH   rI   )rP   r   rQ   r   r   r   r]   pending_switch_proposalsrf   r8   s     r4   _server_switch_eventzConnection._server_switch_event  sc    ;//E4E4E4L"";("4<<#H#HH5,,2s2&& 3 r3   c                 Z   t        | j                  j                        }|t        u rtt	        |      t
        u rc|j                  dk(  r| j                  j                  t               t        |j                  d      r| j                  j                  t               d }|t        u r| j                  |      }| j                  j                  |t	        |      |       t	        |      t
        u r|j                  | _        || j                   u rOt	        |      t
        t"        t$        fv r3t'        t(        t
        t"        t$        f   |      }|j*                  | _        t	        |      t
        t"        fv r@t/        t'        t(        t
        t"        f   |            s| j                  j1                          t	        |      t
        u rt3        |      rd| _        t	        |      t$        t"        fv rd| _        |t        u rt	        |      t6        t8        fv rd| _        | j;                  ||       y )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>d5kW4||z);;OLz:;;OL"6>"&";";E"B""4e6IJ ;'!#(<<D 4??"tE{!7
 (

 w2GGH%PE&+&8&8D# ;7H--kw()517
 LL446 ;'!&=e&D6:D3;0(;;6;D36>d5kdL-AA6;D3&&z59r3   io_dict.c                    | j                   j                  |   }|t        u rQt        t	        t
        | j                        t	        t        t        t        f   |            \  }} |t           |   | S |j                  ||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014gx>O8PRW3X"L$ 479%l3T:: ;;e}--r3   rv   c                    | j                   || j                     k7  r&| j                  | j                  |t              | _        | j
                  || j                     k7  r'| j                  | j                  |t              | _        y y rq   )	rm   rW   r^   r(   r_   ro   r\   r   r`   )rf   rv   r8   s      r4   ru   z$Connection._respond_to_state_changesV  si     >>Z66..t}}eWMDLz$//::..twODL ;r3   c                 D    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-HIIr3   datac                 p    |r-| j                   rt        d      | xj                  |z  c_        yd| _         y)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  s6    N **"#MNN  D( *.D'r3   c                    | j                   }|t        u r| j                  rt        S |t        u s|t
        u rt        S | j                  J | j                  | j                        }|S| j                  sG| j                  r;t        | j                  d      r| j                  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1M))U6G-GM||''T112=''D,G,G
 4<<4 LL113E,.E=Er3   c                    | j                   t        u rt        d      	 | j                         }|t        t
        fvr*| j                  | j                  t        t        |             |t        u rFt        | j                        | j                  kD  rt        dd      | j                  rt        d      |S # t        $ rG}| j                  | j                         t!        |t"              r|j%                          n Y d}~y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  s    P u$%&STT	446EY//##DOOT%5GH	!t++,t/N/NN .1S  .. ..STTL 	0#12557 8	s   BB7 7	D =DDc                      y rq   r2   r~   s     r4   sendzConnection.send      r3   c                      y rq   r2   r~   s     r4   r   zConnection.send  s     	r3   c                      y rq   r2   r~   s     r4   r   zConnection.send  r   r3   c                 L    | j                  |      }|ydj                  |      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  s,    * 33E:	88I&&r3   c                 n   | j                   t        u rt        d      	 t        |      t        u r| j                  |      }| j                  }| j                  | j                  |       t        |      t        u ry|J g } |||j                         |S #  | j                  | 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	E{h&CCEJ \\Fu5E{.. )))+	ui../  	.s   AB ;B B4c                 :    | j                  | j                         y)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     	DMM*r3   responsec                 z   t        |      t        u sJ |j                  }d}t        t        | j
                        }|dk(  rd}t        ||      \  }}|dv rUt        |dg       }| j                  | j                  dk  rt        |dg       }| j
                  dk7  rd}nt        |dd	g      }| j                  j                  r|rMt        t        |d
            }|j                  d       |j                  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  s6   H~))""
 '+5$2F2F&G#&'1*0''(CXNa22 'w0A2FG&&.$2I2IF2R
 +74H"M
 ''72!%J*74H:,W||&&*-g}EFJ}-NN8$&wvj?Q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x.%8 $'%8 
	%8N )T(^T(^;< ) ) 24> 2 2 4T(^ 4 4 U4 U U3(34> 3d 3
	% 	HT(^4L 	.:4> .:% .:D .:`.8n. . {K/0	.
 
(38$	%.2 "&	Phh78	P 	P 
		P JuUD[1 J J,/ ,/4 ,/\	ud9otF|3	48?E%i$v,"FG ?B * t   7$98T<WX	 
 % HUO  '% 'HUO '6 (4;:O B+29
x 9
H 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 * )   
 + 0	H 		X 	 %. !uWh./ D 0 0 "'(9":0 
3eBis+,,-0 t|
 |
r3   