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.33 2002-09-23 00:50:32 richard Exp $
21 # python version check
22 from roundup import version_check
23 from roundup.i18n import _
24 import sys
26 #
27 ## Configuration
28 #
30 # Configuration can also be provided through the OS environment (or via
31 # the Apache "SetEnv" configuration directive). If the variables
32 # documented below are set, they _override_ any configuation defaults
33 # given in this file.
35 # TRACKER_HOMES is a list of instances, in the form
36 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
37 # separator (";" on Windows, ":" on Unix).
39 # Make sure the NAME part doesn't include any url-unsafe characters like
40 # spaces, as these confuse the cookie handling in browsers like IE.
42 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
43 # logging is turned off (unless you changed the default below).
45 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
46 # used in the code. Higher numbers means more debugging output.
48 # This indicates where the Roundup instance lives
49 TRACKER_HOMES = {
50 'demo': '/var/roundup/instances/demo',
51 }
53 # Where to log debugging information to. Use an instance of DevNull if you
54 # don't want to log anywhere.
55 class DevNull:
56 def write(self, info):
57 pass
58 def close(self):
59 pass
60 #LOG = open('/var/log/roundup.cgi.log', 'a')
61 LOG = DevNull()
63 #
64 ## end configuration
65 #
68 #
69 # Set up the error handler
70 #
71 try:
72 import traceback, StringIO, cgi
73 from roundup.cgi import cgitb
74 except:
75 print "Content-Type: text/plain\n"
76 print _("Failed to import cgitb!\n\n")
77 s = StringIO.StringIO()
78 traceback.print_exc(None, s)
79 print s.getvalue()
82 #
83 # Check environment for config items
84 #
85 def checkconfig():
86 import os, string
87 global TRACKER_HOMES, LOG
89 # see if there's an environment var. ROUNDUP_INSTANCE_HOMES is the
90 # old name for it.
91 if os.environ.has_key('ROUNDUP_INSTANCE_HOMES'):
92 homes = os.environ.get('ROUNDUP_INSTANCE_HOMES')
93 else:
94 homes = os.environ.get('TRACKER_HOMES', '')
95 if homes:
96 TRACKER_HOMES = {}
97 for home in string.split(homes, os.pathsep):
98 try:
99 name, dir = string.split(home, '=', 1)
100 except ValueError:
101 # ignore invalid definitions
102 continue
103 if name and dir:
104 TRACKER_HOMES[name] = dir
106 logname = os.environ.get('ROUNDUP_LOG', '')
107 if logname:
108 LOG = open(logname, 'a')
110 # ROUNDUP_DEBUG is checked directly in "roundup.cgi.client"
113 #
114 # Provide interface to CGI HTTP response handling
115 #
116 class RequestWrapper:
117 '''Used to make the CGI server look like a BaseHTTPRequestHandler
118 '''
119 def __init__(self, wfile):
120 self.wfile = wfile
121 def write(self, data):
122 self.wfile.write(data)
123 def send_response(self, code):
124 self.write('Status: %s\r\n'%code)
125 def send_header(self, keyword, value):
126 self.write("%s: %s\r\n" % (keyword, value))
127 def end_headers(self):
128 self.write("\r\n")
130 #
131 # Main CGI handler
132 #
133 def main(out, err):
134 import os, string
135 import roundup.instance
136 path = string.split(os.environ.get('PATH_INFO', '/'), '/')
137 request = RequestWrapper(out)
138 request.path = os.environ.get('PATH_INFO', '/')
139 instance = path[1]
140 os.environ['TRACKER_NAME'] = instance
141 os.environ['PATH_INFO'] = string.join(path[2:], '/')
142 if TRACKER_HOMES.has_key(instance):
143 # redirect if we need a trailing '/'
144 if len(path) == 2:
145 request.send_response(301)
146 # redirect
147 if os.environ.get('HTTPS', '') == 'on':
148 protocol = 'https'
149 else:
150 protocol = 'http'
151 absolute_url = '%s://%s%s/'%(protocol, os.environ['HTTP_HOST'],
152 os.environ['REQUEST_URI'])
153 request.send_header('Location', absolute_url)
154 request.end_headers()
155 out.write('Moved Permanently')
156 else:
157 instance_home = TRACKER_HOMES[instance]
158 instance = roundup.instance.open(instance_home)
159 from roundup.cgi.client import Unauthorised, NotFound
160 client = instance.Client(instance, request, os.environ)
161 try:
162 client.main()
163 except Unauthorised:
164 request.send_response(403)
165 request.send_header('Content-Type', 'text/html')
166 request.end_headers()
167 out.write('Unauthorised')
168 except NotFound:
169 request.send_response(404)
170 request.send_header('Content-Type', 'text/html')
171 request.end_headers()
172 out.write('Not found: %s'%client.path)
174 else:
175 import urllib
176 request.send_response(200)
177 request.send_header('Content-Type', 'text/html')
178 request.end_headers()
179 w = request.write
180 w(_('<html><head><title>Roundup instances index</title></head>\n'))
181 w(_('<body><h1>Roundup instances index</h1><ol>\n'))
182 homes = TRACKER_HOMES.keys()
183 homes.sort()
184 for instance in homes:
185 w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
186 'instance_url': os.environ['SCRIPT_NAME']+'/'+urllib.quote(instance),
187 'instance_name': cgi.escape(instance)})
188 w(_('</ol></body></html>'))
190 #
191 # Now do the actual CGI handling
192 #
193 out, err = sys.stdout, sys.stderr
194 try:
195 # force input/output to binary (important for file up/downloads)
196 if sys.platform == "win32":
197 import os, msvcrt
198 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
199 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
200 checkconfig()
201 sys.stdout = sys.stderr = LOG
202 main(out, err)
203 except SystemExit:
204 pass
205 except:
206 sys.stdout, sys.stderr = out, err
207 out.write('Content-Type: text/html\n\n')
208 cgitb.handler()
209 sys.stdout.flush()
210 sys.stdout, sys.stderr = out, err
211 LOG.close()
213 # vim: set filetype=python ts=4 sw=4 et si