HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberCP/lib/python3.10/site-packages/websockets/__pycache__/protocol.cpython-310.pyc
o

�h�g�@s@ddlmZddlZddlZddlZddlmZddlmZddl	m
Z
mZmZm
Z
mZmZddlmZddlmZmZmZmZmZmZmZmZmZmZdd	lmZmZdd
l m!Z!ddlm"Z"m#Z#m$Z$gd�Z%eeeefZ&	Gd
d�dej'�Z(e(j)Z)e(j*Z*Gdd�dej'�Z+e+j,Z,e+j-Z-e+j.Z.e+j/Z/dZ0	Gdd�d�Z1dS)�)�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@seZdZdZed�\ZZdS)rz6A WebSocket connection is either a server or a client.�N)�__name__�
__module__�__qualname__�__doc__�range�SERVER�CLIENT�r)r)�F/usr/local/CyberCP/lib/python3.10/site-packages/websockets/protocol.pyr/src@s eZdZdZed�\ZZZZdS)rz6A WebSocket connection is in one of these four states.�N)	r"r#r$r%r&�
CONNECTING�OPEN�CLOSING�CLOSEDr)r)r)r*r9sr�c@seZdZdZeddd�dMdd�ZedNdd��ZejdOdd��ZedPdd��Z	edQdd��Z
edRdd��ZdSdd�ZdTd d!�Z
dUd$d%�ZdVdUd'd(�ZdVdUd)d*�ZdWdXd/d0�ZdSd1d2�ZdSd3d4�ZdYdZd6d7�Zd[d9d:�Zd\d<d=�Zd]d>d?�Zd^dAdB�Zd^dCdD�Zd_dGdH�Zd_dIdJ�ZdTdKdL�ZdS)`ra6
    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.

    iN)�state�max_size�logger�siderr1rr2�
int | Noner3�LoggerLike | None�return�NonecCs�t��|_	|durt�d|j�����}||_	|�tj	�|_
||_||_||_
d|_d|_d|_g|_d|_d|_d|_d|_d|_	d|_t�|_g|_g|_|��|_t|j�d|_dS)Nzwebsockets.F) �uuid�uuid4�id�logging�	getLogger�name�lowerr3�isEnabledFor�DEBUG�debugr4r1r2�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)�selfr4r1r2r3r)r)r*�__init__Ys6
	


zProtocol.__init__cCs|jS)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�rTr)r)r*r1�s
zProtocol.statecCs |jr|j�d|j�||_dS)Nz= connection is %s)rBr3r>rV)rTr1r)r)r*r1�s
cCs&|jturdS|jdurtjS|jjS)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)r1r/rHr�ABNORMAL_CLOSURE�coderWr)r)r*�
close_code�s


zProtocol.close_code�
str | NonecCs$|jturdS|jdurdS|jjS)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�)r1r/rH�reasonrWr)r)r*�close_reason�s


zProtocol.close_reasonrcCsf|jtus	Jd��|jdur"|jdur"|jjtvr"|jjtvr"t}nt}||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.

        zconnection isn't closed yetN)r1r/rHrIrYr
rrrJrS�	__cause__)rT�exc_type�excr)r)r*�	close_exc�s

�zProtocol.close_exc�data�bytescCs|j�|�t|j�dS)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)rM�	feed_datarRrQ�rTrcr)r)r*�receive_data�s
zProtocol.receive_datacCs$|jjrdS|j��t|j�dS)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)rM�eof�feed_eofrRrQrWr)r)r*�receive_eofs
zProtocol.receive_eof�fin�boolcCsL|jstd��|jturtd|jj������||_|�t	t
||��dS)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)rDrrVr-r	r1r>r?�
send_framerr�rTrcrkr)r)r*�send_continuation#s
zProtocol.send_continuationTcC�L|jrtd��|jturtd|jj������||_|�t	t
||��dS)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 framernN)rDrrVr-r	r1r>r?rorrrpr)r)r*�	send_text;�
zProtocol.send_textcCrr)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.

        rsrnN)rDrrVr-r	r1r>r?rorrrpr)r)r*�send_binaryRruzProtocol.send_binaryr\rYr]�strcCs�|jturtd|jj������|dur%|dkrtd��ttj	d�}d}n	t||�}|�
�}|�tt
|��|jdus=J�||_t|_dS)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.

        rnNr\z#cannot send a reason without a coder0)rVr-r	r1r>r?rrr�NO_STATUS_RCVD�	serializerorrrHrIr.�rTrYr]�closercr)r)r*�
send_closeis


zProtocol.send_closecC�>|jtur|jturtd|jj������|�tt	|��dS)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.

        rnN)
rVr-r.r	r1r>r?rorrrfr)r)r*�	send_ping��zProtocol.send_pingcCr})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.

        rnN)
rVr-r.r	r1r>r?rorrrfr)r)r*�	send_pong�rzProtocol.send_pong�intcCs�|jtur)|tjkr)t||�}|��}|�tt|��||_	|j
dur&d|_t|_|j
tur5|js5|��|��|_t|j�dS)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)r1r-rrXrryrorrrIrHrJr.r4r'rL�send_eof�discardrQrRrzr)r)r*�fail�s




z
Protocol.fail�list[Event]cC�|jg}|_|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.
        )rN)rTrNr)r)r*�events_received�szProtocol.events_received�list[bytes]cCr�)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.

        )rO)rTrOr)r)r*�data_to_send�szProtocol.data_to_sendcCs>|jturdS|jturdS|jturdS|jtusJ�|jS)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)r1r-r.r/r,rLrWr)r)r*�close_expecteds
"

zProtocol.close_expected�Generator[None]c
cs��zO	|j��EdHr|jr|j�d�td��|jdur d}n|jdur)|j}n|j|j}tj|jj	|j
tu||jd�EdH}|jrK|j�d|�|�
|�qtyn}z|�tjt|��||_WYd}~n�d}~wty�}z|�tjt|��||_WYd}~nld}~wty�}z|�tj|j�d|j���||_WYd}~nJd}~wty�}z|�|j�|�tjt|��||_WYd}~n'd}~wty�}z|jjddd	�|�tj�||_WYd}~nd}~wwdVtd
��)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�< EOFzunexpected end of stream)�maskr2rFz< %sz
 at position z
parser failed)�exc_infoz"parse() shouldn't step after error) rM�at_eofrBr3�EOFErrorr2rCrrP�
read_exactr4r'rF�
recv_framerr�r�PROTOCOL_ERRORrwrSrX�UnicodeDecodeError�INVALID_DATAr]�startr
�set_current_size�MESSAGE_TOO_BIG�	Exception�error�INTERNAL_ERROR�AssertionError)rTr2�framerar)r)r*rP/s^�

�
� ������zProtocol.parseccs��|jtup
|jtu|jksJ�|j��EdHs%|j��|j��EdHr|jr.|j	�d�|jt
ur<|jtur<|��t|_dVt
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)r4r'r1r,rLrMr�r�rBr3r(r�r/r�rWr)r)r*r�ws�
�zProtocol.discardr�rcCsj|jtus
|jtur|jdurtd��|jst|j�|_n�|jtur=|jdur+td��|jr2d|_n{|jt|j�7_np|jt	urNt
t|j�}|�|�n_|jturTnY|jt
ur�t�|j�|_|jturo|jduslJ�d|_|jdurxtd��|jtur�|�t
t
|j��|j|_d|_t|_|jtur�|��|��|_t|j�n	td|jd����|j�|�dS)	z-
        Process an incoming frame.

        NrsrmFzincomplete fragmented messageTzunexpected opcode: �02x)�opcoderrrCrrk�lenrcrrrrrorrrPrHr1r.rIrJr-r4r'r�r�rQrRr�rN�append)rTr��
pong_framer)r)r*r��sF
�









zProtocol.recv_framecCs6|jr
|j�d|�|j�|j|jtu|jd��dS)Nz> %s)r�rF)rBr3rOr�ryr4r(rF)rTr�r)r)r*ro�s��zProtocol.send_framecCs2|jrJ�d|_|jr|j�d�|j�t�dS)NTz> EOF)rLrBr3rOr�r rWr)r)r*r��s

zProtocol.send_eof)
r4rr1rr2r5r3r6r7r8)r7r)r1rr7r8)r7r5)r7r[)r7r)rcrdr7r8)r7r8)rcrdrkrlr7r8)T)Nr\)rYr5r]rwr7r8)r\)rYr�r]rwr7r8)r7r�)r7r�)r7rl)r7r�)r�rr7r8)r"r#r$r%r-rU�propertyr1�setterrZr^rbrgrjrqrtrvr|r~r�r�r�r�r�rPr�r�ror�r)r)r)r*rIsB�D
%


$

1


-
H

R
r)2�
__future__r�enumr<r9�collections.abcr�typingr�
exceptionsrrrr	r
rrFr�framesr
rrrrrrrrr�http11rr�streamsrrrr�__all__�Event�IntEnumrr'r(rr,r-r.r/r rr)r)r)r*�<module>s4 0