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

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