
    &thq\                       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 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,j0        Z0dZ1	  G d d          Z2d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Frame)RequestResponse)StreamReader)
LoggerLikeOriginSubprotocol)
Connection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     i/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/websockets/connection.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"   6   s*        @@(-a%Jgvvvr.   r"   r.   c                  4   e Zd ZdZeddfd6dZed7d            Zej        d8d            Zed9d            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@dAd$Zd<d%Zd<d&ZdBdCd(ZdDd*ZdEd,ZdFd-ZdGd/ZdGd0ZdHd3ZdHd4Zd=d5ZdS )Ir    a5  
    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` to disable 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sider!   stater"   max_sizeOptional[int]logger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debugr7   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)selfr7   r8   r9   r;   s        r/   __init__zConnection.__init__V   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    r/   r8   zConnection.state   s     {r.   c                b    | j         r | j                             d|j                   || _        d S )Nz= connection is %s)rI   r;   rE   r^   )r[   r8   s     r/   r8   zConnection.state   s3    : 	@K2EJ???r.   c                L    | j         t          urdS | j        d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   r5   rO   coder_   s    r/   
close_codezConnection.close_code   s.     :V##4_$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   r5   rO   reasonr_   s    r/   close_reasonzConnection.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.connection.State.CLOSING`; wait until
        it's :attr:`~websockets.connection.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   r5   rO   rP   rc   r   r   r   rQ   rZ   	__cause__)r[   exc_typeexcs      r/   	close_exczConnection.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)rT   	feed_datarY   rX   r[   ro   s     r/   receive_datazConnection.receive_data   s0     	d###T[r.   c                `    | j                                          t          | j                   dS )aw  
        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.
        - 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)rT   feed_eofrY   rX   r_   s    r/   receive_eofzConnection.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)rK   r   
send_framer   r   r[   ro   rx   s      r/   send_continuationzConnection.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)rK   r   r|   r   r   r}   s      r/   	send_textzConnection.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)rK   r   r|   r   r   r}   s      r/   send_binaryzConnection.send_binaryA  sM      ) 	A ?@@@-0&is3344444r.   rg   rc   rh   strc                D   | j         rt          d          |(|dk    rt          d          t          d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   Nrg   z#cannot send a reason without a codei  r.   )
rK   r   r   	serializer|   r   r   rP   r4   r8   r[   rc   rh   closero   s        r/   
send_closezConnection.send_closeV  s      ) 	A ?@@@<||#$IJJJ$OOE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   rs   s     r/   	send_pingzConnection.send_pingv  &     	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   rs   s     r/   	send_pongzConnection.send_pong  r   r.   intc                   | j         t          u re|d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.
        rb   N)r8   r3   r   r   r|   r   r   rP   r4   r7   r+   rS   send_eofdiscardrX   rY   r   s        r/   failzConnection.fail  s    & :t||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.
        )rU   )r[   rU   s     r/   events_receivedzConnection.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.connection.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.

        )rV   )r[   rV   s     r/   data_to_sendzConnection.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_*()`` 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   r4   rR   r_   s    r/   close_expectedzConnection.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          $ r5}|                     dt!          |                     || _        Y d}~nd}~wt          $ r4}|                     dt!          |                     || _        Y d}~nd}~wt$          $ r6}|                     d	|j         d
|j                    || _        Y d}~nd}~wt*          $ r4}|                     dt!          |                     || _        Y d}~nRd}~wt,          $ rB}| j                            dd           |                     d           || _        Y d}~nd}~ww xY wdV  t1          d          )z
        Parse incoming data into frames.

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

        TN< EOFzunexpected end of stream)maskr9   rM   z< %si  rb   i  z at position i  zparser failed)exc_infoi  z"parse() shouldn't step after error)rT   at_eofrI   r;   EOFErrorr9   rJ   r   rW   
read_exactr7   r+   rM   
recv_framer   r   r   rZ   UnicodeDecodeErrorrh   startr   	ExceptionerrorAssertionError)r[   r9   framerm   s       r/   rW   zConnection.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&&&='@  	" 	" 	"IIdCHH%%%!DOOOOOO 	" 	" 	"IIdCHH%%%!DOOOOOO! 	" 	" 	"IIdszCC	CCDDD!DOOOOOO 	" 	" 	"IIdCHH%%%!DOOOOOO 	" 	" 	"Ko===IIdOOO!DOOOOOO		" 	ABBBsH   C)C- -
H,7*D''H,4*E##H,0,F!!H,.*GH,*8H''H,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)r7   r+   rS   rT   r   r   rI   r;   r,   r   r5   r8   r   r_   s    r/   r   zConnection.discard=  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   rJ   r   rx   lenro   r   r   r   r   r|   r   r   rW   rO   r8   r4   rP   rQ   r3   r7   r+   r   r   rX   rY   r   rU   append)r[   r   
pong_frames      r/   r   zConnection.recv_frame[  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   rM   )r8   r3   r   rE   rI   r;   rV   r   r   r7   r,   rM   )r[   r   s     r/   r|   zConnection.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)rS   rI   r;   rV   r   r#   r_   s    r/   r   zConnection.send_eof  sS    =   : 	'Kg&&&8$$$$$r.   )
r7   r!   r8   r"   r9   r:   r;   r<   r=   r>   )r=   r"   )r8   r"   r=   r>   )r=   r:   )r=   re   )r=   r
   )ro   rp   r=   r>   )r=   r>   )ro   rp   rx   ry   r=   r>   )T)Nrg   )rc   r:   rh   r   r=   r>   )rg   )rc   r   rh   r   r=   r>   )r=   r   )r=   r   )r=   ry   )r=   r   )r   r   r=   r>   )r&   r'   r(   r)   r3   r\   propertyr8   setterrd   ri   rn   rt   rw   r~   r   r   r   r   r   r   r   r   r   rW   r   r   r|   r   r-   r.   r/   r    r    F   sk        $ "''+A4 A4 A4 A4 A4F    X \   \
 ( ( ( X(" * * * X*" ! ! ! X!J       &3 3 3 3,3 3 3 3 3*5 5 5 5 5*    @. . . .. . . .' ' ' ' 'V       (G G G G4BC BC BC BCHC C C C<N" N" N" N"d

 

 

 

% % % % % %r.   r    )3
__future__r   enumrC   r@   typingr   r   r   r   r   
exceptionsr
   r   r   r   r   r   rM   r   framesr   r   r   r   r   r   r   r   r   http11r   r   streamsr   r   r   r   __all__EventIntEnumr!   r+   r,   r"   r2   r3   r4   r5   r#   r    r-   r.   r/   <module>r      sl   " " " " " "    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&' A    4<    
	1 1 1 1 1DL 1 1 1 
z
-	  Ex	% x	% x	% x	% x	% x	% x	% x	% x	% x	%r.   