mò
­fIc           @   sª   d  Z  d Z d g Z d k Z d k Z d k Z d k Z d k Z 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(	   sr  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.

s   0.4t   CGIHTTPRequestHandlerNc           B   sƒ   t  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   selft   is_cgit   run_cgit
   send_error(   R   (    (    t*   /mit/python/lib/python2.4/CGIHTTPServer.pyt   do_POST3   s     c         C   s/   |  i ƒ  o |  i ƒ  Sn t i i |  ƒ Sd S(   s-   Version of send_head that support CGI scriptsN(   R   R   R   t   SimpleHTTPServert   SimpleHTTPRequestHandlert	   send_head(   R   (    (    R   R   ?   s     c         C   s~   |  i } xn |  i D]c } t | ƒ } | |  | j o@ | | p | | d j o# | |  | | d f |  _ t Sq q Wt S(   s±  Test whether self.path corresponds to a CGI script.

        Return a tuple (dir, rest) if self.path requires running a
        CGI script, None if not.  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   N(	   R   t   patht   cgi_directoriest   xt   lent   it   cgi_infot   Truet   False(   R   R   R   R   (    (    R   R   F   s     	
 .s   /cgi-bins   /htbinc         C   s
   t  | ƒ S(   s1   Test whether argument path is an executable file.N(   t
   executableR   (   R   R   (    (    R   t   is_executable_   s     c         C   s(   t  i i | ƒ \ } } | i ƒ  d j S(   s.   Test whether argument path is a Python script.s   .pys   .pywN(   s   .pys   .pyw(   t   osR   t   splitextt   headt   tailt   lower(   R   R   R   R   (    (    R   t	   is_pythonc   s     c   *      C   sÓ	  |  i \ }( }
 |
 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 Sn t i i |& ƒ p |  i d	 d
 | ƒ d Sn |  i | ƒ } | pj |  i p |  i p |  i p |  i d	 d | ƒ d Sn |  i |& ƒ p |  i d	 d | ƒ d Sq‹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 k* } d k+ } | d | d <| d i, ƒ  d j oh y | i- | d ƒ } Wn | i. j
 o qLX| i( d ƒ } t) | ƒ d j o | d | d <qLqPqTn |  i% i/ d j o |  i% i1 | d  <n |  i% i/ | d  <|  i% i& d! ƒ }% |% o |% | d" <n g  } xY |  i% i4 d# ƒ D]E } | d  d$ j o | i6 | i7 ƒ  ƒ qË| | d% i( d& ƒ } qËWd& i8 | ƒ | d' <|  i% i& d( ƒ }# |# o |# | d) <n t: d |  i% i; d* ƒ ƒ } | o d+ i8 | ƒ | d, <n x dB D] }$ | i> |$ d ƒ qŽWt i? i@ | ƒ |  iA d- d. ƒ |	 iB d/ d0 ƒ } |  i o~| g } d1 | j o | i6 | ƒ n tE ƒ  } |  iG iH ƒ  t iI ƒ  } | d j o~ t iK | d ƒ \ } }' x@ tM iM |  iN g g  g  d ƒ d o |  iN iO d ƒ p PqUqUW|' o |  iP d2 |' ƒ n d Sn yv y t iQ | ƒ Wn t iR j
 o n Xt iS |  iN iT ƒ  d ƒ t iS |  iG iT ƒ  d ƒ t iU |& | t i? ƒ WqÏ	|  i iV |  iW |  i$ ƒ t iX d3 ƒ qÏ	Xnn|  i p
 |  i oVd kY } |  i o t iZ } n
 t i\ } |& } |  i |& ƒ oI t^ i_ } | i, ƒ  ia d4 ƒ o | d5  | d6 } n d7 | | f } n d1 |	 j o! d8 |	 j o d9 | |	 f } n |  ib d: | ƒ y tc |% ƒ } Wn te tf f j
 o d } n X| | d; ƒ } | d } | d }! |  i o | d }) n |  i i, ƒ  d< j o0 | d j o# |  iN iO | ƒ } | il | ƒ n xF tM iM |  iN im g g  g  d ƒ d o |  iN im in d ƒ p PqòqòW| io ƒ  | ip |! |  iG ƒ |  i o5 |) iO ƒ  } |) io ƒ  | o |  iP d= | ƒ q”n |! io ƒ  }' |' o |  iP d2 |' ƒ qÏ	|  ib d> ƒ nt^ ir } t^ it } t^ iv } t^ ix }  y¤ t iz ƒ  } z_ |& g t^ _r d1 | j o t^ ir i6 | ƒ n |  iG t^ _v |  iN t^ _t t| |& h  d? d@ <ƒ Wd | t^ _r | t^ _t | t^ _v |  t^ _x t i} | ƒ XWn+ t~ j
 o }' |  iP dA t |' ƒ ƒ n X|  ib d> ƒ d S(C   s   Execute a CGI script.t   ?i    i   t    R   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   t	   AUTH_TYPEt   basict   :t   REMOTE_USERt   CONTENT_TYPEs   content-lengtht   CONTENT_LENGTHt   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   %s -u %st   "s   %s "%s"s   command: %st   bt   posts   %ss   CGI script exited OKt   __name__t   __main__s   CGI script exit status %s(   R)   R*   R2   R6   R8   (   R   R   t   dirt   restt   rfindR   t   queryt   findt   scriptt
   scriptnamet   translate_patht
   scriptfileR   R   t   existsR   t   isfileR   t   ispyt	   have_forkt   have_popen2t   have_popen3R   t   envt   version_stringt   servert   server_namet   protocol_versiont   strt   server_portt   commandt   urllibt   unquotet   uqrestt   address_stringt   hostt   client_addresst   headerst	   getheaderR,   t   splitR   t   base64t   binasciiR   t   decodestringt   Errort
   typeheadert   Nonet   typet   lengthR3   t   getallmatchingheaderst   linet   appendt   stript   joint   uat   filtert
   getheaderst   cot   kt
   setdefaultt   environt   updatet   send_responset   replacet   decoded_queryt   argst
   nobody_uidt   nobodyt   wfilet   flushR   t   pidt   waitpidt   stst   selectt   rfilet   readt	   log_errort   setuidt   errort   dup2t   filenot   execvet   handle_errort   requestt   _exitt   shutilR   t   popenxR   t   cmdlinet   sysR   t   interpt   endswitht   log_messaget   intt   nbytest	   TypeErrort
   ValueErrort   filest   fit   fot   fet   datat   writet   _sockt   recvt   closet   copyfileobjt   errorst   argvt	   save_argvt   stdint
   save_stdint   stdoutt   save_stdoutt   stderrt   save_stderrt   getcwdt   save_cwdt   execfilet   chdirt
   SystemExit(*   R   R¨   Rq   R¤   R~   R3   R   RG   R¦   RD   RB   R   Rx   R¢   RF   Ra   Rb   RP   R,   R•   R˜   R{   R¬   RL   RŽ   R‘   Ry   R\   RZ   R™   Rj   Rœ   Rª   Rš   R   Rn   Rr   Rh   RI   R€   RA   R›   (    (    R   R   h   sX    


  
		 #
	
		


# &



				 				(   R?   t
   __module__t   __doc__t   hasattrR   RM   RN   RO   t   rbufsizeR	   R   R   R   R   R   R   (    (    (    R   R        s    					c          C   s‰   t  o t  Sn y d k }  Wn t j
 o d Sn 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 uidNiÿÿÿÿR{   i   i   c         C   s   |  d S(   Ni   (   R   (   R   (    (    R   t   <lambda>B  s    (   R{   t   pwdt   ImportErrort   getpwnamt   KeyErrort   maxt   mapt   getpwall(   Rµ   (    (    R   Rz   6  s      	'c         C   sA   y t  i |  ƒ } Wn t  i j
 o t Sn X| i d @d j S(   s   Test for executable file.iI   i    N(   R   t   statR   t   stR†   R   t   st_mode(   R   R½   (    (    R   R   F  s     	c         C   s   t  i |  | ƒ d  S(   N(   R
   t   testt   HandlerClasst   ServerClass(   RÀ   RÁ   (    (    R   R¿   O  s    R@   (   R±   t   __version__t   __all__R   R   RX   t   BaseHTTPServerR
   R   R   R    Rf   R{   Rz   R   t
   HTTPServerR¿   R?   (   RÃ   R
   R   RX   R    R   R¿   Rz   RÄ   RÂ   R   R   (    (    R   R      s    							ÿ 			