� ���gk���ddlZddlZddlmZddlmZddlmZddlmZe��ZGd�dej ��Z dS) �N�)� base_server)� exceptions)�packet)� async_socketc��eZdZdZd�Zd�Zdd�Zd�Zd�Zd�Z d �Z d �Z dd �Z d �Z d�Zd�Zdd�Zd�Zd�Zd�Zd�Zdd�Zd�Zd�Zd S)� AsyncServerawAn Engine.IO server for asyncio. This class implements a fully compliant Engine.IO web server with support for websocket and long-polling transports, compatible with the asyncio framework on Python 3.5 or newer. :param async_mode: The asynchronous model to use. See the Deployment section in the documentation for a description of the available options. Valid async modes are "aiohttp", "sanic", "tornado" and "asgi". If this argument is not given, "aiohttp" is tried first, followed by "sanic", "tornado", and finally "asgi". The first async mode that has all its dependencies installed is the one that is chosen. :param ping_interval: The interval in seconds at which the server pings the client. The default is 25 seconds. For advanced control, a two element tuple can be given, where the first number is the ping interval and the second is a grace period added by the server. :param ping_timeout: The time in seconds that the client waits for the server to respond before disconnecting. The default is 20 seconds. :param max_http_buffer_size: The maximum size that is accepted for incoming messages. The default is 1,000,000 bytes. In spite of its name, the value set in this argument is enforced for HTTP long-polling and WebSocket connections. :param allow_upgrades: Whether to allow transport upgrades or not. :param http_compression: Whether to compress packages when using the polling transport. :param compression_threshold: Only compress messages when their byte size is greater than this value. :param cookie: If set to a string, it is the name of the HTTP cookie the server sends back tot he client containing the client session id. If set to a dictionary, the ``'name'`` key contains the cookie name and other keys define cookie attributes, where the value of each attribute can be a string, a callable with no arguments, or a boolean. If set to ``None`` (the default), a cookie is not sent to the client. :param cors_allowed_origins: Origin or list of origins that are allowed to connect to this server. Only the same origin is allowed by default. Set this argument to ``'*'`` to allow all origins, or to ``[]`` to disable CORS handling. :param cors_credentials: Whether credentials (cookies, authentication) are allowed in requests to this server. :param logger: To enable logging set to ``True`` or pass a logger object to use. To disable logging set to ``False``. Note that fatal errors are logged even when ``logger`` is ``False``. :param json: An alternative json module to use for encoding and decoding packets. Custom json modules must have ``dumps`` and ``loads`` functions that are compatible with the standard library versions. :param async_handlers: If set to ``True``, run message event handlers in non-blocking threads. To run handlers synchronously, set to ``False``. The default is ``True``. :param monitor_clients: If set to ``True``, a background task will ensure inactive clients are closed. Set to ``False`` to disable the monitoring task (not recommended). The default is ``True``. :param transports: The list of allowed transports. Valid transports are ``'polling'`` and ``'websocket'``. Defaults to ``['polling', 'websocket']``. :param kwargs: Reserved for future extensions, any additional parameters given as keyword arguments will be silently ignored. c��dS)NT���selfs �e/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/engineio/async_server.py�is_asyncio_basedzAsyncServer.is_asyncio_basedSs���t�c� �gd�S)N)�aiohttp�sanic�tornado�asgir r s r� async_modeszAsyncServer.async_modesVs��6�6�6�6r� engine.ioc�h�|�d��}|jd||d|�d���dS)z.Attach the Engine.IO server to an application.�/� create_routeN)�strip�_async)r �app� engineio_paths r�attachzAsyncServer.attachYsB��%�+�+�C�0�0� �#�� �N�#�C��/C�=�/C�/C�/C�D�D�D�D�Drc��~K�|�|tjtj|������d{V��dS)aiSend a message to a client. :param sid: The session id of the recipient client. :param data: The data to send to the client. Data can be of type ``str``, ``bytes``, ``list`` or ``dict``. If a ``list`` or ``dict``, the data will be serialized as JSON. Note: this method is a coroutine. )�dataN)� send_packetr�Packet�MESSAGE)r �sidr!s r�sendzAsyncServer.send^sG�������s�F�M�&�.�t�$L�$L�$L�M�M�M�M�M�M�M�M�M�M�Mrc���K� |�|��}n,#t$r|j�d|��YdSwxYw|�|���d{V��dS)z�Send a raw packet to a client. :param sid: The session id of the recipient client. :param pkt: The packet to send to the client. Note: this method is a coroutine. zCannot send to sid %sN)� _get_socket�KeyError�logger�warningr&)r r%�pkt�sockets rr"zAsyncServer.send_packetjs����� ��%�%�c�*�*�F�F��� � � � �K� � � 7�� =� =� =� �F�F� �����k�k�#�����������s��%A�Ac��>K�|�|��}|jS)a;Return the user session for a client. :param sid: The session id of the client. The return value is a dictionary. Modifications made to this dictionary are not guaranteed to be preserved. If you want to modify the user session, use the ``session`` context manager instead. �r(�session�r r%r-s r� get_sessionzAsyncServer.get_sessionzs#�����!�!�#�&�&���~�rc��BK�|�|��}||_dS)z�Store the user session for a client. :param sid: The session id of the client. :param session: The session dictionary. Nr/)r r%r0r-s r� save_sessionzAsyncServer.save_session�s&���� �!�!�#�&�&�� ����rc�:��G�fd�d��}||���S)aTReturn the user session for a client with context manager syntax. :param sid: The session id of the client. This is a context manager that returns the user session dictionary for the client. Any changes that are made to this dictionary inside the context manager block are saved back to the session. Example usage:: @eio.on('connect') def on_connect(sid, environ): username = authenticate_user(environ) if not username: return False with eio.session(sid) as session: session['username'] = username @eio.on('message') def on_message(sid, msg): async with eio.session(sid) as session: print('received message from ', session['username']) c�*��eZdZd�Z�fd�Z�fd�ZdS)�5AsyncServer.session.<locals>._session_context_managerc�0�||_||_d|_dS�N)�serverr%r0)r r:r%s r�__init__z>AsyncServer.session.<locals>._session_context_manager.__init__�s��$�� ����#�� � � rc��`�K�|j�����d{V��|_|jSr9)r:r2r0)r r%s �r� __aenter__z@AsyncServer.session.<locals>._session_context_manager.__aenter__�s8�����%)�[�%<�%<�S�%A�%A�A�A�A�A�A�A�� ��|�#rc��X�K�|j��|j���d{V��dSr9)r:r4r0)r �argsr%s �r� __aexit__z?AsyncServer.session.<locals>._session_context_manager.__aexit__�s9������k�.�.�s�D�L�A�A�A�A�A�A�A�A�A�A�ArN)�__name__� __module__� __qualname__r;r=r@)r%s�r�_session_context_managerr7�s]������� $� $� $�  $� $� $� $� $� B� B� B� B� B� B� BrrDr )r r%rDs ` rr0zAsyncServer.session�sT���, B� B� B� B� B� B� B� B� B� B�(�'��c�2�2�2rNc��\�K�|�b ��|��}|��jj����d{V��|�jvr �j|=dSdS#t $rYdSwxYwt j�fd��j���D�����d{V��i�_dS)z�Disconnect a client. :param sid: The session id of the client to close. If this parameter is not given, then all clients are closed. Note: this method is a coroutine. N��reasonc�t��g|]4}tj|��jj�������5S)rF)�asyncio� create_task�closerG�SERVER_DISCONNECT)�.0�clientr s �r� <listcomp>z*AsyncServer.disconnect.<locals>.<listcomp>�sU��� � � ���#�F�L�L��;�8�%1�%:�%:�;�;� � � r) r(rKrGrL�socketsr)rI�wait�valuesr1s` r� disconnectzAsyncServer.disconnect�s����� �?� *��)�)�#�.�.�� �l�l�$�+�*G�l�H�H�H�H�H�H�H�H�H��$�,�&�&�� �S�)�)�)�'�&�� � � � ���� �����,� � � � �#�l�1�1�3�3� � � ��� � � � � � � � �D�L�L�Ls�A� A%�$A%c�� K�|jd}tj|��r||i|���d{V��}n||i|��}|jgkr}|�d��}|rf|�|��}|�O||vrK|�|dzd��|�|�|dz��|���d{V��S|d}tj � |�dd����}d |vr|d d nd} d } d} |�d d g��d } | |j vrE|�dd��|�|�d��|���d{V��Sd |vr|d d nd} | �_|�d��dgkrE|�dd��|�|�d��|���d{V��Sd|vr<d} t|dd ��} n#ttt f$rYnwxYw| r/| �-|�dd��|�d��} �n�|dk�r=d|vr'|�d�����nd}| �b| d ks| |cxkrdkr"nn|�|| | ���d{V��} �n/|�dd��|�d��} �n| |jvr3|�d| ��d��|�d| ����} �n� |�| ��}|�| ��| kr6| |kr0|�d| ��d��|�d��} �na |�|���d{V��}t/|t0��r|�|| � ��} n|} nM#t4j$r;| |jvr|�| ���d{V��|���} YnwxYw| |jvr|j| jr|j| =�n�#t$r@}|�|�d!| ��d��|�|�d!| ����} Yd}~�n^d}~wwxYw|d"k�r| � | |jvr3|�d| ��d��|�d| ����} �n|�| ��} |�|���d{V��|�| � ��} n�#t4j$r;| |jvr|�| ���d{V��|���} Yn�|j� d#��|�| � ��} YnMxYw|d$kr|���} n/|j�!d%|��|�"��} t/| tF��s| S|j$r�tK| d&��|j&kr{d'�|�d(d���'d)��D��}|D]E}||j(vr:tS|d*|z��| d&��| d&<| d+xxd,|fgz cc<n�F|�| |���d{V��S)-z�Handle an HTTP request from the client. This is the entry point of the Engine.IO application. This function returns the HTTP response to deliver to the client. Note: this method is a coroutine. �translate_requestN� HTTP_ORIGINz is not an accepted origin.z bad-origin�REQUEST_METHOD� QUERY_STRING�r%rF� transport�pollingzInvalid transportz bad-transport�EIO�4zRThe client is using an unsupported version of the Socket.IO or Engine.IO protocolsz bad-version�jTzInvalid JSONP index numberzbad-jsonp-index�GET� HTTP_UPGRADE� websocketzInvalid websocket upgradez bad-upgradezInvalid session zbad-sidzInvalid transport for session )� jsonp_index� �POSTzpost request handler error�OPTIONSzMethod %s not supported�responsec�h�g|]/}|�d��d�����0S)�;r)�splitr)rM�es rrOz.AsyncServer.handle_request.<locals>.<listcomp>\sD��M�M�M�Q�������a��.�.�0�0�M�M�Mr�HTTP_ACCEPT_ENCODING�,�_�headerszContent-Encoding)*rrI�iscoroutinefunction�cors_allowed_origins�get�_cors_allowed_origins�_log_error_once�_make_response� _bad_request�urllib�parse�parse_qs� transports�int� ValueErrorr)� IndexError�lower�_handle_connectrPr(rZ�handle_get_request� isinstance�list�_okr� EngineIOErrorrS�closed�handle_post_requestr*� exceptionr+�_method_not_found�dict�http_compression�len�compression_thresholdri�compression_methods�getattr)r r?�kwargsrU�environ�origin�allowed_origins�method�queryr%�jsonprbrZ�r�upgrade_headerr-�packetsrj� encodings�encodings r�handle_requestzAsyncServer.handle_request�s ����!�K�(;�<�� � &�'8� 9� 9� 9�-�-�t�>�v�>�>�>�>�>�>�>�>�G�G�'�'��8��8�8�G� � $�� *� *��[�[��/�/�F�� !�"&�"<�"<�W�"E�"E��"�.�6�'�4(�4(��(�(��!>�>� �N�N�N�!%�!4�!4��)�)�"�%B�B�D�D��"!�"!�!�!�!�!�!�!�!� �)�*��� �%�%�g�k�k�.�"�&E�&E�F�F��!&�%���e�E�l�1�o�o�T����� ��I�I�k�I�;�7�7��:� � �D�O� +� +� � � �!4�o� F� F� F��,�,��!�!�"5�6�6��A�A�A�A�A�A�A�A� A�"'�%���e�E�l�1�o�o�T�� �;�5�9�9�U�+�+��u�4�4� � � �)�*7� � � ��,�,�T�->�->�)�.�.���������� � �%�<�<��E� �!�%��*�Q�-�0�0� � ����*�5� � � ��� ���� �I )�[�(� � � �!=�!2� 4� 4� 4��!�!�">�?�?�A�A� �u�_�_�!�W�,�,�%�[�[��8�8�>�>�@�@�@�26� ��{�� �)�)�$��E�E�E�E�+�E�E�E�E�E�"�2�2�7�I�3>�@�@�@�@�@�@�@�@�A�A��(�(�)D�)6�8�8�8��)�)�*E�F�F�A�A��d�l�*�*��(�(�)A�C�)A�)A�9�M�M�M��)�)�*B�S�*B�*B�C�C�A�A�6�!%�!1�!1�#�!6�!6��  �>�>�#�.�.�)�;�;� )�^� ;� ;� �0�0� F�� F� F� /�1�1�1�!%� 1� 1�2E� F� F�A�A� 8�06�0I�0I�$+�1-�1-�+-�+-�+-�+-�+-�+-��#-�g�t�#<�#<�!0�(,����=H�)1�)J�)J�A�A�)0�A���#-�#;�8�8�8�#&�$�,�#6�#6�*.�/�/�#�*>�*>�$>�$>�$>�$>�$>�$>�$>�$(�$5�$5�$7�$7����8���� #�d�l�2�2�$(�L��$5�$<� 3�$(�L��$5���3$�<�<�<��,�,��\�\�C�\�\�9�E�E�E� �-�-��l�l�S�l�l�;�;�����������<����4�v� � ��{�c���5�5��$�$�%=��%=�%=�y�I�I�I��%�%�&>��&>�&>�?�?����)�)�#�.�.�� :� �4�4�W�=�=�=�=�=�=�=�=�=����[��9�9�A�A��!�/�,�,�,��d�l�*�*�"�o�o�c�2�2�2�2�2�2�2�2�2��)�)�+�+�A�A�A�:��K�)�)�*F�G�G�G����[��9�9�A�A�A���� �y� � ���� � �A�A� �K� � � 9�6� B� B� B��&�&�(�(�A��!�T�"�"� ��H� � � ��A�j�M�"�"�d�&@�@�@�M�M� ���%;�R�@�@�F�F�s�K�K�M�M�M�I�%� � ���t�7�7�7�5���c�H�n�5�5�a� �m�D�D��j�M��i�L�L�L�&8�(�%C�$D�D�L�L�L��E� 8� �(�(��G�4�4�4�4�4�4�4�4�4sQ�=H�H3�2H3�<Q�!A O,�,AP6�5P6� R(�(5R#�#R(�1T8�8AV4�1V4c��K�|j�d��|jr/|j���|j�d{V��d|_dSdS)z�Stop Socket.IO background tasks. This method stops background activity initiated by the Socket.IO server. It must be called before shutting down the web server. zSocket.IO is shutting downN)r*�info�service_task_event�set�service_task_handler s r�shutdownzAsyncServer.shutdownfsr���� � ���5�6�6�6� � "� ,� � #� '� '� )� )� )��*� *� *� *� *� *� *� *�'+�D� $� $� $� ,� ,rc�6�tj||i|����S)a�Start a background task using the appropriate async model. This is a utility function that applications can use to start a background task using the method that is compatible with the selected async mode. :param target: the target function to execute. :param args: arguments to pass to the function. :param kwargs: keyword arguments to pass to the function. The return value is a ``asyncio.Task`` object. )rI� ensure_future)r �targetr?r�s r�start_background_taskz!AsyncServer.start_background_taskrs$���$�V�V�T�%<�V�%<�%<�=�=�=rrc��:K�tj|���d{V��S)a<Sleep for the requested amount of time using the appropriate async model. This is a utility function that applications can use to put a task to sleep without having to worry about using the correct call for the selected async mode. Note: this method is a coroutine. N)rI�sleep)r �secondss rr�zAsyncServer.sleep�s*�����]�7�+�+�+�+�+�+�+�+�+rc�$�tj|i|��S)aICreate a queue object using the appropriate async model. This is a utility function that applications can use to create a queue without having to worry about using the correct call for the selected async mode. For asyncio based async modes, this returns an instance of ``asyncio.Queue``. )rI�Queue�r r?r�s r� create_queuezAsyncServer.create_queue�����}�d�-�f�-�-�-rc��tjS)aZReturn the queue empty exception for the appropriate async model. This is a utility function that applications can use to work with a queue without having to worry about using the correct call for the selected async mode. For asyncio based async modes, this returns an instance of ``asyncio.QueueEmpty``. )rI� QueueEmptyr s r�get_queue_empty_exceptionz%AsyncServer.get_queue_empty_exception�s ���!�!rc�$�tj|i|��S)aKCreate an event object using the appropriate async model. This is a utility function that applications can use to create an event without having to worry about using the correct call for the selected async mode. For asyncio based async modes, this returns an instance of ``asyncio.Event``. )rI�Eventr�s r� create_eventzAsyncServer.create_event�r�rc��K�|�|��}|jd}tj|��r*||d|d|z|d|���d{V��}n#||d|d|z|d|��}|S)N� make_response�statusrnrf)� _cors_headersrrIro)r � response_dictr�� cors_headersr�rfs rrtzAsyncServer._make_response�s������)�)�'�2�2� �� �O�4� � � &�}� 5� 5� 4�*�]��h�'��i�(�<�7��j�)�7�4�4�4�4�4�4�4�4�H�H� %�}��h�'��i�(�<�7��j�)�7�4�4�H��rc ���K�|jr&d|_|�|j��|_|���}t j||��}||j|<tj tj ||� ||��t|j dz��t|j|jz��dz|jd���}|�|���d{V��|���|�d||d����d{V��}|�=|dur9|j|=|j�d��|�|pd��S|d kr5|�|���d{V��}|jr||jvr|j|=|Sd|_d}|jr[t7|jt8��rd |�||j��fg}n"d |�||jd d d ���fg} |�|����d{V��||���S#t@j!$r|�"��cYSwxYw)z#Handle a client connection request.Fi�)r%�upgrades� pingTimeout� pingInterval� maxPayloadN�connect)� run_asyncTzApplication rejected connectionraz Set-Cookier�Lax)�name�path�SameSite)rnrb)#�start_service_taskr�� _service_taskr�� generate_idr� AsyncSocketrPrr#�OPEN� _upgradesrz� ping_timeout� ping_interval�ping_interval_grace_period�max_http_buffer_sizer&� schedule_ping�_trigger_eventr*r+� _unauthorizedrr�� connected�cookier�r��_generate_sid_cookier��pollrr�ru) r r�rZrbr%�sr,�retrns rr~zAsyncServer._handle_connect�s����� � "� $�&+�D� #�'+�'A�'A��"�($�($�D� $���� � �� � $�T�3� /� /���� �S���m�F�K�����s�I�6�6��t�0�4�7�8�8���"�T�%D�D�F�F�HL�M��3� * �* � � ���f�f�S�k�k�������� �������'�'� �3��27�(�9�9�9�9�9�9�9�9�� �?�s�$���� �S�!� �K� � � A� B� B� B��%�%�c�k�T�2�2� 2� � � #� #��,�,�W�5�5�5�5�5�5�5�5�C��x� &�C�4�<�/�/��L��%��J��A�K��G��{� ��d�k�4�0�0� �$��1�1�#�t�{�C�C� ��G�G� %��1�1�#�$(�K��%�8�8��� ��G�  +��x�x�a�f�f�h�h��������,7� �9�9�9���(� +� +� +��(�(�*�*�*�*�*� +���s�/H7�7#I�Ic��2���K�|�dd��}d}��jvr�tj�j���ri���fd�}|rO��|��}t �|��|�t j��ny|���d{V��}nh���fd�}|rO��|��}t �|��|�t j��n|���d{V��}|S)zInvoke an event handler.r�FNc��N�K� �j����d{V��S#t$r?�dkr7t���dkr$�j��d���d{V��cYS�wxYw#tj$rYdS�j��dz���dkrYdSYdSxYw)NrS�rz async handler errorr�F)�handlers� TypeErrorr�rI�CancelledErrorr*r��r?�eventr s���r�run_async_handlerz5AsyncServer._trigger_event.<locals>.run_async_handler�s�����)� &�)=���u�)=�t�)D�#D�#D�#D�#D�#D�#D�D��(�&�&�&�$� �4�4�$'��I�I��N�N�.B�T�]�5�-A�$�q�'�-J�-J�'J�'J�'J�'J�'J�'J� J� J� J� %�&�����#�1�������)�� �-�-�e�6L�.L�M�M�M� �I�-�-�$)�5�5�.�-�-���s(��AA%�!A(�#A%�%A(�(B$�:$B$c���K� �j���S#t$r9�dkr1t���dkr�j��d��cYS�wxYw#�j��dz���dkrYdSYdSxYw)NrSr�rz handler errorr�F)r�r�r�r*r�r�s���r�run_sync_handlerz4AsyncServer._trigger_event.<locals>.run_sync_handlers������)� &�#7�4�=��#7��#>�>��(�&�&�&�$� �4�4�$'��I�I��N�N�(<�t�}�U�';�D��G�'D�'D� D� D� D� %�&�����)�� �-�-�e�6F�.F�G�G�G� �I�-�-�$)�5�5�.�-�-���s!��?A�A�A�A�%B) �popr�rIror��task_reference_holder�add�add_done_callback�discard)r r�r?r�r�r�r�r�s``` rr�zAsyncServer._trigger_event�so��������J�J�{�E�2�2� ��� �D�M� !� !��*�4�=��+?�@�@�5 3�)�)�)�)�)�)�)�*�4��4�4�5F�G�G�C�)�-�-�c�2�2�2��)�)�*?�*G�H�H�H�H� 1� 1� 3� 3�3�3�3�3�3�3�C�C�)�)�)�)�)�)�)�&�3��4�4�5E�F�F�C�)�-�-�c�2�2�2��)�)�*?�*G�H�H�H�H� 0� 0� 2� 2�2�2�2�2�2�2�C�� rc��K�tj��}|���|_|j����s�t |j��dkrP tj|j���|j ����d{V��dS#tj $rY�~wxYw|j t |j��z } |j� ��� ��D]�}|j r |j|j=n1#t$rYn%wxYw|js|����d{V�� tj|j���|����d{V��t%���#tj $rY��wxYwn�#t&t$tjt*f$r|j�d��YdS|���r|j�d��YdS|j�d��YnxYw|j������dSdS)z;Monitor connected clients and clean up those that time out.r)�timeoutNzservice task canceledz*event loop is closed, exiting service taskzservice task exception)rI�get_running_loopr�r��is_setr�rP�wait_forrQr�� TimeoutError�copyrRr�r%r)�closing�check_ping_timeout�KeyboardInterrupt� SystemExitr�� GeneratorExitr*r�� is_closedr�)r �loop�sleep_intervalr�s rr�zAsyncServer._service_task1s������'�)�)��"&�"3�"3�"5�"5����)�0�0�2�2�. @��4�<� � �A�%�%��!�*�4�+B�+G�+G�+I�+I�37�3D�F�F�F�F�F�F�F�F�F�F��E���+�����H�����"�.��T�\�1B�1B�B�N�! @���*�*�,�,�3�3�5�5�!�!�A��x�5�!� $� �Q�U� 3� 3��'�!�!�!�!�D�!�����Y�5��2�2�4�4�4�4�4�4�4�4�4�!�%�.�t�/F�/K�/K�/M�/M�7E�G�G�G�G�G�G�G�G�G�G�/�1�1�1��"�/�!�!�!� ��!����!��"�!��&�� � � � � � � � �!8�9�9�9���� @��>�>�#�#���K�$�$�&,�-�-�-��E�E�� �%�%�&>�?�?�?�?�?����]�)�0�0�2�2�. @�. @�. @�. @�. @sm�"8B�B.�-B.�4F� D�F� D�F�D�$F�AF�F�F�F�F�<H'�/H'� H')rr9)r)rArBrC�__doc__rrrr&r"r2r4r0rSr�r�r�r�r�r�r�rtr~r�r�r rrr r st������B�B�F���7�7�7�E�E�E�E� N� N� N���� � � �!�!�!�#3�#3�#3�J����4V5�V5�V5�p ,� ,� ,� >� >� >� ,� ,� ,� ,�.�.�.�"�"�"�.�.�.� � � �8+�8+�8+�8+�t;�;�;�z2@�2@�2@�2@�2@rr ) rIrvrYrrrrr�r�� BaseServerr r rr�<module>r�s������� � � � ������������������������� �����T @�T @�T @�T @�T @�+�(�T @�T @�T @�T @�T @r
Memory