ó
    ×›‘h“&  ã                   óþ   • S r SSK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  SSKJr  SSKJr   " S S	\5      r " S
 S\	5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)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                   ó@   • \ rS rSrSrSrS rS rS rS r	S r
S	 rS
rg)ÚProxyClienté   z¥
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                 ó€   • X`l         Xl        X l        SU;   a  US	 SUS'   UR                  SS 5        X@l        XPl        g )Ns   proxy-connections   closes
   connections
   keep-alive)ÚfatherÚcommandÚrestÚpopÚheadersÚdata©Úselfr   r   Úversionr   r   r   s          ÚUC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/web/proxy.pyÚ__init__ÚProxyClient.__init__)   sD   € ØŒØŒØŒ	Ø 'Ó)ØÐ+Ð,Ø!)ˆ�ÑØ�‰�M 4Ô(ØŒØ�	ó    c                 ó"  • U R                  U R                  U R                  5        U R                  R	                  5        H  u  pU R                  X5        M     U R                  5         U R                  R                  U R                  5        g ©N)
ÚsendCommandr   r   r   ÚitemsÚ
sendHeaderÚ
endHeadersÚ	transportÚwriter   )r   ÚheaderÚvalues      r   ÚconnectionMadeÚProxyClient.connectionMade4   s_   € Ø×Ñ˜Ÿ™ t§y¡yÔ1Ø!Ÿ\™\×/Ñ/Ö1‰MˆFØ�O‰O˜FÖ*ñ 2à�‰ÔØ�‰×Ñ˜TŸY™YÕ'r   c                 óN   • U R                   R                  [        U5      U5        g r!   )r   ÚsetResponseCodeÚint)r   r   ÚcodeÚmessages       r   ÚhandleStatusÚProxyClient.handleStatus;   s   € Ø�‰×#Ñ#¤C¨£I¨wÕ7r   c                 óÄ   • UR                  5       S;   a'  U R                  R                  R                  X/5        g U R                  R                  R	                  X5        g )N)s   servers   dates   content-type)Úlowerr   ÚresponseHeadersÚsetRawHeadersÚaddRawHeader)r   Úkeyr)   s      r   ÚhandleHeaderÚProxyClient.handleHeader>   sE   € ð
 �9‰9‹;Ð?Ó?Ø�K‰K×'Ñ'×5Ñ5°c¸7ÕCà�K‰K×'Ñ'×4Ñ4°SÕ@r   c                 ó:   • U R                   R                  U5        g r!   )r   r'   )r   Úbuffers     r   ÚhandleResponsePartÚProxyClient.handleResponsePartH   s   € Ø�‰×Ñ˜&Õ!r   c                 óž   • U R                   (       d<  SU l         U R                  R                  5         U R                  R	                  5         gg)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   ÚhandleResponseEndÚProxyClient.handleResponseEndK   s7   € ð
 �~�~Ø!ˆDŒNØ�K‰K×ÑÔ Ø�N‰N×)Ñ)Õ+ð r   )r@   r   r   r   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r@   r   r*   r1   r9   r=   rC   Ú__static_attributes__© r   r   r   r      s-   † ñð €Iò	ò(ò8òAò"õ,r   r   c                   ó.   • \ rS rSrSr\rS rS rS r	Sr
g)ÚProxyClientFactoryéV   z7
Used by ProxyRequest to implement a simple web proxy.
c                 óL   • X`l         Xl        X l        X@l        XPl        X0l        g r!   )r   r   r   r   r   r   r   s          r   r   ÚProxyClientFactory.__init__^   s    € ØŒØŒØŒ	ØŒØŒ	Ø�r   c                 ó¦   • U R                  U R                  U R                  U R                  U R                  U R
                  U R                  5      $ r!   )Úprotocolr   r   r   r   r   r   )r   Úaddrs     r   ÚbuildProtocolÚ ProxyClientFactory.buildProtocolf   s8   € Ø�}‰}Ø�L‰L˜$Ÿ)™) T§\¡\°4·<±<ÀÇÁÈDÏKÉKó
ð 	
r   c                 óò   • U R                   R                  SS5        U R                   R                  R                  SS5        U R                   R	                  S5        U R                   R                  5         g)zP
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-   r5   r7   r'   rA   )r   Ú	connectorÚreasons      r   ÚclientConnectionFailedÚ)ProxyClientFactory.clientConnectionFailedk   sV   € ð
 	�‰×#Ñ# CÐ)9Ô:Ø�‰×#Ñ#×0Ñ0°À,ÔOØ�‰×ÑÐ7Ô8Ø�‰×ÑÕr   )r   r   r   r   r   r   N)rE   rF   rG   rH   rI   r   rR   r   rT   rY   rJ   rK   r   r   rM   rM   V   s   † ñð
 €Hòò
õ
r   rM   c                   ó<   • \ rS rSrSrS\0rSS0r\\	4S jr
S rSrg)	ÚProxyRequestév   z³
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                 ó>   • [         R                  " XU5        X0l        g r!   ©r   r   r   ©r   ÚchannelÚqueuedr   s       r   r   ÚProxyRequest.__init__�   ó   € Ü×Ò˜¨Ô/Ø�r   c                 óp  • [        U R                  5      nUS   nUS   R                  S5      nU R                  U   nSU;   a  UR	                  S5      u  p4[        U5      n[        SUSS  -   5      nU(       d  US-   nU R                  U   nU R                  5       R                  5       nSU;  a  UR                  S5      US'   U R                  R                  SS5        U R                  R                  5       nU" U R                  XPR                  XxU 5      n	U R                   R#                  X4U	5        g )	Nr   é   ÚasciiÚ:)r   r   é   ó   /ó   host)r   ÚuriÚdecodeÚportsÚsplitr.   r   Ú	protocolsÚgetAllHeadersÚcopyÚencodeÚcontentÚseekÚreadÚmethodÚclientprotor   Ú
connectTCP)
r   ÚparsedrR   ÚhostÚportr   Úclass_r   ÚsÚclientFactorys
             r   ÚprocessÚProxyRequest.process…   s  € Ü˜$Ÿ(™(Ó#ˆØ˜!‘9ˆØ�a‰y×Ñ Ó(ˆØ�z‰z˜(Ñ#ˆØ�$‹;ØŸ™ C›‰JˆDÜ�t“9ˆDÜ˜* v¨a¨b zÑ1Ó2ˆÞØ˜$‘;ˆDØ—‘ Ñ)ˆØ×$Ñ$Ó&×+Ñ+Ó-ˆØ˜'Ó!Ø#Ÿ{™{¨7Ó3ˆG�GÑØ�‰×Ñ˜!˜QÔØ�L‰L×ÑÓˆÙ˜tŸ{™{¨D×2BÑ2BÀGÐPTÓUˆØ�‰×Ñ ¨MÕ:r   r   N)rE   rF   rG   rH   rI   rM   rq   ro   r	   r   r   r�   rJ   rK   r   r   r\   r\   v   s,   † ñð Ð,Ð-€IØ�bˆM€Eà'7Àô õ;r   r\   c                   ó   • \ rS rSrSr\rSrg)ÚProxyéš   aK  
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!
rK   N)rE   rF   rG   rH   rI   r\   ÚrequestFactoryrJ   rK   r   r   r„   r„   š   s   † ñð "ƒNr   r„   c                   ó0   • \ rS rSrSr\r\\4S jr	S r
Srg)ÚReverseProxyRequesté¬   aP  
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                 ó>   • [         R                  " XU5        X0l        g r!   r`   ra   s       r   r   ÚReverseProxyRequest.__init__º   re   r   c                 óÂ  • U R                   R                  SU R                  R                  R	                  S5      /5        U R                  U R                  U R                  U R                  U R                  5       U R                  R                  5       U 5      nU R                  R                  U R                  R                  U R                  R                  U5        g)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.
rl   rh   N)ÚrequestHeadersr6   Úfactoryr|   rt   ÚproxyClientFactoryClassrx   rm   ry   rr   ru   rw   r   rz   r}   )r   r€   s     r   r�   ÚReverseProxyRequest.process¾   s¤   € ð 	×Ñ×)Ñ)¨'°D·L±L×4EÑ4E×4LÑ4LÈWÓ4UÐ3VÔWØ×4Ñ4Ø�K‰KØ�H‰HØ×ÑØ×ÑÓ Ø�L‰L×ÑÓØó
ˆð 	�‰×Ñ §¡× 1Ñ 1°4·<±<×3DÑ3DÀmÕTr   r   N)rE   rF   rG   rH   rI   rM   r�   r	   r   r   r�   rJ   rK   r   r   rˆ   rˆ   ¬   s   † ñ	ð 1Ðà'7Àô õUr   rˆ   c                   ó   • \ rS rSrSr\rSrg)ÚReverseProxyéÐ   zc
Implements a simple reverse proxy.

For details of usage, see the file examples/reverse-proxy.py.
rK   N)rE   rF   rG   rH   rI   rˆ   r†   rJ   rK   r   r   r’   r’   Ð   s   † ñð )ƒNr   r’   c                   ó4   • \ rS rSrSr\r\4S jrS r	S r
Srg)ÚReverseProxyResourceéÚ   a´  
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                 ó`   • [         R                  " U 5        Xl        X l        X0l        X@l        g)aý  
@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   r|   r}   Úpathr   )r   r|   r}   r˜   r   s        r   r   ÚReverseProxyResource.__init__ë   s&   € ô  	×Ò˜$ÔØŒ	ØŒ	ØŒ	Ø�r   c           
      ó¨   • [        U R                  U R                  U R                  S-   [	        USS9R                  S5      -   U R                  5      $ )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.
rk   r   )Úsafezutf-8)r•   r|   r}   r˜   Úurlquotert   r   )r   r˜   Úrequests      r   ÚgetChildÚReverseProxyResource.getChild  sI   € ô $Ø�I‰IØ�I‰IØ�I‰I˜Ñœx¨°3Ñ7×>Ñ>¸wÓGÑGØ�L‰Ló	
ð 	
r   c                 óŒ  • U R                   S:X  a  U R                  nOSU R                  U R                   4-  nUR                  R                  SUR	                  S5      /5        UR
                  R                  SS5        [        UR                  5      S   nU(       a  U R                  S-   U-   nOU R                  nU R                  UR                  UUR                  UR                  5       UR
                  R                  5       U5      nU R                  R!                  U R                  U R                   U5        ["        $ )z:
Render a request by forwarding it to the proxied server.
r^   z%s:%drl   rh   r   é   ó   ?)r}   r|   r�   r6   rt   ru   rv   r   rm   r˜   r�   rx   ry   rr   rw   r   rz   r   )r   r�   r|   Úqsr   r€   s         r   ÚrenderÚReverseProxyResource.render  sÿ   € ð �9‰9˜‹?Ø—9‘9‰Dà˜dŸi™i¨¯©Ð3Ñ3ˆDØ×Ñ×,Ñ,¨W°t·{±{À7Ó7KÐ6LÔMØ�‰×Ñ˜Q Ô"Ü�g—k‘kÓ" 1Ñ%ˆÞØ—9‘9˜tÑ# bÑ(‰Dà—9‘9ˆDØ×4Ñ4Ø�N‰NØØ×ÑØ×!Ñ!Ó#Ø�O‰O× Ñ Ó"Øó
ˆð 	�‰×Ñ §	¡	¨4¯9©9°mÔDÜÐr   )r|   r˜   r}   r   N)rE   rF   rG   rH   rI   rM   r�   r   r   rž   r¤   rJ   rK   r   r   r•   r•   Ú   s!   † ñð 1Ðà18ô ò,
õr   r•   N)rI   Úurllib.parser   rœ   r   r   Útwisted.internetr   Útwisted.internet.protocolr   Útwisted.web.httpr	   r
   r   r   Útwisted.web.resourcer   Útwisted.web.serverr   r   rM   r\   r„   rˆ   r’   r•   rK   r   r   Ú<module>r¬      s�   ðñ
÷" AÑ @å $Ý 3ß OÓ OÝ )Ý +ô4,�*ô 4,ôn˜ô ô@!;�7ô !;ôH"ˆKô "ô$!U˜'ô !UôH)�;ô )ôN˜8õ Nr   