1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
18
# Module: dispatch.request
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
28
from mod_python import (util, Session)
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.
34
Request object attributes:
36
String. The request method (eg. 'GET', 'POST', etc)
38
String. The path portion of the URI.
40
String. Name of the application specified in the URL, or None.
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".
46
String. Login name of the user who is currently logged in, or
49
Table object representing headers sent by the client.
50
headers_out (read, can be written to)
51
Table object representing headers to be sent to the client.
54
Int. Response status number. Use one of the status codes defined
57
String. The Content-Type (mime type) header value.
59
String. Response "Location" header value. Used with HTTP redirect
62
String. HTML page title. Used if write_html_head_foot is True, in
63
the HTML title element text.
65
List of strings. Write a list of URLs to CSS files here, and they
66
will be incorporated as <link rel="stylesheet" type="text/css">
67
elements in the head, if write_html_head_foot is True.
68
URLs should be relative to the IVLE root; they will be fixed up
71
List of strings. Write a list of URLs to JS files here, and they
72
will be incorporated as <script type="text/javascript"> elements
73
in the head, if write_html_head_foot is True.
74
URLs should be relative to the IVLE root; they will be fixed up
76
write_html_head_foot (write)
77
Boolean. If True, dispatch assumes that this is an XHTML page, and
78
will immediately write a full HTML head, open the body element,
79
and write heading contents to the page, before any bytes are
80
written. It will then write footer contents and close the body and
81
html elements at the end of execution.
83
This value should be set to true by all applications for all HTML
84
output (unless there is a good reason, eg. exec). The
85
applications should therefore output HTML content assuming that
86
it will be written inside the body tag. Do not write opening or
87
closing <html> or <body> tags.
90
# Special code for an OK response.
91
# Do not use HTTP_OK; for some reason Apache produces an "OK" error
92
# message if you do that.
98
HTTP_SWITCHING_PROTOCOLS = 101
103
HTTP_NON_AUTHORITATIVE = 203
104
HTTP_NO_CONTENT = 204
105
HTTP_RESET_CONTENT = 205
106
HTTP_PARTIAL_CONTENT = 206
107
HTTP_MULTI_STATUS = 207
108
HTTP_MULTIPLE_CHOICES = 300
109
HTTP_MOVED_PERMANENTLY = 301
110
HTTP_MOVED_TEMPORARILY = 302
112
HTTP_NOT_MODIFIED = 304
114
HTTP_TEMPORARY_REDIRECT = 307
115
HTTP_BAD_REQUEST = 400
116
HTTP_UNAUTHORIZED = 401
117
HTTP_PAYMENT_REQUIRED = 402
120
HTTP_METHOD_NOT_ALLOWED = 405
121
HTTP_NOT_ACCEPTABLE = 406
122
HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
123
HTTP_REQUEST_TIME_OUT = 408
126
HTTP_LENGTH_REQUIRED = 411
127
HTTP_PRECONDITION_FAILED = 412
128
HTTP_REQUEST_ENTITY_TOO_LARGE = 413
129
HTTP_REQUEST_URI_TOO_LARGE = 414
130
HTTP_UNSUPPORTED_MEDIA_TYPE = 415
131
HTTP_RANGE_NOT_SATISFIABLE = 416
132
HTTP_EXPECTATION_FAILED = 417
133
HTTP_UNPROCESSABLE_ENTITY = 422
135
HTTP_FAILED_DEPENDENCY = 424
136
HTTP_INTERNAL_SERVER_ERROR = 500
137
HTTP_NOT_IMPLEMENTED = 501
138
HTTP_BAD_GATEWAY = 502
139
HTTP_SERVICE_UNAVAILABLE = 503
140
HTTP_GATEWAY_TIME_OUT = 504
141
HTTP_VERSION_NOT_SUPPORTED = 505
142
HTTP_VARIANT_ALSO_VARIES = 506
143
HTTP_INSUFFICIENT_STORAGE = 507
144
HTTP_NOT_EXTENDED = 510
146
def __init__(self, req, write_html_head):
147
"""Builds an IVLE request object from a mod_python request object.
148
This results in an object with all of the necessary methods and
151
req: A mod_python request object.
152
write_html_head: Function which is called when writing the automatic
153
HTML header. Accepts a single argument, the IVLE request object.
156
# Methods are mostly wrappers around the Apache request object
157
self.apache_req = req
158
self.func_write_html_head = write_html_head
159
self.headers_written = False
161
# Inherit values for the input members
162
self.method = req.method
164
# Split the given path into the app (top-level dir) and sub-path
165
# (after first stripping away the root directory)
166
(self.app, self.path) = (
167
common.util.split_path(common.util.unmake_path(req.uri)))
169
self.headers_in = req.headers_in
170
self.headers_out = req.headers_out
172
# Default values for the output members
173
self.status = Request.HTTP_OK
174
self.content_type = None # Use Apache's default
176
self.title = None # Will be set by dispatch before passing to app
179
self.write_html_head_foot = False
181
def __writeheaders(self):
182
"""Writes out the HTTP and HTML headers before any real data is
184
self.headers_written = True
185
# Prepare the HTTP and HTML headers before the first write is made
186
if self.content_type != None:
187
self.apache_req.content_type = self.content_type
188
self.apache_req.status = self.status
189
if self.location != None:
190
self.apache_req.headers_out['Location'] = self.location
191
if self.write_html_head_foot:
192
# Write the HTML header, pass "self" (request object)
193
self.func_write_html_head(self)
195
def ensure_headers_written(self):
196
"""Writes out the HTTP and HTML headers if they haven't already been
198
if not self.headers_written:
199
self.__writeheaders()
201
def write(self, string, flush=1):
202
"""Writes string directly to the client, then flushes the buffer,
203
unless flush is 0."""
205
if not self.headers_written:
206
self.__writeheaders()
207
self.apache_req.write(string, flush)
210
"""Flushes the output buffer."""
211
self.apache_req.flush()
213
def sendfile(self, filename):
214
"""Sends the named file directly to the client."""
215
if not self.headers_written:
216
self.__writeheaders()
217
self.apache_req.sendfile(filename)
219
def read(self, len=None):
220
"""Reads at most len bytes directly from the client. (See mod_python
223
self.apache_req.read()
225
self.apache_req.read(len)
227
def throw_error(self, httpcode):
228
"""Writes out an HTTP error of the specified code. Raises an exception
229
which is caught by the dispatch or web server, so any code following
230
this call will not be executed.
232
httpcode: An HTTP response status code. Pass a constant from the
235
raise mod_python.apache.SERVER_RETURN, httpcode
237
def throw_redirect(self, location):
238
"""Writes out an HTTP redirect to the specified URL. Raises an
239
exception which is caught by the dispatch or web server, so any
240
code following this call will not be executed.
242
httpcode: An HTTP response status code. Pass a constant from the
245
mod_python.util.redirect(self.apache_req, location)
247
def get_session(self):
248
"""Returns a mod_python Session object for this request.
249
Note that this is dependent on mod_python and may need to change
250
interface if porting away from mod_python."""
251
# Cache the session object
252
if not hasattr(self, 'session'):
253
self.session = Session.Session(self.apache_req)
256
def get_fieldstorage(self):
257
"""Returns a mod_python FieldStorage object for this request.
258
Note that this is dependent on mod_python and may need to change
259
interface if porting away from mod_python."""
260
# Cache the fieldstorage object
261
if not hasattr(self, 'fields'):
262
self.fields = util.FieldStorage(self.apache_req)