Code

fixes to mailgw tests
[roundup.git] / cgi-bin / roundup.cgi
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.35 2002-11-06 11:38:42 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 trackers, 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 tracker lives
49 TRACKER_HOMES = {
50     'demo': '/var/roundup/trackers/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
105                 
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"
114 # Provide interface to CGI HTTP response handling
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")
131 # Main CGI handler
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     tracker = path[1]
140     os.environ['TRACKER_NAME'] = tracker
141     os.environ['PATH_INFO'] = string.join(path[2:], '/')
142     if TRACKER_HOMES.has_key(tracker):
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.get('REQUEST_URI', ''))
153             request.send_header('Location', absolute_url)
154             request.end_headers()
155             out.write('Moved Permanently')
156         else:
157             tracker_home = TRACKER_HOMES[tracker]
158             tracker = roundup.instance.open(tracker_home)
159             from roundup.cgi.client import Unauthorised, NotFound
160             client = tracker.Client(tracker, 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 trackers index</title></head>\n'))
181         w(_('<body><h1>Roundup trackers index</h1><ol>\n'))
182         homes = TRACKER_HOMES.keys()
183         homes.sort()
184         for tracker in homes:
185             w(_('<li><a href="%(tracker_url)s/index">%(tracker_name)s</a>\n')%{
186                 'tracker_url': os.environ['SCRIPT_NAME']+'/'+
187                                urllib.quote(tracker),
188                 'tracker_name': cgi.escape(tracker)})
189         w(_('</ol></body></html>'))
192 # Now do the actual CGI handling
194 out, err = sys.stdout, sys.stderr
195 try:
196     # force input/output to binary (important for file up/downloads)
197     if sys.platform == "win32":
198         import os, msvcrt
199         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
200         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
201     checkconfig()
202     sys.stdout = sys.stderr = LOG
203     main(out, err)
204 except SystemExit:
205     pass
206 except:
207     sys.stdout, sys.stderr = out, err
208     out.write('Content-Type: text/html\n\n')
209     cgitb.handler()
210 sys.stdout.flush()
211 sys.stdout, sys.stderr = out, err
212 LOG.close()
214 # vim: set filetype=python ts=4 sw=4 et si