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

« back to all changes in this revision

Viewing changes to www/dispatch/request.py

  • Committer: stevenbird
  • Date: 2008-02-19 21:17:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:512
Renaming of problems to exercises (initial commit).
Fix up module naming (exercises sometimes called tutorials).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import common.util
27
27
import mod_python
28
 
from mod_python import (util, Session)
 
28
from mod_python import (util, Session, Cookie)
 
29
import conf
29
30
 
30
31
class Request:
31
32
    """An IVLE request object. This is presented to the IVLE apps as a way of
42
43
            String. The path specified in the URL *not including* the
43
44
            application or the IVLE location prefix. eg. a URL of
44
45
            "/ivle/files/joe/myfiles" has a path of "joe/myfiles".
45
 
        username (read)
46
 
            String. Login name of the user who is currently logged in, or
 
46
        user (read)
 
47
            User object. Details of the user who is currently logged in, or
47
48
            None.
 
49
        hostname (read)
 
50
            String. Hostname the server is running on.
 
51
        headers_in (read)
 
52
            Table object representing headers sent by the client.
 
53
        headers_out (read, can be written to)
 
54
            Table object representing headers to be sent to the client.
 
55
        publicmode (read)
 
56
            Bool. True if the request came for the "public host" as
 
57
            configured in conf.py. Note that public mode requests do not
 
58
            have an app (app is set to None).
48
59
 
49
60
        status (write)
50
61
            Int. Response status number. Use one of the status codes defined
57
68
        title (write)
58
69
            String. HTML page title. Used if write_html_head_foot is True, in
59
70
            the HTML title element text.
 
71
        styles (write)
 
72
            List of strings. Write a list of URLs to CSS files here, and they
 
73
            will be incorporated as <link rel="stylesheet" type="text/css">
 
74
            elements in the head, if write_html_head_foot is True.
 
75
            URLs should be relative to the IVLE root; they will be fixed up
 
76
            to be site-relative.
 
77
        scripts (write)
 
78
            List of strings. Write a list of URLs to JS files here, and they
 
79
            will be incorporated as <script type="text/javascript"> elements
 
80
            in the head, if write_html_head_foot is True.
 
81
            URLs should be relative to the IVLE root; they will be fixed up
 
82
            to be site-relative.
60
83
        write_html_head_foot (write)
61
84
            Boolean. If True, dispatch assumes that this is an XHTML page, and
62
85
            will immediately write a full HTML head, open the body element,
142
165
        self.func_write_html_head = write_html_head
143
166
        self.headers_written = False
144
167
 
 
168
        # Determine if the browser used the public host name to make the
 
169
        # request (in which case we are in "public mode")
 
170
        if req.hostname == conf.public_host:
 
171
            self.publicmode = True
 
172
        else:
 
173
            self.publicmode = False
 
174
 
145
175
        # Inherit values for the input members
146
176
        self.method = req.method
147
177
        self.uri = req.uri
148
178
        # Split the given path into the app (top-level dir) and sub-path
149
179
        # (after first stripping away the root directory)
150
 
        (self.app, self.path) = (
151
 
            common.util.split_path(common.util.unmake_path(req.uri)))
152
 
        self.username = None
 
180
        path = common.util.unmake_path(req.uri)
 
181
        if self.publicmode:
 
182
            self.app = None
 
183
            self.path = path
 
184
        else:
 
185
            (self.app, self.path) = (common.util.split_path(path))
 
186
        self.user = None
 
187
        self.hostname = req.hostname
 
188
        self.headers_in = req.headers_in
 
189
        self.headers_out = req.headers_out
153
190
 
154
191
        # Default values for the output members
155
 
        self.status = Request.OK
 
192
        self.status = Request.HTTP_OK
156
193
        self.content_type = None        # Use Apache's default
157
194
        self.location = None
158
195
        self.title = None     # Will be set by dispatch before passing to app
 
196
        self.styles = []
 
197
        self.scripts = []
159
198
        self.write_html_head_foot = False
 
199
        self.got_common_vars = False
160
200
 
161
201
    def __writeheaders(self):
162
202
        """Writes out the HTTP and HTML headers before any real data is
184
224
 
185
225
        if not self.headers_written:
186
226
            self.__writeheaders()
187
 
        self.apache_req.write(string, flush)
 
227
        if isinstance(string, unicode):
 
228
            # Encode unicode strings as UTF-8
 
229
            # (Otherwise cannot handle being written to a bytestream)
 
230
            self.apache_req.write(string.encode('utf8'), flush)
 
231
        else:
 
232
            # 8-bit clean strings just get written directly.
 
233
            # This includes binary strings.
 
234
            self.apache_req.write(string, flush)
188
235
 
189
236
    def flush(self):
190
237
        """Flushes the output buffer."""
200
247
        """Reads at most len bytes directly from the client. (See mod_python
201
248
        Request.read)."""
202
249
        if len is None:
203
 
            self.apache_req.read()
 
250
            return self.apache_req.read()
204
251
        else:
205
 
            self.apache_req.read(len)
 
252
            return self.apache_req.read(len)
206
253
 
207
254
    def throw_error(self, httpcode):
208
255
        """Writes out an HTTP error of the specified code. Raises an exception
224
271
        """
225
272
        mod_python.util.redirect(self.apache_req, location)
226
273
 
 
274
    def add_cookie(self, cookie, value=None, **attributes):
 
275
        """Inserts a cookie into this request object's headers."""
 
276
        if value is None:
 
277
            Cookie.add_cookie(self.apache_req, cookie)
 
278
        else:
 
279
            Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
 
280
 
227
281
    def get_session(self):
228
282
        """Returns a mod_python Session object for this request.
229
283
        Note that this is dependent on mod_python and may need to change
230
284
        interface if porting away from mod_python."""
231
 
        # Cache the session object
 
285
        # Cache the session object and set the timeout to 24 hours.
232
286
        if not hasattr(self, 'session'):
233
 
            self.session = Session.Session(self.apache_req)
 
287
            self.session = Session.FileSession(self.apache_req,
 
288
                                               timeout = 60 * 60 * 24)
234
289
        return self.session
235
290
 
236
291
    def get_fieldstorage(self):
241
296
        if not hasattr(self, 'fields'):
242
297
            self.fields = util.FieldStorage(self.apache_req)
243
298
        return self.fields
 
299
 
 
300
    def get_cgi_environ(self):
 
301
        """Returns the CGI environment emulation for this request. (Calls
 
302
        add_common_vars). The environment is returned as a mapping
 
303
        compatible with os.environ."""
 
304
        if not self.got_common_vars:
 
305
            self.apache_req.add_common_vars()
 
306
            self.got_common_vars = True
 
307
        return self.apache_req.subprocess_env