
    /Jf(@                        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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   )HEADER_NAME_RE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                 H   d | _         d | _        	 t          j        j        | g|R i | | j        j        j        | _        d | _        d| _	        | j        
                    |            d S # t          $ r- t                              t          j                                w xY w)NF)client_addrserver_addrr   Request__init__channelfactoryserverapplication_queue_response_startedprotocol_connected	Exceptionloggererror	traceback
format_exc)selfargskwargss      W/home/alex/cs2snipeproduction/venv/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                     | _        | j                                        D ]3\  }}t	          j        |          s|                     ddd            d S 4d }| j                            d          r | j                            d          d         }t          | j
        d          rit          | j
        d          rTt          | j
        j                  | j
        j        g| _        t          | j        j                  | j        j        g| _        |                                 rdnd	| _        | j        j        rNt)          | j        | j        j        | j        j        | j        j        | j        | j                  \  | _        | _        	 | j                            d
           n/# t2          $ r" d| _        |                     ddd           Y d S w xY wd| _        d| j        v rj| j                            dd          d         | _        	 | j                            d
           n(# t2          $ r |                     ddd           Y d S w xY w|r|                                dk    r| j        j                            | j         !                                          }|sC| "                    d           tF          $                    d           | %                                 | j        |_&        | j         d c}| _         tO          |tP                    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  }|0                    |           tF          1                    d| j                   | j        2                    |            | j3        j4        5                                 d S g | _6        | j        j7        | _7        | j                                        D ]}\  }}d|v r
|D ]p}	|                                dk    r(tq          |	                    d
                    | _7        B| j6        9                    |                                |	f           q~tF          1                    d| j,        | j                   | j.        :                    dd           tw          | j        j<        | d	| j=                            d          d                             d
          | j,                            d
          tq          | j                            d
                    | j        | j7        | j        | j        | j6        | j        | j        d          V | _>        | j>        | j.        j?        rd S | j        j@        }
	 | j.        /                    |
          }t          |          |
k      }d ||d!}| j>        B                    |           |sd S S# t          $ rF tF          D                    t          jF                               |                     dd"d#           Y d S w xY w)$Ni  s   Bad RequestzInvalid header names   Upgrader   hostporthttpsr   ascii   /z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)r3   body	more_body   Internal Server ErrorzDaphne HTTP processing error)Gtimerequest_startrequestHeadersgetAllRawHeadersr   	fullmatchbasic_error	hasHeadergetRawHeadershasattrr<   strr'   r(   r   r   isSecureclient_schemer   proxy_forwarded_address_headerr   proxy_forwarded_port_headerproxy_forwarded_proto_headerr6   decodeUnicodeDecodeErrorr:   urisplitlower
ws_factorybuildProtocol	transportgetPeersetResponseCoder   warnfinish_raw_query_string
isinstancer   wrappedProtocolprotocolmakeConnectionr5   joincontentreaddataReceiveddebugprotocol_disconnectedr   _networkProducerresumeProducingclean_headersr8   r   appendseekr   create_applicationclientprotor   closedrequest_buffer_sizelen
put_nowaitr   r   r    r!   )r"   name_upgrade_headerr^   rV   datahvaluesvaluebuffer_sizechunkr>   payloads                 r%   processzWebRequest.processD   s     M	!%D  .??AA  a%/55 $$S.:OPPPFF
 "N",,Z88 R!%!4!B!B:!N!Nq!Qt{F++ IV0L0L I %((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     	sw   A"Y (D2Y F6 5Y 6(G"Y !G""9Y H7 6Y 7!IY IGY 8GY <AY Y AZ,+Z,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   rd   r   r   r   connectionLostr   re   )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 )r}   zHTTP close for %sN)
r   r~   r   rd   r   r   r   rZ   r   re   r"   s    r%   rZ   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	        rL| j        
                    d
          s2|                     d| j        j	                                                   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
        Nr3   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.r;   r   s   serverz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)r6   r   r5   r<   
time_takensizezHTTP response chunk for %szCannot handle message type %s!)"finishedr   
ValueErrorr   rX   getresponseHeadersaddRawHeaderr   server_namerF   	setHeaderencoder   rd   r   r   r   writerZ   rQ   rO   rP   repr
log_actioncoder5   tupleduration
sentLengthr   r   r    r!   )r"   messageheaderrw   rQ   s        r%   handle_replyzWebRequest.handle_reply   sH   
 = 	DL0F  :;;;6?333% K !IJJJ%)D"w&& R     !2333!(Y!;!; A A$11&%@@@@{& Lt/C/M/Mh/W/W Ly$+*A*H*H*J*JKKK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 $(#3!*%0@*A*A A A%)*.--//$(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%    G; ;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)rE   )r"   	exceptions     r%   handle_exceptionzWebRequest.handle_exception1  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   warningrZ   rE   r   s    r%   check_timeoutszWebRequest.check_timeouts7  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.
        r3   zhttp.disconnectN)r6   r   rp   r   s    r%   r~   zWebRequest.send_disconnectI  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.
        rA   r   )rH   r@   rA   r   s    r%   r   zWebRequest.durationR  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)r3   r   r;   r   r   r*   )titler=   utf8)r3   r=   N)r   error_templaterI   rO   r   )r"   r   status_textr=   s       r%   rE   zWebRequest.basic_errorZ  s     	- JK 	
 	
 	
 	,'!$Vs!2[5G5G5P5P!P $ 
 &..	 		
 	
 	
 	
 	
r,   c                 :    t          t          |                     S N)hashidr   s    r%   __hash__zWebRequest.__hash__r  s    BtHH~~r,   c                 B    t          |           t          |          k    S r   )r   )r"   others     r%   __eq__zWebRequest.__eq__u  s    $xx2e99$$r,   N)__name__
__module____qualname____doc__r   r   r   r   r{   r   rZ   r   r   r   r~   r   rE   r   r    r,   r%   r   r      s>        	" 	"	
 	
 
						s		/ 4    N N _N`0 0 00 0 0DQ DQ DQL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__  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   rU   r   requestFactoryr   r   r   r    r!   )r"   addrr^   s      r%   rU   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   rU   r   r   r,   r%   r   r   y  sK                 r,   r   )loggingr@   r    urllib.parser   twisted.internet.deferr   r   twisted.internet.interfacesr   twisted.protocols.policiesr   twisted.webr   zope.interfacer	   utilsr   r   	getLoggerr   r   r   r   r   r   r,   r%   <module>r      sC                     A A A A A A A A C C C C C C 6 6 6 6 6 6       & & & & & & 8 8 8 8 8 8 8 8		8	$	$e% e% e% e% e% e% e% e%P ())& & & & &$" & & *)& & &r,   