annotate Lib/CGIHTTPServer.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 2148851f082a
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """CGI-savvy HTTP Server.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 This module builds on SimpleHTTPServer by implementing GET and POST
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 requests to cgi-bin scripts.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
6 If the os.fork() function is not present (e.g. on Windows),
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
7 os.popen2() is used as a fallback, with slightly altered semantics; if
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
8 that function is not present either (e.g. on Macintosh), only Python
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
9 scripts are supported, and they are executed by the current process.
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
10
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
11 In all cases, the implementation is intentionally naive -- all
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
12 requests are executed sychronously.
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
13
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
14 SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
15 -- it may execute arbitrary Python code or external programs.
11384
daf88e8edbc0 Based on comments from Paul Prescod:
Fred Drake <fdrake@acm.org>
parents: 10815
diff changeset
16
34226
ec1925f07ec9 SF patch #1055159 via Titus Brown: Document redirect limitation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33322
diff changeset
17 Note that status code 200 is sent prior to execution of a CGI script, so
ec1925f07ec9 SF patch #1055159 via Titus Brown: Document redirect limitation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33322
diff changeset
18 scripts cannot send other status codes such as 302 (redirect).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
22 __version__ = "0.4"
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 14676
diff changeset
24 __all__ = ["CGIHTTPRequestHandler"]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 import os
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
27 import sys
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 import urllib
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 import BaseHTTPServer
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 import SimpleHTTPServer
27169
0bd07391d921 Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents: 25819
diff changeset
31 import select
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 """Complete HTTP server with GET, HEAD and POST commands.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 GET and HEAD also support running CGI scripts.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 The POST command is *only* implemented for CGI scripts.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
44 # Determine platform specifics
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
45 have_fork = hasattr(os, 'fork')
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
46 have_popen2 = hasattr(os, 'popen2')
21861
f5aab8eb8124 Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents: 20680
diff changeset
47 have_popen3 = hasattr(os, 'popen3')
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
48
14332
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
49 # Make rfile unbuffered -- we need to read one line and then pass
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
50 # the rest to a subprocess, so we can't use buffered input.
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
51 rbufsize = 0
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
52
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def do_POST(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
54 """Serve a POST request.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
56 This is only implemented for CGI scripts.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
58 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
60 if self.is_cgi():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
61 self.run_cgi()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
62 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
63 self.send_error(501, "Can only POST to CGI scripts")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def send_head(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
66 """Version of send_head that support CGI scripts"""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
67 if self.is_cgi():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
68 return self.run_cgi()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
69 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
70 return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def is_cgi(self):
47990
01806cdeddde #3345: fix docstring.
Georg Brandl <georg@python.org>
parents: 41082
diff changeset
73 """Test whether self.path corresponds to a CGI script,
01806cdeddde #3345: fix docstring.
Georg Brandl <georg@python.org>
parents: 41082
diff changeset
74 and return a boolean.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
47990
01806cdeddde #3345: fix docstring.
Georg Brandl <georg@python.org>
parents: 41082
diff changeset
76 This function sets self.cgi_info to a tuple (dir, rest)
01806cdeddde #3345: fix docstring.
Georg Brandl <georg@python.org>
parents: 41082
diff changeset
77 when it returns True, where dir is the directory part before
01806cdeddde #3345: fix docstring.
Georg Brandl <georg@python.org>
parents: 41082
diff changeset
78 the CGI script name. Note that rest begins with a
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
79 slash if it is not empty.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
81 The default implementation tests whether the path
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
82 begins with one of the strings in the list
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
83 self.cgi_directories (and the next character is a '/'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
84 or the end of the string).
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
85 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
87 path = self.path
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
89 for x in self.cgi_directories:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
90 i = len(x)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
91 if path[:i] == x and (not path[i:] or path[i] == '/'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
92 self.cgi_info = path[:i], path[i+1:]
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22475
diff changeset
93 return True
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22475
diff changeset
94 return False
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 cgi_directories = ['/cgi-bin', '/htbin']
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
98 def is_executable(self, path):
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
99 """Test whether argument path is an executable file."""
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
100 return executable(path)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
101
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
102 def is_python(self, path):
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
103 """Test whether argument path is a Python script."""
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
104 head, tail = os.path.splitext(path)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
105 return tail.lower() in (".py", ".pyw")
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
106
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 def run_cgi(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
108 """Execute a CGI script."""
40507
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
109 path = self.path
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
110 dir, rest = self.cgi_info
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40507
diff changeset
111
40507
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
112 i = path.find('/', len(dir) + 1)
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
113 while i >= 0:
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
114 nextdir = path[:i]
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
115 nextrest = path[i+1:]
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
116
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
117 scriptdir = self.translate_path(nextdir)
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
118 if os.path.isdir(scriptdir):
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
119 dir, rest = nextdir, nextrest
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
120 i = path.find('/', len(dir) + 1)
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
121 else:
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
122 break
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
123
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
124 # find an explicit query string, if present.
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
125 i = rest.rfind('?')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
126 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
127 rest, query = rest[:i], rest[i+1:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
128 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
129 query = ''
40507
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
130
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
131 # dissect the part after the directory name into a script name &
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
132 # a possible additional path, to be stored in PATH_INFO.
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
133 i = rest.find('/')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
134 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
135 script, rest = rest[:i], rest[i:]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
136 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
137 script, rest = rest, ''
40507
663c9f1ce3aa [Bug #737202; fix from Titus Brown] Make CGIHTTPServer work for scripts in sub-directories
Andrew M. Kuchling <amk@amk.ca>
parents: 34226
diff changeset
138
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
139 scriptname = dir + '/' + script
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
140 scriptfile = self.translate_path(scriptname)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
141 if not os.path.exists(scriptfile):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
142 self.send_error(404, "No such CGI script (%r)" % scriptname)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
143 return
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
144 if not os.path.isfile(scriptfile):
31715
820bb818d698 SF patch 508730 CGIHTTPServer execfile should save cwd
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
145 self.send_error(403, "CGI script is not a plain file (%r)" %
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
146 scriptname)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
147 return
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
148 ispy = self.is_python(scriptname)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
149 if not ispy:
21861
f5aab8eb8124 Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents: 20680
diff changeset
150 if not (self.have_fork or self.have_popen2 or self.have_popen3):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
151 self.send_error(403, "CGI script is not a Python script (%r)" %
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
152 scriptname)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
153 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
154 if not self.is_executable(scriptfile):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
155 self.send_error(403, "CGI script is not executable (%r)" %
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
156 scriptname)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
157 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
158
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
159 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
160 # XXX Much of the following could be prepared ahead of time!
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
161 env = {}
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
162 env['SERVER_SOFTWARE'] = self.version_string()
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
163 env['SERVER_NAME'] = self.server.server_name
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
164 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
165 env['SERVER_PROTOCOL'] = self.protocol_version
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
166 env['SERVER_PORT'] = str(self.server.server_port)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
167 env['REQUEST_METHOD'] = self.command
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
168 uqrest = urllib.unquote(rest)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
169 env['PATH_INFO'] = uqrest
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
170 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
171 env['SCRIPT_NAME'] = scriptname
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
172 if query:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
173 env['QUERY_STRING'] = query
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
174 host = self.address_string()
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
175 if host != self.client_address[0]:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
176 env['REMOTE_HOST'] = host
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
177 env['REMOTE_ADDR'] = self.client_address[0]
33322
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
178 authorization = self.headers.getheader("authorization")
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
179 if authorization:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
180 authorization = authorization.split()
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
181 if len(authorization) == 2:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
182 import base64, binascii
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
183 env['AUTH_TYPE'] = authorization[0]
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
184 if authorization[0].lower() == "basic":
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
185 try:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
186 authorization = base64.decodestring(authorization[1])
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
187 except binascii.Error:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
188 pass
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
189 else:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
190 authorization = authorization.split(':')
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
191 if len(authorization) == 2:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
192 env['REMOTE_USER'] = authorization[0]
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
193 # XXX REMOTE_IDENT
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
194 if self.headers.typeheader is None:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
195 env['CONTENT_TYPE'] = self.headers.type
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
196 else:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
197 env['CONTENT_TYPE'] = self.headers.typeheader
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
198 length = self.headers.getheader('content-length')
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
199 if length:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
200 env['CONTENT_LENGTH'] = length
41082
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
201 referer = self.headers.getheader('referer')
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
202 if referer:
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
203 env['HTTP_REFERER'] = referer
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
204 accept = []
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
205 for line in self.headers.getallmatchingheaders('accept'):
16520
c802385a6ac3 Eliminate use of string.whitespace and a string import with it.
Eric S. Raymond <esr@thyrsus.com>
parents: 16495
diff changeset
206 if line[:1] in "\t\n\r ":
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
207 accept.append(line.strip())
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
208 else:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
209 accept = accept + line[7:].split(',')
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
210 env['HTTP_ACCEPT'] = ','.join(accept)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
211 ua = self.headers.getheader('user-agent')
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
212 if ua:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
213 env['HTTP_USER_AGENT'] = ua
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
214 co = filter(None, self.headers.getheaders('cookie'))
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
215 if co:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
216 env['HTTP_COOKIE'] = ', '.join(co)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
217 # XXX Other HTTP_* headers
31717
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
218 # Since we're setting the env in the parent, provide empty
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
219 # values to override previously set values
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
220 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
41082
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
221 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
31717
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
222 env.setdefault(k, "")
24922
5fc460981589 Typo repair. Please include in any backports.
Guido van Rossum <guido@python.org>
parents: 24920
diff changeset
223 os.environ.update(env)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
224
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
225 self.send_response(200, "Script output follows")
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
226
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
227 decoded_query = query.replace('+', ' ')
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
228
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
229 if self.have_fork:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
230 # Unix -- fork as we should
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
231 args = [script]
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
232 if '=' not in decoded_query:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
233 args.append(decoded_query)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
234 nobody = nobody_uid()
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
235 self.wfile.flush() # Always flush before forking
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
236 pid = os.fork()
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
237 if pid != 0:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
238 # Parent
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
239 pid, sts = os.waitpid(pid, 0)
27169
0bd07391d921 Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents: 25819
diff changeset
240 # throw away additional data [see bug #427345]
0bd07391d921 Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents: 25819
diff changeset
241 while select.select([self.rfile], [], [], 0)[0]:
29666
710c09f4c05a SF bug #430160: CGIHTTPServer.py POST bug using IE
Raymond Hettinger <python@rcn.com>
parents: 27169
diff changeset
242 if not self.rfile.read(1):
710c09f4c05a SF bug #430160: CGIHTTPServer.py POST bug using IE
Raymond Hettinger <python@rcn.com>
parents: 27169
diff changeset
243 break
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
244 if sts:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
245 self.log_error("CGI script exit status %#x", sts)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
246 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
247 # Child
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
248 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
249 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
250 os.setuid(nobody)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
251 except os.error:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
252 pass
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
253 os.dup2(self.rfile.fileno(), 0)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
254 os.dup2(self.wfile.fileno(), 1)
29849
42e8263d39ac SF bug #770601: CGIHTTPServer and environment variables (bug + solution)
Raymond Hettinger <python@rcn.com>
parents: 29666
diff changeset
255 os.execve(scriptfile, args, os.environ)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
256 except:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
257 self.server.handle_error(self.request, self.client_address)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
258 os._exit(127)
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
259
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
260 else:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
261 # Non Unix - use subprocess
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
262 import subprocess
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
263 cmdline = [scriptfile]
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
264 if self.is_python(scriptfile):
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
265 interp = sys.executable
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
266 if interp.lower().endswith("w.exe"):
20680
e3c24fc20a8d Fix two typos, one noted by Noah Spurrier in SF bug #475166, the
Guido van Rossum <guido@python.org>
parents: 20488
diff changeset
267 # On Windows, use python.exe, not pythonw.exe
e3c24fc20a8d Fix two typos, one noted by Noah Spurrier in SF bug #475166, the
Guido van Rossum <guido@python.org>
parents: 20488
diff changeset
268 interp = interp[:-5] + interp[-4:]
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
269 cmdline = [interp, '-u'] + cmdline
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
270 if '=' not in query:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
271 cmdline.append(query)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
272
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
273 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
274 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
275 nbytes = int(length)
25819
c4403a3efa5d Must catch TypeError from int(length).
Guido van Rossum <guido@python.org>
parents: 24922
diff changeset
276 except (TypeError, ValueError):
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
277 nbytes = 0
57188
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
278 p = subprocess.Popen(cmdline,
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
279 stdin = subprocess.PIPE,
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
280 stdout = subprocess.PIPE,
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
281 stderr = subprocess.PIPE
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
282 )
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
283 if self.command.lower() == "post" and nbytes > 0:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
284 data = self.rfile.read(nbytes)
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
285 else:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
286 data = None
27169
0bd07391d921 Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents: 25819
diff changeset
287 # throw away additional data [see bug #427345]
0bd07391d921 Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents: 25819
diff changeset
288 while select.select([self.rfile._sock], [], [], 0)[0]:
29666
710c09f4c05a SF bug #430160: CGIHTTPServer.py POST bug using IE
Raymond Hettinger <python@rcn.com>
parents: 27169
diff changeset
289 if not self.rfile._sock.recv(1):
710c09f4c05a SF bug #430160: CGIHTTPServer.py POST bug using IE
Raymond Hettinger <python@rcn.com>
parents: 27169
diff changeset
290 break
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
291 stdout, stderr = p.communicate(data)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
292 self.wfile.write(stdout)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
293 if stderr:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
294 self.log_error('%s', stderr)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
295 status = p.returncode
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
296 if status:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
297 self.log_error("CGI script exit status %#x", status)
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
298 else:
20488
be67b66e5fed SF patch #467430.
Guido van Rossum <guido@python.org>
parents: 19046
diff changeset
299 self.log_message("CGI script exited OK")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 nobody = None
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 def nobody_uid():
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 """Internal routine to get nobody's uid"""
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 global nobody
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 if nobody:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
308 return nobody
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
309 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
310 import pwd
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
311 except ImportError:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
312 return -1
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
314 nobody = pwd.getpwnam('nobody')[2]
10766
dbca470af8f2 Two changes suggested by Jan Pieter Riegel:
Guido van Rossum <guido@python.org>
parents: 9599
diff changeset
315 except KeyError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
316 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 return nobody
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 def executable(path):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 """Test for executable file."""
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
323 st = os.stat(path)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
325 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
326 return st.st_mode & 0111 != 0
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 def test(HandlerClass = CGIHTTPRequestHandler,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
330 ServerClass = BaseHTTPServer.HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 SimpleHTTPServer.test(HandlerClass, ServerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 test()