o
    Ð­joj  ã                   @  s6  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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#m$Z$ g d¢Z%eeB eB Z&	 G dd„ dej'ƒZ(e(j)Z)e(j*Z*G dd„ dej'ƒZ+e+j,Z,e+j-Z-e+j.Z.e+j/Z/dZ0	 G dd„ dƒZ1dS )é    )ÚannotationsN)Ú	Generatoré   )Ú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)Ú	BytesLikeÚ
LoggerLikeÚOriginÚSubprotocol)ÚProtocolÚSideÚStateÚSEND_EOFc                   @  s   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© r)   r)   úP/var/www/html/CropPilot/venv/lib/python3.10/site-packages/websockets/protocol.pyr   -   s    r   c                   @  s    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   7   s    r   ó    c                   @  s  e Zd ZdZedddœdPdd„ZedQdd„ƒZejdRdd„ƒZedSdd„ƒZ	edTdd„ƒZ
edUdd„ƒZdVdd „ZdWd!d"„ZdXd&d'„ZdYdXd)d*„ZdYdXd+d,„ZdZd[d2d3„Zd\d4d5„Zd\d6d7„Zd]d^d9d:„Zd_d<d=„Zd`d?d@„ZdadAdB„ZdbdDdE„ZdbdFdG„ZdcdJdK„ZdcdLdM„ZdWdNdO„ZdS )dr   a÷  
    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. You may pass a ``(max_message_size,
            max_fragment_size)`` tuple to set different limits for messages and
            fragments when you expect long messages sent in short fragments.
        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   r1   r   r2   ú*tuple[int | None, int | None] | int | Noner3   úLoggerLike | NoneÚreturnÚNonec                C  sê   t  ¡ | _	 |d u rt d|j ¡ › �¡}|| _	 | tj	¡| _
|| _|| _t|tƒs/|d u r7|d | _| _n|\| _| _d | _d| _d | _g | _d | _d | _d | _d | _d | _	 d| _tƒ | _g | _g | _|  ¡ | _ t!| j ƒ d | _"d S )Nzwebsockets.F)#ÚuuidÚuuid4ÚidÚloggingÚ	getLoggerÚnameÚlowerr3   ÚisEnabledForÚDEBUGÚdebugr4   r1   Ú
isinstanceÚintÚmax_message_sizeÚmax_fragment_sizeÚcurrent_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)Úselfr4   r1   r2   r3   r)   r)   r*   Ú__init__Y   s:   
	


zProtocol.__init__c                 C  s   | 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©rX   r)   r)   r*   r1       s   zProtocol.statec                 C  s    | j r| j  d|j¡ || _d S )Nz= connection is %s)rB   r3   r>   rZ   )rX   r1   r)   r)   r*   r1   ¯   s   
ú
int | Nonec                 C  s&   | j turdS | jdu r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)r1   r/   rL   r   ÚABNORMAL_CLOSUREÚcoder[   r)   r)   r*   Ú
close_codeµ   s
   

zProtocol.close_codeú
str | Nonec                 C  s$   | j turdS | jdu r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Ú )r1   r/   rL   Úreasonr[   r)   r)   r*   Úclose_reasonÈ   s
   

zProtocol.close_reasonr   c                 C  sf   | j tu s	J dƒ‚| jdur"| jdur"| 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 yetN)r1   r/   rL   rM   r^   r   r   r   rN   rW   Ú	__cause__)rX   Úexc_typeÚexcr)   r)   r*   Ú	close_excÛ   s   

ýzProtocol.close_excÚdataúbytes | bytearrayc                 C  s   | 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)rQ   Ú	feed_datarV   rU   ©rX   rh   r)   r)   r*   Úreceive_data  s   zProtocol.receive_datac                 C  s$   | 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)rQ   ÚeofÚfeed_eofrV   rU   r[   r)   r)   r*   Úreceive_eof  s   
zProtocol.receive_eofr   ÚfinÚboolc                 C  sL   | 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)rH   r
   rZ   r-   r   r1   r>   r?   Ú
send_framer   r   ©rX   rh   rp   r)   r)   r*   Úsend_continuation&  s   
zProtocol.send_continuationTc                 C  óL   | 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 framers   N)rH   r
   rZ   r-   r   r1   r>   r?   rt   r   r   ru   r)   r)   r*   Ú	send_text>  ó   
zProtocol.send_textc                 C  rw   )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.

        rx   rs   N)rH   r
   rZ   r-   r   r1   r>   r?   rt   r   r   ru   r)   r)   r*   Úsend_binaryU  rz   zProtocol.send_binaryra   r^   úCloseCode | int | Nonerb   Ústrc                 C  sŠ   | j turtd| jj ¡ › �ƒ‚|du r%|dkrtdƒ‚ttj	dƒ}d}n	t||ƒ}| 
¡ }|  tt|ƒ¡ | jdu s=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.

        rs   Nra   z#cannot send a reason without a coder0   )rZ   r-   r   r1   r>   r?   r
   r   r   ÚNO_STATUS_RCVDÚ	serializert   r   r   rL   rM   r.   ©rX   r^   rb   Úcloserh   r)   r)   r*   Ú
send_closel  s   


zProtocol.send_closec                 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.

        rs   N)
rZ   r-   r.   r   r1   r>   r?   rt   r   r   rk   r)   r)   r*   Ú	send_ping�  ó   zProtocol.send_pingc                 C  rƒ   )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)
rZ   r-   r.   r   r1   r>   r?   rt   r   r   rk   r)   r)   r*   Ú	send_pong   r…   zProtocol.send_pongúCloseCode | intc                 C  s‚   | j tu r)|tjkr)t||ƒ}| ¡ }|  tt|ƒ¡ || _	| j
dur&d| _t| _ | jtu r5| js5|  ¡  |  ¡ | _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)r1   r-   r   r]   r   r   rt   r   r   rM   rL   rN   r.   r4   r'   rP   Úsend_eofÚdiscardrU   rV   r€   r)   r)   r*   Úfail°  s   




zProtocol.failúlist[Event]c                 C  ó   | j g }| _ |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.
        )rR   )rX   rR   r)   r)   r*   Úevents_receivedá  s   zProtocol.events_receivedúlist[bytes]c                 C  rŒ   )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.

        )rS   )rX   rS   r)   r)   r*   Údata_to_sendñ  s   zProtocol.data_to_sendc                 C  s>   | 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)r1   r-   r.   r/   r,   rP   r[   r)   r)   r*   Úclose_expected  s   
"

zProtocol.close_expectedúGenerator[None]c              
   c  s  � za	 | j  ¡ E dH r| jr| j d¡ tdƒ‚d}| jdur.| jdu r(| j}n| j| j }| jdurA|du r;| j}nt|| jƒ}t	j
| j j| jtu || jd�E dH }| jr]| j d|¡ |  |¡ q ty€ } z|  tjt|ƒ¡ || _W Y d}~nŠd}~w ty� } z|  tjt|ƒ¡ || _W Y d}~nmd}~w ty¿ } z|  tj|j› d|j› �¡ || _W Y d}~nKd}~w tyâ } z| | j¡ |  tjt|ƒ¡ || _W Y d}~n(d}~w t�y } z| jjddd	� |  tj ¡ || _W Y d}~nd}~w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)Úmaskr2   rJ   z< %sz at position zparser failed)Úexc_infoz"parse() shouldn't step after error)"rQ   Úat_eofrB   r3   ÚEOFErrorrE   rG   rF   Úminr   rT   Ú
read_exactr4   r'   rJ   Ú
recv_framer
   rŠ   r   ÚPROTOCOL_ERRORr}   rW   r]   ÚUnicodeDecodeErrorÚINVALID_DATArb   Ústartr	   Úset_current_sizeÚMESSAGE_TOO_BIGÚ	ExceptionÚerrorÚINTERNAL_ERRORÚAssertionError)rX   r2   Úframerf   r)   r)   r*   rT   2  sf   €


ü
Û'€€€€€üzProtocol.parsec                 c  sŒ   � | j tu p
| jtu | jksJ ‚| j ¡ E dH s%| j ¡  | j ¡ E dH r| 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)r4   r'   r1   r,   rP   rQ   r•   r‰   rB   r3   r(   rˆ   r/   r£   r[   r)   r)   r*   r‰   �  s   €
ÿzProtocol.discardr¤   r   c                 C  sj  |j tu s
|j tu r| jdurtdƒ‚|jst|jƒ| _n�|j tu r=| jdu r+tdƒ‚|jr2d| _n{|  jt|jƒ7  _np|j t	u rNt
t|jƒ}|  |¡ n_|j tu rTnY|j tu r¤t |j¡| _| jtu ro| jduslJ ‚d| _| jdurxtdƒ‚| jtu r�|  t
t|jƒ¡ | j| _d| _t| _| jtu r™|  ¡  |  ¡ | _t| jƒ n	td|j d›�ƒ‚| j |¡ dS )	z-
        Process an incoming frame.

        Nrx   rr   Fzincomplete fragmented messageTzunexpected opcode: Ú02x)Úopcoder   r   rG   r
   rp   Úlenrh   r   r   r   r   rt   r   r   rT   rL   r1   r.   rM   rN   r-   r4   r'   rˆ   r‰   rU   rV   r£   rR   Úappend)rX   r¤   Ú
pong_framer)   r)   r*   r™   Ÿ  sF   
€









zProtocol.recv_framec                 C  s6   | j r
| j  d|¡ | j |j| jtu | jd�¡ d S )Nz> %s)r“   rJ   )rB   r3   rS   r¨   r   r4   r(   rJ   )rX   r¤   r)   r)   r*   rt   ñ  s   þÿzProtocol.send_framec                 C  s2   | j rJ ‚d| _ | jr| j d¡ | j t¡ d S )NTz> EOF)rP   rB   r3   rS   r¨   r    r[   r)   r)   r*   rˆ   û  s
   
zProtocol.send_eof)
r4   r   r1   r   r2   r5   r3   r6   r7   r8   )r7   r   )r1   r   r7   r8   )r7   r\   )r7   r`   )r7   r   )rh   ri   r7   r8   )r7   r8   )rh   r   rp   rq   r7   r8   )T)Nra   )r^   r|   rb   r}   r7   r8   )rh   r   r7   r8   )ra   )r^   r‡   rb   r}   r7   r8   )r7   r‹   )r7   rŽ   )r7   rq   )r7   r‘   )r¤   r   r7   r8   )r"   r#   r$   r%   r-   rY   Úpropertyr1   Úsetterr_   rc   rg   rl   ro   rv   ry   r{   r‚   r„   r†   rŠ   r�   r�   r�   rT   r‰   r™   rt   rˆ   r)   r)   r)   r*   r   G   sB    úG
%


$

1


-
O

R
r   )2Ú
__future__r   Úenumr<   r9   Úcollections.abcr   Ú
exceptionsr   r   r   r   r	   r
   rJ   r   Úframesr   r   r   r   r   r   r   r   r   r   Úhttp11r   r   Ústreamsr   Útypingr   r   r   r   Ú__all__ÚEventÚIntEnumr   r'   r(   r   r,   r-   r.   r/   r    r   r)   r)   r)   r*   Ú<module>   s2     0