2
# Copyright (C) 2007-2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
# This is a mod_python handler program. The correct way to call it is to have
23
# Apache send all requests to be handled by the module 'dispatch'.
25
# Top-level handler. Handles all requests to all pages in IVLE.
26
# Handles authentication (not authorization).
27
# Then passes the request along to the appropriate ivle app.
30
from mod_python import apache
38
from request import Request
41
from common import util
44
"""Handles a request which may be to anywhere in the site except media.
45
Intended to be called by mod_python, as a handler.
47
req: An Apache request object.
49
# Make the request object into an IVLE request which can be passed to apps
51
req = Request(req, html.write_html_head)
53
# Check req.app to see if it is valid. 404 if not.
54
if req.app is not None and req.app not in conf.apps.app_url:
55
# Maybe it is a special app!
56
if req.app == 'logout':
59
# TODO: Nicer 404 message?
60
req.throw_error(Request.HTTP_NOT_FOUND)
62
# Special handling for public mode - just call public app and get out
63
# NOTE: This will not behave correctly if the public app uses
64
# write_html_head_foot, but "serve" does not.
66
app = conf.apps.app_url[conf.apps.public_app]
67
apps.call_app(app.dir, req)
70
# app is the App object for the chosen app
72
app = conf.apps.app_url[conf.apps.default_app]
74
app = conf.apps.app_url[req.app]
76
# Check if app requires auth. If so, perform authentication and login.
78
req.username = login.login(req)
79
logged_in = req.username is not None
81
req.username = login.get_username(req)
85
# Keep the user's session alive by writing to the session object.
86
req.get_session().save()
87
# If user did not specify an app, HTTP redirect to default app and
90
req.throw_redirect(util.make_path(conf.apps.default_app))
92
# Set the default title to the app's tab name, if any. Otherwise URL
94
if app.name is not None:
99
# Call the specified app with the request object
100
apps.call_app(app.dir, req)
101
# if not logged in, login.login will have written the login box.
102
# Just clean up and exit.
104
# MAKE SURE we write the HTTP (and possibly HTML) header. This
105
# wouldn't happen if nothing else ever got written, so we have to make
107
req.ensure_headers_written()
109
# When done, write out the HTML footer if the app has requested it
110
if req.write_html_head_foot:
111
html.write_html_foot(req)
113
# Note: Apache will not write custom HTML error messages here.
114
# Use req.throw_error to do that.
118
"""Log out the current user (if any) by destroying the session state.
119
Then redirect to the top-level IVLE page."""
120
session = req.get_session()
123
req.throw_redirect(util.make_path(''))