~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-07-28 05:06:15 UTC
  • Revision ID: grantw@unimelb.edu.au-20100728050615-uwbxn9frla3pdw8m
Encode content_type when downloading files. cjson made us write bad code.

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
32
47
except ImportError:
33
48
    # This needs to be importable from outside Apache.
34
49
    pass
38
53
import ivle.util
39
54
import ivle.database
40
55
from ivle.webapp.base.plugins import CookiePlugin
 
56
import ivle.webapp.security
 
57
 
41
58
 
42
59
class Request:
43
60
    """An IVLE request object. This is presented to the IVLE apps as a way of
48
65
            String. The request method (eg. 'GET', 'POST', etc)
49
66
        uri (read)
50
67
            String. The path portion of the URI.
 
68
        unparsed_uri (read)
 
69
            String. The path portion of the URI, unparsed with query string.
51
70
        app (read)
52
71
            String. Name of the application specified in the URL, or None.
53
72
        path (read)
94
113
    HTTP_NOT_FOUND                    = 404
95
114
    HTTP_INTERNAL_SERVER_ERROR        = 500
96
115
 
 
116
    _store = None
 
117
 
97
118
    def __init__(self, req, config):
98
119
        """Create an IVLE request from a mod_python one.
99
120
 
116
137
        # Inherit values for the input members
117
138
        self.method = req.method
118
139
        self.uri = req.uri
 
140
        self.unparsed_uri = req.unparsed_uri
119
141
        # Split the given path into the app (top-level dir) and sub-path
120
142
        # (after first stripping away the root directory)
121
143
        (self.app, self.path) = (ivle.util.split_path(req.uri))
122
 
        self.user = None
123
144
        self.hostname = req.hostname
124
145
        self.headers_in = req.headers_in
125
146
        self.headers_out = req.headers_out
126
147
 
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
 
 
131
148
        # Default values for the output members
132
149
        self.status = Request.HTTP_OK
133
150
        self.content_type = None        # Use Apache's default
138
155
        self.got_common_vars = False
139
156
 
140
157
    def __del__(self):
141
 
        """Cleanup."""
142
 
        self.store.close()
 
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()
143
170
 
144
171
    def __writeheaders(self):
145
172
        """Writes out the HTTP and HTML headers before any real data is
247
274
        """
248
275
        # Cache the session object and set the timeout to 24 hours.
249
276
        if not hasattr(self, 'session'):
250
 
            self.session = mod_python.Session.FileSession(self.apache_req,
251
 
                                               timeout = 60 * 60 * 24)
 
277
            self.session = PotentiallySecureFileSession(
 
278
                self.apache_req, timeout = 60 * 60 * 24)
252
279
        return self.session
253
280
 
254
281
    def get_fieldstorage(self):
269
296
            self.got_common_vars = True
270
297
        return self.apache_req.subprocess_env
271
298
 
 
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