� <��g=T� ��ddlmZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z mZmZddlmZmZddlmZmZmZmZmZmZmZddlmZmZdd lmZm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&dd l'm(Z(dd l)m*Z*m+Z+m,Z,m-Z-m.Z.dd l m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5ddl6m7Z7dgZ8Gd�de-��Z9Gd�de9��Z:e(e;��dddddd����dS)�)� annotationsN)� Generator�Sequence)�Any�Callable�cast�)�Headers�MultipleValuesError)�InvalidHandshake� InvalidHeader�InvalidHeaderValue�InvalidMessage� InvalidOrigin�InvalidUpgrade�NegotiationError)� Extension�ServerExtensionFactory)�build_extension�parse_connection�parse_extension�parse_subprotocol� parse_upgrade)�Request�Response)� lazy_import)� CONNECTING�OPEN�SERVER�Protocol�State)�ConnectionOption�ExtensionHeader� LoggerLike�Origin� StatusLike� Subprotocol�UpgradeProtocol)� accept_key�ServerProtocolc���eZdZdZddddeddd�d/�fd�Zd0d�Zd1d�Zd2d�Zd3d!�Z d4d#�Z d5d%�Z d6d*�Z d7d,�Z d8�fd.� Z�xZS)9r*a- Sans-I/O implementation of a WebSocket server connection. Args: origins: Acceptable values of the ``Origin`` header. Values can be :class:`str` to test for an exact match or regular expressions compiled by :func:`re.compile` to test against a pattern. Include :obj:`None` in the list if the lack of an origin is acceptable. This is useful for defending against Cross-Site WebSocket Hijacking attacks. extensions: List of supported extensions, in order in which they should be tried. subprotocols: List of supported subprotocols, in order of decreasing preference. select_subprotocol: Callback for selecting a subprotocol among those supported by the client and the server. It has the same signature as the :meth:`select_subprotocol` method, including a :class:`ServerProtocol` instance as first argument. 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; defaults to ``logging.getLogger("websockets.server")``; see the :doc:`logging guide <../../topics/logging>` for details. Ni)�origins� extensions� subprotocols�select_subprotocol�state�max_size�loggerr,�0Sequence[Origin | re.Pattern[str] | None] | Noner-�'Sequence[ServerExtensionFactory] | Noner.�Sequence[Subprotocol] | Noner/�LCallable[[ServerProtocol, Sequence[Subprotocol]], Subprotocol | None] | Noner0r!r1� int | Noner2�LoggerLike | None�return�Nonec����t���t|||���||_||_||_|�,t |d|�||j����dSdS)N)�sider0r1r2r/) �super�__init__rr,�available_extensions�available_subprotocols�setattr�__get__� __class__) �selfr,r-r.r/r0r1r2rCs ��a/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/websockets/server.pyr>zServerProtocol.__init__Ls����" ��������� � � � � �� �$.��!�&2��#� � )� ��$�"�*�*�4���@�@� � � � � � *� )��requestrrc��� |�|��\}}}�n�#t$rd}||_||_|jr|j�dd���|�tjj d|�d���cYd}~Sd}~wt$rp}||_||_|jr|j�dd���|�tjj d|�d���}d |j d <|cYd}~Sd}~wt$r�}||_||_|jr|j�d d���tt|��}|�}|j�|j}|d |��z }|j�|�tjjd|�d���cYd}~Sd}~wt$$rY}||_||_|j�d d���|�tjjd��cYd}~Sd}~wwxYwt+��} t,j�d���| d<d | d <d | d<|| d<|�|| d<|�|| d<t3dd| ��S)a� Create a handshake response to accept the connection. If the handshake request is valid and the handshake successful, :meth:`accept` returns an HTTP response with status code 101. Else, it returns an HTTP response with another status code. This rejects the connection, like :meth:`reject` would. You must send the handshake response with :meth:`send_response`. You may modify the response before sending it, typically by adding HTTP headers. Args: request: WebSocket handshake request received from the client. Returns: WebSocket handshake response or HTTP response to send to the client. z! invalid originT)�exc_infoz'Failed to open a WebSocket connection: z. Nz! invalid upgradez^. You cannot access a WebSocket server directly with a browser. You need a WebSocket client. � websocket�Upgradez! invalid handshakez; zopening handshake failedzLFailed to open a WebSocket connection. See server log for more information. ��usegmt�Date� ConnectionzSec-WebSocket-Accept�Sec-WebSocket-Extensions�Sec-WebSocket-Protocol�ezSwitching Protocols)�process_requestr� _exception� handshake_exc�debugr2�reject�http� HTTPStatus� FORBIDDENr�UPGRADE_REQUIRED�headersr r� BaseException� __cause__� BAD_REQUEST� Exception�error�INTERNAL_SERVER_ERRORr �email�utils� formatdater) rDrG� accept_header�extensions_header�protocol_header�exc�response� exc_chain�exc_strr\s rE�acceptzServerProtocol.acceptos=��,9 � �$�$�W�-�-�  ��!����� � � �!$�G� �!$�D� ��z� E�� �!�!�"4�t�!�D�D�D��;�;���)�B�#�B�B�B��� � � � � � ������ � � �!$�G� �!$�D� ��z� F�� �!�!�"5��!�E�E�E��{�{���0�F�c�F�F�F���H�+6�H� �Y� '��O�O�O�O�O�O������ � � �!$�G� �!$�D� ��z� H�� �!�!�"7�$�!�G�G�G��]�C�0�0�I�"�n�G��%�1�%�/� ��+� �+�+�+���%�1��;�;���+�F�'�F�F�F��� � � � � � ������ � � �"%�G� �!$�D� � �K� � �8�4� � H� H� H��;�;���5�=��� � � � � � ����� �����)�)���+�0�0��0�=�=����(�� �� )�� ��*7��&�'� � (�2C�G�.� /� � &�0?�G�,� -���2�G�<�<�<sT�� H�AB�H� H�A%C>�8H�> H� BF�H� H�,AH�:H�H�"tuple[str, str | None, str | None]c���|j}td�|�d��D��g��}td�|D����s't d|rd�|��nd���td�|�d��D��g��}t |��dkr|d ���d ks't d|rd�|��nd��� |d }n8#t$rtd ��d�t$rtd d ��d�wxYw tj |� ��d ���}n(#tj$r}t!d |��|�d}~wwxYwt |��dkrt!d |���t#|��} |d} n8#t$rtd��d�t$rtdd ��d�wxYw| dkrt!d| ���|�|��|_|�|��\} |_|�|��x} |_|| | fS)a Check a handshake request and negotiate extensions and subprotocol. This function doesn't verify that the request is an HTTP/1.1 or higher GET request and doesn't check the ``Host`` header. These controls are usually performed earlier in the HTTP request handling code. They're the responsibility of the caller. Args: request: WebSocket handshake request received from the client. Returns: ``Sec-WebSocket-Accept``, ``Sec-WebSocket-Extensions``, and ``Sec-WebSocket-Protocol`` headers for the handshake response. Raises: InvalidHandshake: If the handshake request is invalid; then the server must return 400 Bad Request error. c�,�g|]}t|����S�)r��.0�values rE� <listcomp>z2ServerProtocol.process_request.<locals>.<listcomp>�s!�� P� P� P�� �e� $� $� P� P� PrFrOc3�FK�|]}|���dkV��dS)�upgradeN)�lowerrrs rE� <genexpr>z1ServerProtocol.process_request.<locals>.<genexpr>�s/����F�F�%�5�;�;�=�=�I�-�F�F�F�F�F�FrF�, Nc�,�g|]}t|����Srq)rrrs rEruz2ServerProtocol.process_request.<locals>.<listcomp>�s �� J� J� J�e�]�5� !� !� J� J� JrFrKr rrJzSec-WebSocket-Key�multiple valuesT)�validate�zSec-WebSocket-Version�13)r\�sum�get_all�anyr�join�lenrx�KeyErrorr r �base64� b64decode�encode�binascii�Errorrr)�process_origin�origin�process_extensionsr-�process_subprotocol� subprotocol) rDrGr\� connectionrw�key�raw_keyrirf�versionrgrhs rErSzServerProtocol.process_request�s���0�/��-0� P� P�'�/�/�,�2O�2O� P� P� P�RT�. �. � ��F�F�:�F�F�F�F�F� � ��z�K�d�i�i� �3�3�3�t��� �*-� J� J�w���y�/I�/I� J� J� J�B�* �* �� �G� � ��!�!�g�a�j�&6�&6�&8�&8�K�&G�&G� ��'�,S�D�I�I�g�,>�,>�,>�t�T�T� T� R��-�.�C�C��� ?� ?� ?�� 3�4�4�$� >�"� R� R� R�� 3�5F�G�G�T� Q� R���� H��&�s�z�z�|�|�d�C�C�C�G�G���~� H� H� H�$�%8�#�>�>�C� G����� H���� �w�<�<�2� � �$�%8�#�>�>� >�"�3��� � V��5�6�G�G��� C� C� C�� 7�8�8�d� B�"� V� V� V�� 7�9J�K�K�QU� U� V���� �d�?�?�$�%<�g�F�F� F��)�)�'�2�2�� �-1�-D�-D�W�-M�-M�*��4�?�-1�-E�-E�g�-N�-N�N��$�*��0�/�B�Bs0�;D�5D9�=(E&�&F �5F�F �G � 5G?r\r � Origin | Nonec�R� |�d��}n#t$rtdd��d�wxYw|�tt|��}|j�R|jD];}||ks1t |tj��r|�|� |���n�<t|���|S)aG Handle the Origin HTTP request header. Args: headers: WebSocket handshake request headers. Returns: origin, if it is acceptable. Raises: InvalidHandshake: If the Origin header is invalid. InvalidOrigin: If the origin isn't acceptable. r%r|N) �getr r rr%r,� isinstance�re�Pattern� fullmatchr)rDr\r��origin_or_regexs rEr�zServerProtocol.process_origins���" G��[�[��*�*�F�F��"� G� G� G���*;�<�<�$� F� G���� � ��&�&�)�)�F� �<� #�#'�<� ,� ,��"�f�,�,���� �;�;�-��*�'�1�1�&�9�9�E��E��#�F�+�+�+�� s��4�"tuple[str | None, list[Extension]]c��d}g}g}|�d��}|r�|jr�td�|D��g��}|D]r\}}|jD]e} | j|kr� | �||��\} } n#t $rY�5wxYw|�|| f��|�| ���s|rt|��}||fS)a� Handle the Sec-WebSocket-Extensions HTTP request header. Accept or reject each extension proposed in the client request. Negotiate parameters for accepted extensions. Per :rfc:`6455`, negotiation rules are defined by the specification of each extension. To provide this level of flexibility, for each extension proposed by the client, we check for a match with each extension available in the server configuration. If no match is found, the extension is ignored. If several variants of the same extension are proposed by the client, it may be accepted several times, which won't make sense in general. Extensions must implement their own requirements. For this purpose, the list of previously accepted extensions is provided. This process doesn't allow the server to reorder extensions. It can only select a subset of the extensions proposed by the client. Other requirements, for example related to mandatory extensions or the order of extensions, may be implemented by overriding this method. Args: headers: WebSocket handshake request headers. Returns: ``Sec-WebSocket-Extensions`` HTTP response header and list of accepted extensions. Raises: InvalidHandshake: If the Sec-WebSocket-Extensions header is invalid. NrPc�,�g|]}t|����Srq)r�rs� header_values rEruz5ServerProtocol.process_extensions.<locals>.<listcomp>fs ��Q�Q�Q�<���.�.�Q�Q�QrF)r�r?r��name�process_request_paramsr�appendr) rDr\�response_header_value�extension_headers�accepted_extensions� header_values�parsed_header_valuesr��request_params� ext_factory�response_params� extensions rEr�z!ServerProtocol.process_extensions6s<��N-1��35��/1�����(B�C�C� � � �T�6� �:=�Q�Q�=�Q�Q�Q�SU�;�;� �)=� � �$��n�#'�#<���K�"�'�4�/�/� �!�5@�5W�5W�*�,?�6�6�2�����,�!�!�!� ��!����&�,�,�d�O�-D�E�E�E�'�.�.�y�9�9�9��� � G�$3�4E�$F�$F� !�$�&9�9�9s�A6�6 B�B�Subprotocol | Nonec��td�|�d��D��g��}|�|��S)a� Handle the Sec-WebSocket-Protocol HTTP request header. Args: headers: WebSocket handshake request headers. Returns: Subprotocol, if one was selected; this is also the value of the ``Sec-WebSocket-Protocol`` response header. Raises: InvalidHandshake: If the Sec-WebSocket-Subprotocol header is invalid. c�,�g|]}t|����Srq)rr�s rEruz6ServerProtocol.process_subprotocol.<locals>.<listcomp>�s0�� � � � �"�,�/�/� � � rFrQ)r�r�r/)rDr\r.s rEr�z"ServerProtocol.process_subprotocol�sW��/2� � �$+�O�O�4L�$M�$M� � � � � / �/ � ��&�&�|�4�4�4rF�Sequence[Subprotocol]c���|jsdS|std���t|��}|jD] }||vr|cS� tdd�|j��z���)aq Pick a subprotocol among those offered by the client. If several subprotocols are supported by both the client and the server, pick the first one in the list declared the server. If the server doesn't support any subprotocols, continue without a subprotocol, regardless of what the client offers. If the server supports at least one subprotocol and the client doesn't offer any, abort the handshake with an HTTP 400 error. You provide a ``select_subprotocol`` argument to :class:`ServerProtocol` to override this logic. For example, you could accept the connection even if client doesn't offer a subprotocol, rather than reject it. Here's how to negotiate the ``chat`` subprotocol if the client supports it and continue without a subprotocol otherwise:: def select_subprotocol(protocol, subprotocols): if "chat" in subprotocols: return "chat" Args: subprotocols: List of subprotocols offered by the client. Returns: Selected subprotocol, if a common subprotocol was found. :obj:`None` to continue without a subprotocol. Raises: NegotiationError: Custom implementations may raise this exception to abort the handshake with an HTTP 400 error. Nzmissing subprotocolz%invalid subprotocol; expected one of rz)r@r�setr�)rDr.�proposed_subprotocolsr�s rEr/z!ServerProtocol.select_subprotocol�s���R�*� ��4�� :�"�#8�9�9� 9�!$�L� 1� 1���6� #� #�K��3�3�3�"�"�"�"�4�� 3��i�i��3�4�4� 5� � � rF�statusr&�text�strc �(�tj|��}|���}tdtj�d���fddtt|����fdg��}t|j |j ||��S)a' Create a handshake response to reject the connection. A short plain text response is the best fallback when failing to establish a WebSocket connection. You must send the handshake response with :meth:`send_response`. You may modify the response before sending it, for example by changing HTTP headers. Args: status: HTTP status code. text: HTTP response body; it will be encoded to UTF-8. Returns: HTTP response to send to the client. rNTrL)rO�closezContent-Length)z Content-Typeztext/plain; charset=utf-8) rXrYr�r rcrdrer�r�rrt�phrase)rDr�r��bodyr\s rErWzServerProtocol.reject�s���*���(�(���{�{�}�}������/�/�t�/�<�<�=�'�!�3�s�4�y�y�>�>�2�=�  � � ���� �f�m�W�d�C�C�CrFrjc���|jr�|j|j}}|j�d||��|j���D]!\}}|j�d||���"|jr-|j�dt|j����|j� |� ����|jdkr8|j tusJ�t|_ |j�d��dS|j�d|j|j��|���|���|_t%|j��dS)z� Send a handshake response to the client. Args: response: WebSocket handshake response event to send. z> HTTP/1.1 %d %sz> %s: %sz> [body] (%d bytes)rRzconnection openzconnection rejected (%d %s)N)rV� status_code� reason_phraser2r\� raw_itemsr�r��writesr�� serializer0rr�info�send_eof�discard�parser�next)rDrj�coder�r�rts rE� send_responsezServerProtocol.send_response�sl�� �:� M�#�/��1G�&�D� �K� � �0�$�� ?� ?� ?�&�.�8�8�:�:� :� :� ��U�� �!�!�*�c�5�9�9�9�9��}� M�� �!�!�"7��X�]�9K�9K�L�L�L� � ���8�-�-�/�/�0�0�0� � �3� &� &��:��+�+�+�+��D�J� �K� � �.� /� /� /� /� /� �K� � �-��$��&� � � � �M�M�O�O�O��,�,�.�.�D�K� ��� � � � � rF�Generator[None]c#��K�|jtu�r tj|jj��Ed{V��}n|#t $ro}td��|_||j_ |� ��|� ��|_ t|j ��dV�Yd}~nd}~wwxYw|jr[|j�d|j��|j���D]!\}}|j�d||���"|j�|��t+�����Ed{V��dS)Nz$did not receive a valid HTTP requestz< GET %s HTTP/1.1z< %s: %s)r0rr�parse�reader� read_liner`rrUr^r�r�r�r�rVr2�pathr\r��eventsr�r=)rDrGrir�rtrCs �rEr�zServerProtocol.parsesy����� �:�� #� #� �%,�]��K�)�&�&����������� � � �%3�:�&�&��"�03��"�,�� � ����"�l�l�n�n�� ��T�[�!�!�!�������������� �����z� >�� �!�!�"5�w�|�D�D�D�")�/�";�";�"=�"=�>�>�J�C���K�%�%�j�#�u�=�=�=�=� �K� � �w� '� '� '��7�7�=�=�?�?�"�"�"�"�"�"�"�"�"s�$9� B2�A%B-�-B2)r,r3r-r4r.r5r/r6r0r!r1r7r2r8r9r:)rGrr9r)rGrr9rn)r\r r9r�)r\r r9r�)r\r r9r�)r.r�r9r�)r�r&r�r�r9r)rjrr9r:)r9r�)�__name__� __module__� __qualname__�__doc__rr>rmrSr�r�r�r/rWr�r�� __classcell__�rCs@rEr*r*0sF���������<EI�>B�59� �!�$�$(�!�!�!�!�!�!�!�!�FZ=�Z=�Z=�Z=�xFC�FC�FC�FC�P!�!�!�!�FO:�O:�O:�O:�b5�5�5�5�0: �: �: �: �xD�D�D�D�B � � � �D#�#�#�#�#�#�#�#�#�#rFc� ��eZdZd�fd� Z�xZS)�ServerConnection�argsr�kwargsr9r:c�n��tjdt��t��j|i|��dS)Nz.ServerConnection was renamed to ServerProtocol)�warnings�warn�DeprecationWarningr=r>)rDr�r�rCs �rEr>zServerConnection.__init__9sA���� � <� � � � � �����$�)�&�)�)�)�)�)rF)r�rr�rr9r:)r�r�r�r>r�r�s@rEr�r�8s=�������*�*�*�*�*�*�*�*�*�*rFr�z.legacy.server)�WebSocketServer�WebSocketServerProtocol� broadcast�serve� unix_serve)�deprecated_aliases)<� __future__rr�r�� email.utilsrcrXr�r��collections.abcrr�typingrrr�datastructuresr r � exceptionsr r rrrrrr-rrr\rrrrr�http11rr�importsr�protocolrrrr r!r"r#r$r%r&r'r(rdr)�__all__r*r��globalsrqrFrE�<module>r�s���"�"�"�"�"�"� � � � ��������� � � � � � � � �����/�/�/�/�/�/�/�/�&�&�&�&�&�&�&�&�&�&�8�8�8�8�8�8�8�8�������������������:�9�9�9�9�9�9�9���������������&�%�%�%�%�%�%�%� � � � � � �?�?�?�?�?�?�?�?�?�?�?�?�?�?������������������������� � ��E#�E#�E#�E#�E#�X�E#�E#�E#�P*�*�*�*�*�~�*�*�*� � � �G�I�I�,�#3�%�!�&� �� � � � � � rF
Memory