o
    b.                     @   s   d Z ddlZddl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mZ G dd	 d	ejejZG d
d dejZG dd deZG dd deje
jZdS )zH
I hold resource classes and helper classes that deal with CGI scripts.
    N)protocol)Logger)filepath)pb)httpresourceserverstaticc                   @   s$   e Zd Zdd Zdd Zdd ZdS )CGIDirectoryc                 C   s   t j|  tj| | d S N)r   Resource__init__r   FilePath)selfpathname r   3/usr/lib/python3/dist-packages/twisted/web/twcgi.pyr      s   zCGIDirectory.__init__c                 C   s6   |  |}| stjjS | rt|jS t|jS r   )	childexistsr	   FilechildNotFoundisdirr
   path	CGIScript)r   r   requestfnpr   r   r   getChild   s   


zCGIDirectory.getChildc                 C   s   t d}||S )Nz1CGI directories do not support directory listing.)r   
NoResourcerender)r   r   notFoundr   r   r   r   $   s   
zCGIDirectory.renderN)__name__
__module____qualname__r   r   r   r   r   r   r   r
      s    	r
   c                   @   s2   e Zd ZdZdZd
ddZdd Zg fdd	ZdS )r   z
    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.
       Nc                 C   s$   || _ |du rddlm} || _dS )zA
        Initialize, with the name of a CGI script file.
        Nr   )reactor)filenametwisted.internetr$   _reactor)r   r%   registryr$   r   r   r   r   6   s   
zCGIScript.__init__c              
   C   s  dd |j }| dd }tj|d|jt| j	|j
|| j|jd	}| j}|dur4||d< |j}|rBdd | |d	< t|d
r`|jdd |j }|jdd t||d< z|jd}W n tyw   d|d< g }	Y nw |j|d d  }
|d< d|
v rg }	n
dd |
dD }	|  D ]\}}|dd }|dvrd| }|||< qtj D ]\}}||vr|||< q| |||	 tjS )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.
           /   :r   zCGI/1.1)	SERVER_SOFTWARESERVER_NAMEGATEWAY_INTERFACESERVER_PROTOCOLSERVER_PORTREQUEST_METHODSCRIPT_NAMESCRIPT_FILENAMEREQUEST_URINREMOTE_ADDR/	PATH_INFOcontent   CONTENT_LENGTH   ? QUERY_STRINGr#      =c                 S   s   g | ]
}t j| qS r   )urllibparseunquotedecode).0xr   r   r   
<listcomp>s   s    z$CGIScript.render.<locals>.<listcomp>   +   -   _)s   content-types   content-lengths   proxys   HTTP_)joinprepathgetRequestHostnamesplitr   versionclientprotostrgetHostportmethodr%   urigetClientAddresshostpostpathhasattrr7   seektellindex
ValueErrorgetAllHeadersitemsreplaceupperosenviron
runProcessNOT_DONE_YET)r   r   
scriptName
serverNameenvippplengthqindexqargsqstitleheaderenvnamekeyvaluer   r   r   r   B   sX   




zCGIScript.renderc              	   C   s4   t |}| j|| j| jg| |tj| j dS )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%   r_   r   dirnamer   re   r   rj   pr   r   r   ra      s   
zCGIScript.runProcess)NN)r    r!   r"   __doc__isLeafr   r   ra   r   r   r   r   r   +   s    
Ar   c                   @   s    e Zd ZdZdZg fddZdS )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              	   C   s8   t |}| j|| j| j| jg| |tj| j dS )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)rq   r'   rr   filterr%   r_   r   rs   rt   r   r   r   ra      s   zFilteredScript.runProcessN)r    r!   r"   rv   ry   ra   r   r   r   r   rx      s    rx   c                   @   s   e Zd ZdZdZdZdZe ZdZ	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )rq   r#   r       Fc                 C      |    d S r   )resumeProducingr   issuerr   r   r   view_resumeProducing      z'CGIProcessProtocol.view_resumeProducingc                 C   r{   r   )pauseProducingr}   r   r   r   view_pauseProducing   r   z&CGIProcessProtocol.view_pauseProducingc                 C   r{   r   )stopProducingr}   r   r   r   view_stopProducing   r   z%CGIProcessProtocol.view_stopProducingc                 C      | j   d S r   )	transportr|   r   r   r   r   r|         z"CGIProcessProtocol.resumeProducingc                 C   r   r   )r   r   r   r   r   r   r      r   z!CGIProcessProtocol.pauseProducingc                 C   r   r   )r   loseConnectionr   r   r   r   r      r   z CGIProcessProtocol.stopProducingc                 C   s   || _ | j  | j d S r   )r   notifyFinishaddBoth	_finished)r   r   r   r   r   r      s   zCGIProcessProtocol.__init__c                 C   sH   | j | d | j jdd | j j }|r| j| | j  d S )Nr#   r   )r   registerProducerr7   rW   readr   write
closeStdin)r   r7   r   r   r   connectionMade   s   z!CGIProcessProtocol.connectionMadec                 C   s   | j | | _ d S r   )	errortext)r   errorr   r   r   errReceived   s   zCGIProcessProtocol.errReceivedc              	   C   s  | j r| j| }g }dD ]}||}|dkr|||f q|rd| j_|  |d \}}|d| | _|dt|d  }| j|}|D ]b}|d}	|	dkr\| j	j
d|d qH|d|	  }
||	d d }|
d	krw| jtj |
d
krz
t|dd }W n ty   | j	
d Y qHw | j| qH|
 dvr| jj|
| qH||t| d }d| _ | j r|| _| j s| j| dS dS )z)
        Handle a chunk of input
        )s   

s   

s   s   

Nr   r8   s   : z)ignoring malformed CGI header: {header!r})rm   s   locations   status   zmalformed status header)s   servers   date)handling_headers
headertextfindappendr   defaultContentTypesortlenrK   _logr   lowersetResponseCoder   FOUNDintBaseExceptionresponseHeadersaddRawHeaderr   )r   outputtext
headerEnds	delimiter	headerend	linebreakheadersrm   br
headerName
headerText	statusNumr   r   r   outReceived   sX   


zCGIProcessProtocol.outReceivedc                 C   s   |j jdkr| jjd| jj|j jd | jr"| jjd| jj| jd | jrD| jjd| jj| jd | j	sD| j
ttjdd	| j | j	sS| j  | j  d S d S )
Nr   z*CGI {uri} exited with exit code {exitCode})rR   exitCodez"Errors from CGI {uri}: {errorText})rR   	errorTextz/Premature end of headers in {uri}: {headerText})rR   r   zCGI Script Errorz Premature end of script headers.)rp   r   r   r   r   rR   r   r   r   _requestFinishedr   r   	ErrorPager   INTERNAL_SERVER_ERRORr   unregisterProducerfinish)r   reasonr   r   r   processEnded0  s>   
zCGIProcessProtocol.processEndedc                 C   s
   d| _ dS )zc
        Record the end of the response generation for the request being
        serviced.
        TN)r   )r   ignoredr   r   r   r   Q  s   
zCGIProcessProtocol._finishedN)r    r!   r"   r   headers_writtenr   r   r   r   r   r   r   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   rq      s&    :!rq   )rv   r_   r>   r&   r   twisted.loggerr   twisted.pythonr   twisted.spreadr   twisted.webr   r   r   r	   r   r   r
   r   rx   ProcessProtocolViewablerq   r   r   r   r   <module>   s   s-