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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: William Grant
  • Date: 2010-02-11 08:53:40 UTC
  • Revision ID: grantw@unimelb.edu.au-20100211085340-4ms0t2f195dv0cqa
Correct the XHTML in the logout view.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    import mod_python.Session
30
30
    import mod_python.Cookie
31
31
    import mod_python.util
32
 
    import mod_python.apache
33
 
 
34
 
    class PotentiallySecureFileSession(mod_python.Session.FileSession):
35
 
        """A mod_python FileSession that sets secure cookie when appropriate.
36
 
 
37
 
        A secure cookie will be set if the request itself is over HTTPS, or if
38
 
        a proxy in front has set X-Forwarded-Proto: https. Otherwise the cookie
39
 
        will be insecure.
40
 
        """
41
 
        def make_cookie(self):
42
 
            cookie = super(PotentiallySecureFileSession, self).make_cookie()
43
 
            if (self._req.is_https() or
44
 
                self._req.headers_in.get('X-Forwarded-Proto') == 'https'):
45
 
                cookie.secure = True
46
 
            return cookie
47
32
except ImportError:
48
33
    # This needs to be importable from outside Apache.
49
34
    pass
53
38
import ivle.util
54
39
import ivle.database
55
40
from ivle.webapp.base.plugins import CookiePlugin
56
 
import ivle.webapp.security
57
 
 
58
41
 
59
42
class Request:
60
43
    """An IVLE request object. This is presented to the IVLE apps as a way of
65
48
            String. The request method (eg. 'GET', 'POST', etc)
66
49
        uri (read)
67
50
            String. The path portion of the URI.
68
 
        unparsed_uri (read)
69
 
            String. The path portion of the URI, unparsed with query string.
70
51
        app (read)
71
52
            String. Name of the application specified in the URL, or None.
72
53
        path (read)
113
94
    HTTP_NOT_FOUND                    = 404
114
95
    HTTP_INTERNAL_SERVER_ERROR        = 500
115
96
 
116
 
    _store = None
117
 
 
118
97
    def __init__(self, req, config):
119
98
        """Create an IVLE request from a mod_python one.
120
99
 
137
116
        # Inherit values for the input members
138
117
        self.method = req.method
139
118
        self.uri = req.uri
140
 
        self.unparsed_uri = req.unparsed_uri
141
119
        # Split the given path into the app (top-level dir) and sub-path
142
120
        # (after first stripping away the root directory)
143
121
        (self.app, self.path) = (ivle.util.split_path(req.uri))
 
122
        self.user = None
144
123
        self.hostname = req.hostname
145
124
        self.headers_in = req.headers_in
146
125
        self.headers_out = req.headers_out
147
126
 
 
127
        # Open a database connection and transaction, keep it around for users
 
128
        # of the Request object to use
 
129
        self.store = ivle.database.get_store(config)
 
130
 
148
131
        # Default values for the output members
149
132
        self.status = Request.HTTP_OK
150
133
        self.content_type = None        # Use Apache's default
155
138
        self.got_common_vars = False
156
139
 
157
140
    def __del__(self):
158
 
        self.cleanup()
159
 
 
160
 
    def cleanup(self):
161
 
        """Cleanup."""
162
 
        if self._store is not None:
163
 
            self._store.close()
164
 
            self._store = None
165
 
 
166
 
    def commit(self):
167
 
        """Cleanup."""
168
 
        if self._store is not None:
169
 
            self._store.commit()
 
141
        """Cleanup."""
 
142
        self.store.close()
170
143
 
171
144
    def __writeheaders(self):
172
145
        """Writes out the HTTP and HTML headers before any real data is
274
247
        """
275
248
        # Cache the session object and set the timeout to 24 hours.
276
249
        if not hasattr(self, 'session'):
277
 
            self.session = PotentiallySecureFileSession(
278
 
                self.apache_req, timeout = 60 * 60 * 24)
 
250
            self.session = mod_python.Session.FileSession(self.apache_req,
 
251
                                               timeout = 60 * 60 * 24)
279
252
        return self.session
280
253
 
281
254
    def get_fieldstorage(self):
296
269
            self.got_common_vars = True
297
270
        return self.apache_req.subprocess_env
298
271
 
299
 
    @property
300
 
    def store(self):
301
 
        # Open a database connection and transaction, keep it around for users
302
 
        # of the Request object to use.
303
 
        if self._store is None:
304
 
            self._store = ivle.database.get_store(self.config)
305
 
        return self._store
306
 
 
307
 
    @property
308
 
    def user(self):
309
 
        # Get and cache the request user, or None if it's not valid.
310
 
        # This is a property so that we don't create a store unless
311
 
        # some code actually requests the user.
312
 
        try:
313
 
            return self._user
314
 
        except AttributeError:
315
 
            if self.publicmode:
316
 
                self._user = None
317
 
            else:
318
 
                temp_user = ivle.webapp.security.get_user_details(self)
319
 
                if temp_user and temp_user.valid:
320
 
                    self._user = temp_user
321
 
                else:
322
 
                    self._user = None
323
 
            return self._user
324