HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/web/__pycache__/proxy.cpython-38.pyc
U


W[k&�@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�3/usr/lib/python3/dist-packages/twisted/web/proxy.py�__init__)szProxyClient.__init__cCsJ|�|j|j�|j��D]\}}|�||�q|��|j�|j	�dS�N)
ZsendCommandrrr�itemsZ
sendHeaderZ
endHeaders�	transport�writer)r�header�valuerrr�connectionMade5s
zProxyClient.connectionMadecCs|j�t|�|�dSr)r�setResponseCode�int)rr�code�messagerrr�handleStatus=szProxyClient.handleStatuscCs4|��dkr |jj�||g�n|jj�||�dS)N)sserversdatescontent-type)�lowerr�responseHeaders�
setRawHeaders�addRawHeader)r�keyr rrr�handleHeaderAszProxyClient.handleHeadercCs|j�|�dSr)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/rr!r&r,r.r1rrrrr src@s,eZdZdZeZdd�Zdd�Zdd�ZdS)	�ProxyClientFactoryz?
    Used by ProxyRequest to implement a simple web proxy.
    cCs(||_||_||_||_||_||_dSr)rrrrrrrrrrrdszProxyClientFactory.__init__cCs |�|j|j|j|j|j|j�Sr)�protocolrrrrrr)rZaddrrrr�
buildProtocolms
�z 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*rr0)rZ	connector�reasonrrr�clientConnectionFailedrsz)ProxyClientFactory.clientConnectionFailedN)	r2r3r4r5rr7rr8r:rrrrr6\s
	r6c@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�|||�||_dSr�rrr�rZchannelZqueuedrrrrr�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�:)�rB��/�host)r�urlparse�uri�decode�ports�splitr#Z
urlunparse�	protocols�
getAllHeaders�copy�encode�content�seek�read�method�clientprotor�
connectTCP)
rZparsedr7�host�portrZclass_r�s�
clientFactoryrrr�process�s*


�zProxyRequest.processN)r2r3r4r5r6rKrIr
rrrYrrrrr;~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)r2r3r4r5r;�requestFactoryrrrrrZ�srZc@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�|||�||_dSrr=r>rrrr�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.
        rEr@N)�requestHeadersr)�factoryrUrN�proxyClientFactoryClassrRrGrSrLrOrQrrTrV)rrXrrrrY�s���zReverseProxyRequest.processN)
r2r3r4r5r6r_r
rrrYrrrrr\�sr\c@seZdZdZeZdS)�ReverseProxyzo
    Implements a simple reverse proxy.

    For details of usage, see the file examples/reverse-proxy.py.
    N)r2r3r4r5r\r[rrrrr`�sr`c@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)aS
        @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{str}
        N)rrrUrV�pathr)rrUrVrbrrrrr�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.
        rDrB)Zsafezutf-8)rarUrVrbrrNr)rrb�requestrrr�getChilds�zReverseProxyResource.getChildcCs�|jdkr|j}n|jdt|j�}|j�d|�d�g�|j�dd�t�	|j
�d}|rn|jd|}n|j}|�|j
||j|��|j��|�}|j�|j|j|�tS)zJ
        Render a request by forwarding it to the proxied server.
        r<rArEr@r��?)rVrU�strr]r)rNrOrPrrFrGrbr_rRrSrLrQrrTr	)rrcrUZqsrrXrrr�renders&
�zReverseProxyResource.renderN)
r2r3r4r5r6r_rrrdrhrrrrra�s
raN)r5Z
__future__rrZtwisted.python.compatrrZtwisted.internetrZtwisted.internet.protocolrZtwisted.web.resourcerZtwisted.web.serverr	Ztwisted.web.httpr
rrr
rr6r;rZr\r`rarrrr�<module>s<"'#