Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/proxy.cpython-37.pyc

B

;��]m&�@s�dZddlmZmZddlmZmZddlmZddl	m
Z
ddlmZddl
mZddlmZmZmZmZGd	d
�d
e�ZGdd�de
�ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdS)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.
�)�absolute_import�division)�urllib_parse�urlquote)�reactor)�
ClientFactory)�Resource)�NOT_DONE_YET)�
HTTPClient�Request�HTTPChannel�_QUEUED_SENTINELc@sDeZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�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.
    FcCsD||_||_||_d|kr |d=d|d<|�dd�||_||_dS)Nsproxy-connectionscloses
connections
keep-alive)�father�command�rest�pop�headers�data)�selfrr�versionrrr�r�D/opt/alt/python37/lib64/python3.7/site-packages/twisted/web/proxy.py�__init__)szProxyClient.__init__cCsN|�|j|j�x"|j��D]\}}|�||�qW|��|j�|j	�dS)N)
ZsendCommandrrr�itemsZ
sendHeaderZ
endHeaders�	transport�writer)r�header�valuerrr�connectionMade5s
zProxyClient.connectionMadecCs|j�t|�|�dS)N)r�setResponseCode�int)rr�code�messagerrr�handleStatus=szProxyClient.handleStatuscCs4|��dkr |jj�||g�n|jj�||�dS)N)sserversdatescontent-type)�lowerr�responseHeaders�
setRawHeaders�addRawHeader)r�keyrrrr�handleHeaderAszProxyClient.handleHeadercCs|j�|�dS)N)rr)r�bufferrrr�handleResponsePartLszProxyClient.handleResponsePartcCs$|js d|_|j��|j��dS)z�
        Finish the original request, indicating that the response has been
        completely written to it, and disconnect the outgoing transport.
        TN)�	_finishedr�finishrZloseConnection)rrrr�handleResponseEndPs
zProxyClient.handleResponseEndN)�__name__�
__module__�__qualname__�__doc__r-rrr$r*r,r/rrrrr src@s,eZdZdZeZdd�Zdd�Zdd�ZdS)	�ProxyClientFactoryz?
    Used by ProxyRequest to implement a simple web proxy.
    cCs(||_||_||_||_||_||_dS)N)rrrrrr)rrrrrrrrrrrdszProxyClientFactory.__init__cCs |�|j|j|j|j|j|j�S)N)�protocolrrrrrr)r�addrrrr�
buildProtocolmsz ProxyClientFactory.buildProtocolcCs8|j�dd�|jj�dd�|j�d�|j��dS)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)rr r&r(rr.)rZ	connector�reasonrrr�clientConnectionFailedrsz)ProxyClientFactory.clientConnectionFailedN)	r0r1r2r3rr5rr7r9rrrrr4\s
	r4c@s6eZdZdZdeiZddiZeefdd�Z	dd�Z
dS)	�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}
    shttp�PcCst�|||�||_dS)N)rrr)r�channel�queuedrrrrr�szProxyRequest.__init__c
Cs�t�|j�}|d}|d�d�}|j|}d|krJ|�d�\}}t|�}t�d|dd��}|sl|d}|j|}|�	��
�}d|kr�|�d�|d<|j�
dd�|j��}||j||j|||�}	|j�|||	�dS)	Nr��ascii�:)�rA��/shost)r�urlparse�uri�decode�ports�splitr!�
urlunparse�	protocols�
getAllHeaders�copy�encode�content�seek�read�method�clientprotor�
connectTCP)
r�parsedr5�host�portr�class_r�s�
clientFactoryrrr�process�s&


zProxyRequest.processN)r0r1r2r3r4rJrGr
rrrZrrrrr:~s
r:c@seZdZdZeZdS)�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)r0r1r2r3r:�requestFactoryrrrrr[�s
r[c@s*eZdZdZeZeefdd�Zdd�Z	dS)�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}
    cCst�|||�||_dS)N)rrr)rr<r=rrrrr�szReverseProxyRequest.__init__cCsZ|j�d|jj�d�g�|�|j|j|j|�	�|j
��|�}|j�
|jj|jj|�dS)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.
        shostr?N)�requestHeadersr'�factoryrUrM�proxyClientFactoryClassrQrErRrKrNrPrrSrV)rrYrrrrZ�szReverseProxyRequest.processN)
r0r1r2r3r4r`r
rrrZrrrrr]�s
r]c@seZdZdZeZdS)�ReverseProxyzo
    Implements a simple reverse proxy.

    For details of usage, see the file examples/reverse-proxy.py.
    N)r0r1r2r3r]r\rrrrra�srac@s0eZdZdZeZefdd�Zdd�Zdd�Z	dS)	�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}
    cCs&t�|�||_||_||_||_dS)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)rrrUrV�pathr)rrUrVrcrrrrr�s

zReverseProxyResource.__init__cCs,t|j|j|jdt|dd��d�|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.
        rCrA)�safezutf-8)rbrUrVrcrrMr)rrc�requestrrr�getChilds"zReverseProxyResource.getChildcCs�|jdkr|j}nd|j|jf}|j�d|�d�g�|j�dd�t�|j	�d}|rj|j
d|}n|j
}|�|j||j
|��|j��|�}|j�|j|j|�tS)zJ
        Render a request by forwarding it to the proxied server.
        r;z%s:%dshostr?r��?)rVrUr^r'rMrNrOrrDrErcr`rQrRrKrPrrSr	)rrerU�qsrrYrrr�renders

zReverseProxyResource.renderN)
r0r1r2r3r4r`rrrfrjrrrrrb�s

rbN)r3�
__future__rrZtwisted.python.compatrrZtwisted.internetrZtwisted.internet.protocolrZtwisted.web.resourcerZtwisted.web.serverr	Ztwisted.web.httpr
rrr
rr4r:r[r]rarbrrrr�<module>s<"'#