Ńň
#8Zc        	   @   sĹ   d  Z  d Z d g Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d e i	 f d     YZ
 d   Z e a d   Z d   Z e
 e i d	  Z e d
 j o e   n d S(   sú  CGI-savvy HTTP Server.

This module builds on SimpleHTTPServer by implementing GET and POST
requests to cgi-bin scripts.

If the os.fork() function is not present (e.g. on Windows),
os.popen2() is used as a fallback, with slightly altered semantics; if
that function is not present either (e.g. on Macintosh), only Python
scripts are supported, and they are executed by the current process.

In all cases, the implementation is intentionally naive -- all
requests are executed sychronously.

SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
-- it may execute arbitrary Python code or external programs.

Note that status code 200 is sent prior to execution of a CGI script, so
scripts cannot send other status codes such as 302 (redirect).
s   0.4t   CGIHTTPRequestHandleri˙˙˙˙Nc           B   s   e  Z d  Z e e d  Z e e d  Z e e d  Z d Z d   Z	 d   Z
 d   Z d d	 g Z d
   Z d   Z d   Z RS(   s­   Complete HTTP server with GET, HEAD and POST commands.

    GET and HEAD also support running CGI scripts.

    The POST command is *only* implemented for CGI scripts.

    t   forkt   popen2t   popen3i    c         C   s/   |  i    o |  i   n |  i d d  d S(   sR   Serve a POST request.

        This is only implemented for CGI scripts.

        iő  s   Can only POST to CGI scriptsN(   t   is_cgit   run_cgit
   send_error(   t   self(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt   do_POST5   s    c         C   s,   |  i    o |  i   St i i |   Sd S(   s-   Version of send_head that support CGI scriptsN(   R   R   t   SimpleHTTPServert   SimpleHTTPRequestHandlert	   send_head(   R   (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR   A   s    c         C   s5   t  |  i  } | d |  i j o | |  _ t St S(   s°  Test whether self.path corresponds to a CGI script.

        Returns True and updates the cgi_info attribute to the tuple
        (dir, rest) if self.path requires running a CGI script.
        Returns False otherwise.

        The default implementation tests whether the normalized url
        path begins with one of the strings in self.cgi_directories
        (and the next character is a '/' or the end of the string).
        i    (   t   _url_collapse_path_splitt   patht   cgi_directoriest   cgi_infot   Truet   False(   R   t	   splitpath(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR   H   s
    	s   /cgi-bins   /htbinc         C   s
   t  |  S(   s1   Test whether argument path is an executable file.(   t
   executable(   R   R   (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt   is_executable[   s    c         C   s(   t  i i |  \ } } | i   d j S(   s.   Test whether argument path is a Python script.s   .pys   .pyw(   s   .pys   .pyw(   t   osR   t   splitextt   lower(   R   R   t   headt   tail(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt	   is_python_   s    c   (      C   s 	  |  i  } |  i \ } } | i d t |  d  } xz | d j ol | |  } | | d } |  i |  } t i  i |  o- | | } } | i d t |  d  } q7 Pq7 W| i d  } | d j o | |  | | d } } n d } | i d  } | d j o | |  | | }	 } n | d }	 } | d |	 }
 |  i |
  } t i  i |  p |  i	 d d |
  d St i  i
 |  p |  i	 d	 d
 |
  d S|  i |
  } | pd |  i p |  i p |  i p |  i	 d	 d |
  d S|  i |  p |  i	 d	 d |
  d Sn h  } |  i   | d <|  i i | d <d | d <|  i | d <t |  i i  | d <|  i | d <t i |  } | | d <|  i |  | d <|
 | d <| o | | d <n |  i   } | |  i d j o | | d <n |  i d | d <|  i i d  } | oĚ | i   } t |  d j oŠ d d k } d d k } | d | d <| d i    d j oh y | i! | d  } Wn | i" j
 o qčX| i d  } t |  d j o | d | d  <qčqěqđn |  i i# d j o |  i i% | d! <n |  i i# | d! <|  i i d"  } | o | | d# <n |  i i d$  } | o | | d% <n g  } xY |  i i& d&  D]E } | d  d' j o | i' | i(    q| | d( i d)  } qWd) i) |  | d* <|  i i d+  } | o | | d, <n t* d |  i i+ d-   } | o d. i) |  | d/ <n x dB D] } | i, | d  qQWt i- i. |  |  i/ d0 d1  | i0 d2 d3  } |  i o{|	 g } d4 | j o | i' |  n t1   } |  i2 i3   t i4   } | d j o{ t i5 | d  \ } } x@ t6 i6 |  i7 g g  g  d  d o |  i7 i8 d  p PqqW| o |  i9 d5 |  n d Syv y t i: |  Wn t i; j
 o n Xt i< |  i7 i=   d  t i< |  i2 i=   d  t i> | | t i-  Wq	|  i i? |  i@ |  i  t iA d6  q	Xnűd d kB } | g }  |  i |  oI tC iD }! |! i    iE d7  o |! d8  |! d9 }! n |! d: g |  }  n d4 | j o |  i' |  n |  iF d; | iG |    y tH |  }" Wn tI tJ f j
 o d }" n X| iK |  d< | iL d= | iL d> | iL }# |  i i    d? j o# |" d j o |  i7 i8 |"  }$ n d }$ xF t6 i6 |  i7 iM g g  g  d  d o |  i7 iM iN d  p PqeqeW|# iO |$  \ }% }& |  i2 iP |%  |& o |  i9 d@ |&  n |# iQ }' |' o |  i9 d5 |'  n |  iF dA  d S(C   s   Execute a CGI script.t   /i   i    t   ?t    i  s   No such CGI script (%r)Ni  s#   CGI script is not a plain file (%r)s&   CGI script is not a Python script (%r)s!   CGI script is not executable (%r)t   SERVER_SOFTWAREt   SERVER_NAMEs   CGI/1.1t   GATEWAY_INTERFACEt   SERVER_PROTOCOLt   SERVER_PORTt   REQUEST_METHODt	   PATH_INFOt   PATH_TRANSLATEDt   SCRIPT_NAMEt   QUERY_STRINGt   REMOTE_HOSTt   REMOTE_ADDRt   authorizationi   i˙˙˙˙t	   AUTH_TYPEt   basict   :t   REMOTE_USERt   CONTENT_TYPEs   content-lengtht   CONTENT_LENGTHt   referert   HTTP_REFERERt   accepts   	
 i   t   ,t   HTTP_ACCEPTs
   user-agentt   HTTP_USER_AGENTt   cookies   , t   HTTP_COOKIEiČ   s   Script output followst   +t    t   =s   CGI script exit status %#xi   s   w.exeiű˙˙˙iü˙˙˙s   -us   command: %st   stdint   stdoutt   stderrt   posts   %ss   CGI script exited OK(   R'   R(   R0   R6   R8   R2   (R   R   R   t   findt   lent   translate_pathR   t   isdirt   rfindt   existsR   t   isfileR   t	   have_forkt   have_popen2t   have_popen3R   t   version_stringt   servert   server_namet   protocol_versiont   strt   server_portt   commandt   urllibt   unquotet   address_stringt   client_addresst   headerst	   getheadert   splitt   base64t   binasciiR   t   decodestringt   Errort
   typeheadert   Nonet   typet   getallmatchingheaderst   appendt   stript   joint   filtert
   getheaderst
   setdefaultt   environt   updatet   send_responset   replacet
   nobody_uidt   wfilet   flushR   t   waitpidt   selectt   rfilet   readt	   log_errort   setuidt   errort   dup2t   filenot   execvet   handle_errort   requestt   _exitt
   subprocesst   sysR   t   endswitht   log_messaget   list2cmdlinet   intt	   TypeErrort
   ValueErrort   Popent   PIPEt   _sockt   recvt   communicatet   writet
   returncode((   R   R   t   dirt   restt   it   nextdirt   nextrestt	   scriptdirt   queryt   scriptt
   scriptnamet
   scriptfilet   ispyt   envt   uqrestt   hostR*   RX   RY   t   lengthR1   R3   t   linet   uat   cot   kt   decoded_queryt   argst   nobodyt   pidt   stsRz   t   cmdlinet   interpt   nbytest   pt   dataR=   R>   t   status(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR   d   s>   	 
 	


  
		 #
				# &
	(   t   __name__t
   __module__t   __doc__t   hasattrR   RG   RH   RI   t   rbufsizeR   R   R   R   R   R   R   (    (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR    "   s   					c         C   s  g  } xB |  i  d  D]1 } | d j o | i d  q | i |  q Wg  } | d  D] } | o | | qZ qZ ~ | d } | o | i   } n d } g  } x6 | D]. } | d j o | i   q­ | i |  q­ W| o! | d j o | i   d } n d d i |  | f S(   sÔ  
    Given a URL path, remove extra '/'s and '.' path elements and collapse
    any '..' references.

    Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.

    Returns: A tuple of (head, tail) where tail is everything after the final /
    and head is everything before it.  Head will always start with a '/' and,
    if it contains anything else, never have a trailing '/'.

    Raises: IndexError if too many '..' occur within the path.
    R   t   .R   i˙˙˙˙s   ..(   RW   R`   t   popRb   (   R   t
   path_partst   partt   _[1]t	   tail_partt
   head_parts(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR   (  s(     8 

c       	   C   s   t  o t  Sy d d k }  Wn t j
 o d SXy |  i d  d a  Wn5 t j
 o) d t t d   |  i     a  n Xt  S(   s$   Internal routine to get nobody's uidi˙˙˙˙NR   i   i   c         S   s   |  d  S(   i   (    (   t   x(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt   <lambda>]  s    (   R   t   pwdt   ImportErrort   getpwnamt   KeyErrort   maxt   mapt   getpwall(   Rľ   (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyRj   Q  s    'c         C   s>   y t  i |   } Wn t  i j
 o t SX| i d @d j S(   s   Test for executable file.iI   i    (   R   t   statRs   R   t   st_mode(   R   t   st(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyR   a  s
    c         C   s   t  i |  |  d  S(   N(   R	   t   test(   t   HandlerClasst   ServerClass(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyRż   j  s    t   __main__(   RŠ   t   __version__t   __all__R   R{   RQ   t   BaseHTTPServerR	   Rn   R
   R    R   R]   R   Rj   R   t
   HTTPServerRż   R§   (    (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt   <module>   s$   	˙ 	'			