
    z
g>                        d dl Z 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 d dlmZ d dlmZ dd	lmZ  e j        e          Z G d
 dej                  Z ee	           G d dej                              ZdS )    N)unquote)inlineCallbacksmaybeDeferred)IProtocolNegotiationFactory)ProtocolWrapper)http)implementer   )parse_x_forwarded_forc                      e Zd ZdZd                    dd                              dd                              dd                              dd          Zd	 Zed
             Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd ZdS )
WebRequestz
    Request that either hands off information to channels, or offloads
    to a WebSocket class.

    Does some extra processing over the normal Twisted Web request to separate
    GET and POST out.
    a  
        <html>
            <head>
                <title>%(title)s</title>
                <style>
                    body { font-family: sans-serif; margin: 0; padding: 0; }
                    h1 { padding: 0.6em 0 0.2em 20px; color: #896868; margin: 0; }
                    p { padding: 0 0 0.3em 20px; margin: 0; }
                    footer { padding: 1em 0 0.3em 20px; color: #999; font-size: 80%%; font-style: italic; }
                </style>
            </head>
            <body>
                <h1>%(title)s</h1>
                <p>%(body)s</p>
                <footer>Daphne</footer>
            </body>
        </html>
    
 z     z   z  c                 ,   	 t          j        j        | g|R i | | j        j        j        | _        d | _        d| _        | j                            |            d S # t          $ r- t                              t          j                                w xY w)NF)r   Request__init__channelfactoryserverapplication_queue_response_startedprotocol_connected	Exceptionloggererror	traceback
format_exc)selfargskwargss      F/var/www/html/env/lib/python3.11/site-packages/daphne/http_protocol.pyr   zWebRequest.__init__4   s    		L!$8888888,.5DK%)D"%*D"K**400000 	 	 	LL-//000	s   AA 7Bc              #     K   	 t          j                     | _        d }| j                            d          r | j                            d          d         }t          | j        d          rjt          | j        d          rUt          | j        j                  | j        j	        g| _
        t          | j        j                  | j        j	        g| _        nd | _
        d | _        |                                 rdnd| _        | j        j        rNt!          | j        | j        j        | j        j        | j        j        | j
        | j                  \  | _
        | _        	 | j                            d           n/# t*          $ r" d| _        |                     d	d
d           Y d S w xY wd| _        d| j        v rj| j                            dd          d         | _        	 | j                            d           n(# t*          $ r |                     d	d
d           Y d S w xY w|r|                                dk    r| j        j                            | j                                                  }|sC|                     d           t@          !                    d           | "                                 | j        |_#        | j        d c}| _        tI          |tJ                    r||_&        n||_'        |(                    |           | j)        dz   | j        z   dz   }| j        *                                D ]/}||d         dz   d+                    |d                   z   dz   z  }0|dz  }|| j,        -                                z  }|.                    |           t@          /                    d| j
                   | j        0                    |            | j1        j2        3                                 d S g | _4        | j        j5        | _5        | j        *                                D ]}\  }}d|v r
|D ]p}|                                dk    r(tm          |                    d                    | _5        B| j4        7                    |                                |f           q~t@          /                    d| j)        | j
                   | j,        8                    dd           ts          | j        j:        | d| j;                            d          d                             d          | j)                            d          tm          | j                            d                    | j        | j5        | j        | j        | j4        | j
        | j        d          V | _<        | j<        | j,        j=        rd S | j        j>        }		 | j,        -                    |	          }
t          |
          |	k      }d|
|d }| j<        @                    |           |sd S S# t          $ rF t@          B                    t          jD                               |                     dd!d"           Y d S w xY w)#Ns   Upgrader   hostporthttpsr   ascii   /i  s   Bad RequestzInvalid characters in path       ?r
   zInvalid query strings	   websocket  z!Could not make WebSocket protocol    s    HTTP/1.1
s   :    ,s   
z#Upgraded connection %s to WebSocket   _s   daphne-root-pathzHTTP %s request for %s)typehttp_versionmethodpathraw_path	root_pathschemequery_stringheadersclientr   Tzhttp.request)r0   body	more_body   Internal Server ErrorzDaphne HTTP processing error)Etimerequest_startrequestHeaders	hasHeadergetRawHeadershasattrr9   strr$   r%   client_addrserver_addrisSecureclient_schemer   proxy_forwarded_address_headerr   proxy_forwarded_port_headerproxy_forwarded_proto_headerr3   decodeUnicodeDecodeErrorbasic_errorr7   urisplitlower
ws_factorybuildProtocol	transportgetPeersetResponseCoder   warnfinish_raw_query_string
isinstancer   wrappedProtocolprotocolmakeConnectionr2   getAllRawHeadersjoincontentreaddataReceiveddebugprotocol_disconnectedr   _networkProducerresumeProducingclean_headersr5   r   appendseekr   create_applicationclientprotor   closedrequest_buffer_sizelen
put_nowaitr   r   r   r   )r   upgrade_headerr[   rS   datahnamevaluesvaluebuffer_sizechunkr;   payloads                r"   processzWebRequest.processB   s     I	!%D!N",,Z88 R!%!4!B!B:!N!Nq!Qt{F++ (V0L0L ( %((8$9$94;;K#L $'	$7$7#H  #' #' ,0MMOO!GD {9 7L'K>K;K<$&8 84 $"4	  ))))%    	  n6RSSS
 !$Dtx$(HNN4$;$;A$>!%,,W5555)   $$S.:PQQQFF  V."6"6"8"8L"H"H;1??N**,,    "((---KK CDDDKKMMM-1->*,0ND)	4>i99 208I--)1I&''	222{T)DH47KK,==?? I IAAaD5L499QqT??:[HHDD#))+++%%d+++BDDTUUU11$777-==?????
 &("!%!6$($7$H$H$J$J M MLD&t|| !' M M::<<+>>>-4U\\'5J5J-K-KDNN .55tzz||U6KLLLL	M
 5t{DDTUUU!!!Q'''/<K2 &(,(8(>(>t(D(DR(H(O(O#) ) #'+"4"4W"="= '	(8(8(A(A B B$(I%)^"&"4(,(9#'#5"&"2"&"2 0 0 * *&* )1T\5H1F"k=
 L--k::E%(ZZ+%= >I . %%. G
 *55g>>>$ 
  	 	 	LL-//000-/M     	sp   EX E8 7X 8(F$ X #F$$9X G9 8X 9!HX HGX :GX >AX X AY.-Y.c                     | j         r|                                  t                              d| j                   t
          j                            | |           | j        	                    |            dS )3
        Cleans up reply channel on close.
        zHTTP disconnect for %sN)
r   send_disconnectr   rb   rD   r   r   connectionLostr   rc   )r   reasons     r"   r|   zWebRequest.connectionLost   sm     ! 	#  """-t/?@@@##D&111))$/////r)   c                     | j         r|                                  t                              d| j                   t
          j                            |            | j        	                    |            dS )rz   zHTTP close for %sN)
r   r{   r   rb   rD   r   r   rW   r   rc   r   s    r"   rW   zWebRequest.finish   sk     ! 	#  """($*:;;;D!!!))$/////r)   c                 .   | j         s| j        dS d|vrt          d          |d         dk    r| j        rt          d          d| _        d|vrt          d          |                     |d                    |                    d	i           D ] \  }}| j                            ||           !| j        j	        rU| j        j	        
                                d
k    r3|                     d| j        j	                            d                     t                              d|d         | j                   dS |d         dk    r| j        st          d|d         z            t           j                            | |                    dd                     |                    dd          s)|                                  t                              d| j                   	 | j                            d          }n$# t,          $ r t/          | j                  }Y nw xY w	 | j                            dd|| j        | j                            dd          | j        rdt7          | j                  z  nd|                                 | j        d           dS # t<          $ r/ t                              tA          j!                               Y dS w xY wt                              d| j                   dS t          d|d         z            )z1
        Handles a reply from the client
        Nr0   zMessage has no type definedhttp.response.startz&HTTP response has already been startedTstatusz<Specifying a status code is required for a Response message.r8   daphnes   serverzutf-8zHTTP %s response started for %shttp.response.bodyz1HTTP response has not yet been started but got %sr:   r)   r;   FzHTTP response complete for %sr'   r   completereplacez%s:%s)r3   r   r2   r9   
time_takensizezHTTP response chunk for %szCannot handle message type %s!)"finishedr   
ValueErrorr   rU   getresponseHeadersaddRawHeaderr   server_namerP   	setHeaderencoder   rb   rD   r   r   writerW   rN   rK   rL   repr
log_actioncoder2   tupleduration
sentLengthr   r   r   r   )r   messageheaderrt   rN   s        r"   handle_replyzWebRequest.handle_reply   sJ   
 = 	DL0F  :;;;6?333% K !IJJJ%)D"w&& R     !2333!(Y!;!; A A$11&%@@@@{& S4;+B+H+H+J+Jh+V+Vy$+*A*H*H*Q*QRRRLL1783DdFV     V_ 444)  Gfo&  
 LtW[[%=%=>>>;;{E22 M<d>NOOO)(//'22CC) ) ) )tx..CCC)9K**"$'&*i&*k&8&8)&L&L#/'&gd6F0G0G&G&G!%*.--//$(O	 	     ! 9 9 9LL!5!7!78888889 94;KLLLLL=OPPPs%   )H H%$H%)A6J! !5KKc                 4    |                      ddd           dS )zF
        Called by the server when our application tracebacks
        r+   r<   zException inside application.N)rM   )r   	exceptions     r"   handle_exceptionzWebRequest.handle_exception)  s$     	68WXXXXXr)   c                    | j         j        rr|                                 | j         j        k    rR| j        r0t                              d           |                                  dS |                     ddd           dS dS dS )zK
        Called periodically to see if we should timeout something
        z,Application timed out while sending responsei  s   Service Unavailablez0Application failed to respond within time limit.N)r   http_timeoutr   r   r   warningrW   rM   r   s    r"   check_timeoutszWebRequest.check_timeouts/  s    
 ;# 		$+:R(R(R% MNNN  *F    		 		(R(Rr)   c                 P    | j         r| j                            ddi           dS dS )z_
        Sends a http.disconnect message.
        Useful only really for long-polling.
        r0   zhttp.disconnectN)r3   r   rn   r   s    r"   r{   zWebRequest.send_disconnectA  s>     9 	K"--v7H.IJJJJJ	K 	Kr)   c                 \    t          | d          sdS t          j                    | j        z
  S )zB
        Returns the time since the start of the request.
        r>   r   )rB   r=   r>   r   s    r"   r   zWebRequest.durationJ  s/     t_-- 	1y{{T///r)   c                     |                      d|dgd           |                      d| j        t          |          dz   |                    d          z   |dz                      d          d	           d
S )zF
        Responds with a server-level error page (very basic)
        r   )s   Content-Types   text/html; charset=utf-8)r0   r   r8   r   r   r'   )titler:   utf8)r0   r:   N)r   error_templaterC   rK   r   )r   r   status_textr:   s       r"   rM   zWebRequest.basic_errorR  s     	- JK 	
 	
 	
 	,'!$Vs!2[5G5G5P5P!P $ 
 &..	 		
 	
 	
 	
 	
r)   c                 :    t          t          |                     S N)hashidr   s    r"   __hash__zWebRequest.__hash__j  s    BtHH~~r)   c                 B    t          |           t          |          k    S r   )r   )r   others     r"   __eq__zWebRequest.__eq__m  s    $xx2e99$$r)   N)__name__
__module____qualname____doc__r   r   r   r   rx   r|   rW   r   r   r   r{   r   rM   r   r    r)   r"   r   r      s>        	" 	"	
 	
 
						s		/ 4
 
 
 J J _JX0 0 00 0 0BQ BQ BQHY Y Y  $K K K0 0 0
 
 
0  % % % % %r)   r   c                   $    e Zd ZdZd Zd Zd ZdS )HTTPFactoryz
    Factory which takes care of tracking which protocol
    instances or request instances are responsible for which
    named response channels, so incoming messages can be
    routed appropriately.
    c                 R    t           j                            |            || _        d S r   )r   r   r   r   )r   r   s     r"   r   zHTTPFactory.__init__z  s$    !!$'''r)   c                     	 t           j                            | |          }t          |_        |S # t
          $ r0 t                              dt          j	                    z              w xY w)z
        Builds protocol instances. This override is used to ensure we use our
        own Request object instead of the default.
        zCannot build protocol: %s)
r   r   rR   r   requestFactoryr   r   r   r   r   )r   addrr[   s      r"   rR   zHTTPFactory.buildProtocol~  sk    
	'55dDAAH&0H#O 	 	 	LL4y7K7M7MMNNN	s	   -0 :A*c                 P    dg}t           j        r|                    dd           |S )a)  
        Protocols this server can speak after ALPN negotiation. Currently that
        is HTTP/1.1 and optionally HTTP/2. Websockets cannot be negotiated
        using ALPN, so that doesn't go here: anyone wanting websockets will
        negotiate HTTP/1.1 and then do the upgrade dance.
        s   http/1.1r   s   h2)r   
H2_ENABLEDinsert)r   baseProtocolss     r"   acceptableProtocolszHTTPFactory.acceptableProtocols  s1     %? 	+  E***r)   N)r   r   r   r   r   rR   r   r   r)   r"   r   r   q  sK                 r)   r   )loggingr=   r   urllib.parser   twisted.internet.deferr   r   twisted.internet.interfacesr   twisted.protocols.policiesr   twisted.webr   zope.interfacer	   utilsr   	getLoggerr   r   r   r   r   r   r)   r"   <module>r      s=                     A A A A A A A A C C C C C C 6 6 6 6 6 6       & & & & & & ( ( ( ( ( (		8	$	$]% ]% ]% ]% ]% ]% ]% ]%@ ())& & & & &$" & & *)& & &r)   