
    i9                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrSSKJr  SSKJ	r	  \R                  " S5      r " S S5      rg)    N   )packet)payloadzengineio.serverc                       \ rS rSrSS/r/ SQrSS/rSrSr " S	 S
5      r	      S"S jr
S rS rS#S jrS rS rS rS rS rS rS rS rS$S jrS#S jrS rS#S jrS rS rS rS rS  rS!rg)%
BaseServer   gzipdeflate)connect
disconnectmessagepolling	websocketTr   c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
BaseServer.reason   zDisconnection reasons.zserver disconnectzclient disconnectzping timeoutztransport closeztransport error N)__name__
__module____qualname____firstlineno____doc__SERVER_DISCONNECTCLIENT_DISCONNECTPING_TIMEOUTTRANSPORT_CLOSETRANSPORT_ERROR__static_attributes__r       Q/home/admin/cozy_coffee/venv/lib/python3.13/site-packages/engineio/base_server.pyreasonr      s     $//%++r   r!   Nc                    X0l         [        U[        5      (       a  US   U l        US   U l        OX l        SU l        X@l        XPl        X`l        Xpl        Xl	        Xl
        Xl        Xl        0 U l        0 U l        [        5       U l        Ub  UOU R"                  U l        S U l        S U l        Ub  U[*        R,                  l        [        U[0        5      (       d  Xl        O[4        U l        U R2                  R6                  [8        R:                  :X  a  U(       a*  U R2                  R=                  [8        R>                  5        O)U R2                  R=                  [8        R@                  5        U R2                  RC                  [8        RD                  " 5       5        U RG                  5       nUb  UU;   a  U/O/ nS U l$        S U l%        U Hj  n [L        RN                  " SU-   5      RH                  U l$        SU RH                  ;   a  U RH                  S   OSnUU RQ                  5       :w  a  Mc  UU l%          O   U RJ                  c  [U        S5      eU RQ                  5       (       a/  SU RH                  ;  d  U RH                  S   (       d  [U        S5      eU RQ                  5       (       d/  SU RH                  ;   a  U RH                  S   (       a  [U        S5      eUbN  [        U[V        5      (       a  U/nU Vs/ s H  nUU RX                  ;   d  M  UPM     nnU(       d  [U        S	5      eU=(       d    U RX                  U l-        U R2                  R]                  S
U RJ                  5        g ! [R         a     GM  f = fs  snf )Nr   r   zengineio.async_drivers.asyncioFzInvalid async_mode specifiedz1The selected async_mode is not asyncio compatiblezKThe selected async_mode requires asyncio and must use the AsyncServer classzNo valid transports providedzServer 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strvalid_transports
transportsinfo)selfrH   r'   r$   r)   r*   r+   r,   r-   r.   r/   r<   r:   r0   monitor_clientsrP   kwargsmodesmodeasyncio_based	transports                        r    __init__BaseServer.__init__#   s    )mU++!.q!1D.;A.>D+!../D+$8!, 0%:"$8! 0, #* #2040M0M 	#' "&!%FMM&$'' K(DK{{  GNN2KK((6KK((7&&w'<'<'>?  "!$.%$7ZLRED
'55-466<f  !DKK/ !%I 65:  D$9$9$;;"&  ??";<<  ""$++-Y' * + +$$&&9+CI& > ? ?!*c**(\
5? AZ	&$*?*?? $ZJ A !?@@$=(=(=5tG+   As%   AM,/M,9M>M>,
M;:M;c                     g)NFr   rR   s    r    rK   BaseServer.is_asyncio_basedr   s    r   c                 
    / SQ$ )N)eventletgevent_uwsgigevent	threadingr   r\   s    r    rF   BaseServer.async_modesu   s    BBr   c                 f   ^ ^ TT R                   ;  a  [        S5      eUU 4S jnUc  U$ U" U5        g)a9  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                 &   > U TR                   T'   U $ N)r2   )handlereventrR   s    r    set_handler"BaseServer.on.<locals>.set_handler   s    #*DMM% Nr   N)event_namesrM   )rR   rh   rg   ri   s   ``  r    onBaseServer.onx   s8    > (((_--	 ?Gr   c                 J    U R                  U5      R                  (       a  S$ S$ )zReturn 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.
r   r   )_get_socketupgraded)rR   sids     r    rX   BaseServer.transport   s"     #..s3<<{K)Kr   c                 ,    U R                   S   " U0 UD6$ )zCreate 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rG   rR   argsrT   s      r    create_queueBaseServer.create_queue        {{7#T4V44r   c                      U R                   S   $ )zReturn 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_emptyru   r\   s    r    get_queue_empty_exception$BaseServer.get_queue_empty_exception   s     {{=))r   c                 ,    U R                   S   " U0 UD6$ )zCreate 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.
rh   ru   rv   s      r    create_eventBaseServer.create_event   rz   r   c                     [         R                  " [        R                  " S5      U R                  R                  SS5      -   5      nU R                  S-   S-  U l        UR                  S5      R                  SS5      R                  S	S
5      $ )zGenerate a unique session id.      bigr   i utf-8/_+-)base64	b64encodesecretstoken_bytessequence_numberto_bytesdecodereplace)rR   ids     r    generate_idBaseServer.generate_id   sz    #d&:&:&C&CAu&MMO $ 4 4q 8HDyy!))#s3;;CEEr   c                     UR                  SS5      S-   U-   nUR                  5        HA  u  pEUS:X  a  M  [        U5      (       a  U" 5       nUSL a
  USU-   -  nM3  USU-   S-   U-   -  nMC     U$ )zGenerate the sid cookie.nameio=Tz; )getitemscallable)rR   rq   
attributesr-   	attributevalues         r    _generate_sid_cookieBaseServer._generate_sid_cookie   s    -3c9 * 0 0 2IF"}$**$*S0588 !3 r   c                     U R                   (       a&  U R                  U5      R                  (       d  US:X  a  / $ U R                  S   c  U R	                  SS5        / $ S/$ )z=Return the list of possible upgrades for a client connection.r   zThe 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)r*   ro   rp   rG   _log_error_once)rR   rq   rX   s      r    	_upgradesBaseServer._upgrades   sb    ""d&6&6s&;&D&D[(I;;{#+  @ 	 
 I}r   c                      U R                   U   nUR                  (       a  U R                   U	 [        S5      eU$ ! [         a    [        S5      ef = f)z-Return the socket object for a given session.zSession not foundzSession is disconnected)r1   KeyErrorclosed)rR   rq   ss      r    ro   BaseServer._get_socket   sX    	0S!A 88S!455  	0.//	0s	   < Ac                     Ub?  Uc  / nUS/-  nSU[         R                  " US9R                  US9R                  S5      S.$ SS/SS.$ )	z$Generate a successful HTTP response.)Content-Typeztext/plain; charset=UTF-8z200 OK)packets)jsonp_indexr   statusheadersresponser   z
text/plains   OK)r   Payloadencode)rR   r   r   r   s       r    _okBaseServer._ok   ss    EFFG&& ' @ G G$/ !H !117B B
 ' >? %' 'r   c                     Uc  Sn[         R                  R                  R                  U5      nSS/UR	                  S5      S.$ )z+Generate a bad request HTTP error response.zBad Requestz400 BAD REQUESTr   r   r   r   r9   r:   dumpsr   rR   r   s     r    _bad_requestBaseServer._bad_request  sE    ?#G--$$**73+:;#NN735 	5r   c                     SS/SS.$ )z0Generate a method not found HTTP error response.z405 METHOD NOT FOUNDr   s   Method Not Foundr   r   r\   s    r    _method_not_foundBaseServer._method_not_found  s    0:;/1 	1r   c                     Uc  Sn[         R                  R                  R                  U5      nSS/UR	                  S5      S.$ )z,Generate a unauthorized HTTP error response.Unauthorizedz401 UNAUTHORIZED)r   zapplication/jsonr   r   r   r   s     r    _unauthorizedBaseServer._unauthorized  sE    ?$G--$$**73,@A#NN735 	5r   c           
      N   U R                   c  / nSU;   a  SU;   a  UR                  SR                  US   US   S95        SU;   d  SU;   a  UR                  SUS   5      R	                  S5      S   R                  5       nUR                  SR                  X1R                  SUS   5      R	                  S5      S   R                  5       S95        U$ U R                   S	:X  a  S nU$ [        U R                   [        5      (       a  U R                   /nU$ [        U R                   5      (       a3  UR                  S
5      n U R                  XA5      nU(       a  U/nU$ / nU$ S	U R                   ;   a  S nU$ U R                   nU$ ! [         a    U R                  U5      n NOf = f)Nzwsgi.url_scheme	HTTP_HOSTz{scheme}://{host})schemehostHTTP_X_FORWARDED_PROTOHTTP_X_FORWARDED_HOST,r   *HTTP_ORIGIN)
r.   appendformatr   splitstripr%   rN   r   	TypeError)rR   environallowed_originsr   origin
is_alloweds         r    _cors_allowed_origins BaseServer._cors_allowed_origins  s   $$, O G+w0F&&':'A'A"#45 - (B (/ 0 ,w6/7:$[[0 12449E#JqBBG%'  $**+>+E+E%KK3#K0-227% #3%%&-( ).	 ,F ,1 2* ! &&#-"O  11377#889O  d//00[[/F?!66vG
 +5vhO  ;=O 	 d///"&  #'";";  ?!66v>
?s   F F$#F$c                     U R                   / :X  a  / $ / nU R                  U5      nSU;   a  Ub	  US   U;   a  SUS   4/nUS   S:X  a  US/-  nSU;   a  USUS   4/-  nU R                  (       a  US/-  nU$ )	z1Return the cross-origin-resource-sharing headers.r   zAccess-Control-Allow-OriginREQUEST_METHODOPTIONS)zAccess-Control-Allow-MethodszOPTIONS, GET, POST#HTTP_ACCESS_CONTROL_REQUEST_HEADERSzAccess-Control-Allow-Headers)z Access-Control-Allow-Credentialstrue)r.   r   r/   )rR   r   r   r   s       r    _cors_headersBaseServer._cors_headers?  s    $$*I44W=G# (GM,B -!5w}7MNOG#$	1NOOG0G;7 EFH I IG  DEEGr   c                     [         R                  " 5       n[        R                  " USS9 nUR	                  U5        SSS5        UR                  5       $ ! , (       d  f       UR                  5       $ = f)z%Apply gzip compression to a response.w)fileobjrV   N)r   BytesIOr	   GzipFilewritegetvalue)rR   r   bytesiogzs       r    _gzipBaseServer._gzipS  sU    **,]]75HHX 6!! 65!!s   A
A2c                 .    [         R                  " U5      $ )z(Apply deflate compression to a response.)zlibcompress)rR   r   s     r    _deflateBaseServer._deflateZ  s    }}X&&r   c                     X R                   ;  a:  U R                  R                  US-   5        U R                   R                  U5        gU R                  R	                  U5        g)zOLog 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)r4   r<   erroraddrQ   )rR   r   message_keys      r    r   BaseServer._log_error_once^  sW     333KKg )@ @ A!!%%k2KKW%r   )rG   r*   r0   rH   r,   r-   r.   r/   r2   r+   r4   r<   r)   r'   r(   r$   r   r8   r7   r1   r6   rP   )N      i@B TTi   NNTFNTNNrf   )NNN) r   r   r   r   compression_methodsrk   rO   r5   r   r!   rY   rK   rF   rl   rX   rx   r}   r   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r      s    !9-6K!;/#O, , HJ>B>B37;?GKMH^C(TL5*5F	'515!F("'&r   r   )r   r	   rI   r   r?   r   r    r   r   	getLoggerr=   r   r   r   r    <module>r      s=       	     ""#45W& W&r   