1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18 #
19 # $Id: roundup.cgi,v 1.20 2001-11-26 22:55:56 richard Exp $
21 # python version check
22 import sys
23 if int(sys.version[0]) < 2:
24 print "Content-Type: text/plain\n"
25 print "Roundup requires Python 2.0 or newer."
26 sys.exit(0)
28 #
29 ## Configuration
30 #
32 # Configuration can also be provided through the OS environment (or via
33 # the Apache "SetEnv" configuration directive). If the variables
34 # documented below are set, they _override_ any configuation defaults
35 # given in this file.
37 # ROUNDUP_INSTANCE_HOMES is a list of instances, in the form
38 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
39 # separator (";" on Windows, ":" on Unix).
41 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
42 # logging is turned off (unless you changed the default below).
44 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
45 # used in the code. Higher numbers means more debugging output.
47 # This indicates where the Roundup instance lives
48 ROUNDUP_INSTANCE_HOMES = {
49 'demo': '/var/roundup/instances/demo',
50 }
52 # Where to log debugging information to. Use an instance of DevNull if you
53 # don't want to log anywhere.
54 class DevNull:
55 def write(self, info):
56 pass
57 def close():
58 pass
59 #LOG = open('/var/log/roundup.cgi.log', 'a')
60 LOG = DevNull()
62 #
63 ## end configuration
64 #
67 #
68 # Set up the error handler
69 #
70 try:
71 import traceback, StringIO, cgi
72 from roundup import cgitb
73 except:
74 print "Content-Type: text/html\n"
75 print "Failed to import cgitb.<pre>"
76 s = StringIO.StringIO()
77 traceback.print_exc(None, s)
78 print cgi.escape(s.getvalue()), "</pre>"
81 #
82 # Check environment for config items
83 #
84 def checkconfig():
85 import os, string
86 global ROUNDUP_INSTANCE_HOMES, LOG
88 homes = os.environ.get('ROUNDUP_INSTANCE_HOMES', '')
89 if homes:
90 ROUNDUP_INSTANCE_HOMES = {}
91 for home in string.split(homes, os.pathsep):
92 try:
93 name, dir = string.split(home, '=', 1)
94 except ValueError:
95 # ignore invalid definitions
96 continue
97 if name and dir:
98 ROUNDUP_INSTANCE_HOMES[name] = dir
100 logname = os.environ.get('ROUNDUP_LOG', '')
101 if logname:
102 LOG = open(logname, 'a')
104 # ROUNDUP_DEBUG is checked directly in "roundup.cgi_client"
107 #
108 # Provide interface to CGI HTTP response handling
109 #
110 class RequestWrapper:
111 '''Used to make the CGI server look like a BaseHTTPRequestHandler
112 '''
113 def __init__(self, wfile):
114 self.wfile = wfile
115 def write(self, data):
116 self.wfile.write(data)
117 def send_response(self, code):
118 self.write('Status: %s\r\n'%code)
119 def send_header(self, keyword, value):
120 self.write("%s: %s\r\n" % (keyword, value))
121 def end_headers(self):
122 self.write("\r\n")
124 #
125 # Main CGI handler
126 #
127 def main(out, err):
128 import os, string
129 import roundup.instance
130 path = string.split(os.environ.get('PATH_INFO', '/'), '/')
131 instance = path[1]
132 os.environ['INSTANCE_NAME'] = instance
133 os.environ['PATH_INFO'] = string.join(path[2:], '/')
134 request = RequestWrapper(out)
135 if ROUNDUP_INSTANCE_HOMES.has_key(instance):
136 # redirect if we need a trailing '/'
137 if len(path) == 2:
138 request.send_response(301)
139 absolute_url = 'http://%s%s/'%(os.environ['HTTP_HOST'],
140 os.environ['REQUEST_URI'])
141 request.send_header('Location', absolute_url)
142 request.end_headers()
143 out.write('Moved Permanently')
144 else:
145 instance_home = ROUNDUP_INSTANCE_HOMES[instance]
146 instance = roundup.instance.open(instance_home)
147 from roundup import cgi_client
148 client = instance.Client(instance, request, os.environ)
149 try:
150 client.main()
151 except cgi_client.Unauthorised:
152 request.send_response(403)
153 request.send_header('Content-Type', 'text/html')
154 request.end_headers()
155 out.write('Unauthorised')
156 except cgi_client.NotFound:
157 request.send_response(404)
158 request.send_header('Content-Type', 'text/html')
159 request.end_headers()
160 out.write('Not found: %s'%client.path)
162 else:
163 import urllib
164 request.send_response(200)
165 request.send_header('Content-Type', 'text/html')
166 request.end_headers()
167 w = request.write
168 w('<html><head><title>Roundup instances index</title></head>\n')
169 w('<body><h1>Roundup instances index</h1><ol>\n')
170 homes = ROUNDUP_INSTANCE_HOMES.keys()
171 homes.sort()
172 for instance in homes:
173 w('<li><a href="%s/%s/index">%s</a>\n'%(
174 os.environ['SCRIPT_NAME'], urllib.quote(instance),
175 cgi.escape(instance)))
176 w('</ol></body></html>')
178 #
179 # Now do the actual CGI handling
180 #
181 out, err = sys.stdout, sys.stderr
182 try:
183 # force input/output to binary (important for file up/downloads)
184 if sys.platform == "win32":
185 import os, msvcrt
186 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
187 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
188 checkconfig()
189 sys.stdout = sys.stderr = LOG
190 main(out, err)
191 except SystemExit:
192 pass
193 except:
194 sys.stdout, sys.stderr = out, err
195 out.write('Content-Type: text/html\n\n')
196 cgitb.handler()
197 sys.stdout.flush()
198 sys.stdout, sys.stderr = out, err
199 LOG.close()
201 #
202 # $Log: not supported by cvs2svn $
203 # Revision 1.19 2001/11/22 00:25:10 richard
204 # quick fix for file uploads on windows in roundup.cgi
205 #
206 # Revision 1.18 2001/11/06 22:10:11 jhermann
207 # Added env config; fixed request wrapper & index list; sort list by key
208 #
209 # Revision 1.17 2001/11/06 21:51:19 richard
210 # Fixed HTTP headers for top-level index in CGI script
211 #
212 # Revision 1.16 2001/11/01 22:04:37 richard
213 # Started work on supporting a pop3-fetching server
214 # Fixed bugs:
215 # . bug #477104 ] HTML tag error in roundup-server
216 # . bug #477107 ] HTTP header problem
217 #
218 # Revision 1.15 2001/10/29 23:55:44 richard
219 # Fix to CGI top-level index (thanks Juergen Hermann)
220 #
221 # Revision 1.14 2001/10/27 00:22:35 richard
222 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
223 #
224 # Revision 1.13 2001/10/05 02:23:24 richard
225 # . roundup-admin create now prompts for property info if none is supplied
226 # on the command-line.
227 # . hyperdb Class getprops() method may now return only the mutable
228 # properties.
229 # . Login now uses cookies, which makes it a whole lot more flexible. We can
230 # now support anonymous user access (read-only, unless there's an
231 # "anonymous" user, in which case write access is permitted). Login
232 # handling has been moved into cgi_client.Client.main()
233 # . The "extended" schema is now the default in roundup init.
234 # . The schemas have had their page headings modified to cope with the new
235 # login handling. Existing installations should copy the interfaces.py
236 # file from the roundup lib directory to their instance home.
237 # . Incorrectly had a Bizar Software copyright on the cgitb.py module from
238 # Ping - has been removed.
239 # . Fixed a whole bunch of places in the CGI interface where we should have
240 # been returning Not Found instead of throwing an exception.
241 # . Fixed a deviation from the spec: trying to modify the 'id' property of
242 # an item now throws an exception.
243 #
244 # Revision 1.12 2001/10/01 05:55:41 richard
245 # Fixes to the top-level index
246 #
247 # Revision 1.11 2001/09/29 13:27:00 richard
248 # CGI interfaces now spit up a top-level index of all the instances they can
249 # serve.
250 #
251 # Revision 1.10 2001/08/07 00:24:42 richard
252 # stupid typo
253 #
254 # Revision 1.9 2001/08/07 00:15:51 richard
255 # Added the copyright/license notice to (nearly) all files at request of
256 # Bizar Software.
257 #
258 # Revision 1.8 2001/08/05 07:43:52 richard
259 # Instances are now opened by a special function that generates a unique
260 # module name for the instances on import time.
261 #
262 # Revision 1.7 2001/08/03 01:28:33 richard
263 # Used the much nicer load_package, pointed out by Steve Majewski.
264 #
265 # Revision 1.6 2001/08/03 00:59:34 richard
266 # Instance import now imports the instance using imp.load_module so that
267 # we can have instance homes of "roundup" or other existing python package
268 # names.
269 #
270 # Revision 1.5 2001/07/29 07:01:39 richard
271 # Added vim command to all source so that we don't get no steenkin' tabs :)
272 #
273 # Revision 1.4 2001/07/23 04:47:27 anthonybaxter
274 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
275 # sys.exit(0) if python version wrong.
276 #
277 # Revision 1.3 2001/07/23 04:33:30 richard
278 # brought the CGI instance config dict in line with roundup-server
279 #
280 # Revision 1.2 2001/07/23 04:31:40 richard
281 # Fixed the roundup CGI script for updates to cgi_client.py
282 #
283 # Revision 1.1 2001/07/22 11:47:07 richard
284 # More Grande Splite
285 #
286 #
287 # vim: set filetype=python ts=4 sw=4 et si