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

« back to all changes in this revision

Viewing changes to src/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:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2008 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Module: dispatch.request
19
 
# Author: Matt Giuca
20
 
# Date:   12/12/2007
21
 
 
22
 
# Builds an IVLE request object from a mod_python request object.
23
 
# See design notes/apps/dispatch.txt for a full specification of this request
24
 
# object.
25
 
 
26
 
import common.util
27
 
 
28
 
class Request:
29
 
    """An IVLE request object. This is presented to the IVLE apps as a way of
30
 
    interacting with the web server and the dispatcher.
31
 
 
32
 
    Request object attributes:
33
 
        uri (read)
34
 
            String. The path portion of the URI.
35
 
        app (read)
36
 
            String. Name of the application specified in the URL, or None.
37
 
        path (read)
38
 
            String. The path specified in the URL *not including* the
39
 
            application or the IVLE location prefix. eg. a URL of
40
 
            "/ivle/files/joe/myfiles" has a path of "joe/myfiles".
41
 
 
42
 
        status (write)
43
 
            Int. Response status number. Use one of the status codes defined
44
 
            in class Request.
45
 
        content_type (write)
46
 
            String. The Content-Type (mime type) header value.
47
 
        location (write)
48
 
            String. Response "Location" header value. Used with HTTP redirect
49
 
            responses.
50
 
        title (write)
51
 
            String. HTML page title. Used if write_html_head_foot is True, in
52
 
            the HTML title element text.
53
 
        write_html_head_foot (write)
54
 
            Boolean. If True, dispatch assumes that this is an XHTML page, and
55
 
            will immediately write a full HTML head, open the body element,
56
 
            and write heading contents to the page, before any bytes are
57
 
            written. It will then write footer contents and close the body and
58
 
            html elements at the end of execution.  
59
 
 
60
 
            This value should be set to true by all applications for all HTML
61
 
            output (unless there is a good reason, eg. exec). The
62
 
            applications should therefore output HTML content assuming that
63
 
            it will be written inside the body tag. Do not write opening or
64
 
            closing <html> or <body> tags.
65
 
    """
66
 
 
67
 
    # Special code for an OK response.
68
 
    # Do not use HTTP_OK; for some reason Apache produces an "OK" error
69
 
    # message if you do that.
70
 
    OK  = 0
71
 
 
72
 
    # HTTP status codes
73
 
 
74
 
    HTTP_CONTINUE                     = 100
75
 
    HTTP_SWITCHING_PROTOCOLS          = 101
76
 
    HTTP_PROCESSING                   = 102
77
 
    HTTP_OK                           = 200
78
 
    HTTP_CREATED                      = 201
79
 
    HTTP_ACCEPTED                     = 202
80
 
    HTTP_NON_AUTHORITATIVE            = 203
81
 
    HTTP_NO_CONTENT                   = 204
82
 
    HTTP_RESET_CONTENT                = 205
83
 
    HTTP_PARTIAL_CONTENT              = 206
84
 
    HTTP_MULTI_STATUS                 = 207
85
 
    HTTP_MULTIPLE_CHOICES             = 300
86
 
    HTTP_MOVED_PERMANENTLY            = 301
87
 
    HTTP_MOVED_TEMPORARILY            = 302
88
 
    HTTP_SEE_OTHER                    = 303
89
 
    HTTP_NOT_MODIFIED                 = 304
90
 
    HTTP_USE_PROXY                    = 305
91
 
    HTTP_TEMPORARY_REDIRECT           = 307
92
 
    HTTP_BAD_REQUEST                  = 400
93
 
    HTTP_UNAUTHORIZED                 = 401
94
 
    HTTP_PAYMENT_REQUIRED             = 402
95
 
    HTTP_FORBIDDEN                    = 403
96
 
    HTTP_NOT_FOUND                    = 404
97
 
    HTTP_METHOD_NOT_ALLOWED           = 405
98
 
    HTTP_NOT_ACCEPTABLE               = 406
99
 
    HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
100
 
    HTTP_REQUEST_TIME_OUT             = 408
101
 
    HTTP_CONFLICT                     = 409
102
 
    HTTP_GONE                         = 410
103
 
    HTTP_LENGTH_REQUIRED              = 411
104
 
    HTTP_PRECONDITION_FAILED          = 412
105
 
    HTTP_REQUEST_ENTITY_TOO_LARGE     = 413
106
 
    HTTP_REQUEST_URI_TOO_LARGE        = 414
107
 
    HTTP_UNSUPPORTED_MEDIA_TYPE       = 415
108
 
    HTTP_RANGE_NOT_SATISFIABLE        = 416
109
 
    HTTP_EXPECTATION_FAILED           = 417
110
 
    HTTP_UNPROCESSABLE_ENTITY         = 422
111
 
    HTTP_LOCKED                       = 423
112
 
    HTTP_FAILED_DEPENDENCY            = 424
113
 
    HTTP_INTERNAL_SERVER_ERROR        = 500
114
 
    HTTP_NOT_IMPLEMENTED              = 501
115
 
    HTTP_BAD_GATEWAY                  = 502
116
 
    HTTP_SERVICE_UNAVAILABLE          = 503
117
 
    HTTP_GATEWAY_TIME_OUT             = 504
118
 
    HTTP_VERSION_NOT_SUPPORTED        = 505
119
 
    HTTP_VARIANT_ALSO_VARIES          = 506
120
 
    HTTP_INSUFFICIENT_STORAGE         = 507
121
 
    HTTP_NOT_EXTENDED                 = 510
122
 
 
123
 
    def __init__(self, req, write_html_head):
124
 
        """Builds an IVLE request object from a mod_python request object.
125
 
        This results in an object with all of the necessary methods and
126
 
        additional fields.
127
 
 
128
 
        req: A mod_python request object.
129
 
        write_html_head: Function which is called when writing the automatic
130
 
            HTML header. Accepts a single argument, the IVLE request object.
131
 
        """
132
 
 
133
 
        # Methods are mostly wrappers around the Apache request object
134
 
        self.apache_req = req
135
 
        self.func_write_html_head = write_html_head
136
 
        self.headers_written = False
137
 
 
138
 
        # Inherit values for the input members
139
 
        self.uri = req.uri
140
 
        # Split the given path into the app (top-level dir) and sub-path
141
 
        # (after first stripping away the root directory)
142
 
        (self.app, self.path) = (
143
 
            common.util.split_path(common.util.unmake_path(req.uri)))
144
 
 
145
 
        # Default values for the output members
146
 
        self.status = Request.OK
147
 
        self.content_type = None        # Use Apache's default
148
 
        self.location = None
149
 
        self.title = None     # Will be set by dispatch before passing to app
150
 
        self.write_html_head_foot = False
151
 
 
152
 
    def write(self, string, flush=1):
153
 
        """Writes string directly to the client, then flushes the buffer,
154
 
        unless flush is 0."""
155
 
 
156
 
        if not self.headers_written:
157
 
            self.headers_written = True
158
 
            # Prepare the HTTP and HTML headers before the first write is made
159
 
            if self.content_type != None:
160
 
                self.apache_req.content_type = self.content_type
161
 
            self.apache_req.status = self.status
162
 
            if self.location != None:
163
 
                self.apache_req.headers_out['Location'] = self.location
164
 
            if self.write_html_head_foot:
165
 
                # Write the HTML header, pass "self" (request object)
166
 
                self.func_write_html_head(self)
167
 
 
168
 
        self.apache_req.write(string, flush)
169
 
 
170
 
    def flush(self):
171
 
        """Flushes the output buffer."""
172
 
        self.apache_req.flush()