
    f ja<                    `   d dl mZ 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
 d dlmZ d dl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 d dlmZmZ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+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4  G d d          Z5 G d de          Z6dS )    )annotationsN)AnyLiteralOptionalSequencecast)unquote)Headers)ConnectionClosed)ServerExtensionFactory)ServerPerMessageDeflateFactory)HTTPResponse)WebSocketServerProtocol)Subprotocol)ASGI3ApplicationASGISendEventWebSocketAcceptEventWebSocketCloseEventWebSocketConnectEventWebSocketDisconnectEventWebSocketReceiveEventWebSocketResponseBodyEventWebSocketResponseStartEventWebSocketScopeWebSocketSendEvent)Config)TRACE_LOG_LEVEL)ClientDisconnectedget_local_addrget_path_with_query_stringget_remote_addris_ssl)ServerStatec                  *    e Zd ZdZddZddZdd	Zd
S )ServerFwsr   returnNonec                    d S N selfr&   s     C:\Users\Terasoftware\OneDrive\Desktop\faahhh\fyndo\fyndo\venv\Lib\site-packages\uvicorn/protocols/websockets/websockets_impl.pyregisterzServer.register/           c                    d S r*   r+   r,   s     r.   
unregisterzServer.unregister2   r0   r1   boolc                    | j          S r*   )closingr-   s    r.   
is_servingzServer.is_serving5   s    <r1   N)r&   r   r'   r(   )r'   r4   )__name__
__module____qualname__r6   r/   r3   r8   r+   r1   r.   r%   r%   ,   sU        G                 r1   r%   c                       e Zd ZU ded<   ded<   	 d1d2 fdZd3 fdZd4 fdZd5dZd6dZd7d Z	d8d%Z
d5d&Zd9d*Zd5d+Zd:d.Zd;d0Z xZS )<WebSocketProtocolzlist[tuple[str, str]]extra_headersz+logging.Logger | logging.LoggerAdapter[Any]loggerNconfigr   server_stater#   	app_statedict[str, Any]_loop asyncio.AbstractEventLoop | Nonec                   |j         s|                                 || _        t          t          |j                  | _        |pt          j                    | _	        |j
        | _
        || _        |j        | _        |j        | _        d | _        d | _        d | _        d | _        |  t          j                    | _        t          j                    | _        t          j                    | _        d | _        d| _        d| _        d | _        t5                      | _        g }| j        j        r!|                    t=                                 t?                                           | j!        | j        | j        j"        | j        j#        | j        j$        | j        j%        |tM          j'        d                     d | _(        d |j)        D             | _*        d S )NFzuvicorn.error)
ws_handler	ws_servermax_size	max_queueping_intervalping_timeout
extensionsr?   c                h    g | ]/\  }}|                     d           |                     d           f0S latin-1decode.0namevalues      r.   
<listcomp>z.WebSocketProtocol.__init__.<locals>.<listcomp>r   sF     
 
 
BM$T[[##U\\)%<%<=
 
 
r1   )+loadedloadr@   r   r   
loaded_appappasyncioget_event_looploop	root_pathrB   connectionstasks	transportserverclientschemeEventhandshake_started_eventhandshake_completed_eventclosed_eventinitial_responseconnect_sent lost_connection_before_handshakeaccepted_subprotocolr%   rH   ws_per_message_deflateappendr   super__init__rG   ws_max_sizews_max_queuews_ping_intervalws_ping_timeoutlogging	getLoggerserver_headerdefault_headersr>   )r-   r@   rA   rB   rD   rM   	__class__s         r.   rq   zWebSocketProtocol.__init__=   s    } 	KKMMM(&*;<<5W355	)" (3!'
 -1.2.2,0 	"'.}$)0&#MOO59!05-8<!!'35
;- 	@<>>???n[,k.+64!$_55 	 		
 		
 		
 "
 
Q]Qm
 
 
r1   rb   asyncio.Transportr'   r(   c                   | j                             |            || _        t          |          | _        t          |          | _        t          |          rdnd| _        | j	        
                    t                    r4| j        r
d| j        z  nd}| j	                            t          d|           t                                          |           d S )Nwssr&   %s:%d -  z%sWebSocket connection made)r`   addrb   r   rc   r!   rd   r"   re   r?   isEnabledForr   logrp   connection_made)r-   rb   prefixrz   s      r.   r   z!WebSocketProtocol.connection_madev   s     	T""""$Y//%i00%i00:eed;##O44 	T15DZ$+--"FKOOO-JFSSS	*****r1   excException | Nonec                   | j                             |            | j                            t                    r4| j        r
d| j        z  nd}| j                            t          d|           | j                                         | _	        | j        
                                 t                                          |           || j                                         d S d S )Nr~   r   z%sWebSocket connection lost)r`   remover?   r   r   rd   r   rh   is_setrl   setrp   connection_lostrb   close)r-   r   r   rz   s      r.   r   z!WebSocketProtocol.connection_lost   s    %%%;##O44 	T15DZ$+--"FKOOO-JFSSS484R4Y4Y4[4[0[-&**,,,$$$ 	#N  """""	# 	#r1   c                    d| j         _        | j                                        r|                     d           n|                                  | j                                         d S )NT  )rH   r6   rh   r   fail_connectionsend_500_responserb   r   r7   s    r.   shutdownzWebSocketProtocol.shutdown   sf    !%)0022 	%  &&&&""$$$r1   taskasyncio.Task[None]c                :    | j                             |           d S r*   )ra   discard)r-   r   s     r.   on_task_completez"WebSocketProtocol.on_task_complete   s    
4     r1   pathstrrequest_headersr
   HTTPResponse | Nonec                  K   |                     d          \  }}}t          j        j                            |           g }|                    d          D ]4}|                    d |                    d          D                        5d |                                D             }t          |          }| j
        |z   }	| j
                            d          |                    d          z   }
d| j        j        dd	d
| j        | j        | j        | j
        |	|
|                    d          ||| j                                        di id| _        | j                            |                                           }|                    | j                   | j                            |           | j                                         d{V  | 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                6    g | ]}|                                 S r+   )strip)rT   tokens     r.   rW   z5WebSocketProtocol.process_request.<locals>.<listcomp>   s      N N N5 N N Nr1   ,c                l    g | ]1\  }}|                     d           |                     d d          f2S )asciisurrogateescape)errors)encoderS   s      r.   rW   z5WebSocketProtocol.process_request.<locals>.<listcomp>   sN     
 
 
e [[!!5<<@Q<#R#RS
 
 
r1   r   	websocketz2.4)versionspec_versionz1.1zwebsocket.http.response)typeasgihttp_versionre   rc   rd   r_   r   raw_pathquery_stringheaderssubprotocolsstaterM   N)	partition
websocketslegacy	handshakecheck_requestget_allextendsplit	raw_itemsr	   r_   r   r@   asgi_versionre   rc   rd   rB   copyscoper^   create_taskrun_asgiadd_done_callbackr   ra   r   rg   waitrj   )r-   r   r   path_portion_r   r   headerasgi_headers	full_pathfull_raw_pathr   s               r.   process_requestz!WebSocketProtocol.process_request   s      )-s(;(;%a#11/BBB"$%--.FGG 	P 	PF N NFLL<M<M N N NOOOO
 
.88::
 
 
 |$$NT)	--g669L9LW9U9UU   $ 8%PP!kkk%(//88#(^((**4b9
 

  y$$T]]__55t4555
t*//111111111$$r1   r   available_subprotocolsSequence[Subprotocol] | NoneSubprotocol | Nonec                    | j         S )z
        We override the standard 'process_subprotocol' behavior here so that
        we return whatever subprotocol is sent in the 'accept' message.
        )rm   )r-   r   r   s      r.   process_subprotocolz%WebSocketProtocol.process_subprotocol   s     ((r1   c                
   d}ddt          t          |                                        d          z   dz   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
r1   )r   lenr   rb   writejoinrg   r   )r-   msgcontents      r.   r   z#WebSocketProtocol.send_500_response   s    &d#c#hh--"6"6w"?"??'I$
 	SXXg../// 	$((*****r1   protocolr   r   c                p   K   | j                                          |                                  d{V  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)rh   r   wait_closed)r-   r   r   s      r.   rG   zWebSocketProtocol.ws_handler   sG       	&**,,,           r1   c                  K   	 |                      | j        | j        | j                   d{V }| j                                         | j                                        sI| j        	                    d           | 
                                 | j                                         dS |U| j        	                    d|           | j                                         d{V  | j                                         dS dS # t          $ r6 | j                                         | j                                         Y dS t           $ r | j                                         | j                            d           | j                                        s| 
                                 n| j                                         d{V  | j                                         Y dS w xY w)zr
        Wrapper around the ASGI callable, handling exceptions and unexpected
        termination states.
        Nz1ASGI callable returned without sending handshake.z4ASGI callable should return None, but returned '%s'.zException in ASGI application
)r[   r   asgi_receive	asgi_sendri   r   rg   r   r?   errorr   rb   r   rh   r   r   BaseException	exception)r-   results     r.   r   zWebSocketProtocol.run_asgi   s     
	'88DJ0A4>RRRRRRRRF !!###/6688 '!!"UVVV&&((($$&&&&& '!!"XZ`aaa499;;;;;;;;;$$&&&&&' '# " 	# 	# 	#!!###N  """""" 	# 	# 	#!!###K!!"CDDD/6688 <&&((((499;;;;;;;;;N  """"""	#s   ,D <G)B"G)(G)messager   c                  K   |d         }| j                                         s<|dk    rt          d|          }| j                            d| j        d         t          | j                             d | _        t          t          t                   |
                    d                    | _        d|v r*| j                            d |d         D                        | j                                          d S |d	k    rt          d
|          }| j                            d| j        d         t          | j                             t          j        j        g df| _        | j                                          | j                                         d S |dk    rt          d|          }| j                            d| j        d         t          | j                  |d                    t          j        |d                   }d |
                    dg           D             }||df| _        | j                                          d S d}t'          ||z            | j                                        s6| j        .| j                                         d {V  	 |dk    r\t          d|          }|
                    d          }|
                    d          }||n|}|                     |           d {V  n|d	k    rtt          d
|          }|
                    dd          }	|
                    dd          pd}
|                     |	|
           d {V  | j                                         nd}t'          ||z            d S d S # t0          $ r}t2          |d }~ww xY w| j        |dk    rqt          d|          }| j        d         |d         z   }| j        d d         |fz   | _        |
                    d d!          s| j                                         d S d S d"}t'          ||z            d#}t'          ||z            )$Nr   zwebsocket.acceptr   z%s - "WebSocket %s" [accepted]rd   subprotocolr   c              3  p   K   | ]1\  }}|                     d           |                     d           fV  2dS )rP   NrQ   rS   s      r.   	<genexpr>z.WebSocketProtocol.asgi_send.<locals>.<genexpr>  sY       . . (D% Y//i1H1HI. . . . . .r1   zwebsocket.closer   z%s - "WebSocket %s" 403r1   zwebsocket.http.response.startr   z%s - "WebSocket %s" %dstatusc                h    g | ]/\  }}|                     d           |                     d           f0S rO   rQ   rS   s      r.   rW   z/WebSocketProtocol.asgi_send.<locals>.<listcomp>6  sG       JU$PUT[[++U\\)-D-DE  r1   zmExpected ASGI message 'websocket.accept', 'websocket.close', or 'websocket.http.response.start' but got '%s'.zwebsocket.sendr   bytestextcodei  reasonr   zJExpected ASGI message 'websocket.send' or 'websocket.close', but got '%s'.zwebsocket.http.response.bodyr      body	more_bodyFzBExpected ASGI message 'websocket.http.response.body' but got '%s'.z\Unexpected ASGI message '%s', after sending 'websocket.close' or response already completed.)rg   r   r   r?   infor   r    rj   r   r   getrm   r>   r   r   http
HTTPStatus	FORBIDDENri   RuntimeErrorrh   r   sendr   r   r   )r-   r   message_typer   r   r   
bytes_data	text_datadatar   r   r   r   s                r.   r   zWebSocketProtocol.asgi_send  s     v+2244 Z	311 275w??  4Jx(.tz::  
 )-%,0+1FTaHbHb,c,c)' &-- . . ,39+=	. . .    ,0022222!22  74g>>  -Jx(.tz::  
 *.)BB(L%,00222!%%'''''!@@ 7<gFF  ,Jx(.tz::H%	   ):;; Y`YdYdenprYsYs   *0#(>%,0022222G  #3#5666"))++ %	30E %	30557777777772#33 ;"#7AAG!(W!5!5J 'F 3 3I(2J99
D))D//))))))))!%66 	;"#8'BBG";;vt44D$[[266<"F**T6222222222%))++++ jC&s\'9::: *) ,+
 $ 2 2 2(c12 " 	3== 7;WEE,Q/'&/A(,(=bqb(ATG(K%{{;66 ,%))+++++, , ^"3#5666 tCs\1222s   #C0N 
N.!N))N.HWebSocketDisconnectEvent | WebSocketConnectEvent | WebSocketReceiveEventc                  K   | j         sd| _         ddiS | j                                         d {V  | j        rdddS | j                                        rdddS 	 |                                  d {V }nP# t          $ rC}| j                                         | j	        j
        r
dddcY d }~S d|j        dcY d }~S d }~ww xY wt          |t                    rd	|d
S d	|dS )NTr   zwebsocket.connectzwebsocket.disconnecti  )r   r   i  r   zwebsocket.receive)r   r   )r   r   )rk   rh   r   rl   ri   r   recvr   r   rH   r6   r   
isinstancer   )r-   r   r   s      r.   r   zWebSocketProtocol.asgi_receivej  si        	1 $D/00,113333333330 	B 3DAAA##%% 	B2DAAA	F$$$$$$DD 	F 	F 	F!!###~% F 6EEEEEEEE2CHEEEEEEEE		F dC   	?/>>>+d;;;s*   A: :
C)C-C3	C<CCr*   )r@   r   rA   r#   rB   rC   rD   rE   )rb   r{   r'   r(   )r   r   r'   r(   )r'   r(   )r   r   r'   r(   )r   r   r   r
   r'   r   )r   r
   r   r   r'   r   )r   r   r   r   r'   r   )r   r   r'   r(   )r'   r   )r9   r:   r;   __annotations__rq   r   r   r   r   r   r   r   rG   r   r   r   __classcell__)rz   s   @r.   r=   r=   9   sX        ((((7777 377
 7
 7
 7
 7
 7
 7
r+ + + + + +# # # # # #   ! ! ! !-% -% -% -%^) ) ) )+ + + +	! 	! 	! 	!' ' ' ':]3 ]3 ]3 ]3~< < < < < < < <r1   r=   )7
__future__r   r\   r   rv   typingr   r   r   r   r   urllib.parser	   r   websockets.legacy.handshakewebsockets.datastructuresr
   websockets.exceptionsr   websockets.extensions.baser   (websockets.extensions.permessage_deflater   websockets.legacy.serverr   websockets.serverr   websockets.typingr   uvicorn._typesr   r   r   r   r   r   r   r   r   r   r   uvicorn.configr   uvicorn.loggingr   uvicorn.protocols.utilsr   r   r    r!   r"   uvicorn.serverr#   r%   r=   r+   r1   r.   <module>r     sm   " " " " " "    9 9 9 9 9 9 9 9 9 9 9 9 9 9                 " " " " - - - - - - 2 2 2 2 2 2 = = = = = = S S S S S S 1 1 1 1 1 1 5 5 5 5 5 5 ) ) ) ) ) )                          " ! ! ! ! ! + + + + + +              ' & & & & &
  
  
  
  
  
  
  
 L< L< L< L< L</ L< L< L< L< L<r1   