� <��g�g���ddlmZddlZddlZddlZddlmZddlmZddl m Z m Z m Z m Z mZmZddlmZddlmZmZmZmZmZmZmZmZmZmZdd lmZmZdd l m!Z!dd lm"Z"m#Z#m$Z$gd �Z%eeeefZ& Gd �dej'��Z(e(j)Z)e(j*Z*Gd�dej'��Z+e+j,Z,e+j-Z-e+j.Z.e+j/Z/dZ0 Gd�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�.�eZdZdZed��\ZZdS)rz6A WebSocket connection is either a server or a client.�N)�__name__� __module__� __qualname__�__doc__�range�SERVER�CLIENT���c/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/websockets/protocol.pyrr/s%������@�@��U�1�X�X�N�F�F�F�Fr,rc�2�eZdZdZed��\ZZZZdS)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 9s*������@�@�(-��a���%�J��g�v�v�vr,r r,c�6�eZdZdZeddd�d7d�Zed8d���Zejd9d���Zed:d���Z ed;d���Z ed<d���Z d=d�Z d>d�Z d?d�Zd@d?d�Zd@d?d �ZdAdBd%�Zd=d&�Zd=d'�ZdCdDd)�ZdEd+�ZdFd-�ZdGd.�ZdHd0�ZdHd1�ZdId4�ZdId5�Zd>d6�ZdS)Jra6 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�siderr5r r6� int | Noner7�LoggerLike | None�return�Nonec�R�tj��|_ |�.tjd|j�������}||_ |�tj ��|_ ||_ ||_ ||_ d|_d|_d|_g|_d|_d|_d|_d|_d|_ d|_t1��|_g|_g|_|���|_t=|j��d|_dS)Nz websockets.F) �uuid�uuid4�id�logging� getLogger�name�lowerr7� isEnabledFor�DEBUG�debugr8r5r6�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)�selfr8r5r6r7s r-�__init__zProtocol.__init__Ys��"�Z�\�\���B� �>��&�'H�T�Y�_�_�5F�5F�'H�'H�I�I�F�"(�� �)��(�(���7�7�� ��� ��� �!�� �%)�� �*/��&�&*�� �+-���/3���)-���(,���15��!�04��� ��� �#�n�n�� �#%�� �#%�� ��j�j�l�l�� � �T�[����,0����r,c��|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�rYs r-r5zProtocol.state�s ���{�r,c�b�|jr |j�d|j��||_dS)Nz= connection is %s)rGr7rCr\)rYr5s r-r5zProtocol.state�s3�� �:� @� �K� � �2�E�J� ?� ?� ?��� � � r,c�`�|jturdS|j� tjS|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)r5r3rMr�ABNORMAL_CLOSURE�coder]s r-� close_codezProtocol.close_code�s2�� �:�V� #� #��4� �_� $��-� -��?�'� 'r,� str | Nonec�L�|jturdS|j�dS|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�)r5r3rM�reasonr]s r-� close_reasonzProtocol.close_reason�s.�� �:�V� #� #��4� �_� $��2��?�)� )r,rc� �|jtus Jd���|j�5|j�.|jjt vr|jjt vrt }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 yet) r5r3rMrNrarr rrOrX� __cause__)rY�exc_type�excs r-� close_exczProtocol.close_exc�s��� �z�V�#�#�#�%B�#�#�#� �O� '���+���$��6�6���$��6�6�)�H�H�,�H� (�� �O� �O� � %�! �! �� ��� �� r,�data�bytesc�b�|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)rR� feed_datarWrV�rYrms r-� receive_datazProtocol.receive_data�s0�� � ���d�#�#�#� �T�[�����r,c�|�|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)rR�eof�feed_eofrWrVr]s r-� receive_eofzProtocol.receive_eofs?�� �;�?� � �F� � ������ �T�[�����r,�fin�boolc� �|jstd���|jtur.t d|jj��������| |_|�tt||����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) rIr r\r1r r5rCrD� send_framerr�rYrmrws r-�send_continuationzProtocol.send_continuation#s���"�-� A�� ?�@�@� @� �;�d� "� "��I�� ��0E�0E�0G�0G�I�I�J�J� J�-0���&� ����g�t�S�1�1�2�2�2�2�2r,Tc� �|jrtd���|jtur.t d|jj��������| |_|�tt||����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 framer{N) rIr r\r1r r5rCrDr|rrr}s r-� send_textzProtocol.send_text;s��� � )� A�� ?�@�@� @� �;�d� "� "��I�� ��0E�0E�0G�0G�I�I�J�J� J�-0���&� ����g�t�S�1�1�2�2�2�2�2r,c� �|jrtd���|jtur.t d|jj��������| |_|�tt||����dS)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) rIr r\r1r r5rCrDr|rrr}s r-� send_binaryzProtocol.send_binaryRs��� � )� A�� ?�@�@� @� �;�d� "� "��I�� ��0E�0E�0G�0G�I�I�J�J� J�-0���&� ����i��s�3�3�4�4�4�4�4r,rerarf�strc��|jtur.td|jj��������|�2|dkrt d���ttj d��}d}n$t||��}|� ��}|� tt|����|j�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. r{Nrez#cannot send a reason without a coder,)r\r1r r5rCrDr rr�NO_STATUS_RCVD� serializer|rrrMrNr2�rYrarf�closerms r-� send_closezProtocol.send_closeis���$ �;�d� "� "��I�� ��0E�0E�0G�0G�I�I�J�J� J� �<���|�|�#�$I�J�J�J��)�2�B�7�7�E��D�D��$��'�'�E��?�?�$�$�D� ����h��-�-�.�.�.���&�&�&������ � � r,c���|jtur<|jtur.td|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. r{N) r\r1r2r r5rCrDr|rrrqs r-� send_pingzProtocol.send_ping��f�� �;�d� "� "�t�{�'�'A�'A��I�� ��0E�0E�0G�0G�I�I�J�J� J� ����g�t�,�,�-�-�-�-�-r,c���|jtur<|jtur.td|jj��������|�tt|����dS)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\r1r2r r5rCrDr|rrrqs r-� send_pongzProtocol.send_pong�r�r,�intc���|jtur}|tjkrmt ||��}|���}|�tt|����||_ |j �d|_ t|_|j tur|js|���|���|_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)r5r1rr`rr�r|rrrNrMrOr2r8r)rQ�send_eof�discardrVrWr�s r-�failz Protocol.fail�s���& �:�� � ��y�1�1�1��d�F�+�+�����(�(������h�� 5� 5�6�6�6�"'��� �?�.�04�D�-�$�� � �9�� � �t�}� � �M�M�O�O�O� �l�l�n�n�� � �T�[�����r,� list[Event]c�$�|jgc}|_|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)rYrSs r-�events_receivedzProtocol.events_received�s��#�k�2���� �� r,� list[bytes]c�$�|jgc}|_|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)rYrTs r-� data_to_sendzProtocol.data_to_send�s��"#�k�2���� �� r,c��|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)r5r1r2r3r0rQr]s r-�close_expectedzProtocol.close_expectedsV��D �:�� � ��5� �:�� � ��4� �:�� � ��5��z�Z�'�'�'�'��}�r,�Generator[None]c#�K� |j���Ed{V��r0|jr|j�d��t d���|j�d}n|j�|j}n|j|jz }tj|jj |j tu||j ���Ed{V��}|jr|j�d|��|� |����#t$r?}|�t jt%|����||_Yd}~�nKd}~wt$r?}|�t jt%|����||_Yd}~�nd}~wt*$r@}|�t j|j�d|j����||_Yd}~n�d}~wt2$rX}|�|j��|�t jt%|����||_Yd}~n\d}~wt8$rL}|j�dd� ��|�t j��||_Yd}~nd}~wwxYwdV�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�< EOFzunexpected end of stream)�maskr6rKz< %sz at position z parser failed)�exc_infoz"parse() shouldn't step after error) rR�at_eofrGr7�EOFErrorr6rHrrU� read_exactr8r)rK� recv_framer r�r�PROTOCOL_ERRORr�rXr`�UnicodeDecodeError� INVALID_DATArf�startr �set_current_size�MESSAGE_TOO_BIG� Exception�error�INTERNAL_ERROR�AssertionError)rYr6�framerks r-rUzProtocol.parse/s�����6 "� '�#�{�1�1�3�3�3�3�3�3�3�3�?��z�3�� �)�)�'�2�2�2� #�#=�>�>�>��=�(�#�H�H��]�*�#�}�H�H�#�}�t�}�<�H� $)�;��K�*���f�,�%�#�� $�$�$���������:�5��K�%�%�f�e�4�4�4�����&�&�&�= '��@� "� "� "� �I�I�i�.��C��� 9� 9� 9�!�D�O�O�O�O�O�O������ "� "� "� �I�I�i�0�#�c�(�(� ;� ;� ;�!�D�O�O�O�O�O�O�����!� "� "� "� �I�I�i�,���.U�.U�#�)�.U�.U� V� V� V�!�D�O�O�O�O�O�O������ "� "� "� � � ��� /� /� /� �I�I�i�/��S��� :� :� :�!�D�O�O�O�O�O�O������ "� "� "� �K� � �o�� � =� =� =� �I�I�i�.� /� /� /�!�D�O�O�O�O�O�O�����  "���� �����A�B�B�BsJ�C)C-�- I9�74D1�1 I9�>4E8�8 I9�6G� I9� AH � I9�-AI4�4I9c#��K�|jtup |jtu|jksJ�|j���Ed{V��s8|j���|j���Ed{V���8|jr|j �d��|jtur"|jtur|� ��t|_dV�td���)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)r8r)r5r0rQrRr�r�rGr7r*r�r3r�r]s r-r�zProtocol.discardws������ �V�#�?�t�z�Z�'?�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�#?�#?� �M�M�O�O�O��� � �����A�B�B�Br,r�rc�t�|jtus|jtur8|j�t d���|jst |j��|_�n�|jturJ|j�t d���|jr d|_�n�|xjt |j��z c_�nq|jtur1tt|j��}|� |���n2|jtur�n"|jtur�tj|j��|_|jt$ur|j�J�d|_|j�t d���|jt*urL|� tt|j����|j|_d|_t$|_|jt.ur|���|���|_t7|j��nt9d|jd�����|j�|��dS) z- Process an incoming frame. Nr�rzFzincomplete fragmented messageTzunexpected opcode: �02x)�opcoderrrHr rw�lenrmrrrrr|rrrUrMr5r2rNrOr1r8r)r�r�rVrWr�rS�append)rYr�� pong_frames r-r�zProtocol.recv_frame�s�� �<�7� "� "�e�l�i�&?�&?��}�(�#�$C�D�D�D��9� 0� #�E�J���� �� �\�W� $� $��}�$�#�$C�D�D�D��y� 1� $�� � �� � ��U�Z���0� � � � �\�W� $� $��w�� �3�3�J� �O�O�J� '� '� '� '� �\�W� $� $� � �\�X� %� %�$�k�%�*�5�5�D�O��z�W�$�$���2�2�2�,1��)��}�(�#�$C�D�D�D��z�T�!�!� ����h�� � ;� ;�<�<�<�"&�/���,0��)�$�� ��y�F�"�"�� � �����,�,�.�.�D�K� ��� � � � �!�!I�u�|�!I�!I�!I�J�J� J� � ���5�!�!�!�!�!r,c���|jr|j�d|��|j�|�|jt u|j�����dS)Nz> %s)r�rK)rGr7rTr�r�r8r*rK)rYr�s r-r|zProtocol.send_frame�sm�� �:� -� �K� � �f�e� ,� ,� ,� � ��� �O�O��Y�&�(��?� � � � � � � � r,c��|jrJ�d|_|jr|j�d��|j�t ��dS)NTz> EOF)rQrGr7rTr�r!r]s r-r�zProtocol.send_eof�sS���=� � � ��� � �:� '� �K� � �g� &� &� &� � ���8�$�$�$�$�$r,) r8rr5r r6r9r7r:r;r<)r;r )r5r r;r<)r;r9)r;rc)r;r)rmrnr;r<)r;r<)rmrnrwrxr;r<)T)Nre)rar9rfr�r;r<)re)rar�rfr�r;r<)r;r�)r;r�)r;rx)r;r�)r�rr;r<)r$r%r&r'r1rZ�propertyr5�setterrbrgrlrrrvr~r�r�r�r�r�r�r�r�r�rUr�r�r|r�r+r,r-rrIsn������ � �&�$�$(� B1�B1�B1�B1�B1�B1�H� � � ��X� � �\�����\�� �(�(�(��X�(�$�*�*�*��X�*�$�!�!�!��X�!�J���� ����*3�3�3�3�03�3�3�3�3�.5�5�5�5�5�."�"�"�"�"�H.�.�.�.� .�.�.�.� -�-�-�-�-�b � � � � ����()�)�)�)�ZFC�FC�FC�FC�PC�C�C�C�<N"�N"�N"�N"�d � � � �%�%�%�%�%�%r,r)2� __future__r�enumrAr>�collections.abcr�typingr� exceptionsrrr r r r rKr �framesrrrrrrrrrr�http11rr�streamsrrrr�__all__�Event�IntEnumrr)r*r r0r1r2r3r!rr+r,r-�<module>r�sp��"�"�"�"�"�"� � � � ����� � � � �%�%�%�%�%�%�����������������������"�!�!�!�!�!� � � � � � � � � � � � � � � � � � � � � � � � �&�%�%�%�%�%�%�%�!�!�!�!�!�!�3�3�3�3�3�3�3�3�3�3� � � �� �g�x��&�'��?������4�<���� ��� ���1�1�1�1�1�D�L�1�1�1� � � � �z�� �-�� ��� ��E�m %�m %�m %�m %�m %�m %�m %�m %�m %�m %r,
Memory