1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 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
18
# Author: Matt Giuca, Will Grant
21
This is a mod_python handler program. The correct way to call it is to have
22
Apache send all requests to be handled by the module 'dispatch'.
24
Top-level handler. Handles all requests to all pages in IVLE.
25
Handles authentication and delegates to views for authorization,
26
then passes the request along to the appropriate view.
44
from ivle.dispatch.request import Request
45
import ivle.webapp.security
46
from ivle.webapp.base.plugins import ViewPlugin, PublicViewPlugin
47
from ivle.webapp.errors import HTTPError, Unauthorized
49
def generate_route_mapper(view_plugins, attr):
51
Build a Mapper object for doing URL matching using 'routes', based on the
52
given plugin registry.
54
m = routes.Mapper(explicit=True)
55
for plugin in view_plugins:
56
# Establish a URL pattern for each element of plugin.urls
57
assert hasattr(plugin, 'urls'), "%r does not have any urls" % plugin
58
for url in getattr(plugin, attr):
61
kwargs_dict = url[2] if len(url) >= 3 else {}
62
m.connect(routex, view=view_class, **kwargs_dict)
65
def handler(apachereq):
66
"""Handles an HTTP request.
68
Intended to be called by mod_python, as a handler.
70
@param apachereq: An Apache request object.
72
# Make the request object into an IVLE request which can be given to views
73
req = Request(apachereq)
75
# Hack? Try and get the user login early just in case we throw an error
76
# (most likely 404) to stop us seeing not logged in even when we are.
77
if not req.publicmode:
78
user = ivle.webapp.security.get_user_details(req)
80
# Don't set the user if it is disabled or hasn't accepted the ToS.
81
if user and user.valid:
84
conf = ivle.config.Config()
88
req.mapper = generate_route_mapper(conf.plugin_index[PublicViewPlugin],
91
req.mapper = generate_route_mapper(conf.plugin_index[ViewPlugin],
94
matchdict = req.mapper.match(req.uri)
95
if matchdict is not None:
96
viewcls = matchdict['view']
97
# Get the remaining arguments, less 'view', 'action' and 'controller'
98
# (The latter two seem to be built-in, and we don't want them).
99
kwargs = matchdict.copy()
102
# Instantiate the view, which should be a BaseView class
103
view = viewcls(req, **kwargs)
105
# Check that the request (mainly the user) is permitted to access
107
if not view.authorize(req):
112
# A view explicitly raised an HTTP error. Respect it.
115
# Try to find a custom error view.
116
if hasattr(viewcls, 'get_error_view'):
117
errviewcls = viewcls.get_error_view(e)
122
errview = errviewcls(req, e)
131
# A non-HTTPError appeared. We have an unknown exception. Panic.
132
handle_unknown_exception(req, *sys.exc_info())
138
return req.HTTP_NOT_FOUND # TODO: Prettify.
140
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
142
Given an exception that has just been thrown from IVLE, print its details
144
This is a full handler. It assumes nothing has been written, and writes a
146
req: May be EITHER an IVLE req or an Apache req.
147
The handler code may pass an apache req if an exception occurs before
148
the IVLE request is created.
150
req.content_type = "text/html"
151
logfile = os.path.join(ivle.conf.log_path, 'ivle_error.log')
154
httpcode = exc_value.httpcode
155
req.status = httpcode
156
except AttributeError:
158
req.status = mod_python.apache.HTTP_INTERNAL_SERVER_ERROR
160
publicmode = req.publicmode
161
except AttributeError:
164
login = req.user.login
165
except AttributeError:
169
except AttributeError:
174
for h in logging.getLogger().handlers:
175
logging.getLogger().removeHandler(h)
176
logging.basicConfig(level=logging.INFO,
177
format='%(asctime)s %(levelname)s: ' +
178
'(HTTP: ' + str(req.status) +
179
', Ref: ' + str(login) + '@' +
180
str(socket.gethostname()) + str(req.uri) +
187
# A "bad" error message. We shouldn't get here unless IVLE
188
# misbehaves (which is currently very easy, if things aren't set up
190
# Write the traceback.
191
# If this is a non-4xx IVLEError, get the message and httpcode and
192
# make the error message a bit nicer (but still include the
194
# We also need to special-case IVLEJailError, as we can get another
195
# almost-exception out of it.
197
codename, msg = None, None
199
if exc_type is util.IVLEJailError:
200
msg = exc_value.type_str + ": " + exc_value.message
201
tb = 'Exception information extracted from IVLEJailError:\n'
202
tb += urllib.unquote(exc_value.info)
205
codename, msg = req.get_http_codename(httpcode)
206
except AttributeError:
209
tb = ''.join(traceback.format_exception(exc_type, exc_value,
212
logging.error('%s\n%s'%(str(msg), tb))
214
# Error messages are only displayed is the user is NOT a student,
215
# or if there has been a problem logging the error message
216
show_errors = (not publicmode) and ((login and \
217
str(role) != "student") or logfail)
218
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
219
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
220
<html xmlns="http://www.w3.org/1999/xhtml">
221
<head><title>IVLE Internal Server Error</title></head>
223
<h1>IVLE Internal Server Error""")
225
if codename is not None and \
226
httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR:
227
req.write(": %s" % cgi.escape(codename))
230
<p>An error has occured which is the fault of the IVLE developers or
234
req.write("Please report this issue to the server administrators, "
235
"along with the following information.")
237
req.write("Details have been logged for further examination.")
242
req.write("<p>%s</p>\n" % cgi.escape(msg))
243
if httpcode is not None:
244
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
245
req.write("<h2>Debugging information</h2>")
247
req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
248
req.write("</body></html>")