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