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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: David Coles
  • Date: 2009-07-21 02:19:56 UTC
  • mto: (1281.1.8 aufsless)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: coles.david@gmail.com-20090721021956-c1jiwu7fhi2dna1g
Updated to work on bind mounts

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
111
94
    HTTP_NOT_FOUND                    = 404
112
95
    HTTP_INTERNAL_SERVER_ERROR        = 500
113
96
 
114
 
    _store = None
115
 
 
116
97
    def __init__(self, req, config):
117
98
        """Create an IVLE request from a mod_python one.
118
99
 
138
119
        # Split the given path into the app (top-level dir) and sub-path
139
120
        # (after first stripping away the root directory)
140
121
        (self.app, self.path) = (ivle.util.split_path(req.uri))
 
122
        self.user = None
141
123
        self.hostname = req.hostname
142
124
        self.headers_in = req.headers_in
143
125
        self.headers_out = req.headers_out
144
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
 
145
131
        # Default values for the output members
146
132
        self.status = Request.HTTP_OK
147
133
        self.content_type = None        # Use Apache's default
152
138
        self.got_common_vars = False
153
139
 
154
140
    def __del__(self):
155
 
        self.cleanup()
156
 
 
157
 
    def cleanup(self):
158
 
        """Cleanup."""
159
 
        if self._store is not None:
160
 
            self._store.close()
161
 
            self._store = None
162
 
 
163
 
    def commit(self):
164
 
        """Cleanup."""
165
 
        if self._store is not None:
166
 
            self._store.commit()
 
141
        """Cleanup."""
 
142
        self.store.close()
167
143
 
168
144
    def __writeheaders(self):
169
145
        """Writes out the HTTP and HTML headers before any real data is
271
247
        """
272
248
        # Cache the session object and set the timeout to 24 hours.
273
249
        if not hasattr(self, 'session'):
274
 
            self.session = PotentiallySecureFileSession(
275
 
                self.apache_req, timeout = 60 * 60 * 24)
 
250
            self.session = mod_python.Session.FileSession(self.apache_req,
 
251
                                               timeout = 60 * 60 * 24)
276
252
        return self.session
277
253
 
278
254
    def get_fieldstorage(self):
293
269
            self.got_common_vars = True
294
270
        return self.apache_req.subprocess_env
295
271
 
296
 
    @property
297
 
    def store(self):
298
 
        # Open a database connection and transaction, keep it around for users
299
 
        # of the Request object to use.
300
 
        if self._store is None:
301
 
            self._store = ivle.database.get_store(self.config)
302
 
        return self._store
303
 
 
304
 
    @property
305
 
    def user(self):
306
 
        # Get and cache the request user, or None if it's not valid.
307
 
        # This is a property so that we don't create a store unless
308
 
        # some code actually requests the user.
309
 
        try:
310
 
            return self._user
311
 
        except AttributeError:
312
 
            if self.publicmode:
313
 
                self._user = None
314
 
            else:
315
 
                temp_user = ivle.webapp.security.get_user_details(self)
316
 
                if temp_user and temp_user.valid:
317
 
                    self._user = temp_user
318
 
                else:
319
 
                    self._user = None
320
 
            return self._user
321