
    z
gp4                        d dl Z d dlZd dlZd dlmZ  e j                    Zej                            dd          Z	e	; e
e	ej                  s) ej        de           ej        d=  ej        e           n ej        e           d dlZd dl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 d d	lmZ d
dlmZ d
dlmZ  ej         e!          Z" G d de#          Z$dS )    N)asyncioreactorztwisted.internet.reactorzSomething has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.)CancelledError)deferreactor)serverFromString)STDLibLogObserverglobalLogBeginner)http   )HTTPFactory)WebSocketFactoryc                       e Zd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Zd Zd Z	d Z
d Zed             Zd Zd Zd Zd ZdS )ServerNT    Q        r      
   Daphnec                    || _         |pg | _        g | _        g | _        || _        || _        || _        |	| _        |
| _        || _	        || _
        || _        || _        || _        || _        || _        || _        || _        || _        d| _        || _        || _        | j        s0t,                              d           t1          j        d           d S d S )NFz6No endpoints. This server will not listen on anything.r   )application	endpoints	listenerslistening_addressessignal_handlersaction_loggerhttp_timeoutping_intervalping_timeoutrequest_buffer_sizeproxy_forwarded_address_headerproxy_forwarded_port_headerproxy_forwarded_proto_headerwebsocket_timeoutwebsocket_connect_timeoutwebsocket_handshake_timeoutapplication_close_timeout	root_path	verbosityabort_startready_callableserver_nameloggererrorsysexit)selfr   r   r   r   r   r"   r&   r'   r    r!   r*   r#   r$   r%   r+   r(   r)   r-   r.   ws_protocolss                        ?/var/www/html/env/lib/python3.11/site-packages/daphne/server.py__init__zServer.__init__&   s    0 '"b#% .*(*(#6 .L++F(,H)!2)B&+F()B&"" ,&~ 	LLQRRRHQKKKKK	 	    c                    i | _         t          |           | _        t          | | j                  | _        | j                            | j        d| j                   | j	        dk    rt          j        d gdd           n't          j        t          t                    g           t          j        rt                               d           nt                               d	           t%          j        d| j                   t%          j        d
| j                   | j        D ]}t                               d|           t/          t$          t1          |                    }|                    | j                  }|                    | j                   |                    | j                   | j                            |           tA          j!        t$          j"                   | j	        dk    r&tA          j#                    $                    d           t%          j%        dd| j&                   | j'        s7| j(        r| (                                 t%          j)        | j*                   d S d S )N)serverT)autoPingTimeoutallowNullOriginopenHandshakeTimeoutr   c                     d S N )_s    r5   <lambda>zServer.run.<locals>.<lambda>g   s    4 r7   F)redirectStandardIOdiscardBufferzHTTP/2 support enabledzEHTTP/2 support not enabled (install the http2 and tls Twisted extras)   zConfiguring endpoint %s   beforeshutdown)installSignalHandlers)+connectionsr   http_factoryr   r.   
ws_factorysetProtocolOptionsr!   r(   r+   r	   beginLoggingTor   __name__r
   
H2_ENABLEDr/   infor   	callLaterapplication_checkertimeout_checkerr   r   strlistenaddCallbacklisten_success
addErrbacklisten_errorr   appendasyncioset_event_loop_asyncioEventloopget_event_loop	set_debugaddSystemEventTriggerkill_all_applicationsr,   r-   runr   )r3   socket_descriptioneplisteners       r5   rb   z
Server.runY   sg   '--*48HIII** - !%!A 	+ 	
 	
 	

 >Q, U$     ,.?.I.I-JKKK ? 	KK01111KKW  
 	!T5666!T1222"&. 	, 	,KK13EFFF!'3/A+B+BCCByy!233H  !4555 1222N!!(++++ 	w8999 >Q"$$..t444%h
D<VWWW 	D" &##%%%Kd.BCCCCCC	D 	Dr7   c                 z   t          |d          r|                                }t          |d          rt          |d          rv| j                            |j        |j        f           t                              d|                                j        |                                j                   dS dS dS dS )z_
        Called when a listen succeeds so we can store port details (if there are any)
        getHosthostportzListening on TCP address %s:%sN)hasattrrg   r   rZ   rh   ri   r/   rP   )r3   ri   rh   s      r5   rW   zServer.listen_success   s     4## 	<<>>DtV$$ v)>)> (//DI0FGGG4LLNN'LLNN'    		 	   r7   c                     t                               d|                                           |                                  d S )NzListen failure: %s)r/   criticalgetErrorMessagestop)r3   failures     r5   rY   zServer.listen_error   s4    ,g.E.E.G.GHHH		r7   c                 V    t           j        rt          j                     dS d| _        dS )z)
        Force-stops the server.
        TN)r   runningrn   r,   )r3   s    r5   rn   zServer.stop   s+     ? 	$LNNNNN#Dr7   c                 v    || j         v rt          d|z            dt          j                    i| j         |<   dS )z:
        Adds a protocol as a current connection.
        z)Protocol %r was added to main list twice!	connectedN)rI   RuntimeErrortimer3   protocols     r5   protocol_connectedzServer.protocol_connected   sD     t'''JXUVVV&149;;%?"""r7   c                 j    d| j         |         vr#t          j                    | j         |         d<   d S d S )Ndisconnected)rI   ru   rv   s     r5   protocol_disconnectedzServer.protocol_disconnected   s=    
 !1(!;;;9=DX&~666 <;r7   c                 D    d j                  vsJ t          j                    }|                    dddi                                ||j         fd          } j         vrdS t          j        |t          j                               j                  d<   |S )	a  
        Creates a new application instance that fronts a Protocol instance
        for one of our supported protocols. Pass it the protocol,
        and it will work out the type, supply appropriate callables, and
        return you the application's input queue
        application_instanceasgiversionz3.0c                 0                         |           S r>   )handle_reply)messagerw   r3   s    r5   rA   z+Server.create_application.<locals>.<lambda>   s    !2!28W!E!E r7   )scopereceivesendN)loop)rI   r[   Queue
setdefaultr   getensure_futurer^   )r3   rw   r   input_queuer}   s   ``   r5   create_applicationzServer.create_application   s     &T-=h-GGGGGmoo)U!3444#//OEEEEE  0  
  
 4+++4=D=R '))>
 >
 >
"#9: r7   c                   K   || j         vs!| j         |                             dd          rdS 	 |                     |           n&# t          $ r |                    ddd            w xY w|                    |           dS )zP
        Coroutine that jumps the reply message from asyncio to Twisted
        rz   Ni  s   Server ErrorzServer Error)rI   r   check_headers_type
ValueErrorbasic_errorr   )r3   rw   r   s      r5   r   zServer.handle_reply   s      
 4+++t/?/I/M/MD0
 0
+ F	##G,,,, 	 	 	  o~FFF	
 	g&&&&&s   A #A)c           	      n   | d         dk    sd S |                      dg           D ]\  }}t          |t                    s0t          d                    |t          |                              t          |t                    s0t          d                    |t          |                              d S )Ntypezhttp.response.startheadersz5Header name '{}' expected to be `bytes`, but got `{}`z6Header value '{}' expected to be `bytes`, but got `{}`)r   
isinstancebytesr   formatr   )r   kvs      r5   r   zServer.check_headers_type   s    v"777FKK	2.. 	 	DAqa''  KRR477   
 a''  LSS477   	 	r7   c                 F   t          | j                                                  D ]^\  }}|                    dd          }|                    dd          }|rrt	          j                    |z
  | j        k    rS|rQ|                                s=t                              d|t          |                     |
                                 |r|                                r	 |                                }|r^t          |t                    r|                                  nEt                              d||           |s|                    |           n# t"          $ r Y nw xY w| j        |         d= d}|s
|r| j        |= `t%          j        d| j                   dS )z
        Goes through the set of current application Futures and cleans up
        any that are done/prints exceptions for any that errored.
        rz   Nr}   zTApplication instance %r for connection %s took too long to shut down and was killed.z Exception inside application: %s)exc_infor   )listrI   itemsr   ru   r)   doner/   warningreprcancel	exceptionr   KeyboardInterruptrn   r0   handle_exceptionr   r   rQ   rR   )r3   rw   detailsrz   r}   r   s         r5   rR   zServer.application_checker   s   
 "&d&6&<&<&>&>!?!? (	/ (	/Hg";;~t<<L#*;;/Et#L#L  
2IKK,.1OOO' 20D0I0I0K0K 2NNn,X  
 )//111# ,(<(A(A(C(C ,E 4 > > @ @I
 ! E%i1BCC 
E IIKKKK"LL B ))2 )   
 $0 E ( 9 9) D D D &   D  $X./EF'+$' /L /$X.!T566666s   $E
E&%E&c                    g }| j                                         D ]G}|d         }|                                s)|                                 |                    |           Ht
                              dt          |                     t          j	        
                    t          j        |           }|                    d            |S )zG
        Kills all application coroutines before reactor exit.
        r}   z'Killed %i pending application instancesc                     d S r>   r?   )xs    r5   rA   z.Server.kill_all_applications.<locals>.<lambda>=  s    4 r7   )rI   valuesr   r   rZ   r/   rP   lenr   Deferred
fromFuturer[   gatherrX   )r3   wait_forr   r}   wait_deferreds        r5   ra   zServer.kill_all_applications/  s    
 '..00 	6 	6G#*+A#B ',,.. 6$++--- 4555=s8}}MMM11'.(2KLL  000r7   c                     t          | j                                                  D ]}|                                 t	          j        d| j                   dS )z~
        Called periodically to enforce timeout rules on all connections.
        Also checks pings at the same time.
        rD   N)r   rI   keyscheck_timeoutsr   rQ   rS   rv   s     r5   rS   zServer.timeout_checker@  s[    
 T-224455 	& 	&H##%%%%!T122222r7   c                 F    | j         r|                      |||           dS dS )zN
        Dispatches to any registered action logger, if there is one.
        N)r   )r3   rw   actionr   s       r5   
log_actionzServer.log_actionI  s7      	:x99999	: 	:r7   )NTNNr   r   r   r   r   r   NNNr   r   r   Nr   N)rN   
__module____qualname__r6   rb   rW   rY   rn   rx   r{   r   r   staticmethodr   rR   ra   rS   r   r?   r7   r5   r   r   %   s>         "$'+$(%)$%"$-1 1 1 1f5D 5D 5Dn    $ $ $@ @ @E E E  4' ' '$   \&.7 .7 .7`  "3 3 3: : : : :r7   r   )%r[   r1   warningstwisted.internetr   new_event_looptwisted_loopmodulesr   current_reactorr   AsyncioSelectorReactorwarnUserWarninginstallloggingru   concurrent.futuresr   r   r   twisted.internet.endpointsr   twisted.loggerr   r	   twisted.webr
   http_protocolr   ws_protocolr   	getLoggerrN   r/   objectr   r?   r7   r5   <module>r      s    



  + + + + + +%w%''+//"<dCC:o~'LMM -T 		
 	
 	
 K23|,,,N<(((   - - - - - - + + + + + + + + 7 7 7 7 7 7 ? ? ? ? ? ? ? ?       & & & & & & ) ) ) ) ) )		8	$	$i: i: i: i: i:V i: i: i: i: i:r7   