Kaydet (Commit) 8cb65406 authored tarafından Guido van Rossum's avatar Guido van Rossum

Wesley Chun's SF patch 511380: add CGIHTTPServer error supt for Win32

This uses os.popen3 (if it exists) to ensure that errors from a
non-Python CGI script are logged.

Bugfix candidate.
üst d4c76bf6
...@@ -41,6 +41,7 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -41,6 +41,7 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
# Determine platform specifics # Determine platform specifics
have_fork = hasattr(os, 'fork') have_fork = hasattr(os, 'fork')
have_popen2 = hasattr(os, 'popen2') have_popen2 = hasattr(os, 'popen2')
have_popen3 = hasattr(os, 'popen3')
# Make rfile unbuffered -- we need to read one line and then pass # Make rfile unbuffered -- we need to read one line and then pass
# the rest to a subprocess, so we can't use buffered input. # the rest to a subprocess, so we can't use buffered input.
...@@ -123,7 +124,7 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -123,7 +124,7 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
return return
ispy = self.is_python(scriptname) ispy = self.is_python(scriptname)
if not ispy: if not ispy:
if not (self.have_fork or self.have_popen2): if not (self.have_fork or self.have_popen2 or self.have_popen3):
self.send_error(403, "CGI script is not a Python script (%s)" % self.send_error(403, "CGI script is not a Python script (%s)" %
`scriptname`) `scriptname`)
return return
...@@ -214,9 +215,13 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -214,9 +215,13 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.server.handle_error(self.request, self.client_address) self.server.handle_error(self.request, self.client_address)
os._exit(127) os._exit(127)
elif self.have_popen2: elif self.have_popen2 or self.have_popen3:
# Windows -- use popen2 to create a subprocess # Windows -- use popen2 or popen3 to create a subprocess
import shutil import shutil
if self.have_popen3:
popenx = os.popen3
else:
popenx = os.popen2
os.environ.update(env) os.environ.update(env)
cmdline = scriptfile cmdline = scriptfile
if self.is_python(scriptfile): if self.is_python(scriptfile):
...@@ -232,12 +237,21 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): ...@@ -232,12 +237,21 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
nbytes = int(length) nbytes = int(length)
except: except:
nbytes = 0 nbytes = 0
fi, fo = os.popen2(cmdline, 'b') files = popenx(cmdline, 'b')
fi = files[0]
fo = files[1]
if self.have_popen3:
fe = files[2]
if self.command.lower() == "post" and nbytes > 0: if self.command.lower() == "post" and nbytes > 0:
data = self.rfile.read(nbytes) data = self.rfile.read(nbytes)
fi.write(data) fi.write(data)
fi.close() fi.close()
shutil.copyfileobj(fo, self.wfile) shutil.copyfileobj(fo, self.wfile)
if self.have_popen3:
errors = fe.read()
fe.close()
if errors:
self.log_error('%s', errors)
sts = fo.close() sts = fo.close()
if sts: if sts:
self.log_error("CGI script exit status %#x", sts) self.log_error("CGI script exit status %#x", sts)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment