� <��g°���ddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z mZmZddlmZddlmZmZmZmZddlmZddlmZmZmZdd lmZmZm 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-dd l.m/Z/ddl0m1Z1ddlm2Z2m3Z3m4Z4m5Z5m6Z6ddlm7Z7ddl8m9Z9m:Z:ddlm;Z;ddl0m<Z<m=Z=gd�Z>eeee?egeffZ@eAe5eeBfZCGd�de<��ZDGd�d��ZEGd�d��ZFeFZG d&d'd#�ZHd(d%�ZIdS))�)� annotationsN)� Awaitable� Generator�Iterable�Sequence)� TracebackType)�Any�Callable�Union�cast�)�asyncio_timeout)�Headers� HeadersLike�MultipleValuesError)�InvalidHandshake� InvalidHeader�InvalidMessage� InvalidOrigin�InvalidUpgrade�NegotiationError)� Extension�ServerExtensionFactory)� enable_server_permessage_deflate)�build_extension�parse_extension�parse_subprotocol�validate_subprotocols)�SERVER)�State)�ExtensionHeader� LoggerLike�Origin� StatusLike� Subprotocol�)�AbortHandshake)�build_response� check_request)� read_request)�WebSocketCommonProtocol� broadcast)r,�serve� unix_serve�WebSocketServerProtocol�WebSocketServerc ����eZdZdZdZdZdddddedddd� dA�fd!�ZdB�fd$� ZdCd%�Z dDd'�Z dEdFd.�Z dGd3�Z e dEdHd5���Ze dId8���ZdJd;�ZdKd?�Z dLdMd@�Z�xZS)Nr/a� WebSocket server connection. :class:`WebSocketServerProtocol` provides :meth:`recv` and :meth:`send` coroutines for receiving and sending messages. It supports asynchronous iteration to receive messages:: async for message in websocket: await process(message) The iterator exits normally when the connection is closed with close code 1000 (OK) or 1001 (going away) or without a close code. It raises a :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is closed with any other code. You may customize the opening handshake in a subclass by overriding :meth:`process_request` or :meth:`select_subprotocol`. Args: ws_server: WebSocket server that created this connection. See :func:`serve` for the documentation of ``ws_handler``, ``logger``, ``origins``, ``extensions``, ``subprotocols``, ``extra_headers``, and ``server_header``. See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``, ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``. F�serverN� ) �logger�origins� extensions� subprotocols� extra_headers� server_header�process_request�select_subprotocol� open_timeout� ws_handler�nCallable[[WebSocketServerProtocol], Awaitable[Any]] | Callable[[WebSocketServerProtocol, str], Awaitable[Any]]� ws_serverr0r4�LoggerLike | Noner5�Sequence[Origin | None] | Noner6�'Sequence[ServerExtensionFactory] | Noner7�Sequence[Subprotocol] | Noner8�HeadersLikeOrCallable | Noner9� str | Noner:�?Callable[[str, Headers], Awaitable[HTTPResponse | None]] | Noner;�LCallable[[Sequence[Subprotocol], Sequence[Subprotocol]], Subprotocol] | Noner<� float | None�kwargsr �return�Nonec  �h��|�tjd��}t��jdd|i| ��|�*d|vr&t jdt ��d�|D��}t|��|_||_ ||_ ||_ ||_ ||_ ||_| |_| |_| |_dS)N�websockets.serverr4�z!use None instead of '' in originsc�"�g|] }|dkrdn|�� S)rNN�)�.0�origins �h/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/websockets/legacy/server.py� <listcomp>z4WebSocketServerProtocol.__init__.<locals>.<listcomp>zs$��N�N�N�F�v��|�|�t�t��N�N�N�rP)�logging� getLogger�super�__init__�warnings�warn�DeprecationWarning�remove_path_argumentr=r?r5�available_extensions�available_subprotocolsr8r9�_process_request�_select_subprotocolr<)�selfr=r?r4r5r6r7r8r9r:r;r<rI� __class__s �rSrYz WebSocketServerProtocol.__init__\s����0 �>��&�':�;�;�F������1�1��1�&�1�1�1� � �2��=�=� �M�=�?Q� R� R� R�N�N�g�N�N�N�G�/�z�:�:���"����� �$.��!�&2��#�*���*��� /���#5�� �(����rU� transport�asyncio.BaseTransportc����t���|��|j�|��|j�|�����|_dS)zJ Register connection and initialize a task to handle it. N)rX�connection_mader?�register�loop� create_task�handler� handler_task)rbrdrcs �rSrgz'WebSocketServerProtocol.connection_made�s[��� ����� �*�*�*� �����%�%�%� �I�1�1�$�,�,�.�.�A�A����rUc ��p K� t|j��4�d{V��|�|j|j|j|j����d{V��ddd���d{V��n#1�d{V��swxYwY�n`#tj$r�t$r�t$�r9}t|t��r|j |j|j}}}�n�t|t ��r\|jr|j�dd���t&jjt-��d|�d����}}}�nTt|t0��r]|jr|j�dd���t&jjt-d g��d|�d ����}}}n�t|t4��r�|jr|j�d d���t7t8|��}|�}|j�|j}|d |��z }|j�t&jjt-��d|�d����}}}n=|j�d d���t&jj t-��d}}}|�!dtDj#�$d�����|j%r|�!d|j%��|�!dtMtO|������|�!dd��|�!dd��|�(|||��|j�)d|j*|j+��|�,���d{V��Yd}~|j-�.|��|j�)d��dSd}~wwxYw |�/|���d{V��nG#t$r:|j�dd���|j0s|�1d���wxYw |�2���d{V��n5#t$r�t$r|j�dd����wxYwn;#t$r. |j3�2��n#t$rYnwxYwYnwxYw|j-�.|��|j�)d��dS#|j-�.|��|j�)d��wxYw)a Handle the lifecycle of a WebSocket connection. Since this method doesn't have a caller able to handle exceptions, it attempts to log relevant ones and guarantees that the TCP connection is closed before exiting. N)r5r^r_r8z! invalid originT)�exc_infoz'Failed to open a WebSocket connection: z. z! invalid upgrade)�Upgrade� websocketz^. You cannot access a WebSocket server directly with a browser. You need a WebSocket client. z! invalid handshakez; zopening handshake failedsLFailed to open a WebSocket connection. See server log for more information. �Date��usegmt�ServerzContent-Lengthz Content-Typez text/plain� Connection�closezconnection rejected (%d %s)zconnection closedzconnection handler failedi�zclosing handshake failed)4rr<� handshaker5r^r_r8�asyncio� TimeoutError�ConnectionError� Exception� isinstancer'�status�headers�bodyr�debugr4�http� HTTPStatus� FORBIDDENr�encoder�UPGRADE_REQUIREDrr � BaseException� __cause__� BAD_REQUEST�error�INTERNAL_SERVER_ERROR� setdefault�email�utils� formatdater9�str�len�write_http_response�info�value�phrase�close_transportr?� unregisterr=�closed�fail_connectionrvrd)rb�excr}r~r� exc_chain�exc_strs rSrkzWebSocketServerProtocol.handler�s�����i 2�I �*�4�+<�=�=����������.�.� $� �-1�-F�/3�/J�&*�&8� )�������������������������������������������'� � � ��"� � � ���= �= �= ��c�>�2�2�-�,/�J�� �S�X�T�G�F�F���]�3�3�+��z�M�� �)�)�*<�t�)�L�L�L���1�� � �J�#�J�J�J�Q�Q�S�S�&*�G�F�F�  ��^�4�4�#��z�N�� �)�)�*=��)�M�M�M���8��!9� :�;�;�N�c�N�N�N�!�&�(�(�&*�G�F�F� ��%5�6�6���z�P�� �)�)�*?�$�)�O�O�O� $�]�C� 8� 8�I�!*�n�G�#�-�9�$-�$7� ��#3� �#3�#3�3��$�-�9���3�� � �N�'�N�N�N�U�U�W�W�&*�G�F�F� �K�%�%�&@�4�%�P�P�P���=�� � �F� &*�G�F��"�"�6�5�;�+A�+A��+A�+N�+N�O�O�O��%�E��&�&�x��1C�D�D�D��"�"�#3�S��T���^�^�D�D�D��"�"�>�<�@�@�@��"�"�<��9�9�9��(�(���$�?�?�?�� � � �1�6�<�������*�*�,�,�,�,�,�,�,�,�,�����< �N� %� %�d� +� +� +� �K� � �0� 1� 1� 1� 1� 1�����y= ����~ ��o�o�d�+�+�+�+�+�+�+�+�+�+��� � � �� �!�!�"=��!�M�M�M��{�/��(�(��.�.�.��  ����  ��j�j�l�l�"�"�"�"�"�"�"�"��"� � � ��� � � �� �!�!�"<�t�!�L�L�L�� ����#��� � � � ���$�$�&�&�&�&��� � � ��� ������  ���� �N� %� %�d� +� +� +� �K� � �0� 1� 1� 1� 1� 1�� �N� %� %�d� +� +� +� �K� � �0� 1� 1� 1� 1���s��A8�4A%�A8�% A/�/A8�2A/�3A8�6R�8$O�K:O�R�O�R�O5�4R�5AP9�9R�=Q�R�2R � R� S?� S�R3�2S�3 S�=S�?S�S�S?�S�S?�?6T5�tuple[str, Headers]c��zK� t|j���d{V��\}}n1#tj$r�t$r}t d��|�d}~wwxYw|jrQ|j�d|��|���D]!\}}|j�d||���"||_ ||_ ||fS)a: Read request line and headers from the HTTP request. If the request contains a body, it may be read from ``self.reader`` after this coroutine returns. Raises: InvalidMessage: If the HTTP message is malformed or isn't an HTTP/1.1 GET request. Nz$did not receive a valid HTTP requestz< GET %s HTTP/1.1z< %s: %s) r*�readerrx�CancelledErrorr{rr�r4� raw_items�path�request_headers)rbr�r~r��keyr�s rS�read_http_requestz)WebSocketServerProtocol.read_http_request s����� R�".�t�{�";�";�;�;�;�;�;�;�M�D�'�'���%� � � � �� R� R� R� �!G�H�H�c� Q����� R���� �:� :� �K� � �1�4� 8� 8� 8�%�/�/�1�1� :� :� ��U�� �!�!�*�c�5�9�9�9�9��� �&����W�}�s�"�A�A � Ar}�http.HTTPStatusr~rr� bytes | Nonec� �||_|jr�|j�d|j|j��|���D]!\}}|j�d||���"|�(|j�dt |����d|j�d|j�d�}|t|��z }|j� |� ����|�|j� |��dSdS)z� Write status line and headers to the HTTP response. This coroutine is also able to write a response body. z> HTTP/1.1 %d %sz> %s: %sNz> [body] (%d bytes)z HTTP/1.1 � z ) �response_headersr�r4r�r�r�r�r�rd�writer�)rbr}r~rr�r��responses rSr�z+WebSocketServerProtocol.write_http_response's��!(��� �:� D� �K� � �0�&�,�� � N� N� N�%�/�/�1�1� :� :� ��U�� �!�!�*�c�5�9�9�9�9���� �!�!�"7��T���C�C�C�B�v�|�A�A�f�m�A�A�A���C��L�L� �� ����X�_�_�.�.�/�/�/� � � �N� � �� &� &� &� &� &� � rUr�r�r��HTTPResponse | Nonec��K�|j�O|�||��}t|t��r|�d{V��Stjdt ��|SdS)a� Intercept the HTTP request and return an HTTP response if appropriate. You may override this method in a :class:`WebSocketServerProtocol` subclass, for example: * to return an HTTP 200 OK response on a given path; then a load balancer can use this path for a health check; * to authenticate the request and return an HTTP 401 Unauthorized or an HTTP 403 Forbidden when authentication fails. You may also override this method with the ``process_request`` argument of :func:`serve` and :class:`WebSocketServerProtocol`. This is equivalent, except ``process_request`` won't have access to the protocol instance, so it can't store information for later use. :meth:`process_request` is expected to complete quickly. If it may run for a long time, then it should await :meth:`wait_closed` and exit if :meth:`wait_closed` completes, or else it could prevent the server from shutting down. Args: path: Request path, including optional query string. request_headers: Request headers. Returns: tuple[StatusLike, HeadersLike, bytes] | None: :obj:`None` to continue the WebSocket handshake normally. An HTTP response, represented by a 3-uple of the response status, headers, and body, to abort the WebSocket handshake and return that HTTP response instead. N�&declare process_request as a coroutine)r`r|rrZr[r\)rbr�r�r�s rSr:z'WebSocketServerProtocol.process_requestCss����J � � ,��,�,�T�?�C�C�H��(�I�.�.� �%�~�~�~�~�~�~�%�� �<�>P���� ���trU� Origin | Nonec��� |�d��}n##t$r}tdd��|�d}~wwxYw|�tt|��}|�||vrt |���|S)z� Handle the Origin HTTP request header. Args: headers: Request headers. origins: Optional list of acceptable origins. Raises: InvalidOrigin: If the origin isn't acceptable. r#zmultiple valuesN)�getrrr r#r)r~r5rRr�s rS�process_originz&WebSocketServerProtocol.process_origints���" F��[�[��*�*�F�F��"� F� F� F���*;�<�<�#� E����� F���� � ��&�&�)�)�F� � ��W�$�$�#�F�+�+�+�� s�� 8�3�8r^�"tuple[str | None, list[Extension]]c�~�d}g}g}|�d��}|r�|r�td�|D��g��}|D]m\}}|D]e} | j|kr� | �||��\} } n#t$rY�5wxYw|�|| f��|�| ���n|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. Return the Sec-WebSocket-Extensions HTTP response header and the list of accepted extensions. :rfc:`6455` leaves the rules up to 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: Request headers. extensions: Optional list of supported extensions. Raises: InvalidHandshake: To abort the handshake with an HTTP 400 error. N�Sec-WebSocket-Extensionsc�,�g|]}t|����SrP)r�rQ� header_values rSrTz>WebSocketServerProtocol.process_extensions.<locals>.<listcomp>�s ��Q�Q�Q�<���.�.�Q�Q�QrU)�get_all�sum�name�process_request_paramsr�appendr) r~r^�response_header_value�extension_headers�accepted_extensions� header_values�parsed_header_valuesr��request_params� ext_factory�response_params� extensions rS�process_extensionsz*WebSocketServerProtocol.process_extensions�s7��P-1��35��/1�����(B�C�C� � � �1� �:=�Q�Q�=�Q�Q�Q�SU�;�;� �)=� � �$��n�#7���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�A,�, A9�8A9r_�Subprotocol | Nonec��d}|�d��}|r2|r0td�|D��g��}|�||��}|S)a� Handle the Sec-WebSocket-Protocol HTTP request header. Return Sec-WebSocket-Protocol HTTP response header, which is the same as the selected subprotocol. Args: headers: Request headers. available_subprotocols: Optional list of supported subprotocols. Raises: InvalidHandshake: To abort the handshake with an HTTP 400 error. N�Sec-WebSocket-Protocolc�,�g|]}t|����SrP)rr�s rSrTz?WebSocketServerProtocol.process_subprotocol.<locals>.<listcomp>�s!��S�S�S�\�"�<�0�0�S�S�SrU)r�r�r;)rbr~r_� subprotocolr�r�s rS�process_subprotocolz+WebSocketServerProtocol.process_subprotocol�st��"+/� ����(@�A�A� � � �3� �69�S�S�]�S�S�S�UW�7�7� ��1�1�$�&<���K��rU�client_subprotocols�Sequence[Subprotocol]�server_subprotocolsc����|j�|�����St���t���z}|sdSt|��fd����dS)a� Pick a subprotocol among those supported by the client and the server. If several subprotocols are available, select the preferred subprotocol by giving equal weight to the preferences of the client and the server. If no subprotocol is available, proceed without a subprotocol. You may provide a ``select_subprotocol`` argument to :func:`serve` or :class:`WebSocketServerProtocol` to override this logic. For example, you could reject the handshake if the client doesn't support a particular subprotocol, rather than accept the handshake without that subprotocol. Args: client_subprotocols: List of subprotocols offered by the client. server_subprotocols: List of subprotocols available on the server. Returns: Selected subprotocol, if a common subprotocol was found. :obj:`None` to continue without a subprotocol. Nc�Z����|����|��zS�N)�index)�pr�r�s ��rS�<lambda>z<WebSocketServerProtocol.select_subprotocol.<locals>.<lambda>(s+���-�3�3�A�6�6�9L�9R�9R�ST�9U�9U�U�rU)r�r)ra�set�sorted)rbr�r�r7s `` rSr;z*WebSocketServerProtocol.select_subprotocols�����: � #� /��+�+�,?�AT�U�U� U��.�/�/�#�6I�2J�2J�J� �� ��4�� �U�U�U�U�U� � � � � � rUc��zK�|����d{V��\}}|�||��}t|t��r |�d{V��}nt jdt ��|}|jtj urtd���|j � ��stjjgdf}|� t!|��t#|��} |�||��|_|�||��\} |_|�||��x} |_t1��} t3| | ��| �| | d<| �| | d<t5|��r|||j��}|�| �|��| �dt<j� d� ����|j!�| �d |j!��|�"tjj#| ��|j$�%d ��|�&��|S) a� Perform the server side of the opening handshake. Args: origins: List of acceptable values of the Origin HTTP header; include :obj:`None` if the lack of an origin is acceptable. extensions: List of supported extensions, in order in which they should be tried. subprotocols: List of supported subprotocols, in order of decreasing preference. extra_headers: Arbitrary HTTP headers to add to the response when the handshake succeeds. Returns: path of the URI of the request. Raises: InvalidHandshake: If the handshake fails. Nr�z*connection closed during opening handshakesServer is shutting down. r�r�rqTrrrtzconnection open)'r�r:r|rrZr[r\�stater �CLOSED�BrokenPipeErrorr?� is_servingr�r��SERVICE_UNAVAILABLEr'r)r�rRr�r6r�r�rr(�callabler��updater�r�r�r�r9r��SWITCHING_PROTOCOLSr4r��connection_open) rbr5r^r_r8r�r��early_response_awaitable�early_responser��extensions_header�protocol_headerr�s rSrwz!WebSocketServerProtocol.handshake+sr����6'+�&<�&<�&>�&>� >� >� >� >� >� >���o�$(�#7�#7��o�#N�#N� � �.� � :� :� 6�#;�;�;�;�;�;�;�N�N� �M�B�DV� W� W� W�5�N� �:��� %� %�!�"N�O�O� O��~�(�(�*�*� ���3��-��N� � %� �.�1� 1��O�,�,���)�)�/�7�C�C�� �-1�-D�-D� �1�. �. �*��4�?�.2�-E�-E� �3�. �. � ��$�*�#�9�9���'��-�-�-� � (�;L� �7� 8� � &�9H� �5� 6� �M� "� "� F�)�M�$��0D�E�E�M� � $� � #� #�M� 2� 2� 2��#�#�F�E�K�,B�,B�$�,B�,O�,O�P�P�P� � � )� � '� '��$�2D� E� E� E� � � ���!D�FV�W�W�W� � ���*�+�+�+� ������� rU)r=r>r?r0r4r@r5rAr6rBr7rCr8rDr9rEr:rFr;rGr<rHrIr rJrK)rdrerJrK�rJrK)rJr�r�)r}r�r~rrr�rJrK)r�r�r�rrJr�)r~rr5rArJr�)r~rr^rBrJr�)r~rr_rCrJr�)r�r�r�r�rJr�)NNNN) r5rAr^rBr_rCr8rDrJr�)�__name__� __module__� __qualname__�__doc__� is_client�siderrYrgrkr�r�r:� staticmethodr�r�r�r;rw� __classcell__)rcs@rSr/r/9s����������>�I� �D�%)�26�>B�59�6:�$*� � �%'�++)�+)�+)�+)�+)�+)�+)�+)�Z B� B� B� B� B� B�r2�r2�r2�r2�h����<OS�'�'�'�'�'�8/�/�/�/�b�DH������\��6�O:�O:�O:��\�O:�d����@& �& �& �& �T37�HL�?C�6:� \�\�\�\�\�\�\�\�\rUr/c��eZdZdZd$d%d�Zd&d �Zd'd �Zd'd�Zd(d)d�Zd)d�Z d*d�Z d+d�Z d,d�Z d*d�Z d*d�Zed-d���Zd.d�Zd/d#�ZdS)0r0a� WebSocket server returned by :func:`serve`. This class mirrors the API of :class:`~asyncio.Server`. It keeps track of WebSocket connections in order to close them properly when shutting down. Args: logger: Logger for this server. It defaults to ``logging.getLogger("websockets.server")``. See the :doc:`logging guide <../../topics/logging>` for details. Nr4r@rJrKc�x�|�tjd��}||_t��|_d|_|dS)NrM)rVrWr4r�� websockets� close_task)rbr4s rSrYzWebSocketServer.__init__�sC�� �>��&�':�;�;�F��� �9<�����6:��� �0�0�0rUr2�asyncio.base_events.Serverc��||_|jD]�}|jtjkrd|���z}n�|jtjkr d|���dd�z}nK|jtjkr|���}n!t|�����}|j � d|����|� ��� ��|_ dS)a Attach to a given :class:`~asyncio.Server`. Since :meth:`~asyncio.loop.create_server` doesn't support injecting a custom ``Server`` class, the easiest solution that doesn't rely on private :mod:`asyncio` APIs is to: - instantiate a :class:`WebSocketServer` - give the protocol factory a reference to that instance - call :meth:`~asyncio.loop.create_server` with the factory - attach the resulting :class:`~asyncio.Server` with this method z%s:%dz[%s]:%dNr zserver listening on %s)r2�sockets�family�socket�AF_INET� getsockname�AF_INET6�AF_UNIXr�r4r��get_loop� create_future� closed_waiter)rbr2�sockr�s rS�wrapzWebSocketServer.wrap�s����� ��N� =� =�D��{�f�n�,�,���!1�!1�!3�!3�3�������/�/� �4�#3�#3�#5�#5�b�q�b�#9�9�������.�.��'�'�)�)����4�+�+�-�-�.�.�� �K� � �5�t� <� <� <� <�$�_�_�.�.�<�<�>�>����rU�protocolr/c�:�|j�|��dS)z: Register a connection with this server. N)r��add�rbrs rSrhzWebSocketServer.register�s �� ����H�%�%�%�%�%rUc�:�|j�|��dS)z< Unregister a connection with this server. N)r��removers rSr�zWebSocketServer.unregister�s �� ����x�(�(�(�(�(rUT�close_connections�boolc��|j�A|����|�|����|_dSdS)aw Close the server. * Close the underlying :class:`~asyncio.Server`. * When ``close_connections`` is :obj:`True`, which is the default, close existing connections. Specifically: * Reject opening WebSocket connections with an HTTP 503 (service unavailable) error. This happens when the server accepted the TCP connection but didn't complete the opening handshake before closing. * Close open WebSocket connections with close code 1001 (going away). * Wait until all connection handlers terminate. :meth:`close` is idempotent. N)r�r�rj�_close)rbrs rSrvzWebSocketServer.close�sG��$ �?� "�"�m�m�o�o�9�9�� � �-�.�.���D�O�O�O� #� "rUc��K�|j�d��|j���t jd���d{V��|r-d�|jD��}|rt j|���d{V��|j����d{V��|jr)t jd�|jD�����d{V��|j � d��|j�d��dS)a Implementation of :meth:`close`. This calls :meth:`~asyncio.Server.close` on the underlying :class:`~asyncio.Server` object to stop accepting new connections and then closes open connections with close code 1001. zserver closingrNc��g|]<}|jtju�tj|�d������=S)i�)r�r � CONNECTINGrxrjrv�rQrps rSrTz*WebSocketServer._close.<locals>.<listcomp>sI�������?�%�*:�:�:��#�I�O�O�D�$9�$9�:�:�:�:�:rUc��g|] }|j�� SrP)rlrs rSrTz*WebSocketServer._close.<locals>.<listcomp>s��I�I�I�I��'�I�I�IrUz server closed) r4r�r2rvrx�sleepr��wait� wait_closedr�� set_result)rbr� close_taskss rSr zWebSocketServer._close�s[���� � ���)�*�*�*� � ������ �m�A���������� � 0���!%�����K� � 0��l�;�/�/�/�/�/�/�/�/�/��k�%�%�'�'�'�'�'�'�'�'�'� �?� ��,�I�I���I�I�I��� � � � � � � � ��%�%�d�+�+�+� � ����)�)�)�)�)rUc��HK�tj|j���d{V��dS)a� Wait until the server is closed. When :meth:`wait_closed` returns, all TCP connections are closed and all connection handlers have returned. To ensure a fast shutdown, a connection handler should always be awaiting at least one of: * :meth:`~WebSocketServerProtocol.recv`: when the connection is closed, it raises :exc:`~websockets.exceptions.ConnectionClosedOK`; * :meth:`~WebSocketServerProtocol.wait_closed`: when the connection is closed, it returns. Then the connection handler is immediately notified of the shutdown; it can clean up and exit. N)rx�shieldr��rbs rSrzWebSocketServer.wait_closeds3����&�n�T�/�0�0�0�0�0�0�0�0�0�0�0rU�asyncio.AbstractEventLoopc�4�|j���S)z7 See :meth:`asyncio.Server.get_loop`. )r2r�rs rSr�zWebSocketServer.get_loop0s�� �{�#�#�%�%�%rUc�4�|j���S)z9 See :meth:`asyncio.Server.is_serving`. )r2r�rs rSr�zWebSocketServer.is_serving7s�� �{�%�%�'�'�'rUc��HK�|j����d{V��dS)a See :meth:`asyncio.Server.start_serving`. Typical use:: server = await serve(..., start_serving=False) # perform additional setup here... # ... then start the server await server.start_serving() N)r2� start_servingrs rSrzWebSocketServer.start_serving>s4�����k�'�'�)�)�)�)�)�)�)�)�)�)�)rUc��HK�|j����d{V��dS)a� See :meth:`asyncio.Server.serve_forever`. Typical use:: server = await serve(...) # this coroutine doesn't return # canceling it stops the server await server.serve_forever() This is an alternative to using :func:`serve` as an asynchronous context manager. Shutdown is triggered by canceling :meth:`serve_forever` instead of exiting a :func:`serve` context. N)r2� serve_foreverrs rSrzWebSocketServer.serve_foreverLs4���� �k�'�'�)�)�)�)�)�)�)�)�)�)�)rU�Iterable[socket.socket]c��|jjS)z6 See :attr:`asyncio.Server.sockets`. )r2r�rs rSr�zWebSocketServer.sockets^s�� �{�"�"rUc�� K�|Sr�rPrs rS� __aenter__zWebSocketServer.__aenter__fs ����� rU�exc_type�type[BaseException] | None� exc_value�BaseException | None� traceback�TracebackType | Nonec��fK�|���|����d{V��dSr�)rvr�rbr$r&r(s rS� __aexit__zWebSocketServer.__aexit__is?���� � � � � � ���� � � � � � � � � � � rUr�)r4r@rJrK)r2r�rJrK)rr/rJrK)T)rr rJrKr�)rJr)rJr )rJr �rJr0�r$r%r&r'r(r)rJrK)r�r�r�r�rYrrhr�rvr rr�r�rr�propertyr�r#r,rPrUrSr0r0�sN������ � � 1� 1� 1� 1� 1�?�?�?�?�@&�&�&�&�)�)�)�)������.,*�,*�,*�,*�\1�1�1�1�*&�&�&�&�(�(�(�(� *� *� *� *�*�*�*�*�$�#�#�#��X�#�����!�!�!�!�!�!rUr0c�l�eZdZdZ d>dddddddeddddddddddd �d?d1�Zd@d3�ZdAd:�ZdBd<�Zd@d=�Z eZ dS)C�Servea� Start a WebSocket server listening on ``host`` and ``port``. Whenever a client connects, the server creates a :class:`WebSocketServerProtocol`, performs the opening handshake, and delegates to the connection handler, ``ws_handler``. The handler receives the :class:`WebSocketServerProtocol` and uses it to send and receive messages. Once the handler completes, either normally or with an exception, the server performs the closing handshake and closes the connection. Awaiting :func:`serve` yields a :class:`WebSocketServer`. This object provides a :meth:`~WebSocketServer.close` method to shut down the server:: # set this future to exit the server stop = asyncio.get_running_loop().create_future() server = await serve(...) await stop server.close() await server.wait_closed() :func:`serve` can be used as an asynchronous context manager. Then, the server is shut down automatically when exiting the context:: # set this future to exit the server stop = asyncio.get_running_loop().create_future() async with serve(...): await stop Args: ws_handler: Connection handler. It receives the WebSocket connection, which is a :class:`WebSocketServerProtocol`, in argument. host: Network interfaces the server binds to. See :meth:`~asyncio.loop.create_server` for details. port: TCP port the server listens on. See :meth:`~asyncio.loop.create_server` for details. create_protocol: Factory for the :class:`asyncio.Protocol` managing the connection. It defaults to :class:`WebSocketServerProtocol`. Set it to a wrapper or a subclass to customize connection handling. logger: Logger for this server. It defaults to ``logging.getLogger("websockets.server")``. See the :doc:`logging guide <../../topics/logging>` for details. compression: The "permessage-deflate" extension is enabled by default. Set ``compression`` to :obj:`None` to disable it. See the :doc:`compression guide <../../topics/compression>` for details. origins: Acceptable values of the ``Origin`` header, for defending against Cross-Site WebSocket Hijacking attacks. Include :obj:`None` in the list if the lack of an origin is acceptable. extensions: List of supported extensions, in order in which they should be negotiated and run. subprotocols: List of supported subprotocols, in order of decreasing preference. extra_headers (HeadersLike | Callable[[str, Headers] | HeadersLike]): Arbitrary HTTP headers to add to the response. This can be a :data:`~websockets.datastructures.HeadersLike` or a callable taking the request path and headers in arguments and returning a :data:`~websockets.datastructures.HeadersLike`. server_header: Value of the ``Server`` response header. It defaults to ``"Python/x.y.z websockets/X.Y"``. Setting it to :obj:`None` removes the header. process_request (Callable[[str, Headers], Awaitable[tuple[StatusLike, HeadersLike, bytes] | None]] | None): Intercept HTTP request before the opening handshake. See :meth:`~WebSocketServerProtocol.process_request` for details. select_subprotocol: Select a subprotocol supported by the client. See :meth:`~WebSocketServerProtocol.select_subprotocol` for details. open_timeout: Timeout for opening connections in seconds. :obj:`None` disables the timeout. See :class:`~websockets.legacy.protocol.WebSocketCommonProtocol` for the documentation of ``ping_interval``, ``ping_timeout``, ``close_timeout``, ``max_size``, ``max_queue``, ``read_limit``, and ``write_limit``. Any other keyword arguments are passed the event loop's :meth:`~asyncio.loop.create_server` method. For example: * You can set ``ssl`` to a :class:`~ssl.SSLContext` to enable TLS. * You can set ``sock`` to a :obj:`~socket.socket` that you created outside of websockets. Returns: WebSocket server. N�deflater3�i� i)�create_protocolr4� compressionr5r6r7r8r9r:r;r<� ping_interval� ping_timeout� close_timeout�max_size� max_queue� read_limit� write_limitr=r>�host�str | Sequence[str] | None�port� int | Noner5�-Callable[..., WebSocketServerProtocol] | Noner4r@r6rEr5rAr6rBr7rCr8rDr9r:rFr;rGr<rHr7r8r9r:r;r<�intr=rIr rJrKc �\�|�dd��}|�d}ntjdt��|�|}|�dd��}|�t}ntjdt��|�|}|�dd��}|�dd��}|�t j��}n|}tjd t��t|� ��}|�d ��du}|d krt|��}n|�td |�����| �t| ��ttdtf|��}tj|t!|��|fid|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d|�d| �d| �d| �d| �d | �d!|��}|�d"d��r6|�d#d��}|�|�J�tj|j||fi|��} ntj|j|||fi|��} | |_||_dS)$N�timeoutr3zrename timeout to close_timeout�klasszrename klass to create_protocol� legacy_recvFrizremove loop argument)r4�sslr2zunsupported compression: .r>r@�securer<r7r8r9r:r;r<r=r5r6r7r8r9r:r;r4�unixr�)�poprZr[r\r/rx�get_event_loopr0r�r� ValueErrorrr r � functools�partialr]�create_unix_server� create_server�_create_serverr?)!rbr=r>r@r5r4r6r5r6r7r8r9r:r;r<r7r8r9r:r;r<r=rIrErFrG�_looprir?rI�factoryr�rQs! rSrYzServe.__init__�s5��F!'� � �9�d� ;� ;�� �?��G�G� �M�;�=O� P� P� P� � �#�M�7=�j�j��$�6O�6O�� �=�+�E�E� �M�;�=O� P� P� P� � "�#�O�#�J�J�}�e�<�<� �39�*�*�V�T�2J�2J�� �=��)�+�+�D�D��D� �M�0�2D� E� E� E�#�6�2�2�2� ����E�"�"�$�.�� �)� #� #�9�*�E�E�J�J� � $��F��F�F�G�G� G� � #� !�,� /� /� /��x��-D�(D�E��W�W���#� � !�� ,� ,� �  � � ��� ��� ��6� �&�� �(�-� �&�� �(�-� ��X� � �i� � "�z�! �"$� �# �$��% �&$� �' �(�G�) �*"�z�+ �,&��- �.(�-�/ �0(�-�1 �2,�O�3 �4 2�1�5 �6�6�7 ��< �:�:�f�e� $� $� �%�z�z�&�$�7�7�D��<�D�L�L�0�%�-��'��$���:@���M�M�&�-��"�G�T�4���;A���M� ,���"����rUr0c��K�|�d{V��Sr�rPrs rSr#zServe.__aenter__Ps�����z�z�z�z�z�z�rUr$r%r&r'r(r)c��zK�|j���|j����d{V��dSr�)r?rvrr+s rSr,zServe.__aexit__SsH���� ��������n�(�(�*�*�*�*�*�*�*�*�*�*�*rU�%Generator[Any, None, WebSocketServer]c�N�|������Sr�)�__await_impl__� __await__rs rSrZzServe.__await__^s ���"�"�$�$�.�.�0�0�0rUc��|K�|����d{V��}|j�|��|jSr�)rRr?r)rbr2s rSrYzServe.__await_impl__bsG�����*�*�,�,�,�,�,�,�,�,�� ����F�#�#�#��~�rU)NN).r=r>r>r?r@rAr5rBr4r@r6rEr5rAr6rBr7rCr8rDr9rEr:rFr;rGr<rHr7rHr8rHr9rHr:rAr;rAr<rCr=rCrIr rJrKr-r.)rJrW) r�r�r�r�rrYr#r,rZrY�__iter__rPrUrSr1r1ss�������Z�Z�F,0��|#�JN�$(�"+�26�>B�59�6:�$*� � �%'�&(�%'�&*�$� $�� �?|#�|#�|#�|#�|#�|#�@����+�+�+�+�1�1�1�1������H�H�HrUr1r=r>r�rErIr rJc �"�t|f|dd�|��S)a� Start a WebSocket server listening on a Unix socket. This function is identical to :func:`serve`, except the ``host`` and ``port`` arguments are replaced by ``path``. It is only available on Unix. Unrecognized keyword arguments are passed the event loop's :meth:`~asyncio.loop.create_unix_server` method. It's useful for deploying a server behind a reverse proxy such as nginx. Args: path: File system path to the Unix socket. T)r�rJ)r-)r=r�rIs rSr.r.os!��0 �� <�$�T� <� <�V� <� <�<rU�3Callable[[WebSocketServerProtocol], Awaitable[Any]]c��� tj����d��nm#t$r` tj����dd��t jdt ��d�fd� }|cYS#t$rYnwxYwYnwxYwtttgttf���S) NrNz$remove second argument of ws_handlerrpr/rJr c���K�ttttgtt f���||j���d{V��Sr�)r r r/r�rr r�)rpr=s �rS� _ws_handlerz)remove_path_argument.<locals>._ws_handler�sh������T��5�s�;�Y�s�^�K�L�����Y�^�-�-�-�-�-�-�-�-�-rU)rpr/rJr ) �inspect� signature�bind� TypeErrorrZr[r\r r r/rr )r=ras` rSr]r]�s��� ���*�%�%�*�*�4�0�0�0�0�� ���� � � �j� )� )� .� .�t�R� 8� 8� 8� �M�@�BT� U� U� U� -� -� -� -� -� -� � � � ��� � � � �D� ���� �D� ����$ ��)�*�I�c�N�:�;�� � �s2�'+� B�(B�"B� B� B�B�B�Br�)r=r>r�rErIr rJr1)r=r>rJr^)J� __future__rrx� email.utilsr�rNr�rbrVr�rZ�collections.abcrrrr�typesr�typingr r r r �asyncio.compatibilityr�datastructuresrrr� exceptionsrrrrrrr6rr�extensions.permessage_deflaterr~rrrr�http11rrr r!r"r#r$r%r'rwr(r)r*r+r,�__all__r��HeadersLikeOrCallable�tuple�bytes� HTTPResponser/r0r1r-r.r]rPrUrS�<module>rusf��"�"�"�"�"�"������������� � � � ��������� � � � �����D�D�D�D�D�D�D�D�D�D�D�D�������-�-�-�-�-�-�-�-�-�-�-�-�3�3�3�3�3�3�F�F�F�F�F�F�F�F�F�F�����������������;�:�:�:�:�:�:�:�L�L�L�L�L�L������������� ������������Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�Q�&�&�&�&�&�&�4�4�4�4�4�4�4�4�������8�8�8�8�8�8�8�8� � � ���k�8�S�'�N�K�4O�+P�P�Q���Z��e�3�4� �N �N �N �N �N �5�N �N �N �bf!�f!�f!�f!�f!�f!�f!�f!�Rv�v�v�v�v�v�v�v�r ��� =�=�=�=�=�6�����rU
Memory