§
    Ètºf“&  ã                   ó*  — d Z ddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ  G d„ d	e¦  «        Z G d
„ de	¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )aT  
Simplistic HTTP proxy support.

This comes in two main variants - the Proxy and the ReverseProxy.

When a Proxy is in use, a browser trying to connect to a server (say,
www.yahoo.com) will be intercepted by the Proxy, and the proxy will covertly
connect to the server, and return the result.

When a ReverseProxy is in use, the client connects directly to the ReverseProxy
(say, www.yahoo.com) which farms off the request to one of a pool of servers,
and returns the result.

Normally, a Proxy is used on the client end of an Internet connection, while a
ReverseProxy is used on the server end.
é    )ÚquoteÚurlparseÚ
urlunparse)Úreactor)ÚClientFactory)Ú_QUEUED_SENTINELÚHTTPChannelÚ
HTTPClientÚRequest)ÚResource)ÚNOT_DONE_YETc                   ó:   — e Zd ZdZdZd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	S )
ÚProxyClientzµ
    Used by ProxyClientFactory to implement a simple web proxy.

    @ivar _finished: A flag which indicates whether or not the original request
        has been finished yet.
    Fc                 ó�   — || _         || _        || _        d|v r|d= d|d<   |                     dd ¦  «         || _        || _        d S )Ns   proxy-connections   closes
   connections
   keep-alive)ÚfatherÚcommandÚrestÚpopÚheadersÚdata©Úselfr   r   Úversionr   r   r   s          úP/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/proxy.pyÚ__init__zProxyClient.__init__)   s[   € ØˆŒØˆŒØˆŒ	Ø 'Ð)Ð)ØÐ+Ð,Ø!)ˆ�ÑØ�Š�M 4Ñ(Ô(Ð(ØˆŒØˆŒ	ˆ	ˆ	ó    c                 ó  — |                       | j        | j        ¦  «         | j                             ¦   «         D ]\  }}|                      ||¦  «         Œ|                      ¦   «          | j                             | j	        ¦  «         d S ©N)
ÚsendCommandr   r   r   ÚitemsÚ
sendHeaderÚ
endHeadersÚ	transportÚwriter   )r   ÚheaderÚvalues      r   ÚconnectionMadezProxyClient.connectionMade4   s‚   € Ø×Ò˜œ t¤yÑ1Ô1Ð1Ø!œ\×/Ò/Ñ1Ô1ð 	+ð 	+‰MˆF�EØ�OŠO˜F EÑ*Ô*Ð*Ð*Ø�ŠÑÔÐØŒ×Ò˜TœYÑ'Ô'Ð'Ð'Ð'r   c                 óV   — | j                              t          |¦  «        |¦  «         d S r   )r   ÚsetResponseCodeÚint)r   r   ÚcodeÚmessages       r   ÚhandleStatuszProxyClient.handleStatus;   s&   € ØŒ×#Ò#¥C¨¡I¤I¨wÑ7Ô7Ð7Ð7Ð7r   c                 ó¸   — |                      ¦   «         dv r#| j        j                             ||g¦  «         d S | j        j                             ||¦  «         d S )N)s   servers   dates   content-type)Úlowerr   ÚresponseHeadersÚsetRawHeadersÚaddRawHeader)r   Úkeyr&   s      r   ÚhandleHeaderzProxyClient.handleHeader>   s\   € ð
 �9Š9‰;Œ;Ð?Ð?Ð?ØŒKÔ'×5Ò5°c¸E¸7ÑCÔCÐCÐCÐCàŒKÔ'×4Ò4°S¸%Ñ@Ô@Ð@Ð@Ð@r   c                 ó:   — | j                              |¦  «         d S r   )r   r$   )r   Úbuffers     r   ÚhandleResponsePartzProxyClient.handleResponsePartH   s   € ØŒ×Ò˜&Ñ!Ô!Ð!Ð!Ð!r   c                 óŠ   — | j         s;d| _         | j                             ¦   «          | j                             ¦   «          dS dS )z�
        Finish the original request, indicating that the response has been
        completely written to it, and disconnect the outgoing transport.
        TN)Ú	_finishedr   Úfinishr#   ÚloseConnection)r   s    r   ÚhandleResponseEndzProxyClient.handleResponseEndK   sM   € ð
 Œ~ð 	,Ø!ˆDŒNØŒK×ÒÑ Ô Ð ØŒN×)Ò)Ñ+Ô+Ð+Ð+Ð+ð	,ð 	,r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r   r'   r-   r4   r7   r<   © r   r   r   r      s‚   € € € € € ðð ð €Ið	ð 	ð 	ð(ð (ð (ð8ð 8ð 8ðAð Að Að"ð "ð "ð,ð ,ð ,ð ,ð ,r   r   c                   ó(   — e Zd ZdZeZd„ Zd„ Zd„ ZdS )ÚProxyClientFactoryz?
    Used by ProxyRequest to implement a simple web proxy.
    c                 óZ   — || _         || _        || _        || _        || _        || _        d S r   )r   r   r   r   r   r   r   s          r   r   zProxyClientFactory.__init__^   s0   € ØˆŒØˆŒØˆŒ	ØˆŒØˆŒ	ØˆŒˆˆr   c                 ór   — |                       | j        | j        | j        | j        | j        | j        ¦  «        S r   )Úprotocolr   r   r   r   r   r   )r   Úaddrs     r   ÚbuildProtocolz ProxyClientFactory.buildProtocolf   s1   € Ø�}Š}ØŒL˜$œ) T¤\°4´<ÀÄÈDÌKñ
ô 
ð 	
r   c                 óâ   — | j                              dd¦  «         | j         j                             dd¦  «         | j                              d¦  «         | j                              ¦   «          dS )zh
        Report a connection failure in a response to the incoming request as
        an error.
        iõ  s   Gateway errors   Content-Types	   text/htmls   <H1>Could not connect</H1>N)r   r)   r0   r2   r$   r:   )r   Ú	connectorÚreasons      r   ÚclientConnectionFailedz)ProxyClientFactory.clientConnectionFailedk   si   € ð
 	Œ×#Ò# CÐ)9Ñ:Ô:Ð:ØŒÔ#×0Ò0°À,ÑOÔOÐOØŒ×ÒÐ7Ñ8Ô8Ð8ØŒ×ÒÑÔÐÐÐr   N)	r=   r>   r?   r@   r   rF   r   rH   rL   rA   r   r   rC   rC   V   sR   € € € € € ðð ð
 €Hðð ð ð
ð 
ð 
ð
ð ð ð ð r   rC   c                   ó4   — e Zd ZdZdeiZddiZeefd„Z	d„ Z
dS )ÚProxyRequestzÃ
    Used by Proxy to implement a simple web proxy.

    @ivar reactor: the reactor used to create connections.
    @type reactor: object providing L{twisted.internet.interfaces.IReactorTCP}
    s   httpéP   c                 ó@   — t          j        | ||¦  «         || _        d S r   ©r   r   r   ©r   ÚchannelÚqueuedr   s       r   r   zProxyRequest.__init__�   ó"   € ÝÔ˜˜w¨Ñ/Ô/Ð/ØˆŒˆˆr   c                 ó˜  — t          | j        ¦  «        }|d         }|d                              d¦  «        }| j        |         }d|v r'|                     d¦  «        \  }}t          |¦  «        }t          d|dd …         z   ¦  «        }|s|dz   }| j        |         }|                      ¦   «          	                    ¦   «         }d|vr| 
                    d¦  «        |d<   | j                             dd¦  «         | j                             ¦   «         } || j        || j        ||| ¦  «        }	| j                             |||	¦  «         d S )	Nr   é   Úasciiú:)r   r   é   ó   /ó   host)r   ÚuriÚdecodeÚportsÚsplitr*   r   Ú	protocolsÚgetAllHeadersÚcopyÚencodeÚcontentÚseekÚreadÚmethodÚclientprotor   Ú
connectTCP)
r   ÚparsedrF   ÚhostÚportr   Úclass_r   ÚsÚclientFactorys
             r   ÚprocesszProxyRequest.process…   sC  € Ý˜$œ(Ñ#Ô#ˆØ˜!”9ˆØ�aŒy×Ò Ñ(Ô(ˆØŒz˜(Ô#ˆØ�$ˆ;ˆ;ØŸš C™œ‰JˆD�$Ý�t‘9”9ˆDÝ˜* v¨a¨b¨b¤zÑ1Ñ2Ô2ˆØð 	Ø˜$‘;ˆDØ” Ô)ˆØ×$Ò$Ñ&Ô&×+Ò+Ñ-Ô-ˆØ˜'Ð!Ð!Ø#Ÿ{š{¨7Ñ3Ô3ˆG�GÑØŒ×Ò˜!˜QÑÔÐØŒL×ÒÑÔˆØ˜˜tœ{¨D°$Ô2BÀGÈQÐPTÑUÔUˆØŒ×Ò  d¨MÑ:Ô:Ð:Ð:Ð:r   N)r=   r>   r?   r@   rC   ra   r_   r   r   r   rq   rA   r   r   rN   rN   v   s\   € € € € € ðð ð Ð,Ð-€IØ�bˆM€Eà'7Àð ð ð ð ð;ð ;ð ;ð ;ð ;r   rN   c                   ó   — e Zd ZdZeZdS )ÚProxyao  
    This class implements a simple web proxy.

    Since it inherits from L{twisted.web.http.HTTPChannel}, to use it you
    should do something like this::

        from twisted.web import http
        f = http.HTTPFactory()
        f.protocol = Proxy

    Make the HTTPFactory a listener on a port as per usual, and you have
    a fully-functioning web proxy!
    N)r=   r>   r?   r@   rN   ÚrequestFactoryrA   r   r   rs   rs   š   s   € € € € € ðð ð "€N€N€Nr   rs   c                   ó(   — e Zd ZdZeZeefd„Zd„ Z	dS )ÚReverseProxyRequestal  
    Used by ReverseProxy to implement a simple reverse proxy.

    @ivar proxyClientFactoryClass: a proxy client factory class, used to create
        new connections.
    @type proxyClientFactoryClass: L{ClientFactory}

    @ivar reactor: the reactor used to create connections.
    @type reactor: object providing L{twisted.internet.interfaces.IReactorTCP}
    c                 ó@   — t          j        | ||¦  «         || _        d S r   rQ   rR   s       r   r   zReverseProxyRequest.__init__º   rU   r   c                 ó|  — | j                              d| j        j                             d¦  «        g¦  «         |                      | j        | j        | j        |  	                    ¦   «         | j
                             ¦   «         | ¦  «        }| j                             | j        j        | j        j        |¦  «         dS )zµ
        Handle this request by connecting to the proxied server and forwarding
        it there, then forwarding the response back as the response to this
        request.
        r\   rX   N)ÚrequestHeadersr1   Úfactoryrl   rd   ÚproxyClientFactoryClassrh   r]   ri   rb   re   rg   r   rj   rm   )r   rp   s     r   rq   zReverseProxyRequest.process¾   s§   € ð 	Ô×)Ò)¨'°D´LÔ4E×4LÒ4LÈWÑ4UÔ4UÐ3VÑWÔWÐWØ×4Ò4ØŒKØŒHØÔØ×ÒÑ Ô ØŒL×ÒÑÔØñ
ô 
ˆð 	Œ×Ò ¤Ô 1°4´<Ô3DÀmÑTÔTÐTÐTÐTr   N)
r=   r>   r?   r@   rC   r{   r   r   r   rq   rA   r   r   rv   rv   ¬   sS   € € € € € ð	ð 	ð 1Ðà'7Àð ð ð ð ðUð Uð Uð Uð Ur   rv   c                   ó   — e Zd ZdZeZdS )ÚReverseProxyzo
    Implements a simple reverse proxy.

    For details of usage, see the file examples/reverse-proxy.py.
    N)r=   r>   r?   r@   rv   rt   rA   r   r   r}   r}   Ð   s   € € € € € ðð ð )€N€N€Nr   r}   c                   ó,   — e Zd ZdZeZefd„Zd„ Zd„ Z	dS )ÚReverseProxyResourceaØ  
    Resource that renders the results gotten from another server

    Put this resource in the tree to cause everything below it to be relayed
    to a different server.

    @ivar proxyClientFactoryClass: a proxy client factory class, used to create
        new connections.
    @type proxyClientFactoryClass: L{ClientFactory}

    @ivar reactor: the reactor used to create connections.
    @type reactor: object providing L{twisted.internet.interfaces.IReactorTCP}
    c                 óf   — t          j        | ¦  «         || _        || _        || _        || _        dS )aU  
        @param host: the host of the web server to proxy.
        @type host: C{str}

        @param port: the port of the web server to proxy.
        @type port: C{port}

        @param path: the base path to fetch data from. Note that you shouldn't
            put any trailing slashes in it, it will be added automatically in
            request. For example, if you put B{/foo}, a request on B{/bar} will
            be proxied to B{/foo/bar}.  Any required encoding of special
            characters (such as " " or "/") should have been done already.

        @type path: C{bytes}
        N)r   r   rl   rm   Úpathr   )r   rl   rm   r�   r   s        r   r   zReverseProxyResource.__init__ë   s5   € õ  	Ô˜$ÑÔÐØˆŒ	ØˆŒ	ØˆŒ	ØˆŒˆˆr   c           	      óž   — t          | j        | j        | j        dz   t	          |d¬¦  «                             d¦  «        z   | j        ¦  «        S )z¿
        Create and return a proxy resource with the same proxy configuration
        as this one, except that its path also contains the segment given by
        C{path} at the end.
        r[   r   )Úsafezutf-8)r   rl   rm   r�   Úurlquoterd   r   )r   r�   Úrequests      r   ÚgetChildzReverseProxyResource.getChild  sO   € õ $ØŒIØŒIØŒI˜Ñ�x¨°3Ð7Ñ7Ô7×>Ò>¸wÑGÔGÑGØŒLñ	
ô 
ð 	
r   c                 ó4  — | j         dk    r| j        }nd| j        | j         fz  }|j                             d|                     d¦  «        g¦  «         |j                             dd¦  «         t          |j        ¦  «        d         }|r| j	        dz   |z   }n| j	        }|  
                    |j        ||j        |                     ¦   «         |j                             ¦   «         |¦  «        }| j                             | j        | j         |¦  «         t"          S )zJ
        Render a request by forwarding it to the proxied server.
        rO   z%s:%dr\   rX   r   é   ó   ?)rm   rl   ry   r1   rd   re   rf   r   r]   r�   r{   rh   ri   rb   rg   r   rj   r   )r   r…   rl   Úqsr   rp   s         r   ÚrenderzReverseProxyResource.render  s  € ð Œ9˜Š?ˆ?Ø”9ˆDˆDà˜dœi¨¬Ð3Ñ3ˆDØÔ×,Ò,¨W°t·{²{À7Ñ7KÔ7KÐ6LÑMÔMÐMØŒ×Ò˜Q Ñ"Ô"Ð"Ý�g”kÑ"Ô" 1Ô%ˆØð 	Ø”9˜tÑ# bÑ(ˆDˆDà”9ˆDØ×4Ò4ØŒNØØÔØ×!Ò!Ñ#Ô#ØŒO× Ò Ñ"Ô"Øñ
ô 
ˆð 	Œ×Ò ¤	¨4¬9°mÑDÔDÐDÝÐr   N)
r=   r>   r?   r@   rC   r{   r   r   r†   r‹   rA   r   r   r   r   Ú   s[   € € € € € ðð ð 1Ðà18ð ð ð ð ð,
ð 
ð 
ðð ð ð ð r   r   N)r@   Úurllib.parser   r„   r   r   Útwisted.internetr   Útwisted.internet.protocolr   Útwisted.web.httpr   r	   r
   r   Útwisted.web.resourcer   Útwisted.web.serverr   r   rC   rN   rs   rv   r}   r   rA   r   r   ú<module>r’      sâ  ðð
ð ð" AÐ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @à $Ð $Ð $Ð $Ð $Ð $Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ )Ð )Ð )Ð )Ð )Ð )Ø +Ð +Ð +Ð +Ð +Ð +ð4,ð 4,ð 4,ð 4,ð 4,�*ñ 4,ô 4,ð 4,ðnð ð ð ð ˜ñ ô ð ð@!;ð !;ð !;ð !;ð !;�7ñ !;ô !;ð !;ðH"ð "ð "ð "ð "ˆKñ "ô "ð "ð$!Uð !Uð !Uð !Uð !U˜'ñ !Uô !Uð !UðH)ð )ð )ð )ð )�;ñ )ô )ð )ðNð Nð Nð Nð N˜8ñ Nô Nð Nð Nð Nr   