| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | """CGI-savvy HTTP Server.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This module builds on SimpleHTTPServer by implementing GET and POST | 
					
						
							|  |  |  | requests to cgi-bin scripts. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											1999-10-16 02:07:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | __version__ = "0.4" | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | __all__ = ["CGIHTTPRequestHandler"] | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | import urllib | 
					
						
							|  |  |  | import BaseHTTPServer | 
					
						
							|  |  |  | import SimpleHTTPServer | 
					
						
							| 
									
										
										
										
											2003-01-08 18:53:18 +00:00
										 |  |  | import select | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |     # Determine platform specifics | 
					
						
							|  |  |  |     have_fork = hasattr(os, 'fork') | 
					
						
							|  |  |  |     have_popen2 = hasattr(os, 'popen2') | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |     have_popen3 = hasattr(os, 'popen3') | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-01 03:27:34 +00:00
										 |  |  |     # Make rfile unbuffered -- we need to read one line and then pass | 
					
						
							|  |  |  |     # the rest to a subprocess, so we can't use buffered input. | 
					
						
							|  |  |  |     rbufsize = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     def do_POST(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Serve a POST request.
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         This is only implemented for CGI scripts. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if self.is_cgi(): | 
					
						
							|  |  |  |             self.run_cgi() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.send_error(501, "Can only POST to CGI scripts") | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def send_head(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Version of send_head that support CGI scripts""" | 
					
						
							|  |  |  |         if self.is_cgi(): | 
					
						
							|  |  |  |             return self.run_cgi() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self) | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def is_cgi(self): | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         """Test whether self.path corresponds to a CGI script.
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         Return a tuple (dir, rest) if self.path requires running a | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         CGI script, None if not.  Note that rest begins with a | 
					
						
							|  |  |  |         slash if it is not empty. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         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). | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         path = self.path | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         for x in self.cgi_directories: | 
					
						
							|  |  |  |             i = len(x) | 
					
						
							|  |  |  |             if path[:i] == x and (not path[i:] or path[i] == '/'): | 
					
						
							|  |  |  |                 self.cgi_info = path[:i], path[i+1:] | 
					
						
							| 
									
										
										
										
											2002-04-04 22:55:58 +00:00
										 |  |  |                 return True | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     cgi_directories = ['/cgi-bin', '/htbin'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |     def is_executable(self, path): | 
					
						
							|  |  |  |         """Test whether argument path is an executable file.""" | 
					
						
							|  |  |  |         return executable(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def is_python(self, path): | 
					
						
							|  |  |  |         """Test whether argument path is a Python script.""" | 
					
						
							|  |  |  |         head, tail = os.path.splitext(path) | 
					
						
							|  |  |  |         return tail.lower() in (".py", ".pyw") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     def run_cgi(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Execute a CGI script.""" | 
					
						
							|  |  |  |         dir, rest = self.cgi_info | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |         i = rest.rfind('?') | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if i >= 0: | 
					
						
							|  |  |  |             rest, query = rest[:i], rest[i+1:] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             query = '' | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |         i = rest.find('/') | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if i >= 0: | 
					
						
							|  |  |  |             script, rest = rest[:i], rest[i:] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             script, rest = rest, '' | 
					
						
							|  |  |  |         scriptname = dir + '/' + script | 
					
						
							|  |  |  |         scriptfile = self.translate_path(scriptname) | 
					
						
							|  |  |  |         if not os.path.exists(scriptfile): | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |             self.send_error(404, "No such CGI script (%r)" % scriptname) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             return | 
					
						
							|  |  |  |         if not os.path.isfile(scriptfile): | 
					
						
							| 
									
										
										
										
											2004-03-20 21:51:12 +00:00
										 |  |  |             self.send_error(403, "CGI script is not a plain file (%r)" % | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |                             scriptname) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         ispy = self.is_python(scriptname) | 
					
						
							|  |  |  |         if not ispy: | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |             if not (self.have_fork or self.have_popen2 or self.have_popen3): | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |                 self.send_error(403, "CGI script is not a Python script (%r)" % | 
					
						
							|  |  |  |                                 scriptname) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 return | 
					
						
							|  |  |  |             if not self.is_executable(scriptfile): | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |                 self.send_error(403, "CGI script is not executable (%r)" % | 
					
						
							|  |  |  |                                 scriptname) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html | 
					
						
							|  |  |  |         # XXX Much of the following could be prepared ahead of time! | 
					
						
							|  |  |  |         env = {} | 
					
						
							|  |  |  |         env['SERVER_SOFTWARE'] = self.version_string() | 
					
						
							|  |  |  |         env['SERVER_NAME'] = self.server.server_name | 
					
						
							|  |  |  |         env['GATEWAY_INTERFACE'] = 'CGI/1.1' | 
					
						
							|  |  |  |         env['SERVER_PROTOCOL'] = self.protocol_version | 
					
						
							|  |  |  |         env['SERVER_PORT'] = str(self.server.server_port) | 
					
						
							|  |  |  |         env['REQUEST_METHOD'] = self.command | 
					
						
							|  |  |  |         uqrest = urllib.unquote(rest) | 
					
						
							|  |  |  |         env['PATH_INFO'] = uqrest | 
					
						
							|  |  |  |         env['PATH_TRANSLATED'] = self.translate_path(uqrest) | 
					
						
							|  |  |  |         env['SCRIPT_NAME'] = scriptname | 
					
						
							|  |  |  |         if query: | 
					
						
							|  |  |  |             env['QUERY_STRING'] = query | 
					
						
							|  |  |  |         host = self.address_string() | 
					
						
							|  |  |  |         if host != self.client_address[0]: | 
					
						
							|  |  |  |             env['REMOTE_HOST'] = host | 
					
						
							|  |  |  |         env['REMOTE_ADDR'] = self.client_address[0] | 
					
						
							| 
									
										
										
										
											2004-08-29 16:53:26 +00:00
										 |  |  |         authorization = self.headers.getheader("authorization") | 
					
						
							|  |  |  |         if authorization: | 
					
						
							|  |  |  |             authorization = authorization.split() | 
					
						
							|  |  |  |             if len(authorization) == 2: | 
					
						
							|  |  |  |                 import base64, binascii | 
					
						
							|  |  |  |                 env['AUTH_TYPE'] = authorization[0] | 
					
						
							|  |  |  |                 if authorization[0].lower() == "basic": | 
					
						
							|  |  |  |                     try: | 
					
						
							|  |  |  |                         authorization = base64.decodestring(authorization[1]) | 
					
						
							|  |  |  |                     except binascii.Error: | 
					
						
							|  |  |  |                         pass | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         authorization = authorization.split(':') | 
					
						
							|  |  |  |                         if len(authorization) == 2: | 
					
						
							|  |  |  |                             env['REMOTE_USER'] = authorization[0] | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         # XXX REMOTE_IDENT | 
					
						
							|  |  |  |         if self.headers.typeheader is None: | 
					
						
							|  |  |  |             env['CONTENT_TYPE'] = self.headers.type | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             env['CONTENT_TYPE'] = self.headers.typeheader | 
					
						
							|  |  |  |         length = self.headers.getheader('content-length') | 
					
						
							|  |  |  |         if length: | 
					
						
							|  |  |  |             env['CONTENT_LENGTH'] = length | 
					
						
							|  |  |  |         accept = [] | 
					
						
							|  |  |  |         for line in self.headers.getallmatchingheaders('accept'): | 
					
						
							| 
									
										
										
										
											2001-02-09 12:10:26 +00:00
										 |  |  |             if line[:1] in "\t\n\r ": | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |                 accept.append(line.strip()) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |                 accept = accept + line[7:].split(',') | 
					
						
							|  |  |  |         env['HTTP_ACCEPT'] = ','.join(accept) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         ua = self.headers.getheader('user-agent') | 
					
						
							|  |  |  |         if ua: | 
					
						
							|  |  |  |             env['HTTP_USER_AGENT'] = ua | 
					
						
							|  |  |  |         co = filter(None, self.headers.getheaders('cookie')) | 
					
						
							|  |  |  |         if co: | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |             env['HTTP_COOKIE'] = ', '.join(co) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |         # XXX Other HTTP_* headers | 
					
						
							| 
									
										
										
										
											2004-03-20 22:18:03 +00:00
										 |  |  |         # Since we're setting the env in the parent, provide empty | 
					
						
							|  |  |  |         # values to override previously set values | 
					
						
							|  |  |  |         for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH', | 
					
						
							|  |  |  |                   'HTTP_USER_AGENT', 'HTTP_COOKIE'): | 
					
						
							|  |  |  |             env.setdefault(k, "") | 
					
						
							| 
									
										
										
										
											2002-08-20 20:07:10 +00:00
										 |  |  |         os.environ.update(env) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.send_response(200, "Script output follows") | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-09 08:56:30 +00:00
										 |  |  |         decoded_query = query.replace('+', ' ') | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if self.have_fork: | 
					
						
							|  |  |  |             # Unix -- fork as we should | 
					
						
							|  |  |  |             args = [script] | 
					
						
							|  |  |  |             if '=' not in decoded_query: | 
					
						
							|  |  |  |                 args.append(decoded_query) | 
					
						
							|  |  |  |             nobody = nobody_uid() | 
					
						
							|  |  |  |             self.wfile.flush() # Always flush before forking | 
					
						
							|  |  |  |             pid = os.fork() | 
					
						
							|  |  |  |             if pid != 0: | 
					
						
							|  |  |  |                 # Parent | 
					
						
							|  |  |  |                 pid, sts = os.waitpid(pid, 0) | 
					
						
							| 
									
										
										
										
											2003-01-08 18:53:18 +00:00
										 |  |  |                 # throw away additional data [see bug #427345] | 
					
						
							|  |  |  |                 while select.select([self.rfile], [], [], 0)[0]: | 
					
						
							| 
									
										
										
										
											2003-06-29 05:06:56 +00:00
										 |  |  |                     if not self.rfile.read(1): | 
					
						
							|  |  |  |                         break | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 if sts: | 
					
						
							|  |  |  |                     self.log_error("CGI script exit status %#x", sts) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             # Child | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     os.setuid(nobody) | 
					
						
							|  |  |  |                 except os.error: | 
					
						
							|  |  |  |                     pass | 
					
						
							|  |  |  |                 os.dup2(self.rfile.fileno(), 0) | 
					
						
							|  |  |  |                 os.dup2(self.wfile.fileno(), 1) | 
					
						
							| 
									
										
										
										
											2003-07-14 06:56:32 +00:00
										 |  |  |                 os.execve(scriptfile, args, os.environ) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             except: | 
					
						
							|  |  |  |                 self.server.handle_error(self.request, self.client_address) | 
					
						
							|  |  |  |                 os._exit(127) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |         elif self.have_popen2 or self.have_popen3: | 
					
						
							|  |  |  |             # Windows -- use popen2 or popen3 to create a subprocess | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             import shutil | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |             if self.have_popen3: | 
					
						
							|  |  |  |                 popenx = os.popen3 | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 popenx = os.popen2 | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             cmdline = scriptfile | 
					
						
							|  |  |  |             if self.is_python(scriptfile): | 
					
						
							|  |  |  |                 interp = sys.executable | 
					
						
							|  |  |  |                 if interp.lower().endswith("w.exe"): | 
					
						
							| 
									
										
										
										
											2001-10-26 03:38:46 +00:00
										 |  |  |                     # On Windows, use python.exe, not pythonw.exe | 
					
						
							|  |  |  |                     interp = interp[:-5] + interp[-4:] | 
					
						
							| 
									
										
										
										
											2001-08-07 19:55:10 +00:00
										 |  |  |                 cmdline = "%s -u %s" % (interp, cmdline) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             if '=' not in query and '"' not in query: | 
					
						
							|  |  |  |                 cmdline = '%s "%s"' % (cmdline, query) | 
					
						
							| 
									
										
										
										
											2001-10-17 06:45:56 +00:00
										 |  |  |             self.log_message("command: %s", cmdline) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 nbytes = int(length) | 
					
						
							| 
									
										
										
										
											2002-10-17 16:21:35 +00:00
										 |  |  |             except (TypeError, ValueError): | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 nbytes = 0 | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |             files = popenx(cmdline, 'b') | 
					
						
							|  |  |  |             fi = files[0] | 
					
						
							|  |  |  |             fo = files[1] | 
					
						
							|  |  |  |             if self.have_popen3: | 
					
						
							|  |  |  |                 fe = files[2] | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             if self.command.lower() == "post" and nbytes > 0: | 
					
						
							|  |  |  |                 data = self.rfile.read(nbytes) | 
					
						
							|  |  |  |                 fi.write(data) | 
					
						
							| 
									
										
										
										
											2003-01-08 18:53:18 +00:00
										 |  |  |             # throw away additional data [see bug #427345] | 
					
						
							|  |  |  |             while select.select([self.rfile._sock], [], [], 0)[0]: | 
					
						
							| 
									
										
										
										
											2003-06-29 05:06:56 +00:00
										 |  |  |                 if not self.rfile._sock.recv(1): | 
					
						
							|  |  |  |                     break | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             fi.close() | 
					
						
							|  |  |  |             shutil.copyfileobj(fo, self.wfile) | 
					
						
							| 
									
										
										
										
											2002-02-01 16:27:59 +00:00
										 |  |  |             if self.have_popen3: | 
					
						
							|  |  |  |                 errors = fe.read() | 
					
						
							|  |  |  |                 fe.close() | 
					
						
							|  |  |  |                 if errors: | 
					
						
							|  |  |  |                     self.log_error('%s', errors) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             sts = fo.close() | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             if sts: | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 self.log_error("CGI script exit status %#x", sts) | 
					
						
							| 
									
										
										
										
											1998-05-13 20:13:24 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2001-10-17 06:45:56 +00:00
										 |  |  |                 self.log_message("CGI script exited OK") | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Other O.S. -- execute script in this process | 
					
						
							|  |  |  |             save_argv = sys.argv | 
					
						
							|  |  |  |             save_stdin = sys.stdin | 
					
						
							|  |  |  |             save_stdout = sys.stdout | 
					
						
							|  |  |  |             save_stderr = sys.stderr | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2004-03-20 21:51:12 +00:00
										 |  |  |                 save_cwd = os.getcwd() | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     sys.argv = [scriptfile] | 
					
						
							|  |  |  |                     if '=' not in decoded_query: | 
					
						
							|  |  |  |                         sys.argv.append(decoded_query) | 
					
						
							|  |  |  |                     sys.stdout = self.wfile | 
					
						
							|  |  |  |                     sys.stdin = self.rfile | 
					
						
							|  |  |  |                     execfile(scriptfile, {"__name__": "__main__"}) | 
					
						
							|  |  |  |                 finally: | 
					
						
							|  |  |  |                     sys.argv = save_argv | 
					
						
							|  |  |  |                     sys.stdin = save_stdin | 
					
						
							|  |  |  |                     sys.stdout = save_stdout | 
					
						
							|  |  |  |                     sys.stderr = save_stderr | 
					
						
							| 
									
										
										
										
											2004-03-20 21:51:12 +00:00
										 |  |  |                     os.chdir(save_cwd) | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |             except SystemExit, sts: | 
					
						
							|  |  |  |                 self.log_error("CGI script exit status %s", str(sts)) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2001-10-17 06:45:56 +00:00
										 |  |  |                 self.log_message("CGI script exited OK") | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | nobody = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def nobody_uid(): | 
					
						
							|  |  |  |     """Internal routine to get nobody's uid""" | 
					
						
							|  |  |  |     global nobody | 
					
						
							|  |  |  |     if nobody: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         return nobody | 
					
						
							| 
									
										
										
										
											2000-09-19 04:01:01 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         import pwd | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         return -1 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         nobody = pwd.getpwnam('nobody')[2] | 
					
						
							| 
									
										
										
										
											1999-04-28 12:21:47 +00:00
										 |  |  |     except KeyError: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         nobody = 1 + max(map(lambda x: x[2], pwd.getpwall())) | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     return nobody | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def executable(path): | 
					
						
							|  |  |  |     """Test for executable file.""" | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         st = os.stat(path) | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     except os.error: | 
					
						
							| 
									
										
										
										
											2002-04-07 06:36:23 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2002-06-01 19:51:15 +00:00
										 |  |  |     return st.st_mode & 0111 != 0 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(HandlerClass = CGIHTTPRequestHandler, | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |          ServerClass = BaseHTTPServer.HTTPServer): | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     SimpleHTTPServer.test(HandlerClass, ServerClass) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test() |