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

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: wagrant
  • Date: 2008-12-23 04:48:03 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1065
Add some svn:ignores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
# Then passes the request along to the appropriate ivle app.
28
28
 
29
29
import mod_python
30
 
from mod_python import apache
 
30
from mod_python import apache, Cookie
31
31
 
32
32
import sys
33
33
import os
36
36
 
37
37
import conf
38
38
import conf.apps
 
39
import conf.conf
39
40
import apps
40
41
 
41
42
from request import Request
44
45
import login
45
46
from common import (util, forumutil)
46
47
import traceback
 
48
import plugins.console
 
49
import logging
 
50
import socket
 
51
import time
 
52
 
 
53
# List of cookies that IVLE uses (to be removed at logout)
 
54
ivle_cookies = ["ivleforumcookie", "clipboard"]
47
55
 
48
56
def handler(req):
49
57
    """Handles a request which may be to anywhere in the site except media.
80
88
    just used to catch exceptions.
81
89
    Takes both an IVLE request and an Apache req.
82
90
    """
 
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)
 
95
 
83
96
    # Check req.app to see if it is valid. 404 if not.
84
97
    if req.app is not None and req.app not in conf.apps.app_url:
85
98
        # Maybe it is a special app!
89
102
            req.throw_error(Request.HTTP_NOT_FOUND,
90
103
                "There is no application called %s." % repr(req.app))
91
104
 
92
 
    # Special handling for public mode - just call public app and get out
 
105
    # Special handling for public mode - only allow the public app, call it
 
106
    # and get out.
93
107
    # NOTE: This will not behave correctly if the public app uses
94
108
    # write_html_head_foot, but "serve" does not.
95
109
    if req.publicmode:
 
110
        if req.app != conf.apps.public_app:
 
111
            req.throw_error(Request.HTTP_FORBIDDEN,
 
112
                "This application is not available on the public site.")
96
113
        app = conf.apps.app_url[conf.apps.public_app]
97
114
        apps.call_app(app.dir, req)
98
115
        return req.OK
119
136
        # Well, it's a fine idea, but it creates considerable grief in the
120
137
        # concurrent update department, so instead, we'll just make the
121
138
        # sessions not time out.
122
 
        
 
139
        req.get_session().unlock()
 
140
 
123
141
        # If user did not specify an app, HTTP redirect to default app and
124
142
        # exit.
125
143
        if req.app is None:
145
163
 
146
164
    # When done, write out the HTML footer if the app has requested it
147
165
    if req.write_html_head_foot:
 
166
        # Show the console if required
 
167
        if logged_in and app.useconsole:
 
168
            plugins.console.present(req, windowpane=True)
148
169
        html.write_html_foot(req)
149
170
 
150
171
    # Note: Apache will not write custom HTML error messages here.
157
178
    session = req.get_session()
158
179
    session.invalidate()
159
180
    session.delete()
160
 
    req.add_cookie(forumutil.invalidated_forum_cookie())
161
 
    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('')) 
162
187
 
163
188
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
164
189
    """
172
197
    the IVLE request is created.
173
198
    """
174
199
    req.content_type = "text/html"
 
200
    logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
 
201
    logfail = False
175
202
    # For some reason, some versions of mod_python have "_server" instead of
176
203
    # "main_server". So we check for both.
177
204
    try:
187
214
    except AttributeError:
188
215
        httpcode = None
189
216
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
 
217
    try:
 
218
        login = req.user.login
 
219
    except AttributeError:
 
220
        login = None
 
221
 
 
222
    # Log File
 
223
    try:
 
224
        for h in logging.getLogger().handlers:
 
225
            logging.getLogger().removeHandler(h)
 
226
        logging.basicConfig(level=logging.INFO,
 
227
            format='%(asctime)s %(levelname)s: ' +
 
228
                '(HTTP: ' + str(req.status) +
 
229
                ', Ref: ' + str(login) + '@' +
 
230
                str(socket.gethostname()) + str(req.uri) +
 
231
                ') %(message)s',
 
232
            filename=logfile,
 
233
            filemode='a')
 
234
    except IOError:
 
235
        logfail = True
 
236
    logging.debug('Logging Unhandled Exception')
 
237
 
190
238
    # We handle 3 types of error.
191
239
    # IVLEErrors with 4xx response codes (client error).
192
240
    # IVLEErrors with 5xx response codes (handled server error).
200
248
        # IVLEErrors with 4xx response codes are client errors.
201
249
        # Therefore, these have a "nice" response (we even coat it in the IVLE
202
250
        # HTML wrappers).
 
251
        
203
252
        req.write_html_head_foot = True
 
253
        req.write_javascript_settings = False
204
254
        req.write('<div id="ivle_padding">\n')
205
255
        try:
206
256
            codename, msg = req.get_http_codename(httpcode)
218
268
            req.write("<p>%s</p>\n" % cgi.escape(msg))
219
269
        else:
220
270
            req.write("<p>An unknown error occured.</p>\n")
 
271
        
 
272
        # Logging
 
273
        logging.info(str(msg))
 
274
        
221
275
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
276
        if logfail:
 
277
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
278
                %cgi.escape(logfile))
222
279
        req.write('</div>\n')
 
280
        html.write_html_foot(req)
223
281
    else:
224
282
        # A "bad" error message. We shouldn't get here unless IVLE
225
283
        # misbehaves (which is currently very easy, if things aren't set up
253
311
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
254
312
                                                    exc_traceback))
255
313
 
256
 
        req.write("""<html>
 
314
        # Logging
 
315
        logging.error('%s\n%s'%(str(msg), tb))
 
316
 
 
317
        req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
 
318
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
 
319
<html xmlns="http://www.w3.org/1999/xhtml">
257
320
<head><title>IVLE Internal Server Error</title></head>
258
321
<body>
259
322
<h1>IVLE Internal Server Error""")
273
336
administrator). Include the following information:</p>
274
337
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
275
338
 
276
 
        req.write("<pre>\n")
277
 
        req.write(cgi.escape(tb))
278
 
        req.write("</pre>\n</body>\n")
 
339
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
340
        if logfail:
 
341
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
342
                %cgi.escape(logfile))
 
343
        req.write("</body></html>")