+
    Dfj&                         R t ^ RIHtHtHt ^ RIHt ^ RIH	t	 ^ RI
HtHtHtHt ^ RIHt ^ RIHt  ! R R]4      t ! R	 R
]	4      t ! R R]4      t ! R R]4      t ! R R]4      t ! R R]4      t ! R R]4      tR# )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                   L   a  ] tR t^t o RtRtR tR tR tR t	R t
R tR	tV tR
# )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                    W`n         Wn        W n        R V9   d   VR  RVR&   VP                  RR4       W@n        WPn        R# )s   proxy-connections   closes
   connections
   keep-aliveN)fathercommandrestpopheadersdataselfr   r   versionr   r   r   s   &&&&&&&3/usr/lib/python3/dist-packages/twisted/web/proxy.py__init__ProxyClient.__init__)   sD    	')+,!)M4(	    c                $   V P                  V P                  V P                  4       V P                  P	                  4        F  w  rV P                  W4       K  	  V P                  4        V P                  P                  V P                  4       R # N)
sendCommandr   r   r   items
sendHeader
endHeaders	transportwriter   )r   headervalues   &  r   connectionMadeProxyClient.connectionMade4   s_    tyy1!\\//1MFOOF* 2TYY'r   c                P    V P                   P                  \        V4      V4       R # r   )r   setResponseCodeint)r   r   codemessages   &&&&r   handleStatusProxyClient.handleStatus;   s    ##CIw7r   c                    VP                  4       R9   d)   V P                  P                  P                  W.4       R# V P                  P                  P	                  W4       R# )   serverN)r2   s   dates   content-type)lowerr   responseHeaderssetRawHeadersaddRawHeader)r   keyr'   s   &&&r   handleHeaderProxyClient.handleHeader>   sE    
 99;??KK''55c7CKK''44S@r   c                <    V P                   P                  V4       R # r   )r   r%   )r   buffers   &&r   handleResponsePartProxyClient.handleResponsePartH   s    &!r   c                    V P                   '       g>   RV n         V P                  P                  4        V P                  P	                  4        R# R# )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   s9    
 ~~~!DNKK NN))+ r   )r?   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r?   r   r(   r/   r8   r<   rB   __static_attributes____classdictcell____classdict__s   @r   r   r      s5      I	(8A", ,r   r   c                   :   a  ] tR t^Vt o Rt]tR tR tR t	Rt
V tR# )ProxyClientFactoryz7
Used by ProxyRequest to implement a simple web proxy.
c                N    W`n         Wn        W n        W@n        WPn        W0n        R # r   )r   r   r   r   r   r   r   s   &&&&&&&r   r   ProxyClientFactory.__init__^   s     		r   c                    V P                  V P                  V P                  V P                  V P                  V P
                  V P                  4      # r   )protocolr   r   r   r   r   r   )r   addrs   &&r   buildProtocol ProxyClientFactory.buildProtocolf   s8    }}LL$))T\\4<<DKK
 	
r   c                    V P                   P                  RR4       V P                   P                  P                  RR4       V P                   P	                  R4       V P                   P                  4        R# )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+   r4   r6   r%   r@   )r   	connectorreasons   &&&r   clientConnectionFailed)ProxyClientFactory.clientConnectionFailedk   sV    
 	##C)9:##00,O78r   )r   r   r   r   r   r   N)rD   rE   rF   rG   rH   r   rR   r   rT   rY   rI   rJ   rK   s   @r   rN   rN   V   s%     
 H

 r   rN   c                   H   a  ] tR t^vt o RtR]/tR^P/t]]	3R lt
R tRtV tR# )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   httpc                @    \         P                  ! WV4       W0n        R # r   r   r   r   r   channelqueuedr   s   &&&&r   r   ProxyRequest.__init__       /r   c                   \        V P                  4      pV^ ,          pV^,          P                  R4      pV P                  V,          pRV9   d   VP	                  R4      w  r4\        V4      p\        RVR,          ,           4      pV'       g
   VR,           pV P                  V,          pV P                  4       P                  4       pRV9  d   VP                  R4      VR&   V P                  P                  ^ ^ 4       V P                  P                  4       pV! V P                  WPP                  WxV 4      p	V P                   P#                  W4V	4       R# )    ascii::   NN   /   hostN)r   r   )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ay(zz(#$;CJDt9D*vbz12$;D)$$&++-'!#{{73GG!QLLt{{D2B2BGPTUM:r   r   N)rD   rE   rF   rG   rH   rN   ro   rm   r   r   r   r   rI   rJ   rK   s   @r   r\   r\   v   s4      ,-IbME'7 ; ;r   r\   c                       ] tR t^tRt]tRtR# )ProxyaK  
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)rD   rE   rF   rG   rH   r\   requestFactoryrI   r   r   r   r   r      s     "Nr   r   c                   <   a  ] tR t^t o Rt]t]]3R lt	R t
RtV tR# )ReverseProxyRequestaP  
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                @    \         P                  ! WV4       W0n        R # r   r^   r_   s   &&&&r   r   ReverseProxyRequest.__init__   rc   r   c                   V P                   P                  RV P                  P                  P	                  R4      .4       V P                  V P                  V P                  V P                  V P                  4       V P                  P                  4       V 4      pV P                  P                  V P                  P                  V P                  P                  V4       R# )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.
rj   rf   N)requestHeadersr5   factoryrz   rr   proxyClientFactoryClassrv   rk   rw   rp   rs   ru   r   rx   r{   )r   r~   s   & r   r   ReverseProxyRequest.process   s     	))'DLL4E4E4L4LW4U3VW44KKHH LL
 	 1 14<<3D3DmTr   r   N)rD   rE   rF   rG   rH   rN   r   r   r   r   r   rI   rJ   rK   s   @r   r   r      s(     	 1'7 U Ur   r   c                       ] tR t^tRt]tRtR# )ReverseProxyzc
Implements a simple reverse proxy.

For details of usage, see the file examples/reverse-proxy.py.
r   N)rD   rE   rF   rG   rH   r   r   rI   r   r   r   r   r      s     )Nr   r   c                   @   a  ] tR t^t o Rt]t]3R ltR t	R t
RtV tR# )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                b    \         P                  ! V 4       Wn        W n        W0n        W@n        R# )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   rz   r{   pathr   )r   rz   r{   r   r   s   &&&&&r   r   ReverseProxyResource.__init__   s&      	$			r   c           
         \        V P                  V P                  V P                  R,           \	        VRR7      P                  R4      ,           V P                  4      # )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.
ri   r   )safezutf-8)r   rz   r{   r   urlquoterr   r   )r   r   requests   &&&r   getChildReverseProxyResource.getChild  sI     $IIIIIIx37>>wGGLL	
 	
r   c                   V P                   ^P8X  d   V P                  pMRV P                  V P                   3,          pVP                  P                  RVP	                  R4      .4       VP
                  P                  ^ ^ 4       \        VP                  4      ^,          pV'       d   V P                  R,           V,           pMV P                  pV P                  VP                  VVP                  VP                  4       VP
                  P                  4       V4      pV P                  P!                  V P                  V P                   V4       \"        # )z:
Render a request by forwarding it to the proxied server.
z%s:%drj   rf      ?)r{   rz   r   r5   rr   rs   rt   r   rk   r   r   rv   rw   rp   ru   r   rx   r   )r   r   rz   qsr   r~   s   &&    r   renderReverseProxyResource.render  s     99?99Ddii33D,,Wt{{77K6LMQ"gkk"1%99t#b(D99D44NN!!#OO  "
 			499mDr   )rz   r   r{   r   N)rD   rE   rF   rG   rH   rN   r   r   r   r   r   rI   rJ   rK   s   @r   r   r      s)      118 ,
 r   r   N)rH   urllib.parser   r   r   r   twisted.internetr   twisted.internet.protocolr   twisted.web.httpr   r	   r
   r   twisted.web.resourcer   twisted.web.serverr   r   rN   r\   r   r   r   r   r   r   r   <module>r      s   
" A @ $ 3 O O ) +4,* 4,n @!;7 !;H"K "$!U' !UH); )N8 Nr   