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__/test_cgi.cpython-38.pyc
U


W[�5�@s*dZddlZddlZddlZddlmZddlmZddlm	Z	m
Z
mZmZddl
mZmZmZddlmZmZddlmZmZmZmZmZdd	lmZdd
lmZdZdZd
Z dZ!dZ"dZ#dZ$dZ%Gdd�dej&�Z'Gdd�dej(�Z)Gdd�dej(�Z*Gdd�dej(�Z+Gdd�dej(�Z,dd�Z-dS)z!
Tests for L{twisted.web.twcgi}.
�N)�BytesIO)�unittest)�address�reactor�
interfaces�error)�util�failure�log)�	NOT_FOUND�INTERNAL_SERVER_ERROR)�client�twcgi�server�resource�http_headers)�_render)�DummyRequestz2print("Header: OK")
print("")
print("cgi output")
zJprint("Header: spam")
print("Header: eggs")
print("")
print("cgi output")
z+print("XYZ")
print("")
print("cgi output")
zPprint("Server: monkeys")
print("Date: last year")
print("")
print("cgi output")
a&# This is an example of a correctly-written CGI script which reads a body
# from stdin, which only reads env['CONTENT_LENGTH'] bytes.

import os, sys

body_length = int(os.environ.get('CONTENT_LENGTH',0))
indata = sys.stdin.read(body_length)
print("Header: OK")
print("")
print("readinput ok")
a+# This is an example of the typical (incorrect) CGI script which expects
# the server to close stdin when the body of the request is complete.
# A correct CGI should only read env['CONTENT_LENGTH'] bytes.

import sys

indata = sys.stdin.read()
print("Header: OK")
print("")
print("readallinput ok")
zMprint("content-type: text/cgi-duplicate-test")
print("")
print("cgi output")
z�import json
import os
print("")
print("")
vals = {x:y for x,y in os.environ.items() if x.startswith("HTTP_")}
print(json.dumps(vals))
c@seZdZejZdS)�PythonScriptN)�__name__�
__module__�__qualname__�sys�
executable�filter�rr�;/usr/lib/python3/dist-packages/twisted/web/test/test_cgi.pyrXsrc@s�eZdZdZej�e�sdZdd�Z	dd�Z
dd�Zd	d
�Zdd�Z
d
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zde_dd�Zdd�Zde_dd�Zd d!�Zde_d"d#�Zd$d%�Zd&S)'�CGITestsz,
    Tests for L{twcgi.FilteredScript}.
    z5CGI tests require a functional reactor.spawnProcess()cCsHt��}t�t|�}|�dt|��t�|�}t	�
d|�|_|j��j
S)Nscgir)rZResourcerZsibpath�__file__ZputChildrrZSiterZ	listenTCP�pZgetHostZport)�selfZcgi�rootZcgipathZsiterrr�startServerfs
zCGITests.startServercCst|dd�r|j��SdS)Nr)�getattrrZ
stopListening�r rrr�tearDownoszCGITests.tearDownc	Cs4tj�|���}t|d��}|�|�W5QRX|S)N�wt)�os�path�abspath�mktemp�open�write)r �source�cgiFilename�cgiFilerrr�writeCGItszCGITests.writeCGIcCsV|�t�}|�|�}d|f}|�d�}t�t��d|�}|�tj	�|�|j
�|S)N�http://localhost:%d/cgi�ascii�GET)r0�	DUMMY_CGIr"�encoder
�Agentr�request�addCallback�readBody�
_testCGI_1)r r.�portnum�url�drrr�test_CGI{s



zCGITests.test_CGIcCs|�|dtj�d��dS)Ns
cgi outputr2)�assertEqualr'�linesepr5)r �resrrrr:�szCGITests._testCGI_1csb��t�}��|�}d|f}|�d�}t�t�}|�d|�}|�t	��fdd�}|�|�|S)zc
        If the CGI script emits a I{Server} or I{Date} header, these are
        ignored.
        r1r2r3cs,��d|j�d����d|j�d��dS)NZmonkeysrz	last year�date)ZassertNotIn�headers�
getRawHeaders��responser$rr�
checkResponse�s
�
�z;CGITests.test_protectedServerAndDate.<locals>.checkResponse)
r0�SPECIAL_HEADER_CGIr"r5r
r6rr7r8�discardBody�r r.r;r<�agentr=rGrr$r�test_protectedServerAndDate�s






z$CGITests.test_protectedServerAndDatecsb��t�}��|�}d|f}|�d�}t�t�}|�d|�}|�t	��fdd�}|�|�|S)z�
        If the CGI script emits a I{content-type} header, make sure that the
        server doesn't add an additional (duplicate) one, as per ticket 4786.
        r1r2r3cs��|j�d�dg�|S)Nzcontent-typeztext/cgi-duplicate-test�r?rCrDrEr$rrrG�s

�zBCGITests.test_noDuplicateContentTypeHeaders.<locals>.checkResponse)
r0�$NO_DUPLICATE_CONTENT_TYPE_HEADER_CGIr"r5r
r6rr7r8rIrJrr$r�"test_noDuplicateContentTypeHeaders�s






z+CGITests.test_noDuplicateContentTypeHeaderscs|��t�}��|�}d|f}|�d�}t�t�}t�dgdgd��}|j	d||d�}�fdd	�}|�
tj�|�
|�|S)
zV
        The CGI script is never called with the Proxy header passed through.
        r1r2sfoosbar)sProxysX-Innocent-Headerr3)rCcs.t�|�d��}��t|���dddh�dS)Nr2Z	HTTP_HOSTZHTTP_CONNECTIONZHTTP_X_INNOCENT_HEADER)�json�loads�decoder?�set�keys)rFrCr$rrrG�s

�z7CGITests.test_noProxyPassthrough.<locals>.checkResponse)r0�HEADER_OUTPUT_CGIr"r5r
r6rrZHeadersr7r8r9)r r.r;r<rKrCr=rGrr$r�test_noProxyPassthrough�s




�
z CGITests.test_noProxyPassthroughcsb��t�}��|�}d|f}|�d�}t�t�}|�d|�}|�t	��fdd�}|�|�|S)zp
        If a CGI script emits two instances of the same header, both are sent
        in the response.
        r1r2r3cs��|j�d�ddg�dS)N�headerZspamZeggsrMrEr$rrrG�s
�z7CGITests.test_duplicateHeaderCGI.<locals>.checkResponse)
r0�DUAL_HEADER_CGIr"r5r
r6rr7r8rIrJrr$r�test_duplicateHeaderCGI�s






z CGITests.test_duplicateHeaderCGIcs���t�}��|�}d|f}|�d�}t�t�}|�d|�}|�t	�g��fdd�}t
�|���t
j
|���fdd�}|�|�|S)zF
        Check for the error message in the duplicated header
        r1r2r3cs��t�|��dS�N)�appendr
ZtextFromEventDict)Z	eventDict)�loggedMessagesrr�
addMessage�sz4CGITests.test_malformedHeaderCGI.<locals>.addMessagecs��dtd���dS)Nzignoring malformed CGI header: sXYZ)ZassertIn�repr�Zignored�r\r rrrG�s�z7CGITests.test_malformedHeaderCGI.<locals>.checkResponse)r0�BROKEN_HEADER_CGIr"r5r
r6rr7r8rIr
ZaddObserverZ
addCleanupZremoveObserver)r r.r;r<rKr=r]rGrr`r�test_malformedHeaderCGI�s







z CGITests.test_malformedHeaderCGIc	Cs�tj�|���}t|d��}|�t�W5QRX|�|�}t�	t
�}d|f}|�d�}|�d|�}|�
tj�|�
|j�|S)Nr&r1r2r3)r'r(r)r*r+r,�
READINPUT_CGIr"r
r6rr5r7r8r9�_test_ReadEmptyInput_1�r r.r/r;rKr<r=rrr�test_ReadEmptyInputs



zCGITests.test_ReadEmptyInput�cCs&d�tj�}|�d�}|�||�dS�Nzreadinput ok{}r2��formatr'r@r5r?�r rAZexpectedrrrrds
zCGITests._test_ReadEmptyInput_1c	Cs�tj�|���}t|d��}|�t�W5QRX|�|�}t�	t
�}d|f}|�d�}|j|dt�
td��d�}|�tj�|�|j�|S�Nr&r1r2sPOSTsHere is your stdin)Zuri�methodZbodyProducer)r'r(r)r*r+r,rcr"r
r6rr5r7�FileBodyProducerrr8r9�_test_ReadInput_1rerrr�test_ReadInputs"



��zCGITests.test_ReadInputcCs&d�tj�}|�d�}|�||�dSrhrirkrrrro-s
zCGITests._test_ReadInput_1c	Cs�tj�|���}t|d��}|�t�W5QRX|�|�}d|f}|�d�}t	�
t�j|dt	�
td��d�}|�t	j�|�|j�|Srl)r'r(r)r*r+r,�READALLINPUT_CGIr"r5r
r6rr7rnrr8r9�_test_ReadAllInput_1)r r.r/r;r<r=rrr�test_ReadAllInput3s 



��zCGITests.test_ReadAllInputcCs&d�tj�}|�d�}|�||�dS)Nzreadallinput ok{}r2rirkrrrrrGs
zCGITests._test_ReadAllInput_1cCsXGdd�d�}|�}tddg�}t�ddd�|_tjd|d	�}t||�|�|j�d
S)zw
        L{twcgi.FilteredScript.runProcess} uses the reactor passed as an
        argument to the constructor.
        c@seZdZdZdZdd�ZdS)z5CGITests.test_useReactorArgument.<locals>.FakeReactorzR
            A fake reactor recording whether spawnProcess is called.
            Fc_s
d|_dS)z�
                Set the C{called} flag to C{True} if C{spawnProcess} is called.

                @param args: Positional arguments.
                @param kwargs: Keyword arguments.
                TN)�called)r �args�kwargsrrr�spawnProcessWszBCGITests.test_useReactorArgument.<locals>.FakeReactor.spawnProcessN)rrr�__doc__rtrwrrrr�FakeReactorRsry�a�b�TCP�	127.0.0.1�90z
dummy-file�rN)	rr�IPv4Addressr
r�FilteredScriptrZ
assertTruert)r ryZfakeReactorr7rrrr�test_useReactorArgumentMs
z CGITests.test_useReactorArgumentN)rrrrxrZIReactorProcessZ
providedByr�skipr"r%r0r>r:rLrOrVrYrbrfZtimeoutrdrprorsrrr�rrrrr]s.	
rc@seZdZdZdd�ZdS)�CGIScriptTestsz'
    Tests for L{twcgi.CGIScript}.
    cCsbGdd�d�}|�}tj|��|d�}tddg�}t�ddd�|_t||�|�|j	d	d
�dS)zt
        L{twcgi.CGIScript.render} sets the process environment
        I{PATH_INFO} from the request path.
        c@seZdZdZdd�ZdS)z1CGIScriptTests.test_pathInfo.<locals>.FakeReactorzZ
            A fake reactor recording the environment passed to spawnProcess.
            cSs
||_dS)a9
                Store the C{env} L{dict} to an instance attribute.

                @param process: Ignored
                @param filename: Ignored
                @param args: Ignored
                @param env: The environment L{dict} which will be stored
                @param wdir: Ignored
                N)�process_env)r Zprocess�filenameru�envZwdirrrrrwxs
z>CGIScriptTests.test_pathInfo.<locals>.FakeReactor.spawnProcessN)rrrrxrwrrrrrytsryrrzr{r|r}r~Z	PATH_INFOz/a/bN)
rZ	CGIScriptr*rrr�r
rr?r�)r ryZ_reactorrr7rrr�
test_pathInfoos
�zCGIScriptTests.test_pathInfoN)rrrrxr�rrrrr�jsr�c@s eZdZdZdd�Zdd�ZdS)�CGIDirectoryTestsz*
    Tests for L{twcgi.CGIDirectory}.
    cs>t�����}tdg��t|��}��fdd�}|�|�|S)zc
        L{twcgi.CGIDirectory.render} sets the HTTP response code to I{NOT
        FOUND}.
        �cs���jt�dSrZ�r?�responseCoderr_�r7r rr�
cbRendered�sz1CGIDirectoryTests.test_render.<locals>.cbRendered)r�CGIDirectoryr*rrr8)r rr=r�rr�r�test_render�s


zCGIDirectoryTests.test_rendercsX���}t�|�t�|�}tdg��|�d��}t|��}��fdd�}|�|�|S)a
        L{twcgi.CGIDirectory.getChild} returns a resource which renders an
        response with the HTTP I{NOT FOUND} status code if the indicated child
        does not exist as an entry in the directory used to initialized the
        L{twcgi.CGIDirectory}.
        Zfoocs���jt�dSrZr�r_r�rrr��sz8CGIDirectoryTests.test_notFoundChild.<locals>.cbRendered)	r*r'�makedirsrr�rZgetChildrr8)r r(rZchildr=r�rr�r�test_notFoundChild�s




z$CGIDirectoryTests.test_notFoundChildN)rrrrxr�r�rrrrr��sr�c@seZdZdZdd�ZdS)�CGIProcessProtocolTestsz0
    Tests for L{twcgi.CGIProcessProtocol}.
    cCs:tdg�}t�|�}|�t�t����|�|j	t
�dS)z�
        If the process communicating with L{CGIProcessProtocol} ends before
        finishing writing out headers, the response has I{INTERNAL SERVER
        ERROR} as its status code.
        r�N)rrZCGIProcessProtocolZprocessEndedr	ZFailurerZProcessTerminatedr?r�r)r r7Zprotocolrrr�test_prematureEndOfHeaders�s

z2CGIProcessProtocolTests.test_prematureEndOfHeadersN)rrrrxr�rrrrr��sr�cst�����fdd��S)zn
    Discard the body of a HTTP response.

    @param response: The response.

    @return: The response.
    cs�SrZr)�_rErr�<lambda>��zdiscardBody.<locals>.<lambda>)r
r9r8rErrErrI�srI).rxrr'rP�iorZ
twisted.trialrZtwisted.internetrrrrZtwisted.pythonrr	r
Ztwisted.web.httprrZtwisted.webr
rrrrZtwisted.web.test._utilrZtwisted.web.test.test_webrr4rXrarHrcrqrNrUr�rZTestCaserr�r�r�rIrrrr�<module>s6

	%&