o
    96d4                     @   s&  d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z*m+Z+m,Z,m-Z- d dl.m/Z/ G dd dZ0G dd deZ1dS )    N)	AnyDictListLiteralOptionalSequenceTupleUnioncast)unquote)Headers)ConnectionClosed)ServerPerMessageDeflateFactory)HTTPResponse)WebSocketServerProtocol)Subprotocol)ASGISendEventWebSocketAcceptEventWebSocketCloseEventWebSocketConnectEventWebSocketDisconnectEventWebSocketReceiveEventWebSocketScopeWebSocketSendEvent)Config)TRACE_LOG_LEVEL)get_local_addrget_path_with_query_stringget_remote_addris_ssl)ServerStatec                   @   sB   e Zd ZdZdeddfddZdeddfddZdefd	d
ZdS )ServerFwsreturnNc                 C      d S N selfr"   r&   r&   C:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\uvicorn/protocols/websockets/websockets_impl.pyregister1      zServer.registerc                 C   r$   r%   r&   r'   r&   r&   r)   
unregister4   r+   zServer.unregisterc                 C   s   | j  S r%   )closingr(   r&   r&   r)   
is_serving7   s   zServer.is_serving)	__name__
__module____qualname__r-   r   r*   r,   boolr/   r&   r&   r&   r)   r!   .   s
    r!   c                
       s(  e Zd ZU eeeef  ed< 	d*dedede	ee
f deej f fddZd	ejd
df fddZdee d
df fddZd+ddZdejd
dfddZdeded
ee fddZdedeee  d
ee fddZd+ddZdeded
e
fdd Zd+d!d"Zd,d%d&Z d
e!d' fd(d)Z"  Z#S )-WebSocketProtocolextra_headersNconfigserver_state	app_state_loopc                    s  |j s|  || _|j| _|pt | _|j| _|| _	|j
| _
|j| _d | _d | _d | _d | _d | _t | _t | _t | _d | _d| _d| _d | _t | _g }| jjr^|t  t j| j | j| jj!| jj"| jj#| jj$|t%&dd d | _'dd |j(D | _)d S )NFzuvicorn.error)
ws_handler	ws_servermax_sizeZ	max_queueZping_intervalZping_timeout
extensionsloggerc                 S   s$   g | ]\}}| d | d fqS )latin-1decode.0namevaluer&   r&   r)   
<listcomp>s   s    z.WebSocketProtocol.__init__.<locals>.<listcomp>)*Zloadedloadr6   Z
loaded_appappasyncioget_event_looploop	root_pathr8   connectionstasks	transportserverclientschemescopeEventhandshake_started_eventhandshake_completed_eventclosed_eventinitial_responseconnect_sent lost_connection_before_handshakeaccepted_subprotocolr!   r;   Zws_per_message_deflateappendr   super__init__r:   Zws_max_sizeZws_max_queueZws_ping_intervalZws_ping_timeoutlogging	getLoggerZserver_headerdefault_headersr5   )r(   r6   r7   r8   r9   r=   	__class__r&   r)   r^   >   sN   



zWebSocketProtocol.__init__rO   r#   c                    sx   | j |  || _t|| _t|| _t|rdnd| _| j	
tr4| jr*d| j nd}| j	td| t | d S )Nwssr"   %s:%d -  z%sWebSocket connection made)rM   addrO   r   rP   r   rQ   r   rR   r>   isEnabledForr   logr]   connection_made)r(   rO   prefixrb   r&   r)   rj   x   s   

z!WebSocketProtocol.connection_madeexcc                    sz   | j |  | jtr| jrd| j nd}| jtd| | j  | _	| j
  t | |d u r;| j  d S d S )Nre   rf   z%sWebSocket connection lost)rM   remover>   rh   r   rQ   ri   rV   is_setrZ   setr]   connection_lostrO   close)r(   rl   rk   rb   r&   r)   rp      s   

z!WebSocketProtocol.connection_lostc                 C   s4   d| j _| j r| d n|   | j  d S )NT  )r;   r-   rV   rn   Zfail_connectionsend_500_responserO   rq   r.   r&   r&   r)   shutdown   s
   
zWebSocketProtocol.shutdowntaskc                 C   s   | j | d S r%   )rN   discard)r(   ru   r&   r&   r)   on_task_complete   s   z"WebSocketProtocol.on_task_completepathheadersc           
         s   | d\}}}tjj| g }|dD ]}|dd |dD  qdd | D }d| j	j
dd	d
| j| j| j| jt||d|d||| j d| _| j|  }	|	| j | j|	 | j I dH  | jS )a(  
        This hook is called to determine if the websocket should return
        an HTTP response and close.

        Our behavior here is to start the ASGI application, and then wait
        for either `accept` or `close` in order to determine if we should
        close the connection.
        ?zSec-WebSocket-Protocolc                 S   s   g | ]}|  qS r&   )strip)rC   tokenr&   r&   r)   rF      s    z5WebSocketProtocol.process_request.<locals>.<listcomp>,c                 S   s(   g | ]\}}| d |j d ddfqS )asciisurrogateescape)errors)encoderB   r&   r&   r)   rF      s    Z	websocketz2.3)versionspec_versionz1.1r~   )typeZasgihttp_versionrR   rP   rQ   rL   rx   raw_pathquery_stringry   subprotocolsstateN)	partition
websocketslegacy	handshakeZcheck_requestget_allextendsplit	raw_itemsr6   Zasgi_versionrR   rP   rQ   rL   r   r   r8   copyrS   rK   create_taskrun_asgiadd_done_callbackrw   rN   rg   rU   waitrX   )
r(   rx   ry   Zpath_portion_r   r   headerZasgi_headersru   r&   r&   r)   process_request   s8   z!WebSocketProtocol.process_requestavailable_subprotocolsc                 C   s   | j S )z
        We override the standard 'process_subprotocol' behavior here so that
        we return whatever subprotocol is sent in the 'accept' message.
        )r[   )r(   ry   r   r&   r&   r)   process_subprotocol   s   z%WebSocketProtocol.process_subprotocolc                 C   sH   d}ddt t|d d dd|g}| jd| | j  d S )Ns   Internal Server ErrorsM   HTTP/1.1 500 Internal Server Error
content-type: text/plain; charset=utf-8
s   content-length: r~   s   
s   connection: close
    )strlenr   rO   writejoinrU   ro   )r(   msgcontentr&   r&   r)   rs      s   z#WebSocketProtocol.send_500_responseprotocolc                    s   | j   |  I dH  dS )z
        This is the main handler function for the 'websockets' implementation
        to call into. We just wait for close then return, and instead allow
        'send' and 'receive' events to drive the flow.
        N)rV   ro   wait_closed)r(   r   rx   r&   r&   r)   r:      s   
zWebSocketProtocol.ws_handlerc              
      s
  z|  | j| j| jI dH }W n9 tyI } z-| j  d}| jj||d | j	
 s1|   n| j I dH  | j  W Y d}~dS d}~ww | j  | j	
 sgd}| j| |   | j  dS |durd}| j|| | j I dH  | j  dS dS )zr
        Wrapper around the ASGI callable, handling exceptions and unexpected
        termination states.
        NzException in ASGI application
)exc_infoz1ASGI callable returned without sending handshake.z4ASGI callable should return None, but returned '%s'.)rH   rS   asgi_receive	asgi_sendBaseExceptionrW   ro   r>   errorrU   rn   rs   rV   r   rO   rq   )r(   resultrl   r   r&   r&   r)   r      s2   





zWebSocketProtocol.run_asgimessager   c           	         s  |d }| j  sz|dkrGtd|}| jd| jd t| j d | _ttt	 |
d| _d|v r@| jdd	 |d D  | j   d S |d
krrtd|}| jd| jd t| j tjjg df| _| j   | j  d S d}t|| | j s| j I d H  |dkrtd|}|
d}|
d}|d u r|n|}| |I d H  d S |d
krtd|}|
dd}|
ddpd}| ||I d H  | j  d S d}t|| d}t|| )Nr   zwebsocket.acceptr   z%s - "WebSocket %s" [accepted]rQ   Zsubprotocolry   c                 s   s(    | ]\}}| d | d fV  qdS )r?   Nr@   rB   r&   r&   r)   	<genexpr>  s
    
z.WebSocketProtocol.asgi_send.<locals>.<genexpr>zwebsocket.closer   z%s - "WebSocket %s" 403r   zLExpected ASGI message 'websocket.accept' or 'websocket.close', but got '%s'.zwebsocket.sendr   bytestextcodei  reasonrf   zJExpected ASGI message 'websocket.send' or 'websocket.close', but got '%s'.z>Unexpected ASGI message '%s', after sending 'websocket.close'.)rU   rn   r
   r>   inforS   r   rX   r   r   getr[   r5   r   ro   http
HTTPStatus	FORBIDDENrW   RuntimeErrorrV   r   sendrq   )	r(   r   Zmessage_typer   
bytes_dataZ	text_datadatar   r   r&   r&   r)   r     sf   








zWebSocketProtocol.asgi_send)r   r   r   c              
      s   | j sd| _ ddiS | j I d H  | jrdddS | j r%dddS z	|  I d H }W n, tyZ } z | j  | j	j
rJdddW  Y d }~S d|jdW  Y d }~S d }~ww dd	i}t|trj||d
< |S ||d< |S )NTr   zwebsocket.connectzwebsocket.disconnecti  )r   r   i  rr   zwebsocket.receiver   r   )rY   rV   r   rZ   rW   rn   recvr   ro   r;   r-   r   
isinstancer   )r(   r   rl   r   r&   r&   r)   r   T  s2   




zWebSocketProtocol.asgi_receiver%   )r#   N)r   r   r#   N)$r0   r1   r2   r   r   r   __annotations__r   r    r   r   r   rI   AbstractEventLoopr^   	Transportrj   	Exceptionrp   rt   Taskrw   r   r   r   r   r   r   rs   r   r:   r   r   r	   r   __classcell__r&   r&   rb   r)   r4   ;   sb   
 
:

-


	


Fr4   )2rI   r   r_   typingr   r   r   r   r   r   r   r	   r
   urllib.parser   r   Zwebsockets.datastructuresr   Zwebsockets.exceptionsr   Z(websockets.extensions.permessage_deflater   Zwebsockets.legacy.serverr   Zwebsockets.serverr   Zwebsockets.typingr   Zuvicorn._typesr   r   r   r   r   r   r   r   Zuvicorn.configr   Zuvicorn.loggingr   Zuvicorn.protocols.utilsr   r   r   r   Zuvicorn.serverr    r!   r4   r&   r&   r&   r)   <module>   s&    ,(
