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

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: stevenbird
  • Date: 2008-02-19 21:17:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:512
Renaming of problems to exercises (initial commit).
Fix up module naming (exercises sometimes called tutorials).

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import mod_python
30
30
from mod_python import apache
31
31
 
32
 
import sys
33
32
import os
34
33
import os.path
35
34
import conf
38
37
 
39
38
from request import Request
40
39
import html
41
 
import cgi
42
40
import login
43
41
from common import (util, forumutil)
44
 
import traceback
45
 
import cStringIO
46
42
 
47
43
def handler(req):
48
44
    """Handles a request which may be to anywhere in the site except media.
52
48
    """
53
49
    # Make the request object into an IVLE request which can be passed to apps
54
50
    apachereq = req
55
 
    try:
56
 
        req = Request(req, html.write_html_head)
57
 
    except Exception:
58
 
        # Pass the apachereq to error reporter, since ivle req isn't created
59
 
        # yet.
60
 
        handle_unknown_exception(apachereq, *sys.exc_info())
61
 
        # Tell Apache not to generate its own errors as well
62
 
        return apache.OK
63
 
 
64
 
    # Run the main handler, and catch all exceptions
65
 
    try:
66
 
        return handler_(req, apachereq)
67
 
    except mod_python.apache.SERVER_RETURN:
68
 
        # An apache error. We discourage these, but they might still happen.
69
 
        # Just raise up.
70
 
        raise
71
 
    except Exception:
72
 
        handle_unknown_exception(req, *sys.exc_info())
73
 
        # Tell Apache not to generate its own errors as well
74
 
        return apache.OK
75
 
 
76
 
def handler_(req, apachereq):
77
 
    """
78
 
    Nested handler function. May raise exceptions. The top-level handler is
79
 
    just used to catch exceptions.
80
 
    Takes both an IVLE request and an Apache req.
81
 
    """
 
51
    req = Request(req, html.write_html_head)
 
52
 
82
53
    # Check req.app to see if it is valid. 404 if not.
83
54
    if req.app is not None and req.app not in conf.apps.app_url:
84
55
        # Maybe it is a special app!
85
56
        if req.app == 'logout':
86
57
            logout(req)
87
58
        else:
88
 
            req.throw_error(Request.HTTP_NOT_FOUND,
89
 
                "There is no application called %s." % repr(req.app))
 
59
            # TODO: Nicer 404 message?
 
60
            req.throw_error(Request.HTTP_NOT_FOUND)
90
61
 
91
62
    # Special handling for public mode - just call public app and get out
92
63
    # NOTE: This will not behave correctly if the public app uses
133
104
 
134
105
        # Call the specified app with the request object
135
106
        apps.call_app(app.dir, req)
136
 
 
137
107
    # if not logged in, login.login will have written the login box.
138
108
    # Just clean up and exit.
139
109
 
158
128
    session.delete()
159
129
    req.add_cookie(forumutil.invalidated_forum_cookie())
160
130
    req.throw_redirect(util.make_path(''))
161
 
 
162
 
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
163
 
    """
164
 
    Given an exception that has just been thrown from IVLE, print its details
165
 
    to the request.
166
 
    This is a full handler. It assumes nothing has been written, and writes a
167
 
    complete HTML page.
168
 
    req: May be EITHER an IVLE req or an Apache req.
169
 
    IVLE reqs may have the HTML head/foot written (on a 400 error), but
170
 
    the handler code may pass an apache req if an exception occurs before
171
 
    the IVLE request is created.
172
 
    """
173
 
    req.content_type = "text/html"
174
 
    # For some reason, some versions of mod_python have "_server" instead of
175
 
    # "main_server". So we check for both.
176
 
    try:
177
 
        admin_email = apache.main_server.server_admin
178
 
    except AttributeError:
179
 
        try:
180
 
            admin_email = apache._server.server_admin
181
 
        except AttributeError:
182
 
            admin_email = ""
183
 
    try:
184
 
        httpcode = exc_value.httpcode
185
 
        req.status = httpcode
186
 
    except AttributeError:
187
 
        httpcode = None
188
 
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
189
 
    # We handle 3 types of error.
190
 
    # IVLEErrors with 4xx response codes (client error).
191
 
    # IVLEErrors with 5xx response codes (handled server error).
192
 
    # Other exceptions (unhandled server error).
193
 
    # IVLEErrors should not have other response codes than 4xx or 5xx
194
 
    # (eg. throw_redirect should have been used for 3xx codes).
195
 
    # Therefore, that is treated as an unhandled error.
196
 
 
197
 
    if (exc_type == util.IVLEError and httpcode >= 400
198
 
        and httpcode <= 499):
199
 
        # IVLEErrors with 4xx response codes are client errors.
200
 
        # Therefore, these have a "nice" response (we even coat it in the IVLE
201
 
        # HTML wrappers).
202
 
        req.write_html_head_foot = True
203
 
        req.write('<div id="ivle_padding">\n')
204
 
        try:
205
 
            codename, msg = req.get_http_codename(httpcode)
206
 
        except AttributeError:
207
 
            codename, msg = None, None
208
 
        # Override the default message with the supplied one,
209
 
        # if available.
210
 
        if exc_value.message is not None:
211
 
            msg = exc_value.message
212
 
        if codename is not None:
213
 
            req.write("<h1>Error: %s</h1>\n" % cgi.escape(codename))
214
 
        else:
215
 
            req.write("<h1>Error</h1>\n")
216
 
        if msg is not None:
217
 
            req.write("<p>%s</p>\n" % cgi.escape(msg))
218
 
        else:
219
 
            req.write("<p>An unknown error occured.</p>\n")
220
 
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
221
 
        req.write('</div>\n')
222
 
    else:
223
 
        # A "bad" error message. We shouldn't get here unless IVLE
224
 
        # misbehaves (which is currently very easy, if things aren't set up
225
 
        # correctly).
226
 
        # Write the traceback.
227
 
        # If this is a non-4xx IVLEError, get the message and httpcode and
228
 
        # make the error message a bit nicer (but still include the
229
 
        # traceback).
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>
243
 
<head><title>IVLE Internal Server Error</title></head>
244
 
<body>
245
 
<h1>IVLE Internal Server Error""")
246
 
        if (codename is not None
247
 
            and httpcode != apache.HTTP_INTERNAL_SERVER_ERROR):
248
 
            req.write(": %s" % cgi.escape(codename))
249
 
        req.write("""</h1>
250
 
<p>An error has occured which is the fault of the IVLE developers or
251
 
administration.</p>
252
 
""")
253
 
        if msg is not None:
254
 
            req.write("<p>%s</p>\n" % cgi.escape(msg))
255
 
        if httpcode is not None:
256
 
            req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
257
 
        req.write("""
258
 
<p>Please report this to <a href="mailto:%s">%s</a> (the system
259
 
administrator). Include the following information:</p>
260
 
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
261
 
 
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")