� ���g�Y���ddlZddlZddlmZddlmZddlmZddlmZejd��ZGd�d ej ��Z dS) �N�)� base_server)� exceptions)�packet)�socketzengineio.serverc�h�eZdZdZd�Zd�Zd�Zd�Zd�Zdd�Z d �Z d �Z d �Z dd �Z dd�Zd�Zd�ZdS)�Servera,An Engine.IO server. This class implements a fully compliant Engine.IO web server with support for websocket and long-polling transports. :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 "threading", "eventlet", "gevent" and "gevent_uwsgi". If this argument is not given, "eventlet" is tried first, then "gevent_uwsgi", then "gevent", and finally "threading". 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. The default is ``True``. :param http_compression: Whether to compress packages when using the polling transport. The default is ``True``. :param compression_threshold: Only compress messages when their byte size is greater than this value. The default is 1024 bytes. :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. The default is ``True``. :param logger: To enable logging set to ``True`` or pass a logger object to use. To disable logging set to ``False``. The default is ``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�n�|�|tjtj|�����dS)a>Send 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. )�dataN)� send_packetr�Packet�MESSAGE)�self�sidr s �_/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/engineio/server.py�sendz Server.sendSs1�� ����f�m�F�N��F�F�F�G�G�G�G�G�c�� |�|��}n,#t$r|j�d|��YdSwxYw|j|��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. zCannot send to sid %sN)� _get_socket�KeyError�logger�warningr)rr�pktrs rr zServer.send_packet]sq��  ��%�%�c�*�*�F�F��� � � � �K� � � 7�� =� =� =� �F�F� ���� �� �C�����s��%A�Ac�:�|�|��}|jS)aCReturn 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 unless ``save_session()`` is called, or when the ``session`` context manager is used. �r�session)rrrs r� get_sessionzServer.get_sessionks���!�!�#�&�&���~�rc�>�|�|��}||_dS)z�Store the user session for a client. :param sid: The session id of the client. :param session: The session dictionary. Nr)rrrrs r� save_sessionzServer.save_sessionxs"�� �!�!�#�&�&�� ����rc�:��G�fd�d��}||���S)aNReturn 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): with eio.session(sid) as session: print('received message from ', session['username']) c�*��eZdZd�Z�fd�Z�fd�ZdS)�0Server.session.<locals>._session_context_managerc�0�||_||_d|_dS�N)�serverrr)rr%rs r�__init__z9Server.session.<locals>._session_context_manager.__init__�s��$�� ����#�� � � rc�P��|j����|_|jSr$)r%rr)rrs �r� __enter__z:Server.session.<locals>._session_context_manager.__enter__�s"���#�{�6�6�s�;�;�� ��|�#rc�H��|j��|j��dSr$)r%rr)r�argsrs �r�__exit__z9Server.session.<locals>._session_context_manager.__exit__�s#���� �(�(��d�l�;�;�;�;�;rN)�__name__� __module__� __qualname__r&r(r+)rs�r�_session_context_managerr"�sV������� $� $� $�  $� $� $� $� $� <� <� <� <� <� <� <rr/�)rrr/s ` rrzServer.session�sJ���, <� <� <� <� <� <� <� <� <� <�(�'��c�2�2�2rNc�b�|�W |�|��}|j|jj���||jvr |j|=dSdS#t $rYdSwxYw|j������D]"}|�|jj����#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. N)�reason)r�closer2�SERVER_DISCONNECT�socketsr�copy�values)rrr�clients r� disconnectzServer.disconnect�s��� �?� *��)�)�#�.�.�� �� �D�K�$A�B�B�B�B��$�,�&�&�� �S�)�)�)�'�&�� � � � ���� �����,�+�+�-�-�4�4�6�6� C� C��� � �D�K�$A� �B�B�B�B��D�L�L�Ls�A � A�Ac� �|jgkr�|�d��}|rj|�|��}|�S||vrO|�|dzd��|�d��}||d|d��|dgS|d }t j�|�d d ����}d }d} |�d dg��d} | |jvrL|�dd��|�d��}||d|d��|dgSd|vr|ddnd} | �f|�d��dgkrL|�dd��|�d��}||d|d��|dgSd|vr<d} t|dd��} n#tttf$rYnwxYw|r/| �-|�dd��|�d��}�ns|dk�r(d|vr'|�d��� ��nd} | �]| dks| | cxkrdkrnn|�||| | ��}�n |�dd��|�d��}�n�| |jvr3|�d | ��d!��|�d | ����}�n� |�| ��} |�| ��| kr6| | kr0|�d"| ��d��|�d��}�n? | j||��}t'|t(��r|�|| �#��}n|}nG#t,j$r5| |jvr|�| ��|���}YnwxYw| |jvr|j| jr|j| =�n�#t$r@}|�|�d$| ��d!��|�|�d$| ����}Yd}~�nLd}~wwxYw|d%kr�| � | |jvr3|�d | ��d!��|�d | ����}�n|�| ��} | j|��|�| �#��}n�#t,j$r5| |jvr|�| ��|���}Yn�|j�d&��|�| �#��}YnMxYw|d'kr|���}n/|j�d(|��|���}t'|t>��s|S|j r�tC|d��|j"kr{d)�|�d*d ���#d+��D��}|D]E}||j$vr:tK|d,|z��|d��|d<|dxxd-|fgz cc<n�F|�&|��}||d|d|z��|dgS).a+Handle an HTTP request from the client. This is the entry point of the Engine.IO application, using the same interface as a WSGI application. For the typical usage, this function is invoked by the :class:`Middleware` instance, but it can be invoked directly when the middleware is not used. :param environ: The WSGI environment. :param start_response: The WSGI ``start_response`` function. This function returns the HTTP response body to deliver to the client as a byte sequence. � HTTP_ORIGINNz is not an accepted origin.z bad-originzNot an accepted origin.�status�headers�response�REQUEST_METHOD� QUERY_STRING�F� transport�pollingrzInvalid transportz bad-transportr�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 supportedc�h�g|]/}|�d��d�����0S)�;r)�split�strip)�.0�es r� <listcomp>z)Server.handle_request.<locals>.<listcomp>HsD��M�M�M�Q�������a��.�.�0�0�M�M�Mr�HTTP_ACCEPT_ENCODING�,�_zContent-Encoding)'�cors_allowed_origins�get�_cors_allowed_origins�_log_error_once� _bad_request�urllib�parse�parse_qs� transports�int� ValueErrorr� IndexError�lower�_handle_connectr5rrB�handle_get_request� isinstance�list�_okr� EngineIOErrorr9�closed�handle_post_requestr� exceptionr�_method_not_found�dict�http_compression�len�compression_thresholdrP�compression_methods�getattr� _cors_headers)r�environ�start_response�origin�allowed_origins�r�method�query�jsonprJrBr�upgrade_headerr�packetsrS� encodings�encoding� cors_headerss r�handle_requestzServer.handle_request�s��� � $�� *� *��[�[��/�/�F�� +�"&�"<�"<�W�"E�"E��"�.�6�'�4(�4(��(�(��!>�>� �N�N�N��)�)�*C�D�D�A�"�N�1�X�;��)� �=�=�=��j�M�?�*��)�*��� �%�%�g�k�k�.�"�&E�&E�F�F����� ��I�I�k�I�;�7�7��:� � �D�O� +� +� � � �!4�o� F� F� F��!�!�"5�6�6�A� �N�1�X�;��)� � 5� 5� 5��j�M�?� "�"'�%���e�E�l�1�o�o�T�� �;�5�9�9�U�+�+��u�4�4� � � �)�*7� 9� 9� 9��!�!�)�*�*�A� �N�1�X�;��)� � 5� 5� 5��j�M�?� "� �%�<�<��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��,�,�W�n�-6� �E�E�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�*C�&�*C�$+�^�+=�+=��#-�g�t�#<�#<�!0�(,����=H�)1�)J�)J�A�A�)0�A���#-�#;�8�8�8�#&�$�,�#6�#6�$(�O�O�C�$8�$8�$8�$(�$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��%�%�&>��&>�&>�?�?����)�)�#�.�.�� :�.�F�.�w�7�7�7����[��9�9�A�A��!�/�,�,�,��d�l�*�*�����,�,�,��)�)�+�+�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� �)�)�'�2�2� ���q��{�A�i�L�<�$?�@�@�@��*� ��sQ�G�G7�6G7�;P � AN!�!AO%�$O%� Q�5Q�Q�4&S�AU�1Uc��|j�d��|jr;|j���|j���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_handle�join)rs r�shutdownzServer.shutdownTsh�� � ���5�6�6�6� � "� ,� � #� '� '� )� )� )� � $� )� )� +� +� +�'+�D� $� $� $� ,� ,rc�`�|jd|||���}|���|S)aOStart 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. This function returns an object that represents the background task, on which the ``join()`` methond can be invoked to wait for the task to complete. �thread)�targetr*�kwargs)�_async�start)rr�r*r��ths r�start_background_taskzServer.start_background_task`s3��#�T�[�� "�&�t�F� K� K� K�� ��� � � �� rrc�.�|jd|��S)aSleep 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. �sleep)r�)r�secondss rr�z Server.sleepss��$�t�{�7�#�G�,�,�,rc ��|jr&d|_|�|j��|_|���}t j||��}||j|<tj tj ||� ||��t|j dz��t|j|jz��dz|jd���}|�|��|���|�d||d���}|�=|dur9|j|=|j�d��|�|pd��S|d kr0|�||��}|jr||jvr|j|=|Sd|_d} |jr[t7|jt8��rd |�||j��fg} n"d |�||jd d d ���fg} |�|���| |���S#t@j!$r|�"��cYSwxYw)z#Handle a client connection request.Fi�)r�upgrades� pingTimeout� pingInterval� maxPayload�connect)� run_asyncNTzApplication rejected connectionrIz Set-Cookie�/�Lax)�name�path�SameSite)r=rJ)#�start_service_taskr�� _service_taskr�� generate_idr�Socketr5rr �OPEN� _upgradesra� ping_timeout� ping_interval�ping_interval_grace_period�max_http_buffer_sizer� schedule_ping�_trigger_eventrr� _unauthorizedrfrk� connected�cookiergro�_generate_sid_cookieri�pollr� QueueEmptyr\) rrvrwrBrJr�sr�retr=s rrezServer._handle_connect}s��� � "� $�&+�D� #�'+�'A�'A��"�($�($�D� $���� � �� �M�$�� $� $���� �S���m�F�K�����s�I�6�6��t�0�4�7�8�8���"�T�%D�D�F�F�HL�M��3� * �* � � �� ���s� � � � ������ �!�!�)�S�'�U�!�K�K�� �?�s�$���� �S�!� �K� � � A� B� B� B��%�%�c�k�T�2�2� 2� � � #� #��&�&�w��?�?�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������'�,7� �9�9�9���(� +� +� +��(�(�*�*�*�*�*� +���s�4)H�#I�Ic�����|�dd��}��jvr(���fd�}|r��|��S|��SdS)zInvoke an event handler.r�Fc��� �j���S#t$r9�dkr1t���dkr�j��d��cYS�wxYw#�j��dz���dkrYdSYdSxYw)Nr9�rz handler errorr�F)�handlers� TypeErrorrqrrm)r*�eventrs���r� run_handlerz*Server._trigger_event.<locals>.run_handler�s����%� "�3�t�}�U�3�T�:�:��$�"�"�"� �L�0�0� #�D� � �Q���$8�4�=��#7��Q��#@�#@�@�@�@�!�"�����%��K�)�)�%�2B�*B�C�C�C�� �)�)� %�u�u�*�)�)���s!��?A�A�A�A�%BN)�popr�r�)rr�r*r�r�r�s``` rr�zServer._trigger_event�sz������J�J�{�E�2�2� � �D�M� !� !� %� %� %� %� %� %� %�&� %��1�1�+�>�>�>�"�{�}�}�$�/ "� !rc�<�|���|_|j����sht|j��dkr#|j�|j���rdS�Ut|j��t|j��z } |j���� ��D]l}|j r |j|j =n+#t$rYnwxYw|j s|���|j�|���rt����mnN#t tf$r|j�d��YdS|j�d��YnxYw|j�����fdSdS)z;Monitor connected clients and clean up those that time out.r)�timeoutzservice task canceledzservice task exceptionN)� create_eventr��is_setrqr5�waitr��floatr6r7rkrr�closing�check_ping_timeout�KeyboardInterrupt� SystemExitrr�rm)r�sleep_intervalr�s rr�zServer._service_task�s���"&�"3�"3�"5�"5����)�0�0�2�2� @��4�<� � �A�%�%��*�/�/��8I�/�J�J���E��#�4�#4�5�5��D�L�8I�8I�I�N� @���*�*�,�,�3�3�5�5� 2� 2�A��x�/�!� $� �Q�U� 3� 3��'�!�!�!�!�D�!�����Y�/��,�,�.�.�.��.�3�3�N�3�K�K�2�/�1�1�1�2� 2��� 1�2� � � �� � � �!8�9�9�9���� @�� �%�%�&>�?�?�?�?�?����;�)�0�0�2�2� @� @� @� @� @s7�4D2� C�D2� C)�&D2�(C)�)AD2�2+E=� E=r$)r)r,r-r.�__doc__rr rrrr9r�r�r�r�rer�r�r0rrr r s������E�E�LH�H�H� � � � � � �!�!�!�#3�#3�#3�J����*W�W�W�r ,� ,� ,����&-�-�-�-�%)�<+�<+�<+�<+�|%�%�%�8 @� @� @� @� @rr ) �loggingr]rArrrr� getLogger�default_logger� BaseServerr r0rr�<module>r�s������� � � � �������������������������"��"�#4�5�5��k@�k@�k@�k@�k@�[� #�k@�k@�k@�k@�k@r
Memory