� ���g=9���ddlZddlZddlZddlZddlZddlZddlZddlmZddlm Z ej d��Z Gd�d��Z dS)�N�)�packet)�payloadzengineio.serverc���eZdZddgZgd�ZddgZdZdZGd�d ��Z d%d�Z d�Z d�Z d&d�Z d�Z d�Zd�Zd�Zd�Zd�Zd�Zd�Zd'd�Zd&d�Zd�Zd&d�Zd �Zd!�Zd"�Zd#�Zd$�Zd S)(� BaseServer�gzip�deflate)�connect� disconnect�message�polling� websocketTrc�&�eZdZdZdZdZdZdZdZdS)�BaseServer.reasonzDisconnection reasons.zserver disconnectzclient disconnectz ping timeoutztransport closeztransport errorN) �__name__� __module__� __qualname__�__doc__�SERVER_DISCONNECT�CLIENT_DISCONNECT� PING_TIMEOUT�TRANSPORT_CLOSE�TRANSPORT_ERROR���d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/engineio/base_server.py�reasonrs/������$�$�/��/��%� �+��+���rrN���@B�Fc �0��|�_t|t��r|d�_|d�_n|�_d�_|�_|�_|�_|�_|�_ | �_ | �_ | �_ i�_ i�_t���_|�|n�j�_d�_d�_| �| t*j_t| t0��s| �_n�t4�_�jjt8jkrv| r%�j�t8j��n$�j�t8j ���j�!t9j"������#��}|� ||vr|gng}d�_$d�_%|D]m} tMj'd|z��j$�_$d�j$vr �j$dnd}|��(��kr�U|�_%n#tR$rY�jwxYw�j%�tUd�����(��r%d�j$vs �j$dstUd�����(��s%d�j$vr�j$drtUd���|�7t|tV��r|g}�fd �|D��}|stUd ���|p�j,�_-�j�.d �j%��dS) Nrrzengineio.async_drivers.�asyncioFzInvalid async_mode specifiedz1The selected async_mode is not asyncio compatiblezKThe selected async_mode requires asyncio and must use the AsyncServer classc�&��g|] }|�jv� |��Sr)�valid_transports)�.0� transport�selfs �r� <listcomp>z'BaseServer.__init__.<locals>.<listcomp>ks3���A�A�A� �&�$�*?�?�?�$�?�?�?rzNo valid transports providedzServer initialized for %s.)/� ping_timeout� isinstance�tuple� ping_interval�ping_interval_grace_period�max_http_buffer_size�allow_upgrades�http_compression�compression_threshold�cookie�cors_allowed_origins�cors_credentials�async_handlers�sockets�handlers�set�log_message_keys�_default_monitor_clients�start_service_task�service_task_handle�service_task_eventr�Packet�json�bool�logger�default_logger�level�logging�NOTSET�setLevel�INFO�ERROR� addHandler� StreamHandler� async_modes�_async� async_mode� importlib� import_module�is_asyncio_based� ImportError� ValueError�strr%� transports�info)r(rNr-r*r/r0r1r2r3r4r5rBr@r6�monitor_clientsrU�kwargs�modes�mode� asyncio_baseds` r�__init__zBaseServer.__init__#s����)��� �m�U� +� +� 0�!.�q�!1�D� �.;�A�.>�D� +� +�!.�D� �./�D� +�$8��!�,��� 0���%:��"��� �$8��!� 0���,����� ��� � #������*�#2�/�04�0M� ��#'�� �"&��� � �!%�F�M� ��&�$�'�'� @� �D�K�K�(�D�K��{� �G�N�2�2��8��K�(�(���6�6�6�6��K�(�(���7�7�7�� �&�&�w�'<�'>�'>�?�?�?�� � �"�"�� � !�$.�%�$7�$7�Z�L�L�R�E��� ����� � �D� �'�5�-��4�6�6�6<�� �!�D�K�/�/�!%� �I� 6� 6�5:�� �D�$9�$9�$;�$;�;�;��"&������� � � ��� ���� �?� "��;�<�<� <� � � � "� "� +��$�+�-�-���Y�'�.��*�+�+� +��$�$�&�&� ?�9�� �+C�+C�� �I�&�,D��>�?�?� ?� � !��*�c�*�*� *�(�\� �A�A�A�A�Z�A�A�A�J�� A� �!?�@�@�@�$�=��(=��� � ���5�t��G�G�G�G�Gs�<AH�H� H$�#H$c��dS)NFr�r(s rrQzBaseServer.is_asyncio_basedrs���urc� �gd�S)N)�eventlet� gevent_uwsgi�gevent� threadingrr^s rrLzBaseServer.async_modesus��B�B�B�Brc�d�����jvrtd�����fd�}|�|S||��dS)a�Register an event handler. :param event: The event name. Can be ``'connect'``, ``'message'`` or ``'disconnect'``. :param handler: The function that should be invoked to handle the event. When this parameter is not given, the method acts as a decorator for the handler function. Example usage:: # as a decorator: @eio.on('connect') def connect_handler(sid, environ): print('Connection request') if environ['REMOTE_ADDR'] in blacklisted: return False # reject # as a method: def message_handler(sid, msg): print('Received message: ', msg) eio.send(sid, 'response') eio.on('message', message_handler) The handler function receives the ``sid`` (session ID) for the client as first argument. The ``'connect'`` event handler receives the WSGI environment as a second argument, and can return ``False`` to reject the connection. The ``'message'`` handler receives the message payload as a second argument. The ``'disconnect'`` handler does not take a second argument. z Invalid eventc���|�j�<|S�N)r8)�handler�eventr(s ��r� set_handlerz"BaseServer.on.<locals>.set_handler�s���#*�D�M�%� ��NrN)� event_namesrS)r(rhrgris`` r�onz BaseServer.onxsc����> ��(� (� (��_�-�-� -� � � � � � � �?�� �� �G�����rc�>�|�|��jrdndS)z�Return the name of the transport used by the client. The two possible values returned by this function are ``'polling'`` and ``'websocket'``. :param sid: The session of the client. rr )� _get_socket�upgraded)r(�sids rr'zBaseServer.transport�s$��#�.�.�s�3�3�<�K�{�{�)�Krc�(�|jd|i|��S)z�Create 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. �queue�rM�r(�argsrXs r� create_queuezBaseServer.create_queue�� ��$�t�{�7�#�T�4�V�4�4�4rc��|jdS)z�Return 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. � queue_emptyrrr^s r�get_queue_empty_exceptionz$BaseServer.get_queue_empty_exception�s���{�=�)�)rc�(�|jd|i|��S)z�Create 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. rhrrrss r� create_eventzBaseServer.create_event�rvrc�$�tjtjd��|j�dd��z��}|jdzdz|_|�d���dd���d d ��S) zGenerate a unique session id.� ��bigri����utf-8�/�_�+�-)�base64� b64encode�secrets� token_bytes�sequence_number�to_bytes�decode�replace)r(�ids r� generate_idzBaseServer.generate_id�s��� � � � �� #� #�d�&:�&C�&C�A�u�&M�&M� M�O�O�� $� 4�q� 8�H�D����y�y��!�!�)�)�#�s�3�3�;�;�C��E�E�Erc���|�dd��dz|z}|���D]@\}}|dkr� t|��r |��}|dur |d|zz }�2|d|zdz|zz }�A|S)zGenerate the sid cookie.�name�io�=Tz; )�get�items�callable)r(ro� attributesr3� attribute�values r�_generate_sid_cookiezBaseServer._generate_sid_cookie�s��������-�-��3�c�9�� *� 0� 0� 2� 2� 9� 9� �I�u��F�"�"������ ��������}�}��$��*�*����$��*�S�0�5�8�8���� rc��|jr |�|��js|dkrgS|jd�|�dd��gSdgS)z=Return the list of possible upgrades for a client connection.rNz�The WebSocket transport is not available, you must install a WebSocket server that is compatible with your async mode to enable it. See the documentation for details.z no-websocket)r0rmrnrM�_log_error_once)r(ror's r� _upgradeszBaseServer._upgrades�st���"� �d�&6�&6�s�&;�&;�&D� ��[�(�(��I� �;�{� #� +� � � �@��  � � � �I��}�rc�� |j|}n#t$rtd���wxYw|jr|j|=td���|S)z-Return the socket object for a given session.zSession not foundzSession is disconnected)r7�KeyError�closed)r(ro�ss rrmzBaseServer._get_socket�sg�� 0�� �S�!�A�A��� 0� 0� 0��.�/�/� /� 0���� �8� 6�� �S�!��4�5�5� 5��s� �*c��|�J|�g}|dgz }d|tj|����|����d��d�Sddgd d�S) z$Generate a successful HTTP response.N)� Content-Typeztext/plain; charset=UTF-8z200 OK)�packets)� jsonp_indexr���status�headers�response�r�z text/plainsOK)r�Payload�encode)r(r�r�r�s r�_okzBaseServer._ok�s��� � ����� �E�F� F�G�&�&� '��� @� @� @� G� G�$/�!H�!1�!1�17�����B�B� B� '� >�?� %�'�'� 'rc��|�d}tjj�|��}ddg|�d��d�S)z+Generate a bad request HTTP error response.Nz Bad Requestz400 BAD REQUESTr�r�r��rr?r@�dumpsr��r(r s r� _bad_requestzBaseServer._bad_requestsK�� �?�#�G��-�$�*�*�7�3�3��+�:�;�#�N�N�7�3�3�5�5� 5rc��ddgdd�S)z0Generate a method not found HTTP error response.z405 METHOD NOT FOUNDr�sMethod Not Foundr�rr^s r�_method_not_foundzBaseServer._method_not_found s��0�:�;�/�1�1� 1rc��|�d}tjj�|��}ddg|�d��d�S)z,Generate a unauthorized HTTP error response.N� Unauthorizedz401 UNAUTHORIZED)r�zapplication/jsonr�r�r�r�s r� _unauthorizedzBaseServer._unauthorizedsK�� �?�$�G��-�$�*�*�7�3�3��,�@�A�#�N�N�7�3�3�5�5� 5rc �^�|j��g}d|vr�d|vr�|�d�|d|d�����d|vsd|vr�|�d|d���d��d���}|�d�||�d|d���d��d��������n�|jd krd}n�t |jt��r |jg}nut|j��rZ|�d ��} |�||��}n%#t$r|�|��}YnwxYw|r|gng}n|j}|S) Nzwsgi.url_scheme� HTTP_HOSTz{scheme}://{host})�scheme�host�HTTP_X_FORWARDED_PROTO�HTTP_X_FORWARDED_HOST�,r�*� HTTP_ORIGIN) r4�append�formatr��split�stripr+rTr�� TypeError)r(�environ�allowed_originsr��origin� is_alloweds r�_cors_allowed_originsz BaseServer._cors_allowed_originss��� � $� ,� �O� �G�+�+� �w�0F�0F��&�&�':�'A�'A�"�#4�5� ��-�(B�(/�(/�0�0�0�,�w�6�6�/�7�:�:�$�[�[�0�� 1�2�4�4�49�E�#�J�J�q�B�BG�%�'�'��$�*�*�+>�+E�+E�%�G�K�K�3�#�K�0�-2�-2�27�%� #�3%�3%�%&�-(�).���� ,F�,1�,1�2�2�2�� � &�#� -� -�"�O�O� ��1�3� 7� 7� 8�#�8�9�O�O� �d�/� 0� 0� 8��[�[��/�/�F� ?�!�6�6�v�w�G�G� � ��� ?� ?� ?�!�6�6�v�>�>� � � � ?����*4�<�v�h�h�"�O�O�"�7�O��s�"E9�9F�Fc���|jgkrgSg}|�|��}d|vr|� |d|vr d|dfg}|ddkr|dgz }d|vr|d|dfgz }|jr|d gz }|S) z1Return the cross-origin-resource-sharing headers.r�NzAccess-Control-Allow-Origin�REQUEST_METHOD�OPTIONS)zAccess-Control-Allow-MethodszOPTIONS, GET, POST�#HTTP_ACCESS_CONTROL_REQUEST_HEADERSzAccess-Control-Allow-Headers)z Access-Control-Allow-Credentials�true)r4r�r5)r(r�r�r�s r� _cors_headerszBaseServer._cors_headers<s��� � $�� *� *��I����4�4�W�=�=�� �G� #� #� �(�G�M�,B� �-!�-!�5�w�}�7M�N�O�G� �#� $� � 1� 1� �N�O� O�G� 0�G� ;� ;� �7�� E�F�H�I� I�G� � � F� �D�E� E�G��rc���tj��}tj|d���5}|�|��ddd��n #1swxYwY|���S)z%Apply gzip compression to a response.�w)�fileobjrZN)r��BytesIOr�GzipFile�write�getvalue)r(r��bytesio�gzs r�_gzipzBaseServer._gzipPs����*�,�,�� �]�7�� 5� 5� 5� �� �H�H�X� � � � � � � � � � � � � � ���� � � � ����!�!�!s�A � A�Ac�*�tj|��S)z(Apply deflate compression to a response.)�zlib�compress)r(r�s r�_deflatezBaseServer._deflateWs���}�X�&�&�&rc��||jvr9|j�|dz��|j�|��dS|j�|��dS)zWLog message with logging.ERROR level the first time, then log with given level.zC (further occurrences of this error will be logged with level INFO)N)r:rB�error�addrV)r(r � message_keys rr�zBaseServer._log_error_once[sv�� �d�3� 3� 3� �K� � �g�)@�@� A� A� A� � !� %� %�k� 2� 2� 2� 2� 2� �K� � �W� %� %� %� %� %r)Nrrr TTr!NNTFNTNNrf)NNN)rrr�compression_methodsrjr%r;r�rr\rQrLrkr'ruryr{r�r�r�rmr�r�r�r�r�r�r�r�r�rrrrrs�������!�9�-��6�6�6�K�!�;�/��#���O� ,� ,� ,� ,� ,� ,� ,� ,�HJ�>B�>B�37�;?�GK� MH�MH�MH�MH�^���C�C�C�(�(�(�(�TL�L�L�5�5�5�*�*�*�5�5�5�F�F�F� � � � � � � � � � '� '� '� '�5�5�5�5�1�1�1� 5�5�5�5����@���("�"�"�'�'�'�&�&�&�&�&rr) r�rrOr�rEr�r��rr� getLoggerrCrrrr�<module>r�s��� � � � � � � � ����� � � � ��������� � � � �������������"��"�#4�5�5��T&�T&�T&�T&�T&�T&�T&�T&�T&�T&r
Memory