
    F j:                    F   S SK Jr  S SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
  S SKJr  S SKrS SKrS SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJrJrJ r J!r!J"r"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)J*r*J+r+J,r,J-r-J.r.  S SK/J0r0   " S S5      r1 " S S\5      r2g)    )annotationsN)Sequence)AnyLiteralcast)unquote)Headers)ConnectionClosed)ServerExtensionFactory)ServerPerMessageDeflateFactory)HTTPResponse)WebSocketServerProtocol)Subprotocol)ASGI3ApplicationASGISendEventWebSocketConnectEventWebSocketDisconnectEventWebSocketReceiveEventWebSocketScope)Config)TRACE_LOG_LEVEL)ClientDisconnectedget_client_addrget_local_addrget_path_with_query_stringget_remote_addris_ssl)ServerStatec                  6    \ rS rSrSrSS jrSS jrS	S jrSrg)
Server)   Fc                    g N selfwss     Vc:\Py\emelo-reg\venv\Lib\site-packages\uvicorn/protocols/websockets/websockets_impl.pyregisterServer.register,           c                    g r#   r$   r%   s     r(   
unregisterServer.unregister/   r+   r,   c                $    U R                   (       + $ r#   )closingr&   s    r(   
is_servingServer.is_serving2   s    <<r,   r$   N)r'   r   returnNone)r5   bool)	__name__
__module____qualname____firstlineno__r1   r)   r.   r3   __static_attributes__r$   r,   r(   r    r    )   s    G r,   r    c                     ^  \ rS rSr% S\S'   S\S'    S       SU 4S jjjr    SU 4S jjrSU 4S jjrSS	 jrSS
 jr	SS jr
      SS jrSS jrSS jrSS jrSS jrSS jrSrU =r$ )WebSocketProtocol6   zlist[tuple[str, str]]extra_headersz+logging.Logger | logging.LoggerAdapter[Any]loggerc                  > UR                   (       d  UR                  5         Xl        [        [        UR
                  5      U l        U=(       d    [        R                  " 5       U l	        UR                  U l
        X0l        UR                  U l        UR                  U l        S U l        S U l        S U l        S U l        U   [        R$                  " 5       U l        [        R$                  " 5       U l        [        R$                  " 5       U l        S U l        SU l        SU l        S U l        [5        5       U l        / nU R                  R8                  (       a  UR;                  [=        5       5        [>        TU ]  U RB                  U R6                  U R                  RD                  U R                  RF                  U R                  RH                  U R                  RJ                  U[L        RN                  " S5      S9  S U l(        URR                   VVs/ s H'  u  pgURU                  S5      URU                  S5      4PM)     snnU l+        g s  snnf )NFzuvicorn.error)
ws_handler	ws_servermax_size	max_queueping_intervalping_timeout
extensionsrA   latin-1),loadedloadconfigr   r   
loaded_appappasyncioget_event_looploop	root_path	app_state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    rD   ws_per_message_deflateappendr   super__init__rC   ws_max_sizews_max_queuews_ping_intervalws_ping_timeoutlogging	getLoggerserver_headerdefault_headersdecoder@   )	r&   rM   server_staterT   _looprI   namevalue	__class__s	           r(   rf   WebSocketProtocol.__init__:   s    }}KKM(&*;*;<5W335	))" (33!''
 -159.2,0 	'.}}$)0&#MMO59!05-8<!!'35
;;--<>?nn[[,,kk..++6644!$$_5 	 		
 "Q]QmQm
Qm+$T[[#U\\)%<=Qm
 
s   .Ic                  > U R                   R                  U 5        Xl        [        U5      U l        [        U5      U l        [        U5      (       a  SOSU l        U R                  R                  [        5      (       aC  U R                  (       a  SU R                  -  OSnU R                  R                  [        SU5        [        TU ]9  U5        g )Nwssr'   %s:%d -  z%sWebSocket connection made)rU   addrW   r   rX   r   rY   r   rZ   rA   isEnabledForr   logre   connection_made)r&   rW   prefixrt   s      r(   r}   !WebSocketProtocol.connection_mades   s     	T""$Y/%i0%i00ed;;##O4415Z$++-"FKKOOO-JFS	*r,   c                  > U R                   R                  U 5        U R                  R                  [        5      (       aC  U R
                  (       a  SU R
                  -  OSnU R                  R                  [        SU5        U R                  R                  5       (       + U l	        U R                  R                  5         [        TU ]1  U5        Uc  U R                  R                  5         g g )Nrx   ry   z%sWebSocket connection lost)rU   removerA   r{   r   rY   r|   r]   is_setra   setre   connection_lostrW   close)r&   excr~   rt   s      r(   r   !WebSocketProtocol.connection_lost   s    %;;##O4415Z$++-"FKKOOO-JFS484R4R4Y4Y4[0[-&&**,$;NN  " r,   c                    SU R                   l        U R                  R                  5       (       a  U R	                  S5        OU R                  5         U R                  R                  5         g )NT  )rD   r1   r]   r   fail_connectionsend_500_responserW   r   r2   s    r(   shutdownWebSocketProtocol.shutdown   sM    !%))0022  &""$r,   c                :    U R                   R                  U5        g r#   )rV   discard)r&   tasks     r(   on_task_complete"WebSocketProtocol.on_task_complete   s    

4 r,   c                x  #    UR                  S5      u  p4n[        R                  R                  R	                  U5        / nUR                  S5       HA  nUR                  UR                  S5       Vs/ s H  oR                  5       PM     sn5        MC     UR                  5        V	V
s/ s H&  u  pU	R                  S5      U
R                  SSS94PM(     nn	n
[        U5      nU R                  U-   nU R                  R                  S5      UR                  S5      -   nSU R                  R                  SS	.S
U R                  U R                   U R"                  U R                  UUUR                  S5      UUU R$                  R'                  5       S0 0S.U l        U R*                  R-                  U R/                  5       5      nUR1                  U R2                  5        U R4                  R7                  U5        U R8                  R;                  5       I Sh  vN   U R<                  $ s  snf s  sn
n	f  N7f)z
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-Protocol,asciisurrogateescape)errors	websocketz2.4)versionspec_versionz1.1zwebsocket.http.response)typeasgihttp_versionrZ   rX   rY   rS   pathraw_pathquery_stringheaderssubprotocolsstaterI   N)	partition
websocketslegacy	handshakecheck_requestget_allextendsplitstrip	raw_itemsencoder   rS   rM   asgi_versionrZ   rX   rY   rT   copyscoperR   create_taskrun_asgiadd_done_callbackr   rV   rz   r\   waitr_   )r&   r   request_headerspath_portion_r   r   headertokenrr   rs   asgi_headers	full_pathfull_raw_pathr   s                  r(   process_request!WebSocketProtocol.process_request   s     )-s(;%##11/B"$%--.FGFFLL<M N<M5<M NO H
  /88:
: [[!5<<@Q<#RS: 	 
 |$NNT)	--g69L9LW9UU   $ 8 8%P!kkkkkk%(//8#(^^((*4b9

  yy$$T]]_5t445

t**//111$$$= !O
6 	2s+   A2H:4H-
H:+-H2EH:H8H:c                    U R                   $ )z
We override the standard 'process_subprotocol' behavior here so that
we return whatever subprotocol is sent in the 'accept' message.
)rb   )r&   r   available_subprotocolss      r(   process_subprotocol%WebSocketProtocol.process_subprotocol   s     (((r,   c                    SnSS[        [        U5      5      R                  S5      -   S-   SSU/nU R                  R	                  SR                  U5      5        U R                  R                  5         g )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
r,   )strlenr   rW   writejoinr\   r   )r&   msgcontents      r(   r   #WebSocketProtocol.send_500_response   sk    &`#c#h-"6"6w"??'I$
 	SXXg./ 	$$((*r,   c                t   #    U R                   R                  5         U R                  5       I Sh  vN   g N7f)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)r]   r   wait_closed)r&   protocolr   s      r(   rC   WebSocketProtocol.ws_handler   s+      	&&**,   s   .868c                  #     U R                  U R                  U R                  U R                  5      I Sh  vN nU R                  R                  5         U R                  R                  5       (       d,  U R                  R                  S5        U R                  5         OUb  U R                  R                  SU5        U R                  R                  5       I Sh  vN   U R                   R#                  5         g N N!! [         a    U R                  R                  5          NE[         a    U R                  R                  5         U R                  R                  S5        U R                  R                  5       (       d  U R                  5          NU R                  R                  5       I Sh  vN     Nf = f7f)zZ
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
)rO   r   asgi_receive	asgi_sendr^   r   r\   r   rA   errorr   r]   r   r   BaseException	exceptionrW   r   )r&   results     r(   r   WebSocketProtocol.run_asgi   sL    
	888DJJ0A0A4>>RRF !!#//6688!!"UV&&(#!!"XZ`a0055777% S" 8! " 	$!!# 	<!!#KK!!"CD//6688&&(4499;;;	<so   G 4D C>D B!G D G >D  G $F=&G (A-F=G F=4F75F=:G <F==G c                  #    U R                   R                  5       (       GdH  US   S:X  a  U R                  R                  S[	        U R
                  5      [        U R
                  5      5        S U l        [        [        S -  UR                  S5      5      U l        SU;   a%  U R                  R                  S US    5       5        U R                   R                  5         g US   S:X  a  U R                  R                  S[	        U R
                  5      [        U R
                  5      5        [        R                   R"                  / S	4U l        U R                   R                  5         U R$                  R                  5         g US   S
:X  a  U R                  R                  S[	        U R
                  5      [        U R
                  5      US   5        [        R                   " US   5      nUR                  S/ 5       VVs/ s H'  u  p4UR'                  S5      UR'                  S5      4PM)     nnnX%S	4U l        U R                   R                  5         g [)        SUS    S35      eU R$                  R                  5       (       d  U R                  c  U R*                  R-                  5       I S h  vN    US   S:X  aC  UR                  S5      nUR                  S5      nUc  UOUnU R/                  U5      I S h  vN   g US   S:X  aa  UR                  SS5      n	UR                  SS5      =(       d    Sn
U R1                  X5      I S h  vN   U R$                  R                  5         g [)        SUS    S35      eU R                  b{  US   S:X  a`  U R                  S   US   -   nU R                  S S U4-   U l        UR                  SS5      (       d  U R$                  R                  5         g g [)        SUS    S35      e[)        SUS    S35      es  snnf  GNn GN% N! [2         a  n[4        UeS nAff = f7f) Nr   zwebsocket.acceptz%s - "WebSocket %s" [accepted]subprotocolr   c              3  j   #    U  H)  u  pUR                  S 5      UR                  S 5      4v   M+     g7f)rJ   N)ro   ).0rr   rs   s      r(   	<genexpr>.WebSocketProtocol.asgi_send.<locals>.<genexpr>  s2      . ,>KD Y/i1HI+=s   13zwebsocket.closez%s - "WebSocket %s" 403r,   zwebsocket.http.response.startz%s - "WebSocket %s" %dstatusrJ   ziExpected ASGI message 'websocket.accept', 'websocket.close', or 'websocket.http.response.start' but got 'z'.zwebsocket.sendbytestextcodei  reasonry   zFExpected ASGI message 'websocket.send' or 'websocket.close', but got 'zwebsocket.http.response.body   body	more_bodyFz>Expected ASGI message 'websocket.http.response.body' but got 'zUnexpected ASGI message 'zA', after sending 'websocket.close' or response already completed.)r\   r   rA   infor   r   r   r_   r   r   getrb   r@   r   r   http
HTTPStatus	FORBIDDENr^   ro   RuntimeErrorr]   r   sendr   r
   r   )r&   messager   rr   rs   r   
bytes_data	text_datadatar   r   r   r   s                r(   r   WebSocketProtocol.asgi_send  s    ++2244v"44  4#DJJ/.tzz:
 )-%,0t1CW[[Q^E_,`)'&&-- . ,39+=	.  ,,002$55  -#DJJ/.tzz:
 *.)B)BB(L%,,002!!%%'$CC  ,#DJJ/.tzz:H%	 ):;Y`YdYdenprYsYs+$T[[+U\\)-DEYs   *0#(>%,,002 #CCJ6?BSSUW 
 ""))++0E0E0M005577726?&66!(W!5J 'F 3I(2(:9
D))D/))V_(99";;vt4D$[[26<"F**T222%%))+ '`ahioap`qqst  "".v"@@,,Q/'&/A(,(=(=bq(ATG(K%{{;66%%))+ 7 #%cdklrdscttv#wxx +GFO+< =1 1 [ 8 *
 3 $ 2(c12s   HQ.P2BQ4P"5Q:AP*  P%P* QA
P* P(P* /Q0P* B!Q%P* (P* *
Q 4P;;Q  Qc                P  #    U R                   (       d  SU l         SS0$ U R                  R                  5       I S h  vN   U R                  (       a  SSS.$ U R                  R                  5       (       a  SSS.$  U R                  5       I S h  vN n[        U[        5      (       a  S
US.$ S
US.$  Nv N%! [         ab    U R                  R                  5         U R                  R                  (       a  SSS.s $ SU R                  =(       d    SU R                  S	.s $ f = f7f)NTr   zwebsocket.connectzwebsocket.disconnecti  )r   r   i  r   )r   r   r   zwebsocket.receive)r   r   )r   r   )r`   r]   r   ra   r^   r   recvr
   r   rD   r1   
close_codeclose_reason
isinstancer   )r&   r   s     r(   r   WebSocketProtocol.asgi_receive[  s
       $D/00,,1133300 3DAA##%%2DAA	r$D dC  />>+d;;) 	4 % 	r!!#~~%% 6EE2DOO<St_c_p_pqq		rsR   :D&B3>D&<B7 B5B7  D&5B7 7AD#;D&=#D# D&"D##D&)rb   rO   rT   rY   r^   rM   r`   rU   r@   r]   r\   r_   rR   ra   rS   rZ   r   rX   rm   rV   rW   rD   r#   )rM   r   rp   r   rT   zdict[str, Any]rq   z asyncio.AbstractEventLoop | None)rW   zasyncio.Transportr5   r6   )r   zException | Noner5   r6   )r5   r6   )r   zasyncio.Task[None]r5   r6   )r   r   r   r	   r5   zHTTPResponse | None)r   r	   r   zSequence[Subprotocol] | Noner5   zSubprotocol | None)r   r   r   r   r5   r   )r   r   r5   r6   )r5   zHWebSocketDisconnectEvent | WebSocketConnectEvent | WebSocketReceiveEvent)r8   r9   r:   r;   __annotations__rf   r}   r   r   r   r   r   r   rC   r   r   r   r<   __classcell__)rt   s   @r(   r>   r>   6   s    ((77 377
7
 "7
 "	7

 07
 7
r+*+	+#!-%^))8T)	)+!4Vp< <r,   r>   )3
__future__r   rP   r   rk   collections.abcr   typingr   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   uvicorn.configr   uvicorn.loggingr   uvicorn.protocols.utilsr   r   r   r   r   r   uvicorn.serverr   r    r>   r$   r,   r(   <module>r     sr    "    $ % %    " - 2 = S 1 5 )  " +  '
  
 ~</ ~<r,   