Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/web/__pycache__/twcgi.cpython-37.pyc

B

�o]O,�@s�dZddlZddlZddlmZddlmZddlmZddl	m
Z
ddlmZm
Z
mZmZGdd	�d	e
jej�ZGd
d�de
j�ZGdd
�d
e�ZGdd�deje
j�ZdS)zH
I hold resource classes and helper classes that deal with CGI scripts.
�N)�protocol)�Logger)�filepath)�pb)�http�resource�server�staticc@s$eZdZdd�Zdd�Zdd�ZdS)�CGIDirectorycCstj�|�tj�||�dS)N)r�Resource�__init__r�FilePath)�self�pathname�r�D/opt/alt/python37/lib64/python3.7/site-packages/twisted/web/twcgi.pyrszCGIDirectory.__init__cCs>|�|�}|��stjjS|��r,t|j�St|j�St	�
�S)N)�child�existsr	ZFileZ
childNotFound�isdirr
�path�	CGIScriptr�
NoResource)rr�requestZfnprrr�getChilds


zCGIDirectory.getChildcCst�d�}|�|�S)Nz1CGI directories do not support directory listing.)rr�render)rrZnotFoundrrrr'szCGIDirectory.renderN)�__name__�
__module__�__qualname__rrrrrrrr
sr
c@s2eZdZdZdZd
dd�Zdd�Zgfdd	�ZdS)rz�
    L{CGIScript} is a resource which runs child processes according to the CGI
    specification.

    The implementation is complex due to the fact that it requires asynchronous
    IPC with an external process with an unpleasant protocol.
    �NcCs$||_|dkrddlm}||_dS)zA
        Initialize, with the name of a CGI script file.
        Nr)�reactor)�filename�twisted.internetr�_reactor)rr �registryrrrrr7szCGIScript.__init__c
Cs�dd�|j�}|���d�d}tj|d|jt|��j	�|j
||j|jd�	}|�
�j}|dk	rh||d<|j}|r�dd�|�|d	<t|d
�r�|j�dd�|j��}|j�dd�t|�|d<y|j�d
�}Wn tk
r�d|d<g}	Yn@X|j|dd�}
|d<d|
k�rg}	ndd�|
�d�D�}	xD|����D]4\}}|�dd���}
|dk�rhd|
}
|||
<�q>Wx*tj��D]\}}||k�r�|||<�q�W|�|||	�tjS)a
        Do various things to conform to the CGI specification.

        I will set up the usual slew of environment variables, then spin off a
        process.

        @type request: L{twisted.web.http.Request}
        @param request: An HTTP request.
        �/�:rzCGI/1.1)	ZSERVER_SOFTWAREZSERVER_NAMEZGATEWAY_INTERFACEZSERVER_PROTOCOLZSERVER_PORTZREQUEST_METHODZSCRIPT_NAMEZSCRIPT_FILENAMEZREQUEST_URINZREMOTE_ADDR�/Z	PATH_INFO�content�ZCONTENT_LENGTH�?�ZQUERY_STRINGr�=cSsg|]}t�|��qSr)�urllib�unquote)�.0�xrrr�
<listcomp>ssz$CGIScript.render.<locals>.<listcomp>�+�-�_)scontent-typescontent-lengthsproxysHTTP_)�joinZprepathZgetRequestHostname�splitr�versionZclientproto�strZgetHost�port�methodr �uriZgetClientAddress�hostZpostpath�hasattrr'�seek�tell�index�
ValueErrorZ
getAllHeaders�items�replace�upper�os�environ�
runProcessZNOT_DONE_YET)rrZ
scriptNameZ
serverName�envZip�pp�lengthZqindex�qargs�qs�title�header�envname�key�valuerrrrDsR








zCGIScript.renderc	Cs4t|�}|j�||j|jg||tj�|j��dS)aI
        Run the cgi script.

        @type env: A L{dict} of L{str}, or L{None}
        @param env: The environment variables to pass to the process that will
            get spawned. See
            L{twisted.internet.interfaces.IReactorProcess.spawnProcess} for
            more information about environments and process creation.

        @type request: L{twisted.web.http.Request}
        @param request: An HTTP request.

        @type qargs: A L{list} of L{str}
        @param qargs: The command line arguments to pass to the process that
            will get spawned.
        N)�CGIProcessProtocolr"�spawnProcessr rDr�dirname)rrGrrJ�prrrrF�szCGIScript.runProcess)NN)rrr�__doc__ZisLeafrrrFrrrrr.s


@rc@s eZdZdZdZgfdd�ZdS)�FilteredScripta
    I am a special version of a CGI script, that uses a specific executable.

    This is useful for interfacing with other scripting languages that adhere
    to the CGI standard. My C{filter} attribute specifies what executable to
    run, and my C{filename} init parameter describes which script to pass to
    the first argument of that script.

    To customize me for a particular location of a CGI interpreter, override
    C{filter}.

    @type filter: L{str}
    @ivar filter: The absolute path to the executable.
    z/usr/bin/catc	Cs8t|�}|j�||j|j|jg||tj�|j��dS)ad
        Run a script through the C{filter} executable.

        @type env: A L{dict} of L{str}, or L{None}
        @param env: The environment variables to pass to the process that will
            get spawned. See
            L{twisted.internet.interfaces.IReactorProcess.spawnProcess}
            for more information about environments and process creation.

        @type request: L{twisted.web.http.Request}
        @param request: An HTTP request.

        @type qargs: A L{list} of L{str}
        @param qargs: The command line arguments to pass to the process that
            will get spawned.
        N)rQr"rR�filterr rDrrS)rrGrrJrTrrrrF�szFilteredScript.runProcessN)rrrrUrWrFrrrrrV�srVc@szeZdZdZdZdZdZe�Zdd�Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)rQrr�cCs|��dS)N)�resumeProducing)r�issuerrrr�view_resumeProducing�sz'CGIProcessProtocol.view_resumeProducingcCs|��dS)N)�pauseProducing)rrZrrr�view_pauseProducing�sz&CGIProcessProtocol.view_pauseProducingcCs|��dS)N)�
stopProducing)rrZrrr�view_stopProducing�sz%CGIProcessProtocol.view_stopProducingcCs|j��dS)N)�	transportrY)rrrrrY�sz"CGIProcessProtocol.resumeProducingcCs|j��dS)N)r`r\)rrrrr\�sz!CGIProcessProtocol.pauseProducingcCs|j��dS)N)r`ZloseConnection)rrrrr^�sz CGIProcessProtocol.stopProducingcCs
||_dS)N)r)rrrrrr�szCGIProcessProtocol.__init__cCsH|j�|d�|jj�dd�|jj��}|r:|j�|�|j��dS)Nrr)rZregisterProducerr'r=�readr`�writeZ
closeStdin)rr'rrr�connectionMade�sz!CGIProcessProtocol.connectionMadecCs|j||_dS)N)�	errortext)r�errorrrr�errReceived�szCGIProcessProtocol.errReceivedc
Cs�|j�r�|j|}g}x,dD]$}|�|�}|dkr|�||f�qW|�rxd|j_|��|d\}}|d|�|_|dt|�d�}|j�|�}x�|D]�}|�d�}	|	dkr�|j	j
d|d�q�|d|	���}
||	dd�}|
d	kr�|j�t
j�|
d
k�r>yt|dd��}Wn|j	�
d�YnX|j�|�q�|
��d
kr�|jj�|
|�q�W||t|�d�}d|_|j�r�||_|j�s�|j�|�dS)z)
        Handle a chunk of input
        )s

s

s

s

���Nrr(s: z)ignoring malformed CGI header: {header!r})rMslocationsstatus�zmalformed status header)sserversdate)�handling_headers�
headertext�find�appendrZdefaultContentType�sort�lenr5�_logre�lowerZsetResponseCoderZFOUND�intZresponseHeadersZaddRawHeaderrb)
r�output�textZ
headerEnds�	delimiterZ	headerendZ	linebreak�headersrM�brZ
headerName�
headerTextZ	statusNumrrr�outReceived�sN






zCGIProcessProtocol.outReceivedcCs�|jjdkr&|jjd|jj|jjd�|jrD|jjd|jj|jd�|jr�|jjd|jj|jd�|j�	t
�tj
dd	��|j��|j��|j��dS)
Nrz*CGI {uri} exited with exit code {exitCode})r:�exitCodez"Errors from CGI {uri}: {errorText})r:Z	errorTextz/Premature end of headers in {uri}: {headerText})r:rwzCGI Script Errorz Premature end of script headers.)rPryrorerr:rdrirjrbrZ	ErrorPagerZINTERNAL_SERVER_ERRORrZunregisterProducerZfinish)r�reasonrrr�processEnded2s
zCGIProcessProtocol.processEndedN)rrrriZheaders_writtenrjrdrror[r]r_rYr\r^rrcrfrxr{rrrrrQ�s 	:rQ)rUrDr,r!rZtwisted.loggerrZtwisted.pythonrZtwisted.spreadrZtwisted.webrrrr	rr
r
rrVZProcessProtocolZViewablerQrrrr�<module>sm+