³ò
˜›\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.5/CGIHTTPServer.pyt   do_POST5   s    c         C   s/   |  i  ƒ  o |  i ƒ  Sn 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.5/CGIHTTPServer.pyR   A   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   (   t   patht   cgi_directoriest   lent   cgi_infot   Truet   False(   R   R   t   xt   i(    (    s#   /usr/lib/python2.5/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.5/CGIHTTPServer.pyt   is_executablea   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.5/CGIHTTPServer.pyt	   is_pythone   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 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 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 g  } xY |  i i& d$ ƒ D]E } | d  d% j o | i' | i( ƒ  ƒ qs| | d& i d' ƒ } qsWd' i) | ƒ | d( <|  i i d) ƒ } | o | | d* <n t* d |  i i+ d+ ƒ ƒ } | o d, i) | ƒ | d- <n x dC D] } | i, | d ƒ q6Wt i- i. | ƒ |  i/ d. d/ ƒ | i0 d0 d1 ƒ } |  i o~|	 g } d2 | 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 d3 | ƒ n d Sn yv y t i: | ƒ Wn t i; j
 o n Xt i< |  i7 i= ƒ  d ƒ t i< |  i2 i= ƒ  d ƒ t i> | | t i- ƒ Wqz
|  i i? |  i@ |  i ƒ t iA d4 ƒ qz
Xnq|  i p
 |  i oYd d kB } |  i o t iC } n
 t iD } | }  |  i | ƒ oI tE iF }! |! i  ƒ  iG d5 ƒ o |! d6  |! d7 }! n d8 |! |  f }  n d2 | j o! d9 | j o d: |  | f }  n |  iH d; |  ƒ y tI | ƒ }" Wn tJ tK f j
 o d }" n X| |  d< ƒ }# |# d }$ |# d }% |  i o |# d }& n |  i i  ƒ  d= j o0 |" d j o# |  i7 i8 |" ƒ }' |$ iL |' ƒ n xF t6 i6 |  i7 iM g g  g  d ƒ d o |  i7 iM iN d ƒ p PqqW|$ iO ƒ  | iP |% |  i2 ƒ |  i o5 |& i8 ƒ  }( |& iO ƒ  |( o |  i9 d> |( ƒ q?	n |% iO ƒ  } | o |  i9 d3 | ƒ qz
|  iH d? ƒ ntE iQ }) tE iR }* tE iS }+ tE iT }, y¤ t iU ƒ  }- z_ | g tE _Q d2 | j o tE iQ i' | ƒ n |  i2 tE _S |  i7 tE _R tV | h  d@ dA <ƒ Wd |) tE _Q |* tE _R |+ tE _S |, tE _T t iW |- ƒ XWn+ tX j
 o } |  i9 dB t | ƒ ƒ n X|  iH d? ƒ d S(D   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   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   __main__t   __name__s   CGI script exit status %s(   R(   R)   R1   R5   R7   (Y   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   shutilR   R   t   sysR   t   endswitht   log_messaget   intt	   TypeErrort
   ValueErrort   writet   _sockt   recvt   closet   copyfileobjt   argvt   stdint   stdoutt   stderrt   getcwdt   execfilet   chdirt
   SystemExit(.   R   R   t   dirt   restR   t   nextdirt   nextrestt	   scriptdirt   queryt   scriptt
   scriptnamet
   scriptfilet   ispyt   envt   uqrestt   hostR+   RW   RX   t   lengthR2   t   linet   uat   cot   kt   decoded_queryt   argst   nobodyt   pidt   stsRy   t   popenxt   cmdlinet   interpt   nbytest   filest   fit   fot   fet   datat   errorst	   save_argvt
   save_stdint   save_stdoutt   save_stderrt   save_cwd(    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyR   j   sr   	 
 


  
		 #

		


# &



								(   R?   t
   __module__t   __doc__t   hasattrR   RF   RG   RH   t   rbufsizeR   R   R   R   R   R   R   (    (    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyR    "   s   					c       	   C   sŒ   t  o t  Sn y d 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 uidiÿÿÿÿNR¡   i   i   c         S   s   |  d  S(   i   (    (   R   (    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyt   <lambda>W  s    (   R¡   t   pwdt   ImportErrort   getpwnamt   KeyErrort   maxt   mapt   getpwall(   R¸   (    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyRi   K  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    (   R   t   statRr   R   t   st_mode(   R   t   st(    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyR   [  s
    	c         C   s   t  i |  | ƒ d  S(   N(   R	   t   test(   t   HandlerClasst   ServerClass(    (    s#   /usr/lib/python2.5/CGIHTTPServer.pyRÂ   d  s    R>   (   R´   t   __version__t   __all__R   Rz   RP   t   BaseHTTPServerR	   Rm   R
   R    R\   R¡   Ri   R   t
   HTTPServerRÂ   R?   (    (    (    s#   /usr/lib/python2.5/CGIHTTPServer.pys   <module>   s"   	ÿ (			