o
    96d2                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlZd dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dl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e j#Z$dS )    N)Literal)unquote)ConnectionTypeevents)ConnectionState)	ExtensionPerMessageDeflate)RemoteProtocolError)ASGISendEventWebSocketAcceptEventWebSocketCloseEventWebSocketEventWebSocketReceiveEventWebSocketScopeWebSocketSendEvent)Config)TRACE_LOG_LEVEL)get_local_addrget_path_with_query_stringget_remote_addris_ssl)ServerStatec                   @   sR  e Zd Z	d5dededejeejf dej	e
j ddf
ddZd	e
jddfd
dZdej	e ddfddZd6ddZdeddfddZd6ddZd6ddZd6ddZd6ddZde
jddfddZdejddfd d!Zdejddfd"d#Zdejddfd$d%Z dej!ddfd&d'Z"dej#ddfd(d)Z$d6d*d+Z%d6d,d-Z&d7d0d1Z'd8d3d4Z(dS )9
WSProtocolNconfigserver_state	app_state_loopreturnc                 C   s   |j s|  || _|j| _|pt | _t	d| _
|j| _|| _|j| _|j| _|j| _d | _d | _d | _d | _t | _d| _d| _tjtjd| _d| _t | _| j   d| _!d| _"d S )Nzuvicorn.errorF)Zconnection_type     )#Zloadedloadr   Z
loaded_appappasyncioget_event_looplooplogging	getLoggerlogger	root_pathr   connectionstasksdefault_headers	transportserverclientschemeQueuequeuehandshake_complete
close_sentwsprotoZWSConnectionr   ZSERVERconnread_pausedEventwritablesetbytestext)selfr   r   r   r    r=   C:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\uvicorn/protocols/websockets/wsproto_impl.py__init__"   s0   



zWSProtocol.__init__r,   c                 C   sp   | j |  || _t|| _t|| _t|rdnd| _| j	j
tkr6| jr*d| j nd}| j	td| d S d S )Nwssws%s:%d - r   z%sWebSocket connection made)r)   addr,   r   r-   r   r.   r   r/   r'   levelr   log)r<   r,   prefixr=   r=   r>   connection_madeO   s   

zWSProtocol.connection_madeexcc                 C   s|   | j rdnd}| jd|d | j|  | jjtkr.| jr$d| j nd}| j	td| d| _ |d u r<| j
  d S d S )	Ni    websocket.disconnecttypecoderB   r   z%sWebSocket connection lostT)r2   r1   
put_nowaitr)   remover'   rD   r   r.   rE   r,   close)r<   rH   rM   rF   r=   r=   r>   connection_lost\   s   zWSProtocol.connection_lostc                 C   s   d S Nr=   r<   r=   r=   r>   eof_receivedi   s   zWSProtocol.eof_receiveddatac              
   C   sd   z| j | W n# ty+ } z| j| j |j | j  W Y d }~d S d }~ww |   d S rR   )	r5   Zreceive_datar	   r,   writesendZ
event_hintrP   handle_events)r<   rU   errr=   r=   r>   data_receivedl   s   zWSProtocol.data_receivedc                 C   s   | j  D ]=}t|tjr| | qt|tjr| | qt|tjr+| | qt|tj	r7| 
| qt|tjrB| | qd S rR   )r5   r   
isinstanceRequesthandle_connectTextMessagehandle_textBytesMessagehandle_bytesCloseConnectionhandle_closePinghandle_pingr<   eventr=   r=   r>   rX   v   s   
zWSProtocol.handle_eventsc                 C      | j   dS )z\
        Called by the transport when the write buffer exceeds the high water mark.
        N)r8   clearrS   r=   r=   r>   pause_writing      zWSProtocol.pause_writingc                 C   rh   )z_
        Called by the transport when the write buffer drops below the low water mark.
        N)r8   r9   rS   r=   r=   r>   resume_writing   rk   zWSProtocol.resume_writingc                 C   sR   | j r| jddd | jtjjdd}| j	| n| 
  | j  d S )NrJ   i  rK   )rM   )r2   r1   rN   r5   rW   r4   r   rb   r,   rV   send_500_responserP   )r<   outputr=   r=   r>   shutdown   s   zWSProtocol.shutdowntaskc                 C   s   | j | d S rR   )r*   discard)r<   rp   r=   r=   r>   on_task_complete   s   zWSProtocol.on_task_completerg   c                 C   s   d|j  fg}|dd |jD 7 }|jd\}}}d| jjddd| j| j| j	| j
t||d	|d	||j| j d
| _| jddi | j|  }|| j | j| d S )Ns   hostc                 S   s   g | ]
\}}|  |fqS r=   )lower).0keyvaluer=   r=   r>   
<listcomp>   s    z-WSProtocol.handle_connect.<locals>.<listcomp>?Z	websocketz2.3)versionspec_versionz1.1ascii)rL   Zasgihttp_versionr/   r-   r.   r(   pathraw_pathquery_stringheaderssubprotocolsstaterL   zwebsocket.connect)hostencodeextra_headerstarget	partitionr   Zasgi_versionr/   r-   r.   r(   r   r   r   copyscoper1   rN   r$   create_taskrun_asgiadd_done_callbackrr   r*   rC   )r<   rg   r   r~   _r   rp   r=   r=   r>   r]      s*   zWSProtocol.handle_connectc                 C   V   |  j |j7  _ |jr'd| j d}| j| d| _ | js)d| _| j  d S d S d S )Nwebsocket.receive)rL   r;   r   T)r;   rU   message_finishedr1   rN   r6   r,   pause_readingr<   rg   msgr=   r=   r>   r_      s   zWSProtocol.handle_textc                 C   r   )Nr   )rL   r:   r   T)r:   rU   r   r1   rN   r6   r,   r   r   r=   r=   r>   ra      s   zWSProtocol.handle_bytesc                 C   sH   | j jtjkr| j| j |  | j	d|j
d | j  d S )NrJ   rK   )r5   r   r   ZREMOTE_CLOSINGr,   rV   rW   responser1   rN   rM   rP   rf   r=   r=   r>   rc      s   zWSProtocol.handle_closec                 C   s   | j | j|  d S rR   )r,   rV   r5   rW   r   rf   r=   r=   r>   re      s   zWSProtocol.handle_pingc                 C   sL   ddg}| j tjjd|dd}|| j tjjdd7 }| j| d S )N)s   content-types   text/plain; charset=utf-8)s
   connections   closei  T)status_coder   Zhas_bodys   Internal Server ErrorrU   )r5   rW   r4   r   RejectConnectionZ
RejectDatar,   rV   )r<   r   rn   r=   r=   r>   rm      s   zWSProtocol.send_500_responsec                    s   z|  | j| j| jI d H }W n ty,   | jd | js$|   | j	
  Y d S w | jsCd}| j| |   | j	
  d S |d urWd}| j|| | j	
  d S d S )NzException in ASGI application
z4ASGI callable returned without completing handshake.z4ASGI callable should return None, but returned '%s'.)r!   r   receiverW   BaseExceptionr'   	exceptionr2   rm   r,   rP   error)r<   resultr   r=   r=   r>   r      s&   zWSProtocol.run_asgimessager
   c                    sl  | j  I d H  |d }| js|dkrctd|}| jd| jd t| j |	d}| j
t|	dg  }g }| jjrD|t  | j sad| _| jtjj|||d	}| j| d S d S |d
kr| jddd | jd| jd t| j d| _d| _tjdg d}| j|}| j| | j  d S d}t|| | js.|dkrtd|}|	d}	|	d}
|	d u r|
n|	}| jtjj|d}| j s| j| d S d S |d
kr&td|}d| _|	dd}|	ddpd}| jd|d | jtjj||d}| j s$| j| | j  d S d S d}t|| d}t|| )NrL   zwebsocket.acceptr   z%s - "WebSocket %s" [accepted]r.   subprotocolr   T)r   
extensionsr   zwebsocket.closerJ   rI   rK   z%s - "WebSocket %s" 403i  )r   r   zLExpected ASGI message 'websocket.accept' or 'websocket.close', but got '%s'.zwebsocket.sendr   r:   r;   r   r   rM   i  reasonr   )rM   r   zJExpected ASGI message 'websocket.send' or 'websocket.close', but got '%s'.z>Unexpected ASGI message '%s', after sending 'websocket.close'.) r8   waitr2   typingcastr'   infor   r   getr+   listr   Zws_per_message_deflateappendr   r,   
is_closingr5   rW   r4   r   ZAcceptConnectionrV   r1   rN   r3   r   rP   RuntimeErrorMessagerb   )r<   r   Zmessage_typer   r   r   rn   rg   r   
bytes_dataZ	text_datarU   rM   r   r=   r=   r>   rW      s   





zWSProtocol.sendr   c                    s6   | j  I d H }| jr| j  rd| _| j  |S )NF)r1   r   r6   emptyr,   resume_reading)r<   r   r=   r=   r>   r   Q  s   
zWSProtocol.receiverR   )r   N)r   r
   r   N)r   r   ))__name__
__module____qualname__r   r   r   DictstrAnyOptionalr"   AbstractEventLoopr?   	TransportrG   	ExceptionrQ   rT   r:   rZ   rX   rj   rl   ro   Taskrr   r   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rm   r   rW   r   r=   r=   r=   r>   r   !   sF    

-






	


Vr   )%r"   r%   r   r   urllib.parser   r4   r   r   Zwsproto.connectionr   Zwsproto.extensionsr   r   Zwsproto.utilitiesr	   Zuvicorn._typesr
   r   r   r   r   r   r   Zuvicorn.configr   Zuvicorn.loggingr   Zuvicorn.protocols.utilsr   r   r   r   Zuvicorn.serverr   Protocolr   r=   r=   r=   r>   <module>   s     $	