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
50
Int. Response status number. Use one of the status codes defined
53
String. The Content-Type (mime type) header value.
55
String. Response "Location" header value. Used with HTTP redirect
58
String. HTML page title. Used if write_html_head_foot is True, in
59
the HTML title element text.
60
write_html_head_foot (write)
61
Boolean. If True, dispatch assumes that this is an XHTML page, and
62
will immediately write a full HTML head, open the body element,
63
and write heading contents to the page, before any bytes are
64
written. It will then write footer contents and close the body and
65
html elements at the end of execution.
67
This value should be set to true by all applications for all HTML
68
output (unless there is a good reason, eg. exec). The
69
applications should therefore output HTML content assuming that
70
it will be written inside the body tag. Do not write opening or
71
closing <html> or <body> tags.
74
# Special code for an OK response.
75
# Do not use HTTP_OK; for some reason Apache produces an "OK" error
76
# message if you do that.
82
HTTP_SWITCHING_PROTOCOLS = 101
87
HTTP_NON_AUTHORITATIVE = 203
89
HTTP_RESET_CONTENT = 205
90
HTTP_PARTIAL_CONTENT = 206
91
HTTP_MULTI_STATUS = 207
92
HTTP_MULTIPLE_CHOICES = 300
93
HTTP_MOVED_PERMANENTLY = 301
94
HTTP_MOVED_TEMPORARILY = 302
96
HTTP_NOT_MODIFIED = 304
98
HTTP_TEMPORARY_REDIRECT = 307
99
HTTP_BAD_REQUEST = 400
100
HTTP_UNAUTHORIZED = 401
101
HTTP_PAYMENT_REQUIRED = 402
104
HTTP_METHOD_NOT_ALLOWED = 405
105
HTTP_NOT_ACCEPTABLE = 406
106
HTTP_PROXY_AUTHENTICATION_REQUIRED= 407
107
HTTP_REQUEST_TIME_OUT = 408
110
HTTP_LENGTH_REQUIRED = 411
111
HTTP_PRECONDITION_FAILED = 412
112
HTTP_REQUEST_ENTITY_TOO_LARGE = 413
113
HTTP_REQUEST_URI_TOO_LARGE = 414
114
HTTP_UNSUPPORTED_MEDIA_TYPE = 415
115
HTTP_RANGE_NOT_SATISFIABLE = 416
116
HTTP_EXPECTATION_FAILED = 417
117
HTTP_UNPROCESSABLE_ENTITY = 422
119
HTTP_FAILED_DEPENDENCY = 424
120
HTTP_INTERNAL_SERVER_ERROR = 500
121
HTTP_NOT_IMPLEMENTED = 501
122
HTTP_BAD_GATEWAY = 502
123
HTTP_SERVICE_UNAVAILABLE = 503
124
HTTP_GATEWAY_TIME_OUT = 504
125
HTTP_VERSION_NOT_SUPPORTED = 505
126
HTTP_VARIANT_ALSO_VARIES = 506
127
HTTP_INSUFFICIENT_STORAGE = 507
128
HTTP_NOT_EXTENDED = 510
130
def __init__(self, req, write_html_head):
131
"""Builds an IVLE request object from a mod_python request object.
132
This results in an object with all of the necessary methods and
135
req: A mod_python request object.
136
write_html_head: Function which is called when writing the automatic
137
HTML header. Accepts a single argument, the IVLE request object.
140
# Methods are mostly wrappers around the Apache request object
141
self.apache_req = req
142
self.func_write_html_head = write_html_head
143
self.headers_written = False
145
# Inherit values for the input members
146
self.method = req.method
148
# Split the given path into the app (top-level dir) and sub-path
149
# (after first stripping away the root directory)
150
(self.app, self.path) = (
151
common.util.split_path(common.util.unmake_path(req.uri)))
154
# Default values for the output members
155
self.status = Request.OK
156
self.content_type = None # Use Apache's default
158
self.title = None # Will be set by dispatch before passing to app
159
self.write_html_head_foot = False
161
def __writeheaders(self):
162
"""Writes out the HTTP and HTML headers before any real data is
164
self.headers_written = True
165
# Prepare the HTTP and HTML headers before the first write is made
166
if self.content_type != None:
167
self.apache_req.content_type = self.content_type
168
self.apache_req.status = self.status
169
if self.location != None:
170
self.apache_req.headers_out['Location'] = self.location
171
if self.write_html_head_foot:
172
# Write the HTML header, pass "self" (request object)
173
self.func_write_html_head(self)
175
def ensure_headers_written(self):
176
"""Writes out the HTTP and HTML headers if they haven't already been
178
if not self.headers_written:
179
self.__writeheaders()
181
def write(self, string, flush=1):
182
"""Writes string directly to the client, then flushes the buffer,
183
unless flush is 0."""
185
if not self.headers_written:
186
self.__writeheaders()
187
self.apache_req.write(string, flush)
190
"""Flushes the output buffer."""
191
self.apache_req.flush()
193
def sendfile(self, filename):
194
"""Sends the named file directly to the client."""
195
if not self.headers_written:
196
self.__writeheaders()
197
self.apache_req.sendfile(filename)
199
def read(self, len=None):
200
"""Reads at most len bytes directly from the client. (See mod_python
203
self.apache_req.read()
205
self.apache_req.read(len)
207
def throw_error(self, httpcode):
208
"""Writes out an HTTP error of the specified code. Raises an exception
209
which is caught by the dispatch or web server, so any code following
210
this call will not be executed.
212
httpcode: An HTTP response status code. Pass a constant from the
215
raise mod_python.apache.SERVER_RETURN, httpcode
217
def throw_redirect(self, location):
218
"""Writes out an HTTP redirect to the specified URL. Raises an
219
exception which is caught by the dispatch or web server, so any
220
code following this call will not be executed.
222
httpcode: An HTTP response status code. Pass a constant from the
225
mod_python.util.redirect(self.apache_req, location)
227
def get_session(self):
228
"""Returns a mod_python Session object for this request.
229
Note that this is dependent on mod_python and may need to change
230
interface if porting away from mod_python."""
231
# Cache the session object
232
if not hasattr(self, 'session'):
233
self.session = Session.Session(self.apache_req)
236
def get_fieldstorage(self):
237
"""Returns a mod_python FieldStorage object for this request.
238
Note that this is dependent on mod_python and may need to change
239
interface if porting away from mod_python."""
240
# Cache the fieldstorage object
241
if not hasattr(self, 'fields'):
242
self.fields = util.FieldStorage(self.apache_req)