
    ,hg                       d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ ddl	m
Z
mZmZmZ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 dd
l m!Z! ddlm"Z"m#Z#m$Z$ g dZ%eeeef   Z&	  G d dejN                        Z(e(jR                  Z)e(jT                  Z* G d dejN                        Z+e+jX                  Z,e+jZ                  Z-e+j\                  Z.e+j^                  Z/dZ0	  G d d      Z1y)    )annotationsN)	Generator)Union   )ConnectionClosedConnectionClosedErrorConnectionClosedOKInvalidStatePayloadTooBigProtocolError)	Extension)
OK_CLOSE_CODES	OP_BINARYOP_CLOSEOP_CONTOP_PINGOP_PONGOP_TEXTClose	CloseCodeFrame)RequestResponse)StreamReader)
LoggerLikeOriginSubprotocol)ProtocolSideStateSEND_EOFc                  &    e Zd ZdZ ed      \  ZZy)r   z6A WebSocket connection is either a server or a client.   N)__name__
__module____qualname____doc__rangeSERVERCLIENT     U/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/websockets/protocol.pyr   r   /   s    @1XNFFr,   r   c                  *    e Zd ZdZ ed      \  ZZZZy)r    z6A WebSocket connection is in one of these four states.   N)	r$   r%   r&   r'   r(   
CONNECTINGOPENCLOSINGCLOSEDr+   r,   r-   r    r    9   s    @(-a%Jgvr,   r    r,   c                  2   e Zd ZdZeddd	 	 	 	 	 	 	 	 	 ddZedd       Zej                  dd       Zedd       Z	ed d	       Z
ed!d
       Zd"dZd#dZd$dZd%d$dZd%d$dZd&d'dZd"dZd"dZd(d)dZd*dZd+dZd,dZd-dZd-dZd.dZd.dZd#dZy)/r   a6  
    Sans-I/O implementation of a WebSocket connection.

    Args:
        side: :attr:`~Side.CLIENT` or :attr:`~Side.SERVER`.
        state: Initial state of the WebSocket connection.
        max_size: Maximum size of incoming messages in bytes;
            :obj:`None` disables the limit.
        logger: Logger for this connection; depending on ``side``,
            defaults to ``logging.getLogger("websockets.client")``
            or ``logging.getLogger("websockets.server")``;
            see the :doc:`logging guide <../../topics/logging>` for details.

    i   N)statemax_sizeloggerc               H   t        j                         | _        	 |0t        j                  d|j
                  j                                }|| _        	 |j                  t        j                        | _
        || _        || _        || _        d | _        d| _        d | _        g | _        d | _        d | _        d | _        d | _        d | _        	 d| _        t1               | _        g | _        g | _        | j9                         | _        t=        | j:                         d | _        y )Nzwebsockets.F) uuiduuid4idlogging	getLoggernamelowerr7   isEnabledForDEBUGdebugsider5   r6   cur_sizeexpect_continuation_frameorigin
extensionssubprotocol
close_rcvd
close_sentclose_rcvd_then_senthandshake_exceof_sentr   readereventswritesparseparsernext
parser_exc)selfrC   r5   r6   r7   s        r-   __init__zProtocol.__init__Y   s    "ZZ\B >&&TYY__5F4G'HIF"() ((7
 	 
 ! %) */& &*+-/3 )-(,15! 04	  #n#%#%jjlT[[,0r,   c                    | j                   S )a  
        State of the WebSocket connection.

        Defined in 4.1_, 4.2_, 7.1.3_, and 7.1.4_ of :rfc:`6455`.

        .. _4.1: https://datatracker.ietf.org/doc/html/rfc6455#section-4.1
        .. _4.2: https://datatracker.ietf.org/doc/html/rfc6455#section-4.2
        .. _7.1.3: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.3
        .. _7.1.4: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.4

        )_staterU   s    r-   r5   zProtocol.state   s     {{r,   c                v    | j                   r&| j                  j                  d|j                         || _        y )Nz= connection is %s)rB   r7   r>   rX   )rU   r5   s     r-   r5   zProtocol.state   s)    ::KK2EJJ?r,   c                    | j                   t        ury| j                  t        j                  S | j                  j
                  S )z
        WebSocket close code received from the remote endpoint.

        Defined in 7.1.5_ of :rfc:`6455`.

        .. _7.1.5: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5

        :obj:`None` if the connection isn't closed yet.

        N)r5   r3   rI   r   ABNORMAL_CLOSUREcoderY   s    r-   
close_codezProtocol.close_code   s9     ::V#__$---??'''r,   c                n    | j                   t        ury| j                  y| j                  j                  S )a  
        WebSocket close reason  received from the remote endpoint.

        Defined in 7.1.6_ of :rfc:`6455`.

        .. _7.1.6: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6

        :obj:`None` if the connection isn't closed yet.

        N )r5   r3   rI   reasonrY   s    r-   close_reasonzProtocol.close_reason   s0     ::V#__$??)))r,   c                d   | j                   t        u sJ d       | j                  K| j                  ?| j                  j                  t
        v r#| j                  j                  t
        v rt        }nt        } || j                  | j                  | j                        }| j                  |_
        |S )a  
        Exception to raise when trying to interact with a closed connection.

        Don't raise this exception while the connection :attr:`state`
        is :attr:`~websockets.protocol.State.CLOSING`; wait until
        it's :attr:`~websockets.protocol.State.CLOSED`.

        Indeed, the exception includes the close code and reason, which are
        known only once the connection is closed.

        Raises:
            AssertionError: If the connection isn't closed yet.

        zconnection isn't closed yet)r5   r3   rI   rJ   r]   r   r	   r   rK   rT   	__cause__)rU   exc_typeexcs      r-   	close_exczProtocol.close_exc   s      zzV#B%BB OO'+$$6$$6)H,H (OOOO%%!
 
r,   c                d    | j                   j                  |       t        | j                         y)aH  
        Receive data from the network.

        After calling this method:

        - You must call :meth:`data_to_send` and send this data to the network.
        - You should call :meth:`events_received` and process resulting events.

        Raises:
            EOFError: If :meth:`receive_eof` was called earlier.

        N)rN   	feed_datarS   rR   rU   datas     r-   receive_datazProtocol.receive_data   s"     	d#T[[r,   c                    | j                   j                  ry| j                   j                          t        | j                         y)a  
        Receive the end of the data stream from the network.

        After calling this method:

        - You must call :meth:`data_to_send` and send this data to the network;
          it will return ``[b""]``, signaling the end of the stream, or ``[]``.
        - You aren't expected to call :meth:`events_received`; it won't return
          any new events.

        :meth:`receive_eof` is idempotent.

        N)rN   eoffeed_eofrS   rR   rY   s    r-   receive_eofzProtocol.receive_eof  s.     ;;??T[[r,   c                   | j                   st        d      | j                  t        ur0t	        d| j
                  j                  j                                | | _         | j                  t        t        ||             y)a  
        Send a `Continuation frame`_.

        .. _Continuation frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.6

        Parameters:
            data: payload containing the same kind of data
                as the initial frame.
            fin: FIN bit; set it to :obj:`True` if this is the last frame
                of a fragmented message and to :obj:`False` otherwise.

        Raises:
            ProtocolError: If a fragmented message isn't in progress.

        unexpected continuation frameconnection is N)rE   r   rX   r1   r
   r5   r>   r?   
send_framer   r   rU   rk   fins      r-   send_continuationzProtocol.send_continuation#  sh    " -- ?@@;;d"

0E0E0G/HIJJ-0&gtS12r,   c                   | j                   rt        d      | j                  t        ur0t	        d| j
                  j                  j                                | | _         | j                  t        t        ||             y)a  
        Send a `Text frame`_.

        .. _Text frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.6

        Parameters:
            data: payload containing text encoded with UTF-8.
            fin: FIN bit; set it to :obj:`False` if this is the first frame of
                a fragmented message.

        Raises:
            ProtocolError: If a fragmented message is in progress.

        expected a continuation framers   N)rE   r   rX   r1   r
   r5   r>   r?   rt   r   r   ru   s      r-   	send_textzProtocol.send_text;  sh      )) ?@@;;d"

0E0E0G/HIJJ-0&gtS12r,   c                   | j                   rt        d      | j                  t        ur0t	        d| j
                  j                  j                                | | _         | j                  t        t        ||             y)a  
        Send a `Binary frame`_.

        .. _Binary frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.6

        Parameters:
            data: payload containing arbitrary binary data.
            fin: FIN bit; set it to :obj:`False` if this is the first frame of
                a fragmented message.

        Raises:
            ProtocolError: If a fragmented message is in progress.

        ry   rs   N)rE   r   rX   r1   r
   r5   r>   r?   rt   r   r   ru   s      r-   send_binaryzProtocol.send_binaryR  sh      )) ?@@;;d"

0E0E0G/HIJJ-0&is34r,   c                   | j                   t        ur0t        d| j                  j                  j                                |-|dk7  rt        d      t        t        j                  d      }d}nt        ||      }|j                         }| j                  t        t        |             | j                  J || _        t         | _        y)a_  
        Send a `Close frame`_.

        .. _Close frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1

        Parameters:
            code: close code.
            reason: close reason.

        Raises:
            ProtocolError: If the code isn't valid or if a reason is provided
                without a code.

        rs   Nr`   z#cannot send a reason without a coder,   )rX   r1   r
   r5   r>   r?   r   r   r   NO_STATUS_RCVD	serializert   r   r   rI   rJ   r2   rU   r]   ra   closerk   s        r-   
send_closezProtocol.send_closei  s    $ ;;d"

0E0E0G/HIJJ<|#$IJJ)22B7ED$'E??$Dh-. &&
r,   c                    | j                   t        urB| j                   t        ur0t        d| j                  j
                  j                                | j                  t        t        |             y)z
        Send a `Ping frame`_.

        .. _Ping frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2

        Parameters:
            data: payload containing arbitrary binary data.

        rs   N)
rX   r1   r2   r
   r5   r>   r?   rt   r   r   rj   s     r-   	send_pingzProtocol.send_ping  R     ;;d"t{{''A

0E0E0G/HIJJgt,-r,   c                    | j                   t        urB| j                   t        ur0t        d| j                  j
                  j                                | j                  t        t        |             y)z
        Send a `Pong frame`_.

        .. _Pong frame:
            https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3

        Parameters:
            data: payload containing arbitrary binary data.

        rs   N)
rX   r1   r2   r
   r5   r>   r?   rt   r   r   rj   s     r-   	send_pongzProtocol.send_pong  r   r,   c                   | j                   t        u rs|t        j                  k7  r`t	        ||      }|j                         }| j                  t        t        |             || _	        | j                  d| _        t        | _         | j                  t        u r| j                  s| j!                          | j#                         | _        t'        | j$                         y)a?  
        `Fail the WebSocket connection`_.

        .. _Fail the WebSocket connection:
            https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7

        Parameters:
            code: close code
            reason: close reason

        Raises:
            ProtocolError: If the code isn't valid.
        NT)r5   r1   r   r\   r   r   rt   r   r   rJ   rI   rK   r2   rC   r)   rM   send_eofdiscardrR   rS   r   s        r-   failzProtocol.fail  s    & ::y111dF+(h 56"'
 ??.04D-$
 99t}}MMO llnT[[r,   c                .    | j                   g c}| _         |S )a*  
        Fetch events generated from data received from the network.

        Call this method immediately after any of the ``receive_*()`` methods.

        Process resulting events, likely by passing them to the application.

        Returns:
            Events read from the connection.
        )rO   )rU   rO   s     r-   events_receivedzProtocol.events_received  s     #kk2r,   c                .    | j                   g c}| _         |S )a  
        Obtain data to send to the network.

        Call this method immediately after any of the ``receive_*()``,
        ``send_*()``, or :meth:`fail` methods.

        Write resulting data to the connection.

        The empty bytestring :data:`~websockets.protocol.SEND_EOF` signals
        the end of the data stream. When you receive it, half-close the TCP
        connection.

        Returns:
            Data to write to the connection.

        )rP   )rU   rP   s     r-   data_to_sendzProtocol.data_to_send  s    " #kk2r,   c                    | j                   t        u ry| j                   t        u ry| j                   t        u ry| j                   t        u sJ | j
                  S )a  
        Tell if the TCP connection is expected to close soon.

        Call this method immediately after any of the ``receive_*()``,
        ``send_close()``, or :meth:`fail` methods.

        If it returns :obj:`True`, schedule closing the TCP connection after a
        short timeout if the other side hasn't already closed it.

        Returns:
            Whether the TCP connection is expected to close soon.

        FT)r5   r1   r2   r3   r0   rM   rY   s    r-   close_expectedzProtocol.close_expected  sK    D :::: ::zzZ''}}r,   c              #    K   	 	 | j                   j                         E d{   r2| j                  r| j                  j                  d       t	        d      | j
                  d}n2| j                  | j
                  }n| j
                  | j                  z
  }t        j                  | j                   j                  | j                  t        u || j                        E d{   }| j                  r| j                  j                  d|       | j                  |       7 7 B# t        $ r;}| j                  t         j"                  t%        |             || _        Y d}~nKd}~wt        $ r;}| j                  t         j(                  t%        |             || _        Y d}~n	d}~wt*        $ rJ}| j                  t         j,                  |j.                   d|j0                          || _        Y d}~nd}~wt2        $ rU}|j5                  | j                         | j                  t         j6                  t%        |             || _        Y d}~n\d}~wt8        $ rM}| j                  j;                  dd	       | j                  t         j<                         || _        Y d}~nd}~ww xY wd t?        d
      w)a-  
        Parse incoming data into frames.

        :meth:`receive_data` and :meth:`receive_eof` run this generator
        coroutine until it needs more data or reaches EOF.

        :meth:`parse` never raises an exception. Instead, it sets the
        :attr:`parser_exc` and yields control.

        TN< EOFzunexpected end of stream)maskr6   rG   z< %sz at position zparser failed)exc_infoz"parse() shouldn't step after error) rN   at_eofrB   r7   EOFErrorr6   rD   r   rQ   
read_exactrC   r)   rG   
recv_framer   r   r   PROTOCOL_ERRORstrrT   r\   UnicodeDecodeErrorINVALID_DATAra   startr   set_current_sizeMESSAGE_TOO_BIG	ExceptionerrorINTERNAL_ERRORAssertionError)rU   r6   framerf   s       r-   rQ   zProtocol.parse/  s    6	"#{{11333zz))'2
 ##=>>==(#H]]*#}}H#}}t}}<H
 $);;KK**f,%#	$  ::KK%%fe4&= 3&  	"IIi..C9!DOO 	"IIi00#c(;!DOO! 	"IIi,,M#)).UV!DOO 	"  /IIi//S:!DOO 	"KKo=IIi../!DOO		" 	ABBs   KD) D$CD) $D'%A D) 'D) )	J020E("K(J040F*$K*J06A G;6K;J0AIKJ0#AJ+&K+J00Kc              #  $  K   | j                   t        u xs | j                  t        u | j                  k(  sJ | j
                  j                         E d{   s=| j
                  j                          | j
                  j                         E d{   s=| j                  r| j                  j                  d       | j                   t        u r"| j                  t        ur| j                          t        | _        d t        d      7 7 |w)z
        Discard incoming data.

        This coroutine replaces :meth:`parse`:

        - after receiving a close frame, during a normal closure (1.4);
        - after sending a close frame, during an abnormal closure (7.1.7).

        Nr   z"discard() shouldn't step after EOF)rC   r)   r5   r0   rM   rN   r   r   rB   r7   r*   r   r3   r   rY   s    r-   r   zProtocol.discardw  s      		V#?tzzZ'?T]]SS"kk00222KK! #kk00222::KKg& 994::Z#?MMO
 ABB 32s+   ADD;DDDA6DDc                   |j                   t        u s|j                   t        u r@| j                  t	        d      |j
                  st        |j                        | _        n|j                   t        u rV| j                  t	        d      |j
                  r	d| _        n| xj                  t        |j                        z  c_        n|j                   t        u r-t        t        |j                        }| j                  |       nM|j                   t        u rn9|j                   t        u rt        j                  |j                        | _        | j"                  t$        u r| j&                  J d| _        | j                  t	        d      | j"                  t*        u rL| j                  t        t        |j                               | j                   | _        d| _        t$        | _        | j,                  t.        u r| j1                          | j3                         | _        t7        | j4                         nt9        d|j                   d      | j:                  j=                  |       y)	z-
        Process an incoming frame.

        Nry   rr   Fzincomplete fragmented messageTzunexpected opcode: 02x)opcoder   r   rD   r   rv   lenrk   r   r   r   r   rt   r   r   rQ   rI   r5   r2   rJ   rK   r1   rC   r)   r   r   rR   rS   r   rO   append)rU   r   
pong_frames      r-   r   zProtocol.recv_frame  s   
 <<7"elli&?}}(#$CDD99 #EJJ\\W$}}$#$CDDyy $UZZ0\\W$ w

3JOOJ'\\W$ \\X% $kk%**5DOzzW$22,1)}}(#$CDD zzT!
 h

 ;<"&//,0)$
 yyF" ,,.DK !#6u||C6H!IJJ5!r,   c                    | j                   r| j                  j                  d|       | j                  j                  |j	                  | j
                  t        u | j                               y )Nz> %s)r   rG   )rB   r7   rP   r   r   rC   r*   rG   )rU   r   s     r-   rt   zProtocol.send_frame  sS    ::KKfe,OOYY&(??  	
r,   c                    | j                   rJ d| _         | j                  r| j                  j                  d       | j                  j	                  t
               y )NTz> EOF)rM   rB   r7   rP   r   r!   rY   s    r-   r   zProtocol.send_eof  s>    == ::KKg&8$r,   )
rC   r   r5   r    r6   
int | Noner7   zLoggerLike | NonereturnNone)r   r    )r5   r    r   r   )r   r   )r   z
str | None)r   r   )rk   bytesr   r   )r   r   )rk   r   rv   boolr   r   )T)Nr`   )r]   r   ra   r   r   r   )r`   )r]   intra   r   r   r   )r   zlist[Event])r   zlist[bytes])r   r   )r   zGenerator[None])r   r   r   r   )r$   r%   r&   r'   r1   rV   propertyr5   setterr^   rb   rg   rl   rp   rw   rz   r|   r   r   r   r   r   r   r   rQ   r   r   rt   r   r+   r,   r-   r   r   I   s   & $$(B1B1 	B1
 B1 "B1 
B1H   \\ 
 ( ($ * *$ ! !J *303.5."H. . -b ()ZFCPC<N"d
%r,   r   )2
__future__r   enumr<   r9   collections.abcr   typingr   
exceptionsr   r   r	   r
   r   r   rG   r   framesr   r   r   r   r   r   r   r   r   r   http11r   r   streamsr   r   r   r   __all__EventIntEnumr   r)   r*   r    r0   r1   r2   r3   r!   r   r+   r,   r-   <module>r      s    "    %   "   & ! 3 3 	gx&' ?4<<  
	1DLL 1 
zz
--	  Em
% m
%r,   