Mercurial > cpython-lru_cache-weakref
annotate Lib/CGIHTTPServer.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | a67783d812f6 |
| children | 54b8d780c9b9 |
| rev | line source |
|---|---|
| 2887 | 1 """CGI-savvy HTTP Server. |
| 2 | |
| 3 This module builds on SimpleHTTPServer by implementing GET and POST | |
| 4 requests to cgi-bin scripts. | |
| 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 | 19 """ |
| 20 | |
| 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 | 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 | 25 |
| 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 | 28 import urllib |
| 29 import BaseHTTPServer | |
| 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 | 32 |
| 33 | |
| 34 class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
| 35 | |
| 36 """Complete HTTP server with GET, HEAD and POST commands. | |
| 37 | |
| 38 GET and HEAD also support running CGI scripts. | |
| 39 | |
| 40 The POST command is *only* implemented for CGI scripts. | |
| 41 | |
| 42 """ | |
| 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 | 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 | 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 | 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 | 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 | 64 |
| 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 | 71 |
| 72 def is_cgi(self): | |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
73 """Test whether self.path corresponds to a CGI script. |
| 2887 | 74 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
75 Returns True and updates the cgi_info attribute to the tuple |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
76 (dir, rest) if self.path requires running a CGI script. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
77 Returns False otherwise. |
| 2887 | 78 |
|
53706
a67783d812f6
docstring update.
Gregory P. Smith <greg@mad-scientist.com>
parents:
52929
diff
changeset
|
79 If any exception is raised, the caller should assume that |
|
a67783d812f6
docstring update.
Gregory P. Smith <greg@mad-scientist.com>
parents:
52929
diff
changeset
|
80 self.path was rejected as invalid and act accordingly. |
|
a67783d812f6
docstring update.
Gregory P. Smith <greg@mad-scientist.com>
parents:
52929
diff
changeset
|
81 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
82 The default implementation tests whether the normalized url |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
83 path begins with one of the strings in self.cgi_directories |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
84 (and the next character is a '/' or the end of the string). |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
85 """ |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
86 splitpath = _url_collapse_path_split(self.path) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
87 if splitpath[0] in self.cgi_directories: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
88 self.cgi_info = splitpath |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
89 return True |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22475
diff
changeset
|
90 return False |
| 2887 | 91 |
| 92 cgi_directories = ['/cgi-bin', '/htbin'] | |
| 93 | |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
94 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
|
95 """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
|
96 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
|
97 |
|
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_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
|
99 """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
|
100 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
|
101 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
|
102 |
| 2887 | 103 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
|
104 """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
|
105 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
|
106 dir, rest = self.cgi_info |
|
40732
5dae0d7f683a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40507
diff
changeset
|
107 |
|
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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 |
|
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 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 |
|
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 # find an explicit query string, if present. |
|
16495
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
121 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
|
122 if i >= 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
123 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
|
124 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
125 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
|
126 |
|
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
|
127 # 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
|
128 # 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
|
129 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
|
130 if i >= 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
131 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
|
132 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
133 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
|
134 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
135 scriptname = dir + '/' + script |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
136 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
|
137 if not os.path.exists(scriptfile): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29849
diff
changeset
|
138 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
|
139 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
140 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
|
141 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
|
142 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 |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 return |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
150 if not self.is_executable(scriptfile): |
|
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 executable (%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 |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
155 # 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
|
156 # 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
|
157 env = {} |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 if authorization: |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
176 authorization = authorization.split() |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
177 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
|
178 import base64, binascii |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
179 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
|
180 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
|
181 try: |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
182 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
|
183 except binascii.Error: |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
184 pass |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
185 else: |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
186 authorization = authorization.split(':') |
|
1ba7469a3b49
Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents:
31717
diff
changeset
|
187 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
|
188 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
|
189 # 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
|
190 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
|
191 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
|
192 else: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 env['CONTENT_LENGTH'] = length |
|
41082
9cfc9cba6768
Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents:
40732
diff
changeset
|
197 referer = self.headers.getheader('referer') |
|
9cfc9cba6768
Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents:
40732
diff
changeset
|
198 if referer: |
|
9cfc9cba6768
Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents:
40732
diff
changeset
|
199 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
|
200 accept = [] |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
201 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
|
202 if line[:1] in "\t\n\r ": |
|
16495
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
203 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
|
204 else: |
|
16495
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
205 accept = accept + line[7:].split(',') |
|
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 if co: |
|
16495
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
212 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
|
213 # XXX Other HTTP_* headers |
| 31717 | 214 # Since we're setting the env in the parent, provide empty |
| 215 # values to override previously set values | |
| 216 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
|
217 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'): |
| 31717 | 218 env.setdefault(k, "") |
|
24922
5fc460981589
Typo repair. Please include in any backports.
Guido van Rossum <guido@python.org>
parents:
24920
diff
changeset
|
219 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
|
220 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
221 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
|
222 |
|
16495
69f162dcd8b4
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
223 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
|
224 |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
225 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
|
226 # 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 # Parent |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
235 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
|
236 # 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 return |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
243 # Child |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
244 try: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
245 try: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
246 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
|
247 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
|
248 pass |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 except: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
253 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
|
254 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
|
255 |
|
21861
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
256 elif self.have_popen2 or self.have_popen3: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
257 # Windows -- use popen2 or popen3 to create a subprocess |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
258 import shutil |
|
21861
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
259 if self.have_popen3: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
260 popenx = os.popen3 |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
261 else: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
262 popenx = os.popen2 |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
263 cmdline = scriptfile |
|
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:] |
|
19046
7db5b5423ccf
Apply two small changes to the Windows code, according to SF bug
Guido van Rossum <guido@python.org>
parents:
16520
diff
changeset
|
269 cmdline = "%s -u %s" % (interp, 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
|
270 if '=' not in query and '"' not in query: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
271 cmdline = '%s "%s"' % (cmdline, query) |
| 20488 | 272 self.log_message("command: %s", 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
|
273 try: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
274 nbytes = int(length) |
|
25819
c4403a3efa5d
Must catch TypeError from int(length).
Guido van Rossum <guido@python.org>
parents:
24922
diff
changeset
|
275 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
|
276 nbytes = 0 |
|
21861
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
277 files = popenx(cmdline, 'b') |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
278 fi = files[0] |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
279 fo = files[1] |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
280 if self.have_popen3: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
281 fe = files[2] |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
282 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
|
283 data = self.rfile.read(nbytes) |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
284 fi.write(data) |
|
27169
0bd07391d921
Fix bug 427345 [related to IE's additional input on POST request].
Steve Holden <steve@holdenweb.com>
parents:
25819
diff
changeset
|
285 # 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
|
286 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
|
287 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
|
288 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
|
289 fi.close() |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
290 shutil.copyfileobj(fo, self.wfile) |
|
21861
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
291 if self.have_popen3: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
292 errors = fe.read() |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
293 fe.close() |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
294 if errors: |
|
f5aab8eb8124
Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32
Guido van Rossum <guido@python.org>
parents:
20680
diff
changeset
|
295 self.log_error('%s', errors) |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
296 sts = fo.close() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
297 if sts: |
|
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 self.log_error("CGI script exit status %#x", sts) |
|
8321
9ddd679319a9
From: conrad@cgl.ucsf.edu (Conrad Huang %CGL)
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
299 else: |
| 20488 | 300 self.log_message("CGI script exited OK") |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
301 |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
302 else: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
303 # Other O.S. -- execute script in this process |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
304 save_argv = sys.argv |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
305 save_stdin = sys.stdin |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
306 save_stdout = sys.stdout |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
307 save_stderr = sys.stderr |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
308 try: |
|
31715
820bb818d698
SF patch 508730 CGIHTTPServer execfile should save cwd
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
309 save_cwd = os.getcwd() |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
310 try: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
311 sys.argv = [scriptfile] |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
312 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
|
313 sys.argv.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
|
314 sys.stdout = self.wfile |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
315 sys.stdin = self.rfile |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
316 execfile(scriptfile, {"__name__": "__main__"}) |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
317 finally: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
318 sys.argv = save_argv |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
319 sys.stdin = save_stdin |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
320 sys.stdout = save_stdout |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
321 sys.stderr = save_stderr |
|
31715
820bb818d698
SF patch 508730 CGIHTTPServer execfile should save cwd
Tim Peters <tim.peters@gmail.com>
parents:
31515
diff
changeset
|
322 os.chdir(save_cwd) |
|
14676
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
323 except SystemExit, sts: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
324 self.log_error("CGI script exit status %s", str(sts)) |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
325 else: |
| 20488 | 326 self.log_message("CGI script exited OK") |
| 2887 | 327 |
| 328 | |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
329 # TODO(gregory.p.smith): Move this into an appropriate library. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
330 def _url_collapse_path_split(path): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
331 """ |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
332 Given a URL path, remove extra '/'s and '.' path elements and collapse |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
333 any '..' references. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
334 |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
335 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
336 |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
337 Returns: A tuple of (head, tail) where tail is everything after the final / |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
338 and head is everything before it. Head will always start with a '/' and, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
339 if it contains anything else, never have a trailing '/'. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
340 |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
341 Raises: IndexError if too many '..' occur within the path. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
342 """ |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
343 # Similar to os.path.split(os.path.normpath(path)) but specific to URL |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
344 # path semantics rather than local operating system semantics. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
345 path_parts = [] |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
346 for part in path.split('/'): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
347 if part == '.': |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
348 path_parts.append('') |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
349 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
350 path_parts.append(part) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
351 # Filter out blank non trailing parts before consuming the '..'. |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
352 path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:] |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
353 if path_parts: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
354 tail_part = path_parts.pop() |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
355 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
356 tail_part = '' |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
357 head_parts = [] |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
358 for part in path_parts: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
359 if part == '..': |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
360 head_parts.pop() |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
361 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
362 head_parts.append(part) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
363 if tail_part and tail_part == '..': |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
364 head_parts.pop() |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
365 tail_part = '' |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
366 return ('/' + '/'.join(head_parts), tail_part) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
367 |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47990
diff
changeset
|
368 |
| 2887 | 369 nobody = None |
| 370 | |
| 371 def nobody_uid(): | |
| 372 """Internal routine to get nobody's uid""" | |
| 373 global nobody | |
| 374 if nobody: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
375 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
|
376 try: |
|
3c58716fafde
An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents:
14332
diff
changeset
|
377 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
|
378 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
|
379 return -1 |
| 2887 | 380 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
381 nobody = pwd.getpwnam('nobody')[2] |
|
10766
dbca470af8f2
Two changes suggested by Jan Pieter Riegel:
Guido van Rossum <guido@python.org>
parents:
9599
diff
changeset
|
382 except KeyError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
383 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall())) |
| 2887 | 384 return nobody |
| 385 | |
| 386 | |
| 387 def executable(path): | |
| 388 """Test for executable file.""" | |
| 389 try: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6941
diff
changeset
|
390 st = os.stat(path) |
| 2887 | 391 except os.error: |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
392 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
22867
diff
changeset
|
393 return st.st_mode & 0111 != 0 |
| 2887 | 394 |
| 395 | |
| 396 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
|
397 ServerClass = BaseHTTPServer.HTTPServer): |
| 2887 | 398 SimpleHTTPServer.test(HandlerClass, ServerClass) |
| 399 | |
| 400 | |
| 401 if __name__ == '__main__': | |
| 402 test() |
