Code

- put all methods for parsing a message into a list and call all in a
[roundup.git] / frontends / 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.2 2006-12-11 23:36:15 richard Exp $
21 # python version check
22 from roundup import version_check
23 from roundup.i18n import _
24 import sys, time
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 # DEBUG_TO_CLIENT specifies whether debugging goes to the HTTP server (via
46 # stderr) or to the web client (via cgitb).
47 DEBUG_TO_CLIENT = False
49 # This indicates where the Roundup tracker lives
50 TRACKER_HOMES = {
51 #    'example': '/path/to/example',
52 }
54 # Where to log debugging information to. Use an instance of DevNull if you
55 # don't want to log anywhere.
56 class DevNull:
57     def write(self, info):
58         pass
59     def close(self):
60         pass
61     def flush(self):
62         pass
63 #LOG = open('/var/log/roundup.cgi.log', 'a')
64 LOG = DevNull()
66 #
67 ##  end configuration
68 #
71 #
72 # Set up the error handler
73
74 try:
75     import traceback, StringIO, cgi
76     from roundup.cgi import cgitb
77 except:
78     print "Content-Type: text/plain\n"
79     print _("Failed to import cgitb!\n\n")
80     s = StringIO.StringIO()
81     traceback.print_exc(None, s)
82     print s.getvalue()
85 #
86 # Check environment for config items
87 #
88 def checkconfig():
89     import os, string
90     global TRACKER_HOMES, LOG
92     # see if there's an environment var. ROUNDUP_INSTANCE_HOMES is the
93     # old name for it.
94     if os.environ.has_key('ROUNDUP_INSTANCE_HOMES'):
95         homes = os.environ.get('ROUNDUP_INSTANCE_HOMES')
96     else:
97         homes = os.environ.get('TRACKER_HOMES', '')
98     if homes:
99         TRACKER_HOMES = {}
100         for home in string.split(homes, os.pathsep):
101             try:
102                 name, dir = string.split(home, '=', 1)
103             except ValueError:
104                 # ignore invalid definitions
105                 continue
106             if name and dir:
107                 TRACKER_HOMES[name] = dir
108                 
109     logname = os.environ.get('ROUNDUP_LOG', '')
110     if logname:
111         LOG = open(logname, 'a')
113     # ROUNDUP_DEBUG is checked directly in "roundup.cgi.client"
117 # Provide interface to CGI HTTP response handling
119 class RequestWrapper:
120     '''Used to make the CGI server look like a BaseHTTPRequestHandler
121     '''
122     def __init__(self, wfile):
123         self.rfile = sys.stdin
124         self.wfile = wfile
125     def write(self, data):
126         self.wfile.write(data)
127     def send_response(self, code):
128         self.write('Status: %s\r\n'%code)
129     def send_header(self, keyword, value):
130         self.write("%s: %s\r\n" % (keyword, value))
131     def end_headers(self):
132         self.write("\r\n")
133     def start_response(self, headers, response):
134         self.send_response(response)
135         for key, value in headers:
136             self.send_header(key, value)
137         self.end_headers()
140 # Main CGI handler
142 def main(out, err):
143     import os, string
144     import roundup.instance
145     path = string.split(os.environ.get('PATH_INFO', '/'), '/')
146     request = RequestWrapper(out)
147     request.path = os.environ.get('PATH_INFO', '/')
148     tracker = path[1]
149     os.environ['TRACKER_NAME'] = tracker
150     os.environ['PATH_INFO'] = string.join(path[2:], '/')
151     if TRACKER_HOMES.has_key(tracker):
152         # redirect if we need a trailing '/'
153         if len(path) == 2:
154             request.send_response(301)
155             # redirect
156             if os.environ.get('HTTPS', '') == 'on':
157                 protocol = 'https'
158             else:
159                 protocol = 'http'
160             absolute_url = '%s://%s%s/'%(protocol, os.environ['HTTP_HOST'],
161                 os.environ.get('REQUEST_URI', ''))
162             request.send_header('Location', absolute_url)
163             request.end_headers()
164             out.write('Moved Permanently')
165         else:
166             tracker_home = TRACKER_HOMES[tracker]
167             tracker = roundup.instance.open(tracker_home)
168             import roundup.cgi.client
169             if hasattr(tracker, 'Client'):
170                 client = tracker.Client(tracker, request, os.environ)
171             else:
172                 client = roundup.cgi.client.Client(tracker, request, os.environ)
173             try:
174                 client.main()
175             except roundup.cgi.client.Unauthorised:
176                 request.send_response(403)
177                 request.send_header('Content-Type', 'text/html')
178                 request.end_headers()
179                 out.write('Unauthorised')
180             except roundup.cgi.client.NotFound:
181                 request.send_response(404)
182                 request.send_header('Content-Type', 'text/html')
183                 request.end_headers()
184                 out.write('Not found: %s'%client.path)
186     else:
187         import urllib
188         request.send_response(200)
189         request.send_header('Content-Type', 'text/html')
190         request.end_headers()
191         w = request.write
192         w(_('<html><head><title>Roundup trackers index</title></head>\n'))
193         w(_('<body><h1>Roundup trackers index</h1><ol>\n'))
194         homes = TRACKER_HOMES.keys()
195         homes.sort()
196         for tracker in homes:
197             w(_('<li><a href="%(tracker_url)s/index">%(tracker_name)s</a>\n')%{
198                 'tracker_url': os.environ['SCRIPT_NAME']+'/'+
199                                urllib.quote(tracker),
200                 'tracker_name': cgi.escape(tracker)})
201         w(_('</ol></body></html>'))
204 # Now do the actual CGI handling
206 out, err = sys.stdout, sys.stderr
207 try:
208     # force input/output to binary (important for file up/downloads)
209     if sys.platform == "win32":
210         import os, msvcrt
211         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
212         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
213     checkconfig()
214     sys.stdout = sys.stderr = LOG
215     main(out, err)
216 except SystemExit:
217     pass
218 except:
219     sys.stdout, sys.stderr = out, err
220     out.write('Content-Type: text/html\n\n')
221     if DEBUG_TO_CLIENT:
222         cgitb.handler()
223     else:
224         out.write(cgitb.breaker())
225         ts = time.ctime()
226         out.write('''<p>%s: An error occurred. Please check
227             the server log for more infomation.</p>'''%ts)
228         print >> sys.stderr, 'EXCEPTION AT', ts
229         traceback.print_exc(0, sys.stderr)
231 sys.stdout.flush()
232 sys.stdout, sys.stderr = out, err
233 LOG.close()
235 # vim: set filetype=python ts=4 sw=4 et si