annotate Lib/dos_8x3/cgihttps.py @ 5251:1bddba2f100e legacy-trunk

Whole lotta changes.
author Guido van Rossum <guido@python.org>
date Wed, 02 Apr 1997 06:13:34 +0000
parents b41019557b6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3784
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """CGI-savvy HTTP Server.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 This module builds on SimpleHTTPServer by implementing GET and POST
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 requests to cgi-bin scripts.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 __version__ = "0.3"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 import os
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 import sys
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import time
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import socket
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 import string
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 import urllib
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 import BaseHTTPServer
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 import SimpleHTTPServer
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 """Complete HTTP server with GET, HEAD and POST commands.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 GET and HEAD also support running CGI scripts.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 The POST command is *only* implemented for CGI scripts.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def do_POST(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 """Serve a POST request.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 This is only implemented for CGI scripts.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if self.is_cgi():
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.run_cgi()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 self.send_error(501, "Can only POST to CGI scripts")
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def send_head(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 """Version of send_head that support CGI scripts"""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 if self.is_cgi():
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 return self.run_cgi()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def is_cgi(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 """test whether PATH corresponds to a CGI script.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 Return a tuple (dir, rest) if PATH requires running a
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 CGI script, None if not. Note that rest begins with a
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 slash if it is not empty.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 The default implementation tests whether the path
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 begins with one of the strings in the list
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 self.cgi_directories (and the next character is a '/'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 or the end of the string).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 path = self.path
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 for x in self.cgi_directories:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 i = len(x)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if path[:i] == x and (not path[i:] or path[i] == '/'):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.cgi_info = path[:i], path[i+1:]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 return 1
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 cgi_directories = ['/cgi-bin', '/htbin']
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def run_cgi(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 """Execute a CGI script."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 dir, rest = self.cgi_info
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 i = string.rfind(rest, '?')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if i >= 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 rest, query = rest[:i], rest[i+1:]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 query = ''
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 i = string.find(rest, '/')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if i >= 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 script, rest = rest[:i], rest[i:]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 script, rest = rest, ''
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 scriptname = dir + '/' + script
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 scriptfile = self.translate_path(scriptname)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 if not os.path.exists(scriptfile):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.send_error(404, "No such CGI script (%s)" % `scriptname`)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if not os.path.isfile(scriptfile):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 self.send_error(403, "CGI script is not a plain file (%s)" %
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 `scriptname`)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if not executable(scriptfile):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 self.send_error(403, "CGI script is not executable (%s)" %
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 `scriptname`)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 nobody = nobody_uid()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 self.send_response(200, "Script output follows")
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.wfile.flush() # Always flush before forking
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 pid = os.fork()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 if pid != 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # Parent
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 pid, sts = os.waitpid(pid, 0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if sts:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 self.log_error("CGI script exit status x%x" % sts)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # Child
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # XXX Much of the following could be prepared ahead of time!
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 env = {}
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 env['SERVER_SOFTWARE'] = self.version_string()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 env['SERVER_NAME'] = self.server.server_name
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 env['SERVER_PROTOCOL'] = self.protocol_version
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 env['SERVER_PORT'] = str(self.server.server_port)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 env['REQUEST_METHOD'] = self.command
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 uqrest = urllib.unquote(rest)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 env['PATH_INFO'] = uqrest
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 env['SCRIPT_NAME'] = scriptname
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if query:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 env['QUERY_STRING'] = query
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 host = self.address_string()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 if host != self.client_address[0]:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 env['REMOTE_HOST'] = host
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 env['REMOTE_ADDR'] = self.client_address[0]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # AUTH_TYPE
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 # REMOTE_USER
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # REMOTE_IDENT
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 env['CONTENT_TYPE'] = self.headers.type
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 length = self.headers.getheader('content-length')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if length:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 env['CONTENT_LENGTH'] = length
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 accept = []
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 for line in self.headers.getallmatchingheaders('accept'):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 if line[:1] in string.whitespace:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 accept.append(string.strip(line))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 accept = accept + string.split(line[7:])
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 env['HTTP_ACCEPT'] = string.joinfields(accept, ',')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 ua = self.headers.getheader('user-agent')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 if ua:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 env['HTTP_USER_AGENT'] = ua
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # XXX Other HTTP_* headers
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 import regsub
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 decoded_query = regsub.gsub('+', ' ', query)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 os.setuid(nobody)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 except os.error:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 os.dup2(self.rfile.fileno(), 0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 os.dup2(self.wfile.fileno(), 1)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 print scriptfile, script, decoded_query
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 os.execve(scriptfile,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 [script, decoded_query],
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 env)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 except:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 self.server.handle_error(self.request, self.client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 os._exit(127)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 nobody = None
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 def nobody_uid():
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 """Internal routine to get nobody's uid"""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 global nobody
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if nobody:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 return nobody
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 import pwd
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 nobody = pwd.getpwnam('nobody')[2]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 except pwd.error:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 return nobody
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 def executable(path):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 """Test for executable file."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 st = os.stat(path)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 except os.error:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 return 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return st[0] & 0111 != 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def test(HandlerClass = CGIHTTPRequestHandler,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 ServerClass = BaseHTTPServer.HTTPServer):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 SimpleHTTPServer.test(HandlerClass, ServerClass)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 if __name__ == '__main__':
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 test()