Ϫf&dZddlmZmZmZddlmZddlm Z ddl m Z m Z m Z mZddlmZddlmZGdd e ZGd d e ZGd d eZGdde ZGddeZGdde ZGddeZy)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. )quoteurlparse urlunparse)reactor) ClientFactory)_QUEUED_SENTINEL HTTPChannel HTTPClientRequest)Resource) NOT_DONE_YETc8eZdZdZdZdZdZdZdZdZ dZ y ) 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|vr|d=d|d<|jdd||_||_y)Nsproxy-connectionscloses connections keep-alive)fathercommandrestpopheadersdataselfrrversionrrrs 3/usr/lib/python3/dist-packages/twisted/web/proxy.py__init__zProxyClient.__init__)sN   ' )+,!)  M4(  c"|j|j|j|jj D]\}}|j |||j |jj|jyN) sendCommandrrritems sendHeader endHeaders transportwriter)rheadervalues rconnectionMadezProxyClient.connectionMade4sh tyy1!\\//1 +MFE OOFE * +  TYY'rcN|jjt||yr)rsetResponseCodeint)rrcodemessages r handleStatuszProxyClient.handleStatus;s ##CIw7rc|jdvr(|jjj||gy|jjj ||y)N)sserversdates content-type)lowerrresponseHeaders setRawHeaders addRawHeader)rkeyr&s r handleHeaderzProxyClient.handleHeader>sI 99;? ? KK ' ' 5 5cE7 C KK ' ' 4 4S% @rc:|jj|yr)rr$)rbuffers rhandleResponsePartzProxyClient.handleResponsePartHs &!rc|jsCould not connectN)rr)r0r2r$r:)r connectorreasons rclientConnectionFailedz)ProxyClientFactory.clientConnectionFailedksV ##C)9: ##00,O 78 rN) r=r>r?r@rrFrrHrLrArrrCrCVs H rrCc2eZdZdZdeiZddiZeefdZ dZ y) 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} shttpPc@tj|||||_yrr rrrchannelqueuedrs rrzProxyRequest.__init__w/ rcft|j}|d}|djd}|j|}d|vr|j d\}}t |}t d|ddz}|s|dz}|j|}|jj}d|vr|jd|d<|jjdd|jj}||j||j|||} |j j#||| y) Nrascii:)rr/host)ruridecodeportssplitr*r protocols getAllHeaderscopyencodecontentseekreadmethod clientprotor connectTCP) rparsedrFhostportrclass_rs clientFactorys rprocesszProxyRequest.processs$((#!9ay(zz(# $;CJD$t9D*vabz12$;D)$$&++- ' !#{{73GG  !Q LL   t{{D$2B2BGQPTU  dM:rN) r=r>r?r@rCrar_rrrrqrArrrNrNvs,,-I bME'7;rrNceZdZdZeZy)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@rNrequestFactoryrArrrsrss "Nrrsc&eZdZdZeZeefdZdZ y)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@tj|||||_yrrQrRs rrzReverseProxyRequest.__init__rUrc|jjd|jjj dg|j |j |j|j|j|jj|}|jj|jj|jj|y)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\rXN)requestHeadersr1factoryrlrdproxyClientFactoryClassrhr]rirbrergrrjrm)rrps rrqzReverseProxyRequest.processs ))'DLL4E4E4L4LW4U3VW44 KK HH       LL        1 14<<3D3DmTrN) r=r>r?r@rCr{rrrrqrArrrvrvs 1'7UrrvceZdZdZeZy) ReverseProxyzo Implements a simple reverse proxy. For details of usage, see the file examples/reverse-proxy.py. N)r=r>r?r@rvrtrArrr}r}s )Nrr}c*eZdZdZeZefdZdZdZ y)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} cftj|||_||_||_||_y)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 rrlrmpathr)rrlrmrrs rrzReverseProxyResource.__init__s. $    rc t|j|j|jdzt |dj dz|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)safezutf-8)rrlrmrurlquoterdr)rrrequests rgetChildzReverseProxyResource.getChildsI $ II II II x37>>wG G LL   rc|jdk(r |j}nd|j|jfz}|jjd|j dg|j j ddt|jd}|r|jdz|z}n |j}|j|j||j|j|j j|}|jj!|j|j|t"S)zJ Render a request by forwarding it to the proxied server. rOz%s:%dr\rXr?)rmrlryr1rdrerfrr]rr{rhrirbrgrrjr )rrrlqsrrps rrenderzReverseProxyResource.renders 99?99Ddii33D,,Wt{{77K6LMQ" gkk "1 % 99t#b(D99D44 NN      ! ! # OO "     499mDrN) r=r>r?r@rCr{rrrrrArrrrs! 118,  rrN)r@ urllib.parserrrrtwisted.internetrtwisted.internet.protocolrtwisted.web.httprr r r twisted.web.resourcer twisted.web.serverr rrCrNrsrvr}rrArrrs "A@$3OO)+4,*4,n@!;7!;H"K"$!U'!UH);)N8Nr