Ñò
à"Ic        	   @   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*   /mit/python/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*   /mit/python/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*   /mit/python/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*   /mit/python/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*   /mit/python/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 dE 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
Xno|  i p
 |  i oYd d kB } |  i o t iC }  n
 t iD }  | }! |  i | ƒ oI tE iF }" |" i  ƒ  iG d7 ƒ o |" d8  |" d9 }" n d: |" |! f }! n d4 | j o! d; | 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@ |) ƒ qW	n |& iO ƒ  } | o |  i9 d5 | ƒ q
|  iH dA ƒ ntE iQ }* tE iR }+ tE iS }, tE iT }- y¢ t iU ƒ  }. z] | g tE _Q d4 | j o tE iQ i' | ƒ n |  i2 tE _S |  i7 tE _R tV | h dB dC 6ƒ Wd |* tE _Q |+ tE _R |, tE _S |- tE _T t iW |. ƒ XWn+ tX j
 o } |  i9 dD t | ƒ ƒ n X|  iH dA ƒ d S(F   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   %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   R7   R9   R3   (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+   RY   RZ   t   lengthR2   R4   t   linet   uat   cot   kt   decoded_queryt   argst   nobodyt   pidt   stsR{   t   popenxt   cmdlinet   interpt   nbytest   filest   fit   fot   fet   datat   errorst	   save_argvt
   save_stdint   save_stdoutt   save_stderrt   save_cwd(    (    s*   /mit/python/lib/python2.6/CGIHTTPServer.pyR   k   sx   	 
 	


  
		 #

		


# &



								(   RA   t
   __module__t   __doc__t   hasattrR   RH   RI   RJ   t   rbufsizeR   R   R   R   R   R   R   (    (    (    s*   /mit/python/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*   /mit/python/lib/python2.6/CGIHTTPServer.pyt   <lambda>[  s    (   R£   t   pwdt   ImportErrort   getpwnamt   KeyErrort   maxt   mapt   getpwall(   Rº   (    (    s*   /mit/python/lib/python2.6/CGIHTTPServer.pyRk   O  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   statRt   R   t   st_mode(   R   t   st(    (    s*   /mit/python/lib/python2.6/CGIHTTPServer.pyR   _  s
    c         C   s   t  i |  | ƒ d  S(   N(   R	   t   test(   t   HandlerClasst   ServerClass(    (    s*   /mit/python/lib/python2.6/CGIHTTPServer.pyRÄ   h  s    R@   (   R¶   t   __version__t   __all__R   R|   RR   t   BaseHTTPServerR	   Ro   R
   R    R^   R£   Rk   R   t
   HTTPServerRÄ   RA   (    (    (    s*   /mit/python/lib/python2.6/CGIHTTPServer.pyt   <module>   s"   	ÿ ,			