22
22
Apache send all requests to be handled by the module 'dispatch'.
24
24
Top-level handler. Handles all requests to all pages in IVLE.
25
Handles authentication (not authorization).
26
Then passes the request along to the appropriate ivle app.
25
Handles authentication and delegates to views for authorization,
26
then passes the request along to the appropriate view.
42
42
from ivle import util
44
44
from ivle.dispatch.request import Request
45
from ivle.dispatch import login
45
import ivle.webapp.security
46
46
from ivle.webapp.base.plugins import ViewPlugin, PublicViewPlugin
47
from ivle.webapp.errors import HTTPError, Unauthorized
47
from ivle.webapp.base.xhtml import XHTMLView, XHTMLErrorView
48
from ivle.webapp.errors import HTTPError, Unauthorized, NotFound
50
50
def generate_route_mapper(view_plugins, attr):
63
63
m.connect(routex, view=view_class, **kwargs_dict)
67
"""Handles a request which may be to anywhere in the site except media.
66
def handler(apachereq):
67
"""Handles an HTTP request.
68
69
Intended to be called by mod_python, as a handler.
70
req: An Apache request object.
72
# Make the request object into an IVLE request which can be passed to apps
75
req = Request(req, html.write_html_head)
77
# Pass the apachereq to error reporter, since ivle req isn't created
79
handle_unknown_exception(apachereq, *sys.exc_info())
80
# Tell Apache not to generate its own errors as well
81
return mod_python.apache.OK
83
# Run the main handler, and catch all exceptions
85
return handler_(req, apachereq)
86
except mod_python.apache.SERVER_RETURN:
87
# An apache error. We discourage these, but they might still happen.
91
handle_unknown_exception(req, *sys.exc_info())
92
# Tell Apache not to generate its own errors as well
93
return mod_python.apache.OK
95
def handler_(req, apachereq):
97
Nested handler function. May raise exceptions. The top-level handler is
98
just used to catch exceptions.
99
Takes both an IVLE request and an Apache req.
71
@param apachereq: An Apache request object.
73
# Make the request object into an IVLE request which can be given to views
74
req = Request(apachereq)
101
76
# Hack? Try and get the user login early just in case we throw an error
102
77
# (most likely 404) to stop us seeing not logged in even when we are.
103
78
if not req.publicmode:
104
user = login.get_user_details(req)
79
user = ivle.webapp.security.get_user_details(req)
106
81
# Don't set the user if it is disabled or hasn't accepted the ToS.
107
82
if user and user.valid:
176
152
req.content_type = "text/html"
177
153
logfile = os.path.join(ivle.conf.log_path, 'ivle_error.log')
179
# For some reason, some versions of mod_python have "_server" instead of
180
# "main_server". So we check for both.
182
admin_email = mod_python.apache.main_server.server_admin
183
except AttributeError:
185
admin_email = mod_python.apache._server.server_admin
186
except AttributeError:
189
156
httpcode = exc_value.httpcode
190
157
req.status = httpcode
241
203
codename, msg = req.get_http_codename(httpcode)
242
204
except AttributeError:
244
# Override the default message with the supplied one,
246
if hasattr(exc_value, 'message') and exc_value.message is not None:
247
msg = exc_value.message
248
# Prepend the exception type
249
if exc_type != util.IVLEError:
250
msg = exc_type.__name__ + ": " + repr(msg)
252
207
tb = ''.join(traceback.format_exception(exc_type, exc_value,
256
210
logging.error('%s\n%s'%(str(msg), tb))
257
212
# Error messages are only displayed is the user is NOT a student,
258
213
# or if there has been a problem logging the error message
259
show_errors = (not publicmode) and ((login and \
260
str(role) != "student") or logfail)
214
show_errors = (not publicmode) and ((login and req.user.admin) or logfail)
261
215
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
262
216
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
263
217
<html xmlns="http://www.w3.org/1999/xhtml">
264
218
<head><title>IVLE Internal Server Error</title></head>
266
220
<h1>IVLE Internal Server Error""")
268
if (codename is not None
269
and httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR):
222
if codename is not None and \
223
httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR:
270
224
req.write(": %s" % cgi.escape(codename))
272
226
req.write("""</h1>
273
227
<p>An error has occured which is the fault of the IVLE developers or
274
administration. The developers have been notified.</p>
231
req.write("Please report this issue to the server administrators, "
232
"along with the following information.")
234
req.write("Details have been logged for further examination.")
277
238
if msg is not None:
278
239
req.write("<p>%s</p>\n" % cgi.escape(msg))
279
240
if httpcode is not None:
280
241
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
282
<p>Please report this to <a href="mailto:%s">%s</a> (the system
283
administrator). Include the following information:</p>
284
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
242
req.write("<h2>Debugging information</h2>")
286
244
req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
288
req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
289
%cgi.escape(logfile))
290
245
req.write("</body></html>")