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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

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)
76
95
            in class Request.
77
96
        content_type (write)
78
97
            String. The Content-Type (mime type) header value.
 
98
        content_length (write)
 
99
            Integer. The number of octets to be transfered.
79
100
        location (write)
80
101
            String. Response "Location" header value. Used with HTTP redirect
81
102
            responses.
94
115
    HTTP_NOT_FOUND                    = 404
95
116
    HTTP_INTERNAL_SERVER_ERROR        = 500
96
117
 
 
118
    _store = None
 
119
 
97
120
    def __init__(self, req, config):
98
121
        """Create an IVLE request from a mod_python one.
99
122
 
116
139
        # Inherit values for the input members
117
140
        self.method = req.method
118
141
        self.uri = req.uri
 
142
        self.unparsed_uri = req.unparsed_uri
119
143
        # Split the given path into the app (top-level dir) and sub-path
120
144
        # (after first stripping away the root directory)
121
145
        (self.app, self.path) = (ivle.util.split_path(req.uri))
122
 
        self.user = None
123
146
        self.hostname = req.hostname
124
147
        self.headers_in = req.headers_in
125
148
        self.headers_out = req.headers_out
126
149
 
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
150
        # Default values for the output members
132
151
        self.status = Request.HTTP_OK
133
152
        self.content_type = None        # Use Apache's default
 
153
        self.content_length = None        # Don't provide Content-Length
134
154
        self.location = None
135
155
        # In some cases we don't want the template JS (such as the username
136
156
        # and public FQDN) in the output HTML. In that case, set this to 0.
138
158
        self.got_common_vars = False
139
159
 
140
160
    def __del__(self):
141
 
        """Cleanup."""
142
 
        self.store.close()
 
161
        self.cleanup()
 
162
 
 
163
    def cleanup(self):
 
164
        """Cleanup."""
 
165
        if self._store is not None:
 
166
            self._store.close()
 
167
            self._store = None
 
168
 
 
169
    def commit(self):
 
170
        """Cleanup."""
 
171
        if self._store is not None:
 
172
            self._store.commit()
143
173
 
144
174
    def __writeheaders(self):
145
175
        """Writes out the HTTP and HTML headers before any real data is
149
179
        # Prepare the HTTP and HTML headers before the first write is made
150
180
        if self.content_type != None:
151
181
            self.apache_req.content_type = self.content_type
 
182
        if self.content_length:
 
183
            self.apache_req.set_content_length(self.content_length)
152
184
        self.apache_req.status = self.status
153
185
        if self.location != None:
154
186
            self.apache_req.headers_out['Location'] = self.location
247
279
        """
248
280
        # Cache the session object and set the timeout to 24 hours.
249
281
        if not hasattr(self, 'session'):
250
 
            self.session = mod_python.Session.FileSession(self.apache_req,
251
 
                                               timeout = 60 * 60 * 24)
 
282
            self.session = PotentiallySecureFileSession(
 
283
                self.apache_req, timeout = 60 * 60 * 24)
252
284
        return self.session
253
285
 
254
286
    def get_fieldstorage(self):
269
301
            self.got_common_vars = True
270
302
        return self.apache_req.subprocess_env
271
303
 
 
304
    @property
 
305
    def store(self):
 
306
        # Open a database connection and transaction, keep it around for users
 
307
        # of the Request object to use.
 
308
        if self._store is None:
 
309
            self._store = ivle.database.get_store(self.config)
 
310
        return self._store
 
311
 
 
312
    @property
 
313
    def user(self):
 
314
        # Get and cache the request user, or None if it's not valid.
 
315
        # This is a property so that we don't create a store unless
 
316
        # some code actually requests the user.
 
317
        try:
 
318
            return self._user
 
319
        except AttributeError:
 
320
            if self.publicmode:
 
321
                self._user = None
 
322
            else:
 
323
                temp_user = ivle.webapp.security.get_user_details(self)
 
324
                if temp_user and temp_user.valid:
 
325
                    self._user = temp_user
 
326
                else:
 
327
                    self._user = None
 
328
            return self._user
 
329