
    vgg                       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j'                  Z(e(j)        Z)e(j*        Z* G d dej'                  Z+e+j,        Z,e+j-        Z-e+j.        Z.e+j/        Z/dZ0	  G d d          Z1dS )    )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dS )r   z6A WebSocket connection is either a server or a client.   N)__name__
__module____qualname____doc__rangeSERVERCLIENT     P/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/websockets/protocol.pyr   r   /   s%        @@U1XXNFFFFr,   r   c                  2    e Zd ZdZ ed          \  ZZZZdS )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gvvvr,   r    r,   c                  6   e Zd ZdZedddd7dZed8d            Zej        d9d            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dAdBd%Zd=d&Zd=d'ZdCdDd)ZdEd+ZdFd-ZdGd.ZdHd0ZdHd1ZdId4ZdId5Zd>d6ZdS )Jr   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loggersider   r5   r    r6   
int | Noner7   LoggerLike | NonereturnNonec               R   t          j                    | _        	 |.t          j        d|j                                                   }|| _        	 |                    t          j	                  | _
        || _        || _        || _        d | _        d| _        d | _        g | _        d | _        d | _        d | _        d | _        d | _        	 d| _        t1                      | _        g | _        g | _        |                                 | _        t=          | j                   d | _        d S )Nzwebsockets.F) uuiduuid4idlogging	getLoggernamelowerr7   isEnabledForDEBUGdebugr8   r5   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)selfr8   r5   r6   r7   s        r-   __init__zProtocol.__init__Y   s    "Z\\B >&'HTY__5F5F'H'HIIF"() ((77
 	 
 ! %) */& &*+-/3 )-(,15! 04	  #nn#%#%jjll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rY   s    r-   r5   zProtocol.state   s     {r,   c                b    | j         r | j                             d|j                   || _        d S )Nz= connection is %s)rG   r7   rC   r\   )rY   r5   s     r-   r5   zProtocol.state   s3    : 	@K2EJ???r,   c                `    | j         t          urdS | 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   rM   r   ABNORMAL_CLOSUREcoder]   s    r-   
close_codezProtocol.close_code   s2     :V##4_$--?''r,   
str | Nonec                L    | j         t          urdS | j        dS | 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   rM   reasonr]   s    r-   close_reasonzProtocol.close_reason   s.     :V##4_$2?))r,   r   c                   | j         t          u s
J d            | j        5| 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   rM   rN   ra   r   r	   r   rO   rX   	__cause__)rY   exc_typeexcs      r-   	close_exczProtocol.close_exc   s      zV###%B### O'+$66$66)HH,H (OO%!
 !
 
r,   databytesc                b    | j                             |           t          | j                   dS )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)rR   	feed_datarW   rV   rY   rm   s     r-   receive_datazProtocol.receive_data   s0     	d###T[r,   c                |    | j         j        rdS | j                                          t          | j                   dS )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)rR   eoffeed_eofrW   rV   r]   s    r-   receive_eofzProtocol.receive_eof  s?     ;? 	FT[r,   finboolc                   | j         st          d          | j        t          ur.t	          d| j        j                                                   | | _         |                     t          t          ||                     dS )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)rI   r   r\   r1   r
   r5   rC   rD   
send_framer   r   rY   rm   rw   s      r-   send_continuationzProtocol.send_continuation#  s    " - 	A ?@@@;d""I
0E0E0G0GIIJJJ-0&gtS1122222r,   Tc                   | j         rt          d          | j        t          ur.t	          d| j        j                                                   | | _         |                     t          t          ||                     dS )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 framer{   N)rI   r   r\   r1   r
   r5   rC   rD   r|   r   r   r}   s      r-   	send_textzProtocol.send_text;  s      ) 	A ?@@@;d""I
0E0E0G0GIIJJJ-0&gtS1122222r,   c                   | j         rt          d          | j        t          ur.t	          d| j        j                                                   | | _         |                     t          t          ||                     dS )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.

        r   r{   N)rI   r   r\   r1   r
   r5   rC   rD   r|   r   r   r}   s      r-   send_binaryzProtocol.send_binaryR  s      ) 	A ?@@@;d""I
0E0E0G0GIIJJJ-0&is3344444r,   re   ra   rf   strc                   | j         t          ur.t          d| j        j                                                   |2|dk    rt          d          t          t          j	        d          }d}n$t          ||          }|
                                }|                     t          t          |                     | j        J || _        t           | _        dS )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.

        r{   Nre   z#cannot send a reason without a coder,   )r\   r1   r
   r5   rC   rD   r   r   r   NO_STATUS_RCVD	serializer|   r   r   rM   rN   r2   rY   ra   rf   closerm   s        r-   
send_closezProtocol.send_closei  s    $ ;d""I
0E0E0G0GIIJJJ<||#$IJJJ)2B77EDD$''E??$$Dh--... &&&


r,   c                    | j         t          ur<| j         t          ur.t          d| j        j                                                   |                     t          t          |                     dS )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.

        r{   N)
r\   r1   r2   r
   r5   rC   rD   r|   r   r   rq   s     r-   	send_pingzProtocol.send_ping  f     ;d""t{''A'AI
0E0E0G0GIIJJJgt,,-----r,   c                    | j         t          ur<| j         t          ur.t          d| j        j                                                   |                     t          t          |                     dS )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.

        r{   N)
r\   r1   r2   r
   r5   rC   rD   r|   r   r   rq   s     r-   	send_pongzProtocol.send_pong  r   r,   intc                   | j         t          u r}|t          j        k    rmt	          ||          }|                                }|                     t          t          |                     || _	        | j
        d| _        t          | _         | j        t          u r| j        s|                                  |                                 | _        t'          | j                   dS )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   r|   r   r   rN   rM   rO   r2   r8   r)   rQ   send_eofdiscardrV   rW   r   s        r-   failzProtocol.fail  s    & :y111dF++((h 5 5666"'
 ?.04D-$
 9t}MMOOO llnnT[r,   list[Event]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.
        )rS   )rY   rS   s     r-   events_receivedzProtocol.events_received  s     #k2r,   list[bytes]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.

        )rT   )rY   rT   s     r-   data_to_sendzProtocol.data_to_send  s    " #k2r,   c                    | j         t          u rdS | j         t          u rdS | j         t          u rdS | 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   rQ   r]   s    r-   close_expectedzProtocol.close_expected  sV    D :5:  4:5zZ''''}r,   Generator[None]c              #    K   	 	 | j                                         E d{V r0| j        r| j                            d           t	          d          | j        d}n| j        | j        }n| j        | j        z
  }t          j        | j         j	        | j
        t          u || j                  E d{V }| j        r| j                            d|           |                     |           # t          $ r?}|                     t           j        t%          |                     || _        Y d}~nKd}~wt          $ r?}|                     t           j        t%          |                     || _        Y d}~nd}~wt*          $ r@}|                     t           j        |j         d|j                    || _        Y d}~nd}~wt2          $ rX}|                    | j                   |                     t           j        t%          |                     || _        Y d}~n\d}~wt8          $ rL}| j                            dd	           |                     t           j                   || _        Y d}~nd}~ww xY wdV  t?          d
          )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   rK   z< %sz at position zparser failed)exc_infoz"parse() shouldn't step after error) rR   at_eofrG   r7   EOFErrorr6   rH   r   rU   
read_exactr8   r)   rK   
recv_framer   r   r   PROTOCOL_ERRORr   rX   r`   UnicodeDecodeErrorINVALID_DATArf   startr   set_current_sizeMESSAGE_TOO_BIG	ExceptionerrorINTERNAL_ERRORAssertionError)rY   r6   framerk   s       r-   rU   zProtocol.parse/  s     6	"'#{1133333333 ?z 3))'222
 ##=>>>=(#HH]*#}HH#}t}<H
 $);K*f,%#	$ $ $       : 5K%%fe444&&&='@  	" 	" 	"IIi.C999!DOOOOOO 	" 	" 	"IIi0#c((;;;!DOOOOOO! 	" 	" 	"IIi,.U.U#).U.UVVV!DOOOOOO 	" 	" 	"  ///IIi/S:::!DOOOOOO 	" 	" 	"Ko===IIi.///!DOOOOOO		" 	ABBBsJ   C)C- -
I974D11I9>4E88I96G  I9AH  I9-AI44I9c              #    K   | j         t          u p| j        t          u | j        k    sJ | j                                        E d{V s8| j                                         | j                                        E d{V 8| j        r| j	                            d           | j         t          u r"| j        t          ur|                                  t          | _        dV  t          d          )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)r8   r)   r5   r0   rQ   rR   r   r   rG   r7   r*   r   r3   r   r]   s    r-   r   zProtocol.discardw  s       	V#?tzZ'?T]SSSS"k0022222222 	"K!!! #k0022222222 	": 	'Kg&&& 94:Z#?#?MMOOO
 ABBBr,   r   r   c                t   |j         t          u s|j         t          u r8| j        t	          d          |j        st          |j                  | _        n|j         t          u rJ| j        t	          d          |j        r	d| _        n| xj        t          |j                  z  c_        nq|j         t          u r1t          t          |j                  }|                     |           n2|j         t          u rn"|j         t          u rt          j        |j                  | _        | j        t$          u r| j        J d| _        | j        t	          d          | j        t*          u rL|                     t          t          |j                             | j        | _        d| _        t$          | _        | j        t.          u r|                                  |                                 | _        t7          | j                   nt9          d|j         d          | j                            |           dS )	z-
        Process an incoming frame.

        Nr   rz   Fzincomplete fragmented messageTzunexpected opcode: 02x)opcoder   r   rH   r   rw   lenrm   r   r   r   r   r|   r   r   rU   rM   r5   r2   rN   rO   r1   r8   r)   r   r   rV   rW   r   rS   append)rY   r   
pong_frames      r-   r   zProtocol.recv_frame  s   
 <7""eli&?&?}(#$CDDD9 0 #EJ\W$$}$#$CDDDy 1 $UZ0\W$$ w
33JOOJ''''\W$$ \X%% $k%*55DOzW$$222,1)}(#$CDDD zT!!
 h
 ; ;<<<"&/,0)$
 yF"" ,,..DK !!Iu|!I!I!IJJJ5!!!!!r,   c                    | j         r| j                             d|           | j                            |                    | j        t          u | j                             d S )Nz> %s)r   rK   )rG   r7   rT   r   r   r8   r*   rK   )rY   r   s     r-   r|   zProtocol.send_frame  sm    : 	-Kfe,,,OOY&(?   	
 	
 	
 	
 	
r,   c                    | j         rJ d| _         | j        r| j                            d           | j                            t
                     d S )NTz> EOF)rQ   rG   r7   rT   r   r!   r]   s    r-   r   zProtocol.send_eof  sS    =   : 	'Kg&&&8$$$$$r,   )
r8   r   r5   r    r6   r9   r7   r:   r;   r<   )r;   r    )r5   r    r;   r<   )r;   r9   )r;   rc   )r;   r   )rm   rn   r;   r<   )r;   r<   )rm   rn   rw   rx   r;   r<   )T)Nre   )ra   r9   rf   r   r;   r<   )re   )ra   r   rf   r   r;   r<   )r;   r   )r;   r   )r;   rx   )r;   r   )r   r   r;   r<   )r$   r%   r&   r'   r1   rZ   propertyr5   setterrb   rg   rl   rr   rv   r~   r   r   r   r   r   r   r   r   r   rU   r   r   r|   r   r+   r,   r-   r   r   I   sn        & $$(B1 B1 B1 B1 B1 B1H    X \   \
 ( ( ( X($ * * * X*$ ! ! ! X!J       *3 3 3 303 3 3 3 3.5 5 5 5 5." " " " "H. . . . . . . . - - - - -b       () ) ) )ZFC FC FC FCPC C C C<N" N" N" N"d
 
 
 
% % % % % %r,   r   )2
__future__r   enumrA   r>   collections.abcr   typingr   
exceptionsr   r   r	   r
   r   r   rK   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      sp   " " " " " "    % % % % % %                      " ! ! ! ! !                        & % % % % % % % ! ! ! ! ! ! 3 3 3 3 3 3 3 3 3 3   	gx&' ?    4<    
	1 1 1 1 1DL 1 1 1 
z
-	  Em
% m
% m
% m
% m
% m
% m
% m
% m
% m
%r,   