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

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: stevenbird
  • Date: 2008-01-31 23:40:25 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:357
fixed syntax error

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