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