"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/frontends/roundup.cgi" (26 Aug 2019, 7822 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "roundup.cgi": 1.6.1_vs_2.0.0.

    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 # python version check
   20 from __future__ import print_function
   21 from roundup import version_check
   22 from roundup.i18n import _
   23 from roundup.anypy.strings import s2b, StringIO
   24 import sys, time
   25 
   26 #
   27 ##  Configuration
   28 #
   29 
   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. 
   34 
   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). 
   38 
   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.
   41 
   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). 
   44 
   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
   48 
   49 # This indicates where the Roundup tracker lives
   50 TRACKER_HOMES = {
   51 #    'example': '/path/to/example',
   52 }
   53 
   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()
   65 
   66 #
   67 ##  end configuration
   68 #
   69 
   70 
   71 #
   72 # Set up the error handler
   73 # 
   74 try:
   75     import traceback, 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()
   81     traceback.print_exc(None, s)
   82     print(s.getvalue())
   83 
   84 
   85 #
   86 # Check environment for config items
   87 #
   88 def checkconfig():
   89     import os
   90     global TRACKER_HOMES, LOG
   91 
   92     # see if there's an environment var. ROUNDUP_INSTANCE_HOMES is the
   93     # old name for it.
   94     if 'ROUNDUP_INSTANCE_HOMES' in os.environ:
   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 homes.split(os.pathsep):
  101             try:
  102                 name, dir = home.split('=', 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')
  112 
  113     # ROUNDUP_DEBUG is checked directly in "roundup.cgi.client"
  114 
  115 
  116 #
  117 # Provide interface to CGI HTTP response handling
  118 #
  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(s2b('Status: %s\r\n'%code))
  129     def send_header(self, keyword, value):
  130         self.write(s2b("%s: %s\r\n" % (keyword, value)))
  131     def end_headers(self):
  132         self.write(b"\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()
  138 
  139 #
  140 # Main CGI handler
  141 #
  142 def main(out, err):
  143     import os
  144     import roundup.instance
  145     path = os.environ.get('PATH_INFO', '/').split('/')
  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'] = '/'.join(path[2:])
  151     if tracker in TRACKER_HOMES:
  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(b'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(b'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(s2b('Not found: %s'%cgi.escape(client.path)))
  185 
  186     else:
  187         from roundup.anypy 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(s2b(_('<html><head><title>Roundup trackers index</title></head>\n')))
  193         w(s2b(_('<body><h1>Roundup trackers index</h1><ol>\n')))
  194         homes = sorted(TRACKER_HOMES.keys())
  195         for tracker in homes:
  196             w(s2b(_('<li><a href="%(tracker_url)s/index">%(tracker_name)s</a>\n')%{
  197                 'tracker_url': os.environ['SCRIPT_NAME']+'/'+
  198                                urllib_.quote(tracker),
  199                 'tracker_name': cgi.escape(tracker)}))
  200         w(s2b(_('</ol></body></html>')))
  201 
  202 #
  203 # Now do the actual CGI handling
  204 #
  205 out, err = sys.stdout, sys.stderr
  206 try:
  207     # force input/output to binary (important for file up/downloads)
  208     if sys.platform == "win32":
  209         import os, msvcrt
  210         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  211         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  212     checkconfig()
  213     sys.stdout = sys.stderr = LOG
  214     if sys.version_info[0] > 2:
  215         out_buf = out.buffer
  216     else:
  217         out_buf = out
  218     main(out_buf, err)
  219 except SystemExit:
  220     pass
  221 except:
  222     sys.stdout, sys.stderr = out, err
  223     out.write('Content-Type: text/html\n\n')
  224     if DEBUG_TO_CLIENT:
  225         cgitb.handler()
  226     else:
  227         out.write(cgitb.breaker())
  228         ts = time.ctime()
  229         out.write('''<p>%s: An error occurred. Please check
  230             the server log for more information.</p>'''%ts)
  231         print('EXCEPTION AT', ts, file=sys.stderr)
  232         traceback.print_exc(0, sys.stderr)
  233 
  234 sys.stdout.flush()
  235 sys.stdout, sys.stderr = out, err
  236 LOG.close()
  237 
  238 # vim: set filetype=python ts=4 sw=4 et si