~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: William Grant
  • Date: 2008-07-07 05:42:59 UTC
  • Revision ID: wgrant@ugrad.unimelb.edu.au-20080707054259-urcxosg7hsfvp3wo
lib/common/makeuser.py: Create a jail-specific /etc/passwd. This makes
    SVN somewhat happier. It might actually work now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
# Then passes the request along to the appropriate ivle app.
28
28
 
29
29
import mod_python
30
 
from mod_python import apache, Cookie
 
30
from mod_python import apache
31
31
 
32
32
import sys
33
33
import os
34
34
import os.path
35
 
import urllib
36
 
 
37
35
import conf
38
36
import conf.apps
39
 
import conf.conf
40
37
import apps
41
38
 
42
39
from request import Request
45
42
import login
46
43
from common import (util, forumutil)
47
44
import traceback
48
 
import plugins.console
49
 
import logging
50
 
import socket
51
 
import time
52
 
 
53
 
# List of cookies that IVLE uses (to be removed at logout)
54
 
ivle_cookies = ["ivleforumcookie", "clipboard"]
 
45
import cStringIO
55
46
 
56
47
def handler(req):
57
48
    """Handles a request which may be to anywhere in the site except media.
88
79
    just used to catch exceptions.
89
80
    Takes both an IVLE request and an Apache req.
90
81
    """
91
 
    # Hack? Try and get the user login early just in case we throw an error
92
 
    # (most likely 404) to stop us seeing not logged in even when we are.
93
 
    if not req.publicmode:
94
 
        req.user = login.get_user_details(req)
95
 
 
96
82
    # Check req.app to see if it is valid. 404 if not.
97
83
    if req.app is not None and req.app not in conf.apps.app_url:
98
84
        # Maybe it is a special app!
102
88
            req.throw_error(Request.HTTP_NOT_FOUND,
103
89
                "There is no application called %s." % repr(req.app))
104
90
 
105
 
    # Special handling for public mode - only allow the public app, call it
106
 
    # and get out.
 
91
    # Special handling for public mode - just call public app and get out
107
92
    # NOTE: This will not behave correctly if the public app uses
108
93
    # write_html_head_foot, but "serve" does not.
109
94
    if req.publicmode:
110
 
        if req.app != conf.apps.public_app:
111
 
            req.throw_error(Request.HTTP_FORBIDDEN,
112
 
                "This application is not available on the public site.")
113
95
        app = conf.apps.app_url[conf.apps.public_app]
114
96
        apps.call_app(app.dir, req)
115
97
        return req.OK
136
118
        # Well, it's a fine idea, but it creates considerable grief in the
137
119
        # concurrent update department, so instead, we'll just make the
138
120
        # sessions not time out.
139
 
        req.get_session().unlock()
140
 
 
 
121
        
141
122
        # If user did not specify an app, HTTP redirect to default app and
142
123
        # exit.
143
124
        if req.app is None:
163
144
 
164
145
    # When done, write out the HTML footer if the app has requested it
165
146
    if req.write_html_head_foot:
166
 
        # Show the console if required
167
 
        if logged_in and app.useconsole:
168
 
            plugins.console.present(req, windowpane=True)
169
147
        html.write_html_foot(req)
170
148
 
171
149
    # Note: Apache will not write custom HTML error messages here.
178
156
    session = req.get_session()
179
157
    session.invalidate()
180
158
    session.delete()
181
 
    # Invalidates all IVLE cookies
182
 
    all_cookies = Cookie.get_cookies(req)
183
 
    for cookie in all_cookies:
184
 
        if cookie in ivle_cookies:
185
 
            req.add_cookie(Cookie.Cookie(cookie,'',expires=1,path='/'))
186
 
    req.throw_redirect(util.make_path('')) 
 
159
    req.add_cookie(forumutil.invalidated_forum_cookie())
 
160
    req.throw_redirect(util.make_path(''))
187
161
 
188
162
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
189
163
    """
197
171
    the IVLE request is created.
198
172
    """
199
173
    req.content_type = "text/html"
200
 
    logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
201
 
    logfail = False
202
174
    # For some reason, some versions of mod_python have "_server" instead of
203
175
    # "main_server". So we check for both.
204
176
    try:
214
186
    except AttributeError:
215
187
        httpcode = None
216
188
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
217
 
    try:
218
 
        login = req.user.login
219
 
    except AttributeError:
220
 
        login = None
221
 
 
222
 
    # Log File
223
 
    try:
224
 
        for h in logging.getLogger().handlers:
225
 
            logging.getLogger().removeHandler(h)
226
 
        logging.basicConfig(level=logging.INFO,
227
 
            format='%(asctime)s %(levelname)s: ' +
228
 
                '(HTTP: ' + str(req.status) +
229
 
                ', Ref: ' + str(login) + '@' +
230
 
                str(socket.gethostname()) + str(req.uri) +
231
 
                ') %(message)s',
232
 
            filename=logfile,
233
 
            filemode='a')
234
 
    except IOError:
235
 
        logfail = True
236
 
    logging.debug('Logging Unhandled Exception')
237
 
 
238
189
    # We handle 3 types of error.
239
190
    # IVLEErrors with 4xx response codes (client error).
240
191
    # IVLEErrors with 5xx response codes (handled server error).
248
199
        # IVLEErrors with 4xx response codes are client errors.
249
200
        # Therefore, these have a "nice" response (we even coat it in the IVLE
250
201
        # HTML wrappers).
251
 
        
252
202
        req.write_html_head_foot = True
253
 
        req.write_javascript_settings = False
254
203
        req.write('<div id="ivle_padding">\n')
255
204
        try:
256
205
            codename, msg = req.get_http_codename(httpcode)
268
217
            req.write("<p>%s</p>\n" % cgi.escape(msg))
269
218
        else:
270
219
            req.write("<p>An unknown error occured.</p>\n")
271
 
        
272
 
        # Logging
273
 
        logging.info(str(msg))
274
 
        
275
220
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
276
 
        if logfail:
277
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
278
 
                %cgi.escape(logfile))
279
221
        req.write('</div>\n')
280
 
        html.write_html_foot(req)
281
222
    else:
282
223
        # A "bad" error message. We shouldn't get here unless IVLE
283
224
        # misbehaves (which is currently very easy, if things aren't set up
286
227
        # If this is a non-4xx IVLEError, get the message and httpcode and
287
228
        # make the error message a bit nicer (but still include the
288
229
        # traceback).
289
 
        # We also need to special-case IVLEJailError, as we can get another
290
 
        # almost-exception out of it.
291
 
 
292
 
        codename, msg = None, None
293
 
 
294
 
        if exc_type is util.IVLEJailError:
295
 
            msg = exc_value.type_str + ": " + exc_value.message
296
 
            tb = 'Exception information extracted from IVLEJailError:\n'
297
 
            tb += urllib.unquote(exc_value.info)
298
 
        else:
299
 
            try:
300
 
                codename, msg = req.get_http_codename(httpcode)
301
 
            except AttributeError:
302
 
                pass
303
 
            # Override the default message with the supplied one,
304
 
            # if available.
305
 
            if hasattr(exc_value, 'message') and exc_value.message is not None:
306
 
                msg = exc_value.message
307
 
                # Prepend the exception type
308
 
                if exc_type != util.IVLEError:
309
 
                    msg = exc_type.__name__ + ": " + msg
310
 
 
311
 
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
312
 
                                                    exc_traceback))
313
 
 
314
 
        # Logging
315
 
        logging.error('%s\n%s'%(str(msg), tb))
316
 
 
317
 
        req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
318
 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
319
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
230
        try:
 
231
            codename, msg = req.get_http_codename(httpcode)
 
232
        except AttributeError:
 
233
            codename, msg = None, None
 
234
        # Override the default message with the supplied one,
 
235
        # if available.
 
236
        if hasattr(exc_value, 'message') and exc_value.message is not None:
 
237
            msg = exc_value.message
 
238
            # Prepend the exception type
 
239
            if exc_type != util.IVLEError:
 
240
                msg = exc_type.__name__ + ": " + msg
 
241
 
 
242
        req.write("""<html>
320
243
<head><title>IVLE Internal Server Error</title></head>
321
244
<body>
322
245
<h1>IVLE Internal Server Error""")
336
259
administrator). Include the following information:</p>
337
260
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
338
261
 
339
 
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
340
 
        if logfail:
341
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
342
 
                %cgi.escape(logfile))
343
 
        req.write("</body></html>")
 
262
        tb_print = cStringIO.StringIO()
 
263
        traceback.print_exception(exc_type, exc_value, exc_traceback,
 
264
            file=tb_print)
 
265
        req.write("<pre>\n")
 
266
        req.write(cgi.escape(tb_print.getvalue()))
 
267
        req.write("</pre>\n</body>\n")