
    i]                       d dl mZ d dlZd dlZd dlZd dl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 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-j0        Z0e-j1        Z1dZ2	  G d d          Z3dS )    )annotationsN)	GeneratorListOptionalType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     Y/var/www/html/crypto-bot/backend/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/   r0   r#   r#   7   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"   r8   r#   r9   Optional[int]r:   Optional[LoggerLike]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lowerr:   isEnabledForDEBUGdebugr;   r8   r9   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)selfr;   r8   r9   r:   s        r0   __init__zProtocol.__init__W   s    "Z\\B >&'HTY__5F5F'H'HIIF"() ((77
 	 
 ! (, */& )-+-26 ,0+/48! 37	  #nn#%#%jjllT[/3r/   c                    | j         S )zm
        WebSocket connection state.

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

        )_stater\   s    r0   r8   zProtocol.state   s     {r/   c                b    | j         r | j                             d|j                   || _        d S )Nz= connection is %s)rJ   r:   rF   r_   )r\   r8   s     r0   r8   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`_.

        .. _WebSocket close code:
            https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5

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

        N)r8   r6   rP   r   ABNORMAL_CLOSUREcoder`   s    r0   
close_codezProtocol.close_code   s2     :V##4_$--?''r/   Optional[str]c                L    | j         t          urdS | j        dS | j        j        S )z
        `WebSocket close reason`_.

        .. _WebSocket close reason:
            https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.6

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

        N )r8   r6   rP   reasonr`   s    r0   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)r8   r6   rP   rQ   rd   r   r   r   rR   r[   	__cause__)r\   exc_typeexcs      r0   	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)rU   	feed_datarZ   rY   r\   rp   s     r0   receive_datazProtocol.receive_data   s0     	d###T[r/   c                `    | 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.

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

        N)rU   feed_eofrZ   rY   r`   s    r0   receive_eofzProtocol.receive_eof  s.     	T[r/   finboolc                    | j         st          d          | | _         |                     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 frameN)rL   r   
send_framer   r   r\   rp   ry   s      r0   send_continuationzProtocol.send_continuation  sM    " - 	A ?@@@-0&gtS1122222r/   Tc                    | j         rt          d          | | _         |                     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 frameN)rL   r   r}   r   r   r~   s      r0   	send_textzProtocol.send_text/  sM      ) 	A ?@@@-0&gtS1122222r/   c                    | j         rt          d          | | _         |                     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   N)rL   r   r}   r   r   r~   s      r0   send_binaryzProtocol.send_binaryD  sM      ) 	A ?@@@-0&is3344444r/   rh   rd   ri   strc                X   | j         rt          d          |2|dk    rt          d          t          t          j        d          }d}n$t          ||          }|                                }|                     t          t          |                     || _	        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 a fragmented message is being sent, if the code
                isn't valid, or if a reason is provided without a code

        r   Nrh   z#cannot send a reason without a coder/   )rL   r   r   r   NO_STATUS_RCVD	serializer}   r   r   rQ   r5   r8   r\   rd   ri   closerp   s        r0   
send_closezProtocol.send_closeY  s      ) 	A ?@@@<||#$IJJJ)2B77EDD$''E??$$D 	h--...


r/   c                V    |                      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.

        N)r}   r   r   rt   s     r0   	send_pingzProtocol.send_pingy  &     	gt,,-----r/   c                V    |                      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.

        N)r}   r   r   rt   s     r0   	send_pongzProtocol.send_pong  r   r/   intc                   | j         t          u ro|t          j        k    r_t	          ||          }|                                }|                     t          t          |                     || _	        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.
        N)r8   r4   r   rc   r   r   r}   r   r   rQ   r5   r;   r,   rT   send_eofdiscardrY   rZ   r   s        r0   failzProtocol.fail  s    & :y111dF++((h 5 5666"'$
 9t}MMOOO llnnT[r/   List[Event]c                $    | j         g c}| _         |S )a7  
        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:
            List[Event]: Events read from the connection.
        )rV   )r\   rV   s     r0   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:
            List[bytes]: Data to write to the connection.

        )rW   )r\   rW   s     r0   data_to_sendzProtocol.data_to_send  s    " #k2r/   c                0    | j         t          u p| j        du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:
            bool: Whether the TCP connection is expected to close soon.

        N)r8   r5   rS   r`   s    r0   close_expectedzProtocol.close_expected  s    , zW$F(:$(FFr/   Generator[None, None, 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}~n0d}~wt          $ r>}|                     t           j        t%          |                     || _        Y d}~nd}~wt*          $ r@}|                     t           j        |j         d|j                    || _        Y d}~nd}~wt2          $ r>}|                     t           j        t%          |                     || _        Y d}~n\d}~wt6          $ 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)maskr9   rN   z< %sz at position zparser failed)exc_infoz"parse() shouldn't step after error)rU   at_eofrJ   r:   EOFErrorr9   rK   r   rX   
read_exactr;   r,   rN   
recv_framer   r   r   PROTOCOL_ERRORr   r[   rc   UnicodeDecodeErrorINVALID_DATAri   startr   MESSAGE_TOO_BIG	ExceptionerrorINTERNAL_ERRORAssertionError)r\   r9   framern   s       r0   rX   zProtocol.parse  s     5	"'#{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BsI   C)C- -
I74D11I>4E77I6F??I4HIAIIc              #    K   | 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| 	                                 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)r;   r,   rT   rU   r   r   rJ   r:   r-   r   r6   r8   r   r`   s    r0   r   zProtocol.discardC  s       	V#7777"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MMOOO
 ABBBr/   r   r   c                   |j         t          u s|j         t          u rA| j        t	          d          |j        r	d| _        nt          |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   r|   Fzincomplete fragmented messageTzunexpected opcode: 02x)opcoder   r   rK   r   ry   lenrp   r   r   r   r   r}   r   r   rX   rP   r8   r5   rQ   rR   r4   r;   r,   r   r   rY   rZ   r   rV   append)r\   r   
pong_frames      r0   r   zProtocol.recv_framea  s   
 <7""eli&?&?}(#$CDDDy 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         t          urt          d| j         j         d          | j        r| j                            d|           | j                            |                    | j	        t          u | j                             d S )Nz#cannot write to a WebSocket in the z statez> %s)r   rN   )r8   r4   r   rF   rJ   r:   rW   r   r   r;   r-   rN   )r\   r   s     r0   r}   zProtocol.send_frame  s    :T!!MdjoMMM   : 	-Kfe,,,OOf!4OQQ	
 	
 	
 	
 	
r/   c                    | j         rJ d| _         | j        r| j                            d           | j                            t
                     d S )NTz> EOF)rT   rJ   r:   rW   r   r$   r`   s    r0   r   zProtocol.send_eof  sS    =   : 	'Kg&&&8$$$$$r/   )
r;   r"   r8   r#   r9   r<   r:   r=   r>   r?   )r>   r#   )r8   r#   r>   r?   )r>   r<   )r>   rf   )r>   r
   )rp   rq   r>   r?   )r>   r?   )rp   rq   ry   rz   r>   r?   )T)Nrh   )rd   r<   ri   r   r>   r?   )rh   )rd   r   ri   r   r>   r?   )r>   r   )r>   r   )r>   rz   )r>   r   )r   r   r>   r?   )r'   r(   r)   r*   r4   r]   propertyr8   setterre   rj   ro   ru   rx   r   r   r   r   r   r   r   r   r   r   rX   r   r   r}   r   r.   r/   r0   r!   r!   G   sq        & "''+B4 B4 B4 B4 B4 B4H    X \   \
 ( ( ( X(" * * * X*" ! ! ! X!J       (3 3 3 3,3 3 3 3 3*5 5 5 5 5*    @. . . .. . . .' ' ' ' 'V       (G G G G4EC EC EC ECNC C C C<N" N" N" N"d

 

 

 

% % % % % %r/   r!   )4
__future__r   enumrD   rA   typingr   r   r   r   r   
exceptionsr
   r   r   r   r   r   rN   r   framesr   r   r   r   r   r   r   r   r   r   http11r   r   streamsr   r   r   r    __all__EventIntEnumr"   r,   r-   r#   r3   r4   r5   r6   r$   r!   r.   r/   r0   <module>r      sv   " " " " " "    9 9 9 9 9 9 9 9 9 9 9 9 9 9                " ! ! ! ! !                        & % % % % % % % ! ! ! ! ! ! 3 3 3 3 3 3 3 3 3 3   	gx&' ?    4<    
	1 1 1 1 1DL 1 1 1 
z
-	  E}	% }	% }	% }	% }	% }	% }	% }	% }	% }	%r/   