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

« back to all changes in this revision

Viewing changes to www/dispatch/request.py

Created a new help system.

The new help system allows plugins to register their help paths, using
a dict of dicts and helpfile paths. This system allows multiple plugins
to use the same heading for their help files (ie. 'admin').

It also allows XHTMLViews to have a 'help' property, which makes the
context-sensitive help link point to their help path.

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
 
import mod_python
28
 
from mod_python import (util, Session)
29
 
 
30
 
class Request:
31
 
    """An IVLE request object. This is presented to the IVLE apps as a way of
32
 
    interacting with the web server and the dispatcher.
33
 
 
34
 
    Request object attributes:
35
 
        method (read)
36
 
            String. The request method (eg. 'GET', 'POST', etc)
37
 
        uri (read)
38
 
            String. The path portion of the URI.
39
 
        app (read)
40
 
            String. Name of the application specified in the URL, or None.
41
 
        path (read)
42
 
            String. The path specified in the URL *not including* the
43
 
            application or the IVLE location prefix. eg. a URL of
44
 
            "/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
47
 
            None.
48
 
        hostname (read)
49
 
            String. Hostname the server is running on.
50
 
        headers_in (read)
51
 
            Table object representing headers sent by the client.
52
 
        headers_out (read, can be written to)
53
 
            Table object representing headers to be sent to the client.
54
 
 
55
 
        status (write)
56
 
            Int. Response status number. Use one of the status codes defined
57
 
            in class Request.
58
 
        content_type (write)
59
 
            String. The Content-Type (mime type) header value.
60
 
        location (write)
61
 
            String. Response "Location" header value. Used with HTTP redirect
62
 
            responses.
63
 
        title (write)
64
 
            String. HTML page title. Used if write_html_head_foot is True, in
65
 
            the HTML title element text.
66
 
        styles (write)
67
 
            List of strings. Write a list of URLs to CSS files here, and they
68
 
            will be incorporated as <link rel="stylesheet" type="text/css">
69
 
            elements in the head, if write_html_head_foot is True.
70
 
            URLs should be relative to the IVLE root; they will be fixed up
71
 
            to be site-relative.
72
 
        scripts (write)
73
 
            List of strings. Write a list of URLs to JS files here, and they
74
 
            will be incorporated as <script type="text/javascript"> elements
75
 
            in the head, if write_html_head_foot is True.
76
 
            URLs should be relative to the IVLE root; they will be fixed up
77
 
            to be site-relative.
78
 
        write_html_head_foot (write)
79
 
            Boolean. If True, dispatch assumes that this is an XHTML page, and
80
 
            will immediately write a full HTML head, open the body element,
81
 
            and write heading contents to the page, before any bytes are
82
 
            written. It will then write footer contents and close the body and
83
 
            html elements at the end of execution.  
84
 
 
85
 
            This value should be set to true by all applications for all HTML
86
 
            output (unless there is a good reason, eg. exec). The
87
 
            applications should therefore output HTML content assuming that
88
 
            it will be written inside the body tag. Do not write opening or
89
 
            closing <html> or <body> tags.
90
 
    """
91
 
 
92
 
    # Special code for an OK response.
93
 
    # Do not use HTTP_OK; for some reason Apache produces an "OK" error
94
 
    # message if you do that.
95
 
    OK  = 0
96
 
 
97
 
    # HTTP status codes
98
 
 
99
 
    HTTP_CONTINUE                     = 100
100
 
    HTTP_SWITCHING_PROTOCOLS          = 101
101
 
    HTTP_PROCESSING                   = 102
102
 
    HTTP_OK                           = 200
103
 
    HTTP_CREATED                      = 201
104
 
    HTTP_ACCEPTED                     = 202
105
 
    HTTP_NON_AUTHORITATIVE            = 203
106
 
    HTTP_NO_CONTENT                   = 204
107
 
    HTTP_RESET_CONTENT                = 205
108
 
    HTTP_PARTIAL_CONTENT              = 206
109
 
    HTTP_MULTI_STATUS                 = 207
110
 
    HTTP_MULTIPLE_CHOICES             = 300
111
 
    HTTP_MOVED_PERMANENTLY            = 301
112
 
    HTTP_MOVED_TEMPORARILY            = 302
113
 
    HTTP_SEE_OTHER                    = 303
114
 
    HTTP_NOT_MODIFIED                 = 304
115
 
    HTTP_USE_PROXY                    = 305
116
 
    HTTP_TEMPORARY_REDIRECT           = 307
117
 
    HTTP_BAD_REQUEST                  = 400
118
 
    HTTP_UNAUTHORIZED                 = 401
119
 
    HTTP_PAYMENT_REQUIRED             = 402
120
 
    HTTP_FORBIDDEN                    = 403
121
 
    HTTP_NOT_FOUND                    = 404
122
 
    HTTP_METHOD_NOT_ALLOWED           = 405
123
 
    HTTP_NOT_ACCEPTABLE               = 406
124
 
    HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
125
 
    HTTP_REQUEST_TIME_OUT             = 408
126
 
    HTTP_CONFLICT                     = 409
127
 
    HTTP_GONE                         = 410
128
 
    HTTP_LENGTH_REQUIRED              = 411
129
 
    HTTP_PRECONDITION_FAILED          = 412
130
 
    HTTP_REQUEST_ENTITY_TOO_LARGE     = 413
131
 
    HTTP_REQUEST_URI_TOO_LARGE        = 414
132
 
    HTTP_UNSUPPORTED_MEDIA_TYPE       = 415
133
 
    HTTP_RANGE_NOT_SATISFIABLE        = 416
134
 
    HTTP_EXPECTATION_FAILED           = 417
135
 
    HTTP_UNPROCESSABLE_ENTITY         = 422
136
 
    HTTP_LOCKED                       = 423
137
 
    HTTP_FAILED_DEPENDENCY            = 424
138
 
    HTTP_INTERNAL_SERVER_ERROR        = 500
139
 
    HTTP_NOT_IMPLEMENTED              = 501
140
 
    HTTP_BAD_GATEWAY                  = 502
141
 
    HTTP_SERVICE_UNAVAILABLE          = 503
142
 
    HTTP_GATEWAY_TIME_OUT             = 504
143
 
    HTTP_VERSION_NOT_SUPPORTED        = 505
144
 
    HTTP_VARIANT_ALSO_VARIES          = 506
145
 
    HTTP_INSUFFICIENT_STORAGE         = 507
146
 
    HTTP_NOT_EXTENDED                 = 510
147
 
 
148
 
    def __init__(self, req, write_html_head):
149
 
        """Builds an IVLE request object from a mod_python request object.
150
 
        This results in an object with all of the necessary methods and
151
 
        additional fields.
152
 
 
153
 
        req: A mod_python request object.
154
 
        write_html_head: Function which is called when writing the automatic
155
 
            HTML header. Accepts a single argument, the IVLE request object.
156
 
        """
157
 
 
158
 
        # Methods are mostly wrappers around the Apache request object
159
 
        self.apache_req = req
160
 
        self.func_write_html_head = write_html_head
161
 
        self.headers_written = False
162
 
 
163
 
        # Inherit values for the input members
164
 
        self.method = req.method
165
 
        self.uri = req.uri
166
 
        # Split the given path into the app (top-level dir) and sub-path
167
 
        # (after first stripping away the root directory)
168
 
        (self.app, self.path) = (
169
 
            common.util.split_path(common.util.unmake_path(req.uri)))
170
 
        self.username = None
171
 
        self.hostname = req.hostname
172
 
        self.headers_in = req.headers_in
173
 
        self.headers_out = req.headers_out
174
 
 
175
 
        # Default values for the output members
176
 
        self.status = Request.HTTP_OK
177
 
        self.content_type = None        # Use Apache's default
178
 
        self.location = None
179
 
        self.title = None     # Will be set by dispatch before passing to app
180
 
        self.styles = []
181
 
        self.scripts = []
182
 
        self.write_html_head_foot = False
183
 
        self.got_common_vars = False
184
 
 
185
 
    def __writeheaders(self):
186
 
        """Writes out the HTTP and HTML headers before any real data is
187
 
        written."""
188
 
        self.headers_written = True
189
 
        # Prepare the HTTP and HTML headers before the first write is made
190
 
        if self.content_type != None:
191
 
            self.apache_req.content_type = self.content_type
192
 
        self.apache_req.status = self.status
193
 
        if self.location != None:
194
 
            self.apache_req.headers_out['Location'] = self.location
195
 
        if self.write_html_head_foot:
196
 
            # Write the HTML header, pass "self" (request object)
197
 
            self.func_write_html_head(self)
198
 
 
199
 
    def ensure_headers_written(self):
200
 
        """Writes out the HTTP and HTML headers if they haven't already been
201
 
        written."""
202
 
        if not self.headers_written:
203
 
            self.__writeheaders()
204
 
 
205
 
    def write(self, string, flush=1):
206
 
        """Writes string directly to the client, then flushes the buffer,
207
 
        unless flush is 0."""
208
 
 
209
 
        if not self.headers_written:
210
 
            self.__writeheaders()
211
 
        self.apache_req.write(string, flush)
212
 
 
213
 
    def flush(self):
214
 
        """Flushes the output buffer."""
215
 
        self.apache_req.flush()
216
 
 
217
 
    def sendfile(self, filename):
218
 
        """Sends the named file directly to the client."""
219
 
        if not self.headers_written:
220
 
            self.__writeheaders()
221
 
        self.apache_req.sendfile(filename)
222
 
 
223
 
    def read(self, len=None):
224
 
        """Reads at most len bytes directly from the client. (See mod_python
225
 
        Request.read)."""
226
 
        if len is None:
227
 
            return self.apache_req.read()
228
 
        else:
229
 
            return self.apache_req.read(len)
230
 
 
231
 
    def throw_error(self, httpcode):
232
 
        """Writes out an HTTP error of the specified code. Raises an exception
233
 
        which is caught by the dispatch or web server, so any code following
234
 
        this call will not be executed.
235
 
 
236
 
        httpcode: An HTTP response status code. Pass a constant from the
237
 
        Request class.
238
 
        """
239
 
        raise mod_python.apache.SERVER_RETURN, httpcode
240
 
 
241
 
    def throw_redirect(self, location):
242
 
        """Writes out an HTTP redirect to the specified URL. Raises an
243
 
        exception which is caught by the dispatch or web server, so any
244
 
        code following this call will not be executed.
245
 
 
246
 
        httpcode: An HTTP response status code. Pass a constant from the
247
 
        Request class.
248
 
        """
249
 
        mod_python.util.redirect(self.apache_req, location)
250
 
 
251
 
    def get_session(self):
252
 
        """Returns a mod_python Session object for this request.
253
 
        Note that this is dependent on mod_python and may need to change
254
 
        interface if porting away from mod_python."""
255
 
        # Cache the session object
256
 
        if not hasattr(self, 'session'):
257
 
            self.session = Session.Session(self.apache_req)
258
 
        return self.session
259
 
 
260
 
    def get_fieldstorage(self):
261
 
        """Returns a mod_python FieldStorage object for this request.
262
 
        Note that this is dependent on mod_python and may need to change
263
 
        interface if porting away from mod_python."""
264
 
        # Cache the fieldstorage object
265
 
        if not hasattr(self, 'fields'):
266
 
            self.fields = util.FieldStorage(self.apache_req)
267
 
        return self.fields
268
 
 
269
 
    def get_cgi_environ(self):
270
 
        """Returns the CGI environment emulation for this request. (Calls
271
 
        add_common_vars). The environment is returned as a mapping
272
 
        compatible with os.environ."""
273
 
        if not self.got_common_vars:
274
 
            self.apache_req.add_common_vars()
275
 
            self.got_common_vars = True
276
 
        return self.apache_req.subprocess_env