Ńň
mČKc        	   @   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
 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   s{   |  i  } xk |  i D]` } t |  } | |  | j o= | | p | | d j o  | |  | | d f |  _ t Sq Wt S(   s  Test whether self.path corresponds to a CGI script,
        and return a boolean.

        This function sets self.cgi_info to a tuple (dir, rest)
        when it returns True, where dir is the directory part before
        the CGI script name.  Note that rest begins with a
        slash if it is not empty.

        The default implementation tests whether the path
        begins with one of the strings in the list
        self.cgi_directories (and the next character is a '/'
        or the end of the string).
        t   /i   (   t   patht   cgi_directoriest   lent   cgi_infot   Truet   False(   R   R   t   xt   i(    (    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_executableb   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_pythonf   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.R   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)   R1   R7   R9   R3   (R   R   R   t   findR   t   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   restR   t   nextdirt   nextrestt	   scriptdirt   queryt   scriptt
   scriptnamet
   scriptfilet   ispyt   envt   uqrestt   hostR+   RX   RY   t   lengthR2   R4   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   k   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   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   (    (   R   (    (    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   0  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   @  s
    c         C   s   t  i |  |  d  S(   N(   R	   t   test(   t   HandlerClasst   ServerClass(    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyRś   I  s    t   __main__(   R¨   t   __version__t   __all__R   R{   RQ   t   BaseHTTPServerR	   Rn   R
   R    R]   R   Rj   R   t
   HTTPServerRś   RŚ   (    (    (    s#   /usr/lib/python2.6/CGIHTTPServer.pyt   <module>   s"   	˙ 			