U
    W[O,                     @   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   s>   |  |}| stjjS | r,t|jS t|jS t	
 S r   )Zchildexistsr	   ZFileZchildNotFoundisdirr
   path	CGIScriptr   
NoResource)r   r   requestZfnpr   r   r   getChild   s    


zCGIDirectory.getChildc                 C   s   t d}||S )Nz1CGI directories do not support directory listing.)r   r   render)r   r   Z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k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   7   s    zCGIScript.__init__c              
   C   s  dd |j }| dd }tj|d|jt| j	|j
|| j|jd	}| j}|dk	rh||d< |j}|rdd | |d	< t|d
r|jdd |j }|jdd t||d< z|jd}W n  tk
r   d|d< g }	Y n@X |j|d d  }
|d< d|
krg }	ndd |
dD }	|  D ]4\}}|dd }|dkrfd| }|||< q<tj D ]\}}||k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)	ZSERVER_SOFTWAREZSERVER_NAMEZGATEWAY_INTERFACEZSERVER_PROTOCOLZSERVER_PORTZREQUEST_METHODZSCRIPT_NAMEZSCRIPT_FILENAMEZREQUEST_URINZREMOTE_ADDR/Z	PATH_INFOcontent   ZCONTENT_LENGTH   ? ZQUERY_STRINGr   =c                 S   s   g | ]}t |qS r   )urllibZunquote).0xr   r   r   
<listcomp>s   s     z$CGIScript.render.<locals>.<listcomp>+   -   _)s   content-types   content-lengths   proxys   HTTP_)joinZprepathZgetRequestHostnamesplitr   versionZclientprotostrZgetHostZportmethodr    uriZgetClientAddressZhostZpostpathhasattrr'   seektellindex
ValueErrorZgetAllHeadersitemsreplaceupperosenviron
runProcessZNOT_DONE_YET)r   r   Z
scriptNameZ
serverNameenvZipZppZlengthZqindexqargsZqstitleheaderZenvnamekeyvaluer   r   r   r   D   sT    








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    rA   r   dirnamer   rD   r   rE   pr   r   r   rC      s
     zCGIScript.runProcess)NN)r   r   r   __doc__ZisLeafr   r   rC   r   r   r   r   r   .   s
   
@r   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)rJ   r"   rK   filterr    rA   r   rL   rM   r   r   r   rC      s     zFilteredScript.runProcessN)r   r   r   rO   rQ   rC   r   r   r   r   rP      s   rP   c                   @   sz   e Zd ZdZdZdZdZe 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 )rJ   r   r       c                 C   s   |    d S r   )resumeProducingr   Zissuerr   r   r   view_resumeProducing   s    z'CGIProcessProtocol.view_resumeProducingc                 C   s   |    d S r   )pauseProducingrT   r   r   r   view_pauseProducing   s    z&CGIProcessProtocol.view_pauseProducingc                 C   s   |    d S r   )stopProducingrT   r   r   r   view_stopProducing   s    z%CGIProcessProtocol.view_stopProducingc                 C   s   | j   d S r   )	transportrS   r   r   r   r   rS      s    z"CGIProcessProtocol.resumeProducingc                 C   s   | j   d S r   )rZ   rV   r[   r   r   r   rV      s    z!CGIProcessProtocol.pauseProducingc                 C   s   | j   d S r   )rZ   ZloseConnectionr[   r   r   r   rX      s    z CGIProcessProtocol.stopProducingc                 C   s
   || _ d S r   )r   )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   ZregisterProducerr'   r:   readrZ   writeZ
closeStdin)r   r'   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|rpd| j_|  |d \}}|d| | _|dt|d  }| j|}|D ]}|d}	|	dkr| j	j
d|d q|d|	  }
||	d d }|
d	kr| jtj |
d
kr8zt|dd }W n   | j	
d Y nX | j| q|
 dkr| jj|
| q||t| d }d| _ | j r~|| _| j s| j| dS )z)
        Handle a chunk of input
        )s   

s   

s   s   

Nr   r(   s   : z)ignoring malformed CGI header: {header!r})rG   s   locations   status   zmalformed status header)s   servers   date)handling_headers
headertextfindappendr   ZdefaultContentTypesortlenr4   _logr`   lowerZsetResponseCoder   ZFOUNDintZresponseHeadersZaddRawHeaderr]   )r   outputtextZ
headerEndsZ	delimiterZ	headerendZ	linebreakZheadersrG   brZ
headerName
headerTextZ	statusNumr   r   r   outReceived   sT    



 zCGIProcessProtocol.outReceivedc                 C   s   |j jdkr&| jjd| jj|j jd | jrD| jjd| jj| jd | jr| jjd| jj| jd | j	t
tjdd	| j | j  | j  d S )
Nr   z*CGI {uri} exited with exit code {exitCode})r8   exitCodez"Errors from CGI {uri}: {errorText})r8   Z	errorTextz/Premature end of headers in {uri}: {headerText})r8   rp   zCGI Script Errorz Premature end of script headers.)rI   rr   rj   r`   r   r8   r_   rd   re   r]   r   Z	ErrorPager   ZINTERNAL_SERVER_ERRORr   ZunregisterProducerZfinish)r   reasonr   r   r   processEnded2  s2       
zCGIProcessProtocol.processEndedN)r   r   r   rd   Zheaders_writtenre   r_   r   rj   rU   rW   rY   rS   rV   rX   r   r^   ra   rq   rt   r   r   r   r   rJ      s    	:rJ   )rO   rA   r,   r!   r   Ztwisted.loggerr   Ztwisted.pythonr   Ztwisted.spreadr   Ztwisted.webr   r   r   r	   r   r   r
   r   rP   ZProcessProtocolZViewablerJ   r   r   r   r   <module>   s   m+