
    Yf<                       d Z ddlmZ ddgZddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& d dD             Z'dZ(d e'D             Z)e(*                    d          Z+ ee           G d d                      Z, G d d          Z- G d d          Z. G d dej/                  Z0dS )z2
Helpers related to HTTP requests, used by tests.
    )annotationsDummyChannelDummyRequest)BytesIO)DictListOptional)implementerverify)Version)IPv4AddressIPv6Address)Deferred)IAddressISSLTransport)Clock)
deprecated)unittest)FOUND)Headers)Resource)NOT_DONE_YETSessionSitec                    g | ]}d | d	S )Foobar ).0lws     P/var/www/html/env/lib/python3.11/site-packages/twisted/web/test/requesthelper.py
<listcomp>r"      s     !O!O!OB----!O!O!O    )
z
zFoo barc                8    g | ]}|                     d           S )ascii)encode)r   	components     r!   r"   r"   !   s3     # # #"+IW# # #r#   r'   c                      e Zd ZdZdS )NullAddressz/
    A null implementation of L{IAddress}.
    N)__name__
__module____qualname____doc__r   r#   r!   r+   r+   '   s           r#   r+   c                      e Zd Z G d d          Z ee           G d de                      Z e e                      Z	ddZ
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )r   c                  H    e Zd ZdZdZddZd Zd Zd Zd Z	d	 Z
d
 Zd ZdS )DummyChannel.TCPP   FNc                n    |t          ddd          }|| _        t                      | _        g | _        d S )NTCPz192.168.1.1i80  )r   _peerr   written	producersselfpeers     r!   __init__zDummyChannel.TCP.__init__3   s5    |"5-??DJ"99DLDNNNr#   c                    | j         S N)r6   r:   s    r!   getPeerzDummyChannel.TCP.getPeer:   s
    :r#   c                    t          |t                    st          d|          | j                            |           d S )Nz)Can only write bytes to a transport, not )
isinstancebytes	TypeErrorr7   writer:   datas     r!   rE   zDummyChannel.TCP.write=   sH    dE** V TD T TUUULt$$$$$r#   c                :    |D ]}|                      |           d S r>   )rE   )r:   iovecrG   s      r!   writeSequencezDummyChannel.TCP.writeSequenceB   s0     ! !

4    ! !r#   c                .    t          dd| j                  S )Nr5   z10.0.0.1)r   portr?   s    r!   getHostzDummyChannel.TCP.getHostF   s    uj$)<<<r#   c                >    | j                             ||f           d S r>   )r8   appendr:   producer	streamings      r!   registerProducerz!DummyChannel.TCP.registerProducerI   s#    N!!8Y"788888r#   c                    d S r>   r   r?   s    r!   unregisterProducerz#DummyChannel.TCP.unregisterProducerL   s    Dr#   c                    d| _         d S )NT)disconnectedr?   s    r!   loseConnectionzDummyChannel.TCP.loseConnectionO   s     $Dr#   r>   )r,   r-   r.   rL   rW   r<   r@   rE   rJ   rM   rS   rU   rX   r   r#   r!   r5   r2   /   s        	  	  	  	 	 	 		% 	% 	%
	! 	! 	!	= 	= 	=	9 	9 	9	 	 		% 	% 	% 	% 	%r#   r5   c                  8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	DummyChannel.SSLc                    d S r>   r   r?   s    r!   abortConnectionz DummyChannel.SSL.abortConnectionT       Dr#   c                    d S r>   r   r?   s    r!   getTcpKeepAlivez DummyChannel.SSL.getTcpKeepAliveX   r]   r#   c                    d S r>   r   r?   s    r!   getTcpNoDelayzDummyChannel.SSL.getTcpNoDelay\   r]   r#   c                    d S r>   r   r?   s    r!   loseWriteConnectionz$DummyChannel.SSL.loseWriteConnection`   r]   r#   c                    d S r>   r   r:   enableds     r!   setTcpKeepAlivez DummyChannel.SSL.setTcpKeepAlived   r]   r#   c                    d S r>   r   re   s     r!   setTcpNoDelayzDummyChannel.SSL.setTcpNoDelayh   r]   r#   c                    d S r>   r   r?   s    r!   getPeerCertificatez#DummyChannel.SSL.getPeerCertificatel   r]   r#   N)
r,   r-   r.   r\   r_   ra   rc   rg   ri   rk   r   r#   r!   SSLrZ   R   s}        	 	 		 	 		 	 		 	 		 	 		 	 		 	 	 	 	r#   rl   Nc                :    |                      |          | _        d S r>   )r5   	transportr9   s     r!   r<   zDummyChannel.__init__r   s    $r#   c                    d S r>   r   )r:   requests     r!   requestDonezDummyChannel.requestDoneu       r#   c                0   t          |t                    rd |                                D             }|dz   |z   dz   |z   dz   }|g}|                    d |D                        |                    d           | j                            |           d S )Nc                &    g | ]\  }}|D ]}||fS r   r   )r   kvaluesvs       r!   r"   z-DummyChannel.writeHeaders.<locals>.<listcomp>z   sA       &Avf IJA   r#          
c              3  2   K   | ]\  }}|d z   |z   dz   V  dS )s   : ry   Nr   )r   namevalues      r!   	<genexpr>z,DummyChannel.writeHeaders.<locals>.<genexpr>   s4      XXudUlU2W<XXXXXXr#   )rB   r   getAllRawHeadersextendrO   rn   rJ   )r:   versioncodereasonheadersresponse_lineheaderSequences          r!   writeHeaderszDummyChannel.writeHeadersx   s    gw'' 	 *1*B*B*D*D  G  $-4v=G'XXPWXXXXXXg&&&$$^44444r#   c                4    | j                                         S r>   )rn   r@   r?   s    r!   r@   zDummyChannel.getPeer       ~%%'''r#   c                4    | j                                         S r>   )rn   rM   r?   s    r!   rM   zDummyChannel.getHost   r   r#   c                <    | j                             ||           d S r>   )rn   rS   rP   s      r!   rS   zDummyChannel.registerProducer   s     '')<<<<<r#   c                8    | j                                          d S r>   )rn   rU   r?   s    r!   rU   zDummyChannel.unregisterProducer   s    ))+++++r#   c                :    | j                             |           d S r>   )rn   rE   rF   s     r!   rE   zDummyChannel.write   s    T"""""r#   c                :    | j                             |           d S r>   )rn   rJ   )r:   rI   s     r!   rJ   zDummyChannel.writeSequence   s    $$U+++++r#   c                8    | j                                          d S r>   )rn   rX   r?   s    r!   rX   zDummyChannel.loseConnection   s    %%'''''r#   c                    d S r>   r   r?   s    r!   
endRequestzDummyChannel.endRequest   rr   r#   c                6    t          | j        | j                  S r>   )rB   rn   rl   r?   s    r!   isSecurezDummyChannel.isSecure   s    $.$(333r#   c                    d S r>   r   r?   s    r!   r\   zDummyChannel.abortConnection       r#   c                    d S r>   r   r?   s    r!   r_   zDummyChannel.getTcpKeepAlive   r   r#   c                    d S r>   r   r?   s    r!   ra   zDummyChannel.getTcpNoDelay   r   r#   c                    d S r>   r   r?   s    r!   rc   z DummyChannel.loseWriteConnection   r   r#   c                    d S r>   r   r?   s    r!   rg   zDummyChannel.setTcpKeepAlive   r   r#   c                    d S r>   r   r?   s    r!   ri   zDummyChannel.setTcpNoDelay   r   r#   c                    d S r>   r   r?   s    r!   rk   zDummyChannel.getPeerCertificate   r   r#   r>   )r,   r-   r.   r5   r
   r   rl   r   r   siter<   rq   r   r@   rM   rS   rU   rE   rJ   rX   r   r   r\   r_   ra   rc   rg   ri   rk   r   r#   r!   r   r   .   s       !% !% !% !% !% !% !% !%F [    c    : 4

D( ( ( (  	5 	5 	5( ( (( ( (= = =, , ,# # #, , ,( ( (  4 4 4                r#   c                  .   e Zd ZU dZdZdZdZded<   ded<   ded	<   ded
<   ded<   ded<   d Zd Z		 	 d2d3dZ
d Zd Zd Zd4dZd Zd Zd5dZd  Zd! Zd" Zd4d#Zd$ Zd% Z e ed&d'd(d)          d*+          d,             Zd- Zd. Zd/ Zd6d0Zd1 ZdS )7r   a  
    Represents a dummy or fake request. See L{twisted.web.server.Request}.

    @ivar _finishedDeferreds: L{None} or a C{list} of L{Deferreds} which will
        be called back with L{None} when C{finish} is called or which will be
        errbacked if C{processingFailed} is called.

    @type requestheaders: C{Headers}
    @ivar requestheaders: A Headers instance that stores values for all request
        headers.

    @type responseHeaders: C{Headers}
    @ivar responseHeaders: A Headers instance that stores values for all
        response headers.

    @type responseCode: C{int}
    @ivar responseCode: The response code which was passed to
        C{setResponseCode}.

    @type written: C{list} of C{bytes}
    @ivar written: The bytes which have been written to the request.
    s   http://dummy/s   GETNOptional[IAddress]clientzList[bytes]sitepathr7   prepathzDict[bytes, List[bytes]]argszList[Deferred[None]]_finishedDeferredsc                \    d| _         | j         r|                                 | j         dS dS )z
        Call an L{IPullProducer}'s C{resumeProducing} method in a
        loop until it unregisters itself.

        @param prod: The producer.
        @type prod: L{IPullProducer}

        @param s: Whether or not the producer is streaming.
           N)goresumeProducing)r:   prodss      r!   rS   zDummyRequest.registerProducer   sG     g 	#  """ g 	# 	# 	# 	# 	#r#   c                    d| _         d S )Nr   )r   r?   s    r!   rU   zDummyRequest.unregisterProducer   s    r#   postpathlist[bytes]sessionOptional[Session]returnNonec                6   g | _         g | _        d| _        || _        g | _        d | _        |pt          d dt                                | _        i | _	        t                      | _        t                      | _        d | _        g | _        d| _        d| _        d S )Nr      0)r   uidreactors   dummys   HTTP/1.0)r   r7   finishedr   r   r   r   r   protoSessionr   r   requestHeadersresponseHeadersresponseCoder   _serverNameclientproto)r:   r   r   r   s       r!   r<   zDummyRequest.__init__   s      #TwDdEGG'T'T'T	%ii&yy "$#&r#   c                    i }| j                                         D ]"\  }}|d         ||                                <   #|S )ah  
        Return dictionary mapping the names of all received headers to the last
        value received for each.

        Since this method does not return all header information,
        C{self.requestHeaders.getAllRawHeaders()} may be preferred.

        NOTE: This function is a direct copy of
        C{twisted.web.http.Request.getAllRawHeaders}.
        )r   r~   lower)r:   r   ru   rw   s       r!   getAllHeaderszDummyRequest.getAllHeaders  sI     '88:: 	' 	'DAq!"2GAGGIIr#   c                j    | j                             |                                dg          d         S )aH  
        Retrieve the value of a request header.

        @type name: C{bytes}
        @param name: The name of the request header for which to retrieve the
            value.  Header names are compared case-insensitively.

        @rtype: C{bytes} or L{None}
        @return: The value of the specified request header.
        Nr   )r   getRawHeadersr   )r:   r{   s     r!   	getHeaderzDummyRequest.getHeader  s,     "00vFFqIIr#   c                <    | j                             ||           dS )zATODO: make this assert on write() if the header is content-lengthN)r   addRawHeaderr:   r{   r|   s      r!   	setHeaderzDummyRequest.setHeader   s!    ))$66666r#   c                f    | j         r| j         S | j        r
J d            | j        | _         | j         S )Nz8Session cannot be requested after data has been written.)r   r7   r   )r:   sessionInterfaces     r!   
getSessionzDummyRequest.getSession$  sI    < 	 <	F 	FE	F 	F(|r#   c                    |                     |           }|t          u rdS |                     |           |                                  dS )a  
        Render the given resource as a response to this request.

        This implementation only handles a few of the most common behaviors of
        resources.  It can handle a render method that returns a string or
        C{NOT_DONE_YET}.  It doesn't know anything about the semantics of
        request methods (eg HEAD) nor how to set any particular headers.
        Basically, it's largely broken, but sufficient for some tests at least.
        It should B{not} be expanded to do all the same stuff L{Request} does.
        Instead, L{DummyRequest} should be phased out and L{Request} (or some
        other real code factored in a different way) used.
        N)renderr   rE   finish)r:   resourceresults      r!   r   zDummyRequest.render-  sH     &&\!!F

6r#   c                    t          |t                    st          d          | j                            |           d S )Nzwrite() only accepts bytes)rB   rC   rD   r7   rO   rF   s     r!   rE   zDummyRequest.write@  s?    $&& 	:8999D!!!!!r#   Deferred[None]c                V    t                      }| j                            |           |S )z
        Return a L{Deferred} which is called back with L{None} when the request
        is finished.  This will probably only work if you haven't called
        C{finish} yet.
        )r   r   rO   )r:   r   s     r!   notifyFinishzDummyRequest.notifyFinishE  s)     $,::&&x000r#   c                    | j         dz   | _         | j        (| j        }d| _        |D ]}|                    d           dS dS )z}
        Record that the request is finished and callback and L{Deferred}s
        waiting for notification of this.
        r   N)r   r   callback)r:   	observersobss      r!   r   zDummyRequest.finishO  sa    
 )"./I&*D#  # #T""""	 /.# #r#   c                h    | j         (| j         }d| _         |D ]}|                    |           dS dS )zK
        Errback and L{Deferreds} waiting for finish notification.
        N)r   errback)r:   r   r   r   s       r!   processingFailedzDummyRequest.processingFailed[  sS     "./I&*D#  $ $F####	 /.$ $r#   c                    |g| j         |<   d S r>   )r   r   s      r!   addArgzDummyRequest.addArge  s     '	$r#   c                    | j         r5J d                    d                    | j                                         || _        || _        dS )z{
        Set the HTTP status response code, but takes care that this is called
        before any data is written.
        z;Response code cannot be set after data hasbeen written: {}.@@@@N)r7   formatjoinr   responseMessage)r:   r   messages      r!   setResponseCodezDummyRequest.setResponseCodeh  s\     	
 	
KRRKK%%
 
	
 	
 !&r#   c                ~    | j         r5J d                    d                    | j                                         d S )Nz<Last-Modified cannot be set after data has been written: {}.r   r7   r   r   )r:   whens     r!   setLastModifiedzDummyRequest.setLastModifiedu  sL    	
 	
LSSKK%%
 
	
 	
	
 	
r#   c                ~    | j         r5J d                    d                    | j                                         d S )Nz3ETag cannot be set after data has been written: {}.r   r   )r:   tags     r!   setETagzDummyRequest.setETag|  sL    	
 	
CJJKK%%
 
	
 	
	
 	
r#   Twisted      r   getClientAddress)replacementc                `    t          | j        t          t          f          r| j        j        S dS )z|
        Return the IPv4 address of the client which made this request, if there
        is one, otherwise L{None}.
        N)rB   r   r   r   hostr?   s    r!   getClientIPzDummyRequest.getClientIP  s-     dkK#=>> 	$;##tr#   c                :    | j         t                      S | j         S )z
        Return the L{IAddress} of the client that made this request.

        @return: an address.
        @rtype: an L{IAddress} provider.
        )r   r+   r?   s    r!   r   zDummyRequest.getClientAddress  s     ;== {r#   c                    | j         S )z
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        )r   r?   s    r!   getRequestHostnamezDummyRequest.getRequestHostname  s     r#   c                $    t          ddd          S )zz
        Get a dummy transport's host.

        @rtype: C{IPv4Address}
        @returns: a dummy transport's host
        r5   	127.0.0.1r3   )r   r?   s    r!   rM   zDummyRequest.getHost  s     5+r222r#   c                    || _         |                                 rd}nd}||k    r|}nd||fz  }| j                            d|           dS )a_  
        Change the host and port the request thinks it's using.

        @type host: C{bytes}
        @param host: The value to which to change the host header.

        @type ssl: C{bool}
        @param ssl: A flag which, if C{True}, indicates that the request is
            considered secure (if C{True}, L{isSecure} will return C{True}).
        i  r3   s   %b:%ds   hostN)	_forceSSLr   r   r   )r:   r   rL   ssldefault
hostHeaders         r!   setHostzDummyRequest.setHost  sd     ==?? 	GGG7??JJ!T4L0J((*=====r#   c                f    |                      t                     |                     d|           dS )zu
        Utility function that does a redirect.

        The request should have finish() called after this.
        s   locationN)r   r   r   )r:   urls     r!   redirectzDummyRequest.redirect  s2     	U###{C(((((r#   )NN)r   r   r   r   r   r   r   r   r>   )r   r   )r   ) r,   r-   r.   r/   urimethodr   __annotations__rS   rU   r<   r   r   r   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   rM   r   r   r   r#   r!   r   r      s         . CF!%F%%%%"""",,,,# # #   &*%)	' ' ' ' '*   J J J7 7 7     &" " "
   
# 
# 
#$ $ $" " "' ' ' '
 
 

 
 
 Z	2q!,,:LMMM  NM	 	 	     3 3 3> > > >,) ) ) ) )r#   c                  *    e Zd ZdZd Zd Zd Zd ZdS )DummyRequestTestsz$
    Tests for L{DummyRequest}.
    c                   t          g           }|                                 |                     | j        g          }|                     dt          |                     |\  }|                     |                    d          t                     |                     |                    d          d           dS )zp
        L{DummyRequest.getClientIP} is deprecated in favor of
        L{DummyRequest.getClientAddress}
        )offendingFunctionsr   categoryr   z}twisted.web.test.requesthelper.DummyRequest.getClientIP was deprecated in Twisted 18.4.0; please use getClientAddress insteadN)r   r   flushWarningstest_getClientIPDeprecatedassertEquallengetDeprecationWarning)r:   rp   warningswarnings       r!   r  z,DummyRequestTests.test_getClientIPDeprecated  s     r""%% $ ?@ & 
 
 	CMM***	Z002DEEEKK	""6	
 	
 	
 	
 	
r#   c                    t          g           }t          ddd          }||_        |                     d|                                           dS )z
        L{DummyRequest.getClientIP} supports IPv6 addresses, just like
        L{twisted.web.http.Request.getClientIP}.
        r5   z::190  N)r   r   r   r	  r   )r:   rp   r   s      r!   test_getClientIPSupportsIPv6z.DummyRequestTests.test_getClientIPSupportsIPv6  sQ    
 r""UE511 3 3 5 566666r#   c                    t          g           }|                                }t          j        t          |           dS )zu
        L{DummyRequest.getClientAddress} returns an L{IAddress}
        provider no C{client} has been set.
        N)r   r   r   verifyObjectr   )r:   rp   nulls      r!   "test_getClientAddressWithoutClientz4DummyRequestTests.test_getClientAddressWithoutClient  s;    
 r""''))Hd+++++r#   c                    t          g           }t          ddd          }||_        |                                }|                     ||           dS )zI
        L{DummyRequest.getClientAddress} returns the C{client}.
        r5   r   r  N)r   r   r   r   assertIs)r:   rp   r   addresss       r!   test_getClientAddressz'DummyRequestTests.test_getClientAddress  sT     r""UK77**,,gv&&&&&r#   N)r,   r-   r.   r/   r  r  r  r  r   r#   r!   r  r    sZ         
 
 
2	7 	7 	7, , ,' ' ' ' 'r#   r  N)1r/   
__future__r   __all__ior   typingr   r   r	   zope.interfacer
   r   incrementalr   twisted.internet.addressr   r   twisted.internet.deferr   twisted.internet.interfacesr   r   twisted.internet.taskr   twisted.python.deprecater   twisted.trialr   twisted.web._responsesr   twisted.web.http_headersr   twisted.web.resourcer   twisted.web.serverr   r   r   textLinearWhitespaceComponentssanitizedTextbytesLinearWhitespaceComponentsr(   sanitizedBytesr+   r   r   SynchronousTestCaser  r   r#   r!   <module>r/     sy    # " " " " ">
*       ' ' ' ' ' ' ' ' ' ' . . . . . . . .       = = = = = = = = + + + + + + ? ? ? ? ? ? ? ? ' ' ' ' ' ' / / / / / / " " " " " " ( ( ( ( ( ( , , , , , , ) ) ) ) ) ) : : : : : : : : : :!O!O:N!O!O!O # #/M# # #  %%g.. X       J J J J J J J JZL) L) L) L) L) L) L) L)^:' :' :' :' :'4 :' :' :' :' :'r#   