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: //usr/lib/python3/dist-packages/twisted/web/test/__pycache__/requesthelper.cpython-38.pyc
U


W[^4�@s6dZddlmZmZddgZddlmZddlmZm	Z	ddl
mZddlm
Z
dd	lmZdd
lmZddlmZmZddlmZmZdd
lmZddlmZddlmZddlmZm Z m!Z!ddl"m#Z#ee�Gdd�de$��Z%Gdd�d�Z&Gdd�de$�Z'e
edddd�dd�e'j(�e'_(Gdd�dej)�Z*dS)z2
Helpers related to HTTP requests, used by tests.
�)�division�absolute_import�DummyChannel�DummyRequest)�BytesIO)�implementer�verify)�
intToBytes)�
deprecated)�Version)�Deferred)�IPv4Address�IPv6Address)�
ISSLTransport�IAddress)�unittest)�Headers)�Resource)�NOT_DONE_YET�Session�Site)�FOUNDc@seZdZdZdS)�NullAddressz/
    A null implementation of L{IAddress}.
    N)�__name__�
__module__�__qualname__�__doc__�rr�@/usr/lib/python3/dist-packages/twisted/web/test/requesthelper.pyr src@s�eZdZGdd�d�Zee�Gdd�de��Zee��Z	ddd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)rc@sVeZdZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)zDummyChannel.TCP�PFNcCs,|dkrtddd�}||_t�|_g|_dS)N�TCPz192.168.1.1i80)r
�_peerr�written�	producers��selfZpeerrrr�__init__-s
zDummyChannel.TCP.__init__cCs|jS�N)r!�r%rrr�getPeer4szDummyChannel.TCP.getPeercCs(t|t�std|f��|j�|�dS)Nz+Can only write bytes to a transport, not %r)�
isinstance�bytes�	TypeErrorr"�write�r%�datarrrr-7s
zDummyChannel.TCP.writecCs|D]}|�|�qdSr')r-)r%�iovecr/rrr�
writeSequence<szDummyChannel.TCP.writeSequencecCstdd|j�S)Nr z10.0.0.1)r
�portr(rrr�getHost@szDummyChannel.TCP.getHostcCs|j�||f�dSr')r#�append�r%ZproducerZ	streamingrrr�registerProducerCsz!DummyChannel.TCP.registerProducercCsdSr'rr(rrr�unregisterProducerFsz#DummyChannel.TCP.unregisterProducercCs
d|_dS)NT)�disconnectedr(rrr�loseConnectionIszDummyChannel.TCP.loseConnection)N)
rrrr2r8r&r)r-r1r3r6r7r9rrrrr )s
r c@seZdZdS)zDummyChannel.SSLN)rrrrrrr�SSLMsr:NcCs|�|�|_dSr')r �	transportr$rrrr&SszDummyChannel.__init__cCsdSr'r)r%�requestrrr�requestDoneWszDummyChannel.requestDonecCsL|d|d|d}|g}|�dd�|D��|�d�|j�|�dS)N� �
css"|]\}}|d|dVqdS)s: r?Nr)�.0�name�valuerrr�	<genexpr>^sz,DummyChannel.writeHeaders.<locals>.<genexpr>)�extendr4r;r1)r%�version�code�reason�headersZ
response_lineZheaderSequencerrr�writeHeaders[s
�
zDummyChannel.writeHeaderscCs
|j��Sr')r;r)r(rrrr)eszDummyChannel.getPeercCs
|j��Sr')r;r3r(rrrr3iszDummyChannel.getHostcCs|j�||�dSr')r;r6r5rrrr6mszDummyChannel.registerProducercCs|j��dSr')r;r7r(rrrr7qszDummyChannel.unregisterProducercCs|j�|�dSr')r;r-r.rrrr-uszDummyChannel.writecCs|j�|�dSr')r;r1)r%r0rrrr1yszDummyChannel.writeSequencecCs|j��dSr')r;r9r(rrrr9}szDummyChannel.loseConnectioncCsdSr'rr(rrr�
endRequest�szDummyChannel.endRequestcCst|j|j�Sr')r*r;r:r(rrr�isSecure�szDummyChannel.isSecure)N)rrrr rrr:rrZsiter&r=rIr)r3r6r7r-r1r9rJrKrrrrr(s $


c@s�eZdZdZdZdZdZdd�Zdd�Zd2d	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd3dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd4d.d/�Zd0d1�ZdS)5ra
    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/sGETNcCsd|_|jr|��qdS)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)�goZresumeProducing)r%Zprod�srrrr6�szDummyRequest.registerProducercCs
d|_dS)Nr)rMr(rrrr7�szDummyRequest.unregisterProducercCsfg|_g|_d|_||_g|_d|_|p0td|�|_i|_t	�|_
t	�|_d|_g|_
d|_d|_dS)NrsdummysHTTP/1.0)Zsitepathr"�finished�postpathZprepath�sessionr�protoSession�argsr�requestHeaders�responseHeaders�responseCode�_finishedDeferreds�_serverNameZclientproto)r%rPrQ�clientrrrr&�szDummyRequest.__init__cCs,i}|j��D]\}}|d||��<q|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}.
        ���)rTZgetAllRawHeaders�lower)r%rH�k�vrrr�
getAllHeaders�szDummyRequest.getAllHeaderscCs|j�|��dg�dS)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)rTZ
getRawHeadersr[)r%rArrr�	getHeader�szDummyRequest.getHeadercCs|j�||�dS)zJTODO: make this assert on write() if the header is content-length
        N)rU�addRawHeader�r%rArBrrr�	setHeader�szDummyRequest.setHeadercCs(|jr|jS|jrtd��|j|_|jS)Nz8Session cannot be requested after data has been written.)rQr"�AssertionErrorrRr(rrr�
getSession�s
zDummyRequest.getSessioncCs,|�|�}|tkrdS|�|�|��dS)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)�renderrr-�finish)r%�resource�resultrrrre�s


zDummyRequest.rendercCs"t|t�std��|j�|�dS)Nzwrite() only accepts bytes)r*r+r,r"r4r.rrrr-s
zDummyRequest.writecCst�}|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rWr4)r%rOrrr�notifyFinishszDummyRequest.notifyFinishcCs:|jd|_|jdk	r6|j}d|_|D]}|�d�q&dS)z}
        Record that the request is finished and callback and L{Deferred}s
        waiting for notification of this.
        rLN)rOrW�callback)r%�	observers�obsrrrrfs
zDummyRequest.finishcCs.|jdk	r*|j}d|_|D]}|�|�qdS)zK
        Errback and L{Deferreds} waiting for finish notification.
        N)rWZerrback)r%rGrkrlrrr�processingFailed*s

zDummyRequest.processingFailedcCs|g|j|<dSr')rSrarrr�addArg5szDummyRequest.addArgcCs*|jrtdd�|j���||_||_dS)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 has been written: %s.�@@@@N)r"rc�joinrVZresponseMessage)r%rF�messagerrr�setResponseCode9szDummyRequest.setResponseCodecCs|jrtdd�|j���dS)Nz<Last-Modified cannot be set after data has been written: %s.ro�r"rcrp)r%Zwhenrrr�setLastModifiedCszDummyRequest.setLastModifiedcCs|jrtdd�|j���dS)Nz3ETag cannot be set after data has been written: %s.rors)r%�tagrrr�setETagGszDummyRequest.setETagcCst|jttf�r|jjSdS)z|
        Return the IPv4 address of the client which made this request, if there
        is one, otherwise L{None}.
        N)r*rYr
r�hostr(rrr�getClientIPKszDummyRequest.getClientIPcCs|jdkrt�S|jS)z�
        Return the L{IAddress} of the client that made this request.

        @return: an address.
        @rtype: an L{IAddress} provider.
        N)rYrr(rrr�getClientAddressUs
zDummyRequest.getClientAddresscCs|jS)z�
        Get a dummy hostname associated to the HTTP request.

        @rtype: C{bytes}
        @returns: a dummy hostname
        )rXr(rrr�getRequestHostnameaszDummyRequest.getRequestHostnamecCstddd�S)zz
        Get a dummy transport's host.

        @rtype: C{IPv4Address}
        @returns: a dummy transport's host
        r �	127.0.0.1r)r
r(rrrr3kszDummyRequest.getHostrcCsH||_|��rd}nd}||kr&|}n|dt|�}|j�d|�dS)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�r�:shostN)Z	_forceSSLrKr	rTr`)r%rwr2Zssl�defaultZ
hostHeaderrrr�setHostuszDummyRequest.setHostcCs|�t�|�d|�dS)zu
        Utility function that does a redirect.

        The request should have finish() called after this.
        slocationN)rrrrb)r%Zurlrrr�redirect�s
zDummyRequest.redirect)NN)N)r)rrrrZuri�methodrYr6r7r&r^r_rbrdrer-rirfrmrnrrrtrvrxryrzr3r~rrrrrr�s4







ZTwisted��ry)Zreplacementc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�DummyRequestTestsz$
    Tests for L{DummyRequest}.
    cCs^tg�}|��|j|jgd�}|�dt|��|\}|�|�d�t�|�|�d�d�dS)zp
        L{DummyRequest.getClientIP} is deprecated in favor of
        L{DummyRequest.getClientAddress}
        )ZoffendingFunctionsrL�categoryrqz}twisted.web.test.requesthelper.DummyRequest.getClientIP was deprecated in Twisted 18.4.0; please use getClientAddress insteadN)rrxZ
flushWarnings�test_getClientIPDeprecated�assertEqual�len�get�DeprecationWarning)r%r<�warningsZwarningrrrr��s��z,DummyRequestTests.test_getClientIPDeprecatedcCs.tg�}tddd�}||_|�d|���dS)z�
        L{DummyRequest.getClientIP} supports IPv6 addresses, just like
        L{twisted.web.http.Request.getClientIP}.
        r z::1�90N)rrrYr�rx)r%r<rYrrr�test_getClientIPSupportsIPv6�sz.DummyRequestTests.test_getClientIPSupportsIPv6cCs tg�}|��}t�t|�dS)zu
        L{DummyRequest.getClientAddress} returns an L{IAddress}
        provider no C{client} has been set.
        N)rryrZverifyObjectr)r%r<Znullrrr�"test_getClientAddressWithoutClient�sz4DummyRequestTests.test_getClientAddressWithoutClientcCs2tg�}tddd�}||_|��}|�||�dS)zI
        L{DummyRequest.getClientAddress} returns the C{client}.
        r r{r�N)rr
rYryZassertIs)r%r<rYZaddressrrr�test_getClientAddress�s
z'DummyRequestTests.test_getClientAddressN)rrrrr�r�r�r�rrrrr��s

r�N)+rZ
__future__rr�__all__�iorZzope.interfacerrZtwisted.python.compatr	Ztwisted.python.deprecater
ZincrementalrZtwisted.internet.deferrZtwisted.internet.addressr
rZtwisted.internet.interfacesrrZ
twisted.trialrZtwisted.web.http_headersrZtwisted.web.resourcerZtwisted.web.serverrrrZtwisted.web._responsesr�objectrrrrxZSynchronousTestCaser�rrrr�<module>s8b��