ó
ú2ec           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z y d d l Z Wn e k
 rc d Z n Xd d l	 Z	 d d l
 m Z d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d d d d „ Z d d d d „ Z d S(   s%   
Application that runs a CGI script.
iÿÿÿÿN(   t   quote(   t
   converterst   CGIErrort   CGIApplicationc           B   s   e  Z d  Z RS(   s`   
    Raised when the CGI script can't be found or doesn't
    act like a proper CGI script.
    (   t   __name__t
   __module__t   __doc__(    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR      s   c           B   s)   e  Z d  Z d e d d „ Z d „  Z RS(   sý   
    This object acts as a proxy to a CGI application.  You pass in the
    script path (``script``), an optional path to search for the
    script (if the name isn't absolute) (``path``).  If you don't give
    a path, then ``$PATH`` will be used.
    c         C   sA  | r t  d ƒ ‚ n  | |  _ | d  k rK t j j d d ƒ j d ƒ } n  | |  _ d | k r | d  k s‚ t d | | f ƒ ‚ | j d d ƒ \ } } n  t j j	 | ƒ | k r"xs |  j D]C } t j j
 t j j | | ƒ ƒ r¿ t j j | | ƒ |  _ Pq¿ q¿ Wt d | |  j f ƒ ‚ n	 | |  _ | |  _ | |  _ d  S(	   Nsj   global_conf is no longer supported for CGIApplication (use make_cgi_application); please pass None insteadt   PATHt    t   :t   ?sN   You cannot have '?' in your script name (%r) and also give a query_string (%r)i   s   Script %r not found in path %r(   t   NotImplementedt   script_filenamet   Nonet   ost   environt   gett   splitt   patht   AssertionErrort   abspatht   existst   joint   scriptR   t   include_os_environt   query_string(   t   selft   global_confR   R   R   R   t   path_dir(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyt   __init__$   s.    	!	!		c   
      C   s  d | k rA t  | j d d ƒ ƒ t  | j d d ƒ ƒ | d <n  |  j r\ t j j ƒ  } n i  } xD | D]< } | j ƒ  | k ri t | | t ƒ ri | | | | <qi qi W|  j	 d  k	 rñ | j d d ƒ } | rÝ | d 7} n  | |  j	 | d <n  |  j | d <t j |  j g d t j d	 t j d
 t j d | d t j j |  j ƒ ƒ} t | | ƒ } t r™t j d k r™t | d t j | ƒ d	 | d
 | d ƒnK | j t j | ƒ j ƒ  ƒ \ } }	 |	 r×| d j |	 ƒ n  | j | ƒ | j s| | j | j ƒ n  g  S(   Nt   REQUEST_URIt   SCRIPT_NAMER   t	   PATH_INFOt   QUERY_STRINGt   &t   SCRIPT_FILENAMEt   stdint   stdoutt   stderrt   envt   cwdt   win32s   wsgi.errors(   R    R   R   R   R   t   copyt   uppert
   isinstancet   strR   R   R   t
   subprocesst   Popent   PIPER   t   dirnamet	   CGIWritert   selectt   syst   platformt   proc_communicatet   StdinReadert   from_environt   communicatet   readt   writet   headers_finishedt   statust   headers(
   R   R   t   start_responset   cgi_environt   namet   oldt   proct   writerR%   R&   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyt   __call__F   sJ     				$	N(   R   R   R   R   t   TrueR   RE   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR      s
   R2   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   sC   | |  _  | |  _ d |  _ g  |  _ t |  _ d  |  _ d |  _ d  S(   Ns   200 OKR   (	   R   R?   R=   R>   t   FalseR<   R   RD   t   buffer(   R   R   R?   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR   t   s    						c         C   sË  |  j  r |  j | ƒ d  S|  j | 7_ x›d |  j k rÆd |  j k r |  j j d ƒ |  j j d ƒ k  r |  j j d d ƒ \ } |  _ n |  j j d d ƒ \ } |  _ | sý t |  _  |  j |  j |  j ƒ |  _ |  j |  j ƒ |  ` |  ` |  ` Pq, d | k rt	 d | ƒ ‚ q, | j d d ƒ \ } } | j
 ƒ  } | j ƒ  } t j rv| j d ƒ } | j d ƒ } n  | j ƒ  d k r­d | k r¡d	 | } n  | |  _ q, |  j j | | f ƒ q, Wd  S(
   Ns   
s   
i   R	   s   Bad header line: %rt   utf8R=   t    s
   %s General(   R<   RD   RH   t   findR   RF   R?   R=   R>   R   t   lstript   stript   sixt   PY3t   decodet   lowert   append(   R   t   datat   line1RA   t   value(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR;   }   s>    	3!		(   R   R   R   R;   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR2   r   s   		R7   c           B   s,   e  Z d  „  Z e d „  ƒ Z d d „ Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   R$   t   content_length(   R   R$   RV   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR   ¤   s    	c         C   s;   | j  d ƒ } | r$ t | ƒ } n d } |  | d | ƒ S(   Nt   CONTENT_LENGTHi    s
   wsgi.input(   R   t   int(   t   clsR   t   length(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR8   ¨   s
    c         C   sh   |  j  s d S| d  k r1 |  j j |  j  ƒ } n |  j j t |  j  | ƒ ƒ } |  j  t | ƒ 8_  | S(   NR   (   RV   R   R$   R:   t   mint   len(   R   t   sizet   text(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR:   ±   s    	N(   R   R   R   t   classmethodR8   R   R:   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR7   ¢   s   		c         C   sõ  g  } g  } d } |  j  o' t t d ƒ } |  j ri |  j j ƒ  t rY | j |  j ƒ q{ |  j j ƒ  n | d k s{ t	 ‚ |  j
 r— | j |  j
 ƒ n | d k s© t	 ‚ |  j rÅ | j |  j ƒ n | d k s× t	 ‚ xÞ| sæ | r·t j | | g  ƒ \ } }	 }
 |  j |	 k r¯| d } } d t | ƒ } | rL| | j | ƒ 7} n  | sr|  j j ƒ  | j |  j ƒ q¯t j |  j j ƒ  | ƒ } | t | ƒ k  r¯| | } q¯n  |  j
 | k r-t j |  j
 j ƒ  d ƒ } | d k r|  j
 j ƒ  | j |  j
 ƒ n  | r|  j | ƒ } n  | j | ƒ n  |  j | k rÚ t j |  j j ƒ  d ƒ } | d k rƒ|  j j ƒ  | j |  j ƒ n  | r›|  j | ƒ } n  | j t j | ƒ ƒ qÚ qÚ Wy |  j ƒ  Wn( t k
 rð} | j d k rñ‚  qñn Xd S(   sf  
    Run the given process, piping input/output/errors to the given
    file-like objects (which need not be actual file objects, unlike
    the arguments passed to Popen).  Wait for process to terminate.

    Note: this is taken from the posix version of
    subprocess.Popen.communicate, but made more general through the
    use of file-like objects.
    R   t   newlinesi   i   i
   N(   t   universal_newlinest   hasattrt   openR$   t   flusht   inputRR   t   closeR   R   R%   R&   R3   R\   R:   t   removeR   R;   t   filenot   _translate_newlinesRN   t   ensure_textt   waitt   OSErrort   errno(   RC   R$   R%   R&   t   read_sett	   write_sett   input_buffert   trans_nlt   rlistt   wlistt   xlistt   nextt   next_lent   bytes_writtenRS   t   e(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyR6   »   sd    
			c      	   C   s[   | d k r- |  j d ƒ p' |  j d ƒ } n  t j | ƒ } t d | d | d | d | ƒS(   s5  
    Paste Deploy interface for :class:`CGIApplication`

    This object acts as a proxy to a CGI application.  You pass in the
    script path (``script``), an optional path to search for the
    script (if the name isn't absolute) (``path``).  If you don't give
    a path, then ``$PATH`` will be used.
    R   R   R   R   N(   R   R   R   t   asboolR   (   R   R   R   R   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyt   make_cgi_application  s    
!(   R   R   R4   R.   t   six.moves.urllib.parseR    R3   t   ImportErrorR   RN   t
   paste.utilR   t   __all__t	   ExceptionR   t   objectR   R2   R7   R6   Rz   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/cgiapp.pyt   <module>   s$   
W0M