annotate Lib/CGIHTTPServer.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents c67efb8ffca4
children e4fe8fcaef0d
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
65077
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57187
diff changeset
32 import copy
2887
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 """Complete HTTP server with GET, HEAD and POST commands.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 GET and HEAD also support running CGI scripts.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 The POST command is *only* implemented for CGI scripts.
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 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
45 # 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
46 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
47 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
48 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
49
14332
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
50 # 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
51 # 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
52 rbufsize = 0
eb7c27e8fe37 Now that StreamRequestHandler defaults rfile to buffered, make it
Guido van Rossum <guido@python.org>
parents: 11385
diff changeset
53
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 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
55 """Serve a POST request.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
57 This is only implemented for CGI scripts.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
59 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
61 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
62 self.run_cgi()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
63 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
64 self.send_error(501, "Can only POST to CGI scripts")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 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
67 """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
68 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
69 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
70 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
71 return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74 """Test whether self.path corresponds to a CGI script.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
76 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
77 (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
78 Returns False otherwise.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
53706
a67783d812f6 docstring update.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52929
diff changeset
80 If any exception is raised, the caller should assume that
a67783d812f6 docstring update.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52929
diff changeset
81 self.path was rejected as invalid and act accordingly.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
83 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
84 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
85 (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
86 """
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
87 collapsed_path = _url_collapse_path(self.path)
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
88 dir_sep = collapsed_path.find('/', 1)
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
89 head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
76219
89eeaa18700f fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75726
diff changeset
90 if head in self.cgi_directories:
89eeaa18700f fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75726
diff changeset
91 self.cgi_info = head, tail
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
92 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
93 return False
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 cgi_directories = ['/cgi-bin', '/htbin']
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
14676
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
97 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
98 """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
99 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
100
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
101 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
102 """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
103 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
104 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
105
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 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
107 """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
108 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
109 dir, rest = self.cgi_info
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40507
diff changeset
110
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
111 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
112 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
113 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
114 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
115
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 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
117 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
118 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
119 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
120 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
121 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
122
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 # find an explicit query string, if present.
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
124 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
125 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
126 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
127 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
128 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
129
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 # 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
131 # 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
132 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
133 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
134 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
135 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
136 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
137
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
138 scriptname = dir + '/' + script
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
139 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
140 if not os.path.exists(scriptfile):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
141 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
142 return
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
143 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
144 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
145 scriptname)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
146 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
147 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
148 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
149 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
150 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
151 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
152 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
153 if not self.is_executable(scriptfile):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29849
diff changeset
154 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
155 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
156 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
157
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
158 # 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
159 # XXX Much of the following could be prepared ahead of time!
65077
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57187
diff changeset
160 env = copy.deepcopy(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
161 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
162 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
163 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
164 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
165 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
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 if authorization:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
179 authorization = authorization.split()
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
180 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
181 import base64, binascii
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
182 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
183 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
184 try:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
185 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
186 except binascii.Error:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
187 pass
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
188 else:
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
189 authorization = authorization.split(':')
1ba7469a3b49 Patch #727483: Add AUTH_TYPE and REMOTE_USER.
Martin v. Löwis <martin@v.loewis.de>
parents: 31717
diff changeset
190 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
191 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
192 # 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
193 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
194 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
195 else:
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_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
197 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
198 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
199 env['CONTENT_LENGTH'] = length
41082
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
200 referer = self.headers.getheader('referer')
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
201 if referer:
9cfc9cba6768 Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py
Collin Winter <collinw@gmail.com>
parents: 40732
diff changeset
202 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
203 accept = []
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
204 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
205 if line[:1] in "\t\n\r ":
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
206 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
207 else:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
208 accept = accept + line[7:].split(',')
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
209 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
210 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
211 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
212 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
213 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
214 if co:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
215 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
216 # XXX Other HTTP_* headers
31717
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
217 # 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
218 # values to override previously set values
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
219 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
220 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
31717
fd26f29e7eab Fix for SF 777848.
Guido van Rossum <guido@python.org>
parents: 31715
diff changeset
221 env.setdefault(k, "")
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
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
223 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
224
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
225 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
226
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
227 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
228 # 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 # Parent
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
237 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
238 # 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
239 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
240 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
241 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
242 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
243 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
244 return
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
245 # Child
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
246 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
247 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
248 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
249 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
250 pass
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
251 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
252 os.dup2(self.wfile.fileno(), 1)
65077
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57187
diff changeset
253 os.execve(scriptfile, args, 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
254 except:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
255 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
256 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
257
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
258 else:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
259 # Non Unix - use subprocess
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
260 import subprocess
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
261 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
262 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
263 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
264 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
265 # 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
266 interp = interp[:-5] + interp[-4:]
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
267 cmdline = [interp, '-u'] + cmdline
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
268 if '=' not in query:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
269 cmdline.append(query)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
270
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
271 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
272 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
273 nbytes = int(length)
25819
c4403a3efa5d Must catch TypeError from int(length).
Guido van Rossum <guido@python.org>
parents: 24922
diff changeset
274 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
275 nbytes = 0
57188
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
276 p = subprocess.Popen(cmdline,
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
277 stdin = subprocess.PIPE,
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
278 stdout = subprocess.PIPE,
65077
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57187
diff changeset
279 stderr = subprocess.PIPE,
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57187
diff changeset
280 env = env
57188
2148851f082a Merged revisions 76212 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57184
diff changeset
281 )
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)
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
284 else:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
285 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
286 # 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
287 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
288 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
289 break
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
290 stdout, stderr = p.communicate(data)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
291 self.wfile.write(stdout)
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
292 if stderr:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
293 self.log_error('%s', stderr)
65967
62ff11644339 Merged revisions 86195 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65077
diff changeset
294 p.stderr.close()
62ff11644339 Merged revisions 86195 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65077
diff changeset
295 p.stdout.close()
57184
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
296 status = p.returncode
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
297 if status:
2950d9771395 Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 47990
diff changeset
298 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
299 else:
20488
be67b66e5fed SF patch #467430.
Guido van Rossum <guido@python.org>
parents: 19046
diff changeset
300 self.log_message("CGI script exited OK")
2887
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
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
303 def _url_collapse_path(path):
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
304 """
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
305 Given a URL path, remove extra '/'s and '.' path elements and collapse
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
306 any '..' references and returns a colllapsed path.
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
307
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
308 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
309 The utility of this function is limited to is_cgi method and helps
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
310 preventing some security attacks.
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
311
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
312 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
313 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
314 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
315
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
316 Raises: IndexError if too many '..' occur within the path.
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
317
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
318 """
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
319 # 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
320 # path semantics rather than local operating system semantics.
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
321 path_parts = path.split('/')
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
322 head_parts = []
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
323 for part in path_parts[:-1]:
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
324 if part == '..':
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
325 head_parts.pop() # IndexError if more '..' than prior parts
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
326 elif part and part != '.':
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
327 head_parts.append( part )
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
328 if path_parts:
76219
89eeaa18700f fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75726
diff changeset
329 tail_part = path_parts.pop()
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
330 if tail_part:
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
331 if tail_part == '..':
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
332 head_parts.pop()
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
333 tail_part = ''
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
334 elif tail_part == '.':
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
335 tail_part = ''
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
336 else:
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
337 tail_part = ''
76247
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
338
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
339 splitpath = ('/' + '/'.join(head_parts), tail_part)
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
340 collapsed_path = "/".join(splitpath)
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
341
c67efb8ffca4 Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76219
diff changeset
342 return collapsed_path
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
343
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47990
diff changeset
344
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 nobody = None
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 def nobody_uid():
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 """Internal routine to get nobody's uid"""
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 global nobody
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 if nobody:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
351 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
352 try:
3c58716fafde An honest attempt to make this work on Unix, Windows, and even
Guido van Rossum <guido@python.org>
parents: 14332
diff changeset
353 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
354 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
355 return -1
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
357 nobody = pwd.getpwnam('nobody')[2]
10766
dbca470af8f2 Two changes suggested by Jan Pieter Riegel:
Guido van Rossum <guido@python.org>
parents: 9599
diff changeset
358 except KeyError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
359 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 return nobody
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 def executable(path):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 """Test for executable file."""
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
366 st = os.stat(path)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 except os.error:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
368 return False
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
369 return st.st_mode & 0111 != 0
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 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
373 ServerClass = BaseHTTPServer.HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 SimpleHTTPServer.test(HandlerClass, ServerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 test()