49
62
# Make the request object into an IVLE request which can be passed to apps
51
req = Request(req, html.write_html_head)
65
req = Request(req, html.write_html_head)
67
# Pass the apachereq to error reporter, since ivle req isn't created
69
handle_unknown_exception(apachereq, *sys.exc_info())
70
# Tell Apache not to generate its own errors as well
73
# Run the main handler, and catch all exceptions
75
return handler_(req, apachereq)
76
except mod_python.apache.SERVER_RETURN:
77
# An apache error. We discourage these, but they might still happen.
81
handle_unknown_exception(req, *sys.exc_info())
82
# Tell Apache not to generate its own errors as well
85
def handler_(req, apachereq):
87
Nested handler function. May raise exceptions. The top-level handler is
88
just used to catch exceptions.
89
Takes both an IVLE request and an Apache req.
91
# Hack? Try and get the user login early just in case we throw an error
92
# (most likely 404) to stop us seeing not logged in even when we are.
93
if not req.publicmode:
94
req.user = login.get_user_details(req)
53
96
# Check req.app to see if it is valid. 404 if not.
54
97
if req.app is not None and req.app not in conf.apps.app_url:
74
121
app = conf.apps.app_url[req.app]
76
123
# Check if app requires auth. If so, perform authentication and login.
124
# This will either return a User object, None, or perform a redirect
125
# which we will not catch here.
77
126
if app.requireauth:
78
req.username = login.login(req)
79
logged_in = req.username is not None
127
req.user = login.login(req)
128
logged_in = req.user is not None
81
req.username = login.get_username(req)
130
req.user = login.get_user_details(req)
134
# Keep the user's session alive by writing to the session object.
135
# req.get_session().save()
136
# Well, it's a fine idea, but it creates considerable grief in the
137
# concurrent update department, so instead, we'll just make the
138
# sessions not time out.
139
req.get_session().unlock()
85
141
# If user did not specify an app, HTTP redirect to default app and
87
143
if req.app is None:
118
178
session = req.get_session()
119
179
session.invalidate()
121
req.throw_redirect(util.make_path(''))
181
# Invalidates all IVLE cookies
182
all_cookies = Cookie.get_cookies(req)
183
for cookie in all_cookies:
184
if cookie in ivle_cookies:
185
req.add_cookie(Cookie.Cookie(cookie,'',expires=1,path='/'))
186
req.throw_redirect(util.make_path(''))
188
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
190
Given an exception that has just been thrown from IVLE, print its details
192
This is a full handler. It assumes nothing has been written, and writes a
194
req: May be EITHER an IVLE req or an Apache req.
195
IVLE reqs may have the HTML head/foot written (on a 400 error), but
196
the handler code may pass an apache req if an exception occurs before
197
the IVLE request is created.
199
req.content_type = "text/html"
200
logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
202
# For some reason, some versions of mod_python have "_server" instead of
203
# "main_server". So we check for both.
205
admin_email = apache.main_server.server_admin
206
except AttributeError:
208
admin_email = apache._server.server_admin
209
except AttributeError:
212
httpcode = exc_value.httpcode
213
req.status = httpcode
214
except AttributeError:
216
req.status = apache.HTTP_INTERNAL_SERVER_ERROR
218
login = req.user.login
219
except AttributeError:
224
logging.basicConfig(level=logging.INFO,
225
format='%(asctime)s %(levelname)s: ' +
226
'(HTTP: ' + str(req.status) +
227
', Ref: ' + str(login) + '@' +
228
str(socket.gethostname()) + str(req.uri) +
234
logging.debug('Logging Unhandled Exception')
236
# We handle 3 types of error.
237
# IVLEErrors with 4xx response codes (client error).
238
# IVLEErrors with 5xx response codes (handled server error).
239
# Other exceptions (unhandled server error).
240
# IVLEErrors should not have other response codes than 4xx or 5xx
241
# (eg. throw_redirect should have been used for 3xx codes).
242
# Therefore, that is treated as an unhandled error.
244
if (exc_type == util.IVLEError and httpcode >= 400
245
and httpcode <= 499):
246
# IVLEErrors with 4xx response codes are client errors.
247
# Therefore, these have a "nice" response (we even coat it in the IVLE
250
req.write_html_head_foot = True
251
req.write('<div id="ivle_padding">\n')
253
codename, msg = req.get_http_codename(httpcode)
254
except AttributeError:
255
codename, msg = None, None
256
# Override the default message with the supplied one,
258
if exc_value.message is not None:
259
msg = exc_value.message
260
if codename is not None:
261
req.write("<h1>Error: %s</h1>\n" % cgi.escape(codename))
263
req.write("<h1>Error</h1>\n")
265
req.write("<p>%s</p>\n" % cgi.escape(msg))
267
req.write("<p>An unknown error occured.</p>\n")
270
logging.info(str(msg))
272
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
274
req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
275
%cgi.escape(logfile))
276
req.write('</div>\n')
278
# A "bad" error message. We shouldn't get here unless IVLE
279
# misbehaves (which is currently very easy, if things aren't set up
281
# Write the traceback.
282
# If this is a non-4xx IVLEError, get the message and httpcode and
283
# make the error message a bit nicer (but still include the
285
# We also need to special-case IVLEJailError, as we can get another
286
# almost-exception out of it.
288
codename, msg = None, None
290
if exc_type is util.IVLEJailError:
291
msg = exc_value.type_str + ": " + exc_value.message
292
tb = 'Exception information extracted from IVLEJailError:\n'
293
tb += urllib.unquote(exc_value.info)
296
codename, msg = req.get_http_codename(httpcode)
297
except AttributeError:
299
# Override the default message with the supplied one,
301
if hasattr(exc_value, 'message') and exc_value.message is not None:
302
msg = exc_value.message
303
# Prepend the exception type
304
if exc_type != util.IVLEError:
305
msg = exc_type.__name__ + ": " + msg
307
tb = ''.join(traceback.format_exception(exc_type, exc_value,
311
logging.error('%s\n%s'%(str(msg), tb))
314
<head><title>IVLE Internal Server Error</title></head>
316
<h1>IVLE Internal Server Error""")
317
if (codename is not None
318
and httpcode != apache.HTTP_INTERNAL_SERVER_ERROR):
319
req.write(": %s" % cgi.escape(codename))
321
<p>An error has occured which is the fault of the IVLE developers or
325
req.write("<p>%s</p>\n" % cgi.escape(msg))
326
if httpcode is not None:
327
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
329
<p>Please report this to <a href="mailto:%s">%s</a> (the system
330
administrator). Include the following information:</p>
331
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
333
req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
335
req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
336
%cgi.escape(logfile))