HOME


Mini Shell 1.0
DIR: /usr/lib/python3/dist-packages/twisted/web/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/twisted/web/__pycache__/proxy.cpython-312.pyc
�

Ϫ�f�&���dZddlmZmZmZddlmZddlm	Z	ddl
mZmZm
Z
mZddlmZddlmZGd�d	e
�ZGd
�de	�ZGd�d
e�ZGd�de�ZGd�de�ZGd�de�ZGd�de�Zy)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�8�eZdZdZdZd�Zd�Zd�Zd�Zd�Z	d�Z
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)�father�command�rest�pop�headers�data��selfrr�versionrrrs       �3/usr/lib/python3/dist-packages/twisted/web/proxy.py�__init__zProxyClient.__init__)sN����������	��'�)��+�,�!)��
�����M�4�(������	�c�"�|j|j|j�|jj	�D]\}}|j||��|j
�|jj|j�y�N)
�sendCommandrrr�items�
sendHeader�
endHeaders�	transport�writer)r�header�values   r�connectionMadezProxyClient.connectionMade4sh��������t�y�y�1�!�\�\�/�/�1�	+�M�F�E��O�O�F�E�*�	+����������T�Y�Y�'rc�N�|jjt|�|�yr)r�setResponseCode�int)rr�code�messages    r�handleStatuszProxyClient.handleStatus;s�����#�#�C��I�w�7rc���|j�dvr(|jjj||g�y|jjj	||�y)N)sserversdatescontent-type)�lowerr�responseHeaders�
setRawHeaders�addRawHeader)r�keyr&s   r�handleHeaderzProxyClient.handleHeader>sI��
�9�9�;�?�?��K�K�'�'�5�5�c�E�7�C��K�K�'�'�4�4�S�%�@rc�:�|jj|�yr)rr$)r�buffers  r�handleResponsePartzProxyClient.handleResponsePartHs�������&�!rc��|js<d|_|jj�|jj	�yy)z�
        Finish the original request, indicating that the response has been
        completely written to it, and disconnect the outgoing transport.
        TN)�	_finishedr�finishr#�loseConnection)rs r�handleResponseEndzProxyClient.handleResponseEndKs7��
�~�~�!�D�N��K�K��� ��N�N�)�)�+�rN)�__name__�
__module__�__qualname__�__doc__r9rr'r-r4r7r<�rrrrs-����I�	�(�8�A�"�,rrc�&�eZdZdZeZd�Zd�Zd�Zy)�ProxyClientFactoryz?
    Used by ProxyRequest to implement a simple web proxy.
    c�X�||_||_||_||_||_||_yr)rrrrrrrs       rrzProxyClientFactory.__init__^s,����������	������	���rc��|j|j|j|j|j|j
|j�Sr)�protocolrrrrrr)r�addrs  r�
buildProtocolz ProxyClientFactory.buildProtocolfs8���}�}��L�L�$�)�)�T�\�\�4�<�<����D�K�K�
�	
rc���|jjdd�|jjjdd�|jj	d�|jj�y)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)r0r2r$r:)r�	connector�reasons   r�clientConnectionFailedz)ProxyClientFactory.clientConnectionFailedksV��
	
���#�#�C�)9�:����#�#�0�0��,�O������7�8������rN)	r=r>r?r@rrFrrHrLrArrrCrCVs���
�H��
�
rrCc�2�eZdZdZdeiZddiZeefd�Z	d�Z
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}
    shttp�Pc�@�tj|||�||_yr�rrr�r�channel�queuedrs    rrzProxyRequest.__init__��������w��/���rc�f�t|j�}|d}|djd�}|j|}d|vr|j	d�\}}t|�}t
d|ddz�}|s|dz}|j|}|j�j�}d|vr|jd�|d<|jjdd�|jj�}||j||j|||�}	|j j#|||	�y)	Nr��ascii�:)rr��/�host)r�uri�decode�ports�splitr*r�	protocols�
getAllHeaders�copy�encode�content�seek�read�method�clientprotor�
connectTCP)
r�parsedrF�host�portr�class_r�s�
clientFactorys
          r�processzProxyRequest.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�Q�PT�U�
�������d�M�:rN)r=r>r?r@rCrar_rrrrqrArrrNrNvs,����,�-�I�
�b�M�E�'7���;rrNc��eZdZdZeZy)�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@rN�requestFactoryrArrrsrs�s���"�Nrrsc�&�eZdZdZeZeefd�Zd�Z	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	d�g�|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)�requestHeadersr1�factoryrlrd�proxyClientFactoryClassrhr]rirbrergrrjrm)rrps  rrqzReverseProxyRequest.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�TrN)
r=r>r?r@rCr{rrrrqrArrrvrv�s��	�1��'7���Urrvc��eZdZdZeZy)�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�*�eZdZdZeZefd�Zd�Zd�Z	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}
    c�f�tj|�||_||_||_||_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rrlrm�pathr)rrlrmr�rs     rrzReverseProxyResource.__init__�s.�� 	���$����	���	���	���rc	��t|j|j|jdzt	|d��jd�z|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)rrlrmr��urlquoterdr)rr��requests   r�getChildzReverseProxyResource.getChildsI��$��I�I��I�I��I�I���x��3�7�>�>�w�G�G��L�L�	
�	
rc��|jdk(r
|j}nd|j|jfz}|jjd|j	d�g�|j
j
dd�t|j�d}|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]r�r{rhrirbrgrrjr
)rr�rl�qsrrps      r�renderzReverseProxyResource.renders����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��rN)
r=r>r?r@rCr{rrr�r�rArrrr�s!���1��18��,
�rrN)r@�urllib.parserr�rr�twisted.internetr�twisted.internet.protocolr�twisted.web.httprr	r
r�twisted.web.resourcer�twisted.web.serverr
rrCrNrsrvr}rrArr�<module>r�s���
�"A�@�$�3�O�O�)�+�4,�*�4,�n���@!;�7�!;�H"�K�"�$!U�'�!U�H)�;�)�N�8�Nr