
    h&                         S r SSKJrJrJr  SSKJr  SSKJ	r	  SSK
JrJrJrJr  SSKJr  SSKJr   " S S	\5      r " S
 S\	5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)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                   @    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
rg)ProxyClient   z
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                     X`l         Xl        X l        SU;   a  US	 SUS'   UR                  SS 5        X@l        XPl        g )Ns   proxy-connections   closes
   connections
   keep-alive)fathercommandrestpopheadersdataselfr   r   versionr   r   r   s          X/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/web/proxy.py__init__ProxyClient.__init__)   sD    	')+,!)M4(	    c                 "   U R                  U R                  U R                  5        U R                  R	                  5        H  u  pU R                  X5        M     U R                  5         U R                  R                  U R                  5        g 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                 N    U R                   R                  [        U5      U5        g r!   )r   setResponseCodeint)r   r   codemessages       r   handleStatusProxyClient.handleStatus;   s    ##CIw7r   c                     UR                  5       S;   a'  U R                  R                  R                  X/5        g U R                  R                  R	                  X5        g )N)s   servers   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                 :    U R                   R                  U5        g r!   )r   r'   )r   buffers     r   handleResponsePartProxyClient.handleResponsePartH   s    &!r   c                     U R                   (       d<  SU l         U R                  R                  5         U R                  R	                  5         gg)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   s7    
 ~~!DNKK NN))+ r   )r@   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r@   r   r*   r1   r9   r=   rC   __static_attributes__ r   r   r   r      s-     I	(8A",r   r   c                   .    \ rS rSrSr\rS rS rS r	Sr
g)ProxyClientFactoryV   z7
Used by ProxyRequest to implement a simple web proxy.
c                 L    X`l         Xl        X l        X@l        XPl        X0l        g r!   )r   r   r   r   r   r   r   s          r   r   ProxyClientFactory.__init__^   s     		r   c                     U R                  U R                  U R                  U R                  U R                  U R
                  U R                  5      $ r!   )protocolr   r   r   r   r   r   )r   addrs     r   buildProtocol ProxyClientFactory.buildProtocolf   s8    }}LL$))T\\4<<DKK
 	
r   c                     U R                   R                  SS5        U R                   R                  R                  SS5        U R                   R	                  S5        U R                   R                  5         g)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-   r5   r7   r'   rA   )r   	connectorreasons      r   clientConnectionFailed)ProxyClientFactory.clientConnectionFailedk   sV    
 	##C)9:##00,O78r   )r   r   r   r   r   r   N)rE   rF   rG   rH   rI   r   rR   r   rT   rY   rJ   rK   r   r   rM   rM   V   s    
 H

r   rM   c                   <    \ rS rSrSrS\0rSS0r\\	4S jr
S rSrg)	ProxyRequestv   z
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   httpP   c                 >    [         R                  " XU5        X0l        g r!   r   r   r   r   channelqueuedr   s       r   r   ProxyRequest.__init__       /r   c                 p   [        U R                  5      nUS   nUS   R                  S5      nU R                  U   nSU;   a  UR	                  S5      u  p4[        U5      n[        SUSS  -   5      nU(       d  US-   nU R                  U   nU R                  5       R                  5       nSU;  a  UR                  S5      US'   U R                  R                  SS5        U R                  R                  5       nU" U R                  XPR                  XxU 5      n	U R                   R#                  X4U	5        g )	Nr      ascii:)r   r         /   host)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*vabz12$;D)$$&++-'!#{{73GG!QLLt{{D2B2BGPTUM:r   r   N)rE   rF   rG   rH   rI   rM   rq   ro   r	   r   r   r   rJ   rK   r   r   r\   r\   v   s,     ,-IbME'7 ;r   r\   c                       \ rS rSrSr\rSrg)Proxy   aK  
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!
rK   N)rE   rF   rG   rH   rI   r\   requestFactoryrJ   rK   r   r   r   r      s     "Nr   r   c                   0    \ rS rSrSr\r\\4S jr	S r
Srg)ReverseProxyRequest   aP  
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                 >    [         R                  " XU5        X0l        g r!   r`   ra   s       r   r   ReverseProxyRequest.__init__   re   r   c                    U R                   R                  SU R                  R                  R	                  S5      /5        U R                  U R                  U R                  U R                  U R                  5       U R                  R                  5       U 5      nU R                  R                  U R                  R                  U R                  R                  U5        g)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.
rl   rh   N)requestHeadersr6   factoryr|   rt   proxyClientFactoryClassrx   rm   ry   rr   ru   rw   r   rz   r}   )r   r   s     r   r   ReverseProxyRequest.process   s     	))'DLL4E4E4L4LW4U3VW44KKHH LL
 	 1 14<<3D3DmTr   r   N)rE   rF   rG   rH   rI   rM   r   r	   r   r   r   rJ   rK   r   r   r   r      s    	 1'7 Ur   r   c                       \ rS rSrSr\rSrg)ReverseProxy   zc
Implements a simple reverse proxy.

For details of usage, see the file examples/reverse-proxy.py.
rK   N)rE   rF   rG   rH   rI   r   r   rJ   rK   r   r   r   r      s     )Nr   r   c                   4    \ rS rSrSr\r\4S jrS r	S r
Srg)ReverseProxyResource   a  
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                 `    [         R                  " U 5        Xl        X l        X0l        X@l        g)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   r|   r}   pathr   )r   r|   r}   r   r   s        r   r   ReverseProxyResource.__init__   s&      	$			r   c           
          [        U R                  U R                  U R                  S-   [	        USS9R                  S5      -   U R                  5      $ )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.
rk   r   )safezutf-8)r   r|   r}   r   urlquotert   r   )r   r   requests      r   getChildReverseProxyResource.getChild  sI     $IIIIIIx37>>wGGLL	
 	
r   c                    U R                   S:X  a  U R                  nOSU R                  U R                   4-  nUR                  R                  SUR	                  S5      /5        UR
                  R                  SS5        [        UR                  5      S   nU(       a  U R                  S-   U-   nOU R                  nU R                  UR                  UUR                  UR                  5       UR
                  R                  5       U5      nU R                  R!                  U R                  U R                   U5        ["        $ )z:
Render a request by forwarding it to the proxied server.
r^   z%s:%drl   rh   r         ?)r}   r|   r   r6   rt   ru   rv   r   rm   r   r   rx   ry   rr   rw   r   rz   r   )r   r   r|   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   )r|   r   r}   r   N)rE   rF   rG   rH   rI   rM   r   r   r   r   r   rJ   rK   r   r   r   r      s!     118 ,
r   r   N)rI   urllib.parser   r   r   r   twisted.internetr   twisted.internet.protocolr   twisted.web.httpr	   r
   r   r   twisted.web.resourcer   twisted.web.serverr   r   rM   r\   r   r   r   r   rK   r   r   <module>r      s   
" A @ $ 3 O O ) +4,* 4,n @!;7 !;H"K "$!U' !UH); )N8 Nr   