27
27
"""An IVLE request object. This is presented to the IVLE apps as a way of
28
interacting with the web server and the dispatcher."""
30
# Special code for "everything's OK" (somehow different to 200).
28
interacting with the web server and the dispatcher.
30
Request object attributes:
32
String. The path portion of the URI.
35
Int. Response status number. Use one of the status codes defined
38
String. The Content-Type (mime type) header value.
40
String. Response "Location" header value. Used with HTTP redirect
43
String. HTML page title. Used if write_html_head_foot is True, in
44
the HTML title element text.
45
write_html_head_foot (write)
46
Boolean. If True, dispatch assumes that this is an XHTML page, and
47
will immediately write a full HTML head, open the body element,
48
and write heading contents to the page, before any bytes are
49
written. It will then write footer contents and close the body and
50
html elements at the end of execution.
52
This value should be set to true by all applications for all HTML
53
output (unless there is a good reason, eg. exec). The
54
applications should therefore output HTML content assuming that
55
it will be written inside the body tag. Do not write opening or
56
closing <html> or <body> tags.
59
# Special code for an OK response.
60
# Do not use HTTP_OK; for some reason Apache produces an "OK" error
61
# message if you do that.
33
64
# HTTP status codes
81
112
HTTP_INSUFFICIENT_STORAGE = 507
82
113
HTTP_NOT_EXTENDED = 510
84
def __init__(self, req):
115
def __init__(self, req, write_html_head):
85
116
"""Builds an IVLE request object from a mod_python request object.
86
117
This results in an object with all of the necessary methods and
87
118
additional fields.
89
120
req: A mod_python request object.
121
write_html_head: Function which is called when writing the automatic
122
HTML header. Accepts a single argument, the IVLE request object.
92
125
# Methods are mostly wrappers around the Apache request object
93
126
self.apache_req = req
127
self.func_write_html_head = write_html_head
128
self.headers_written = False
95
130
# Inherit values for the input members
96
131
self.uri = req.uri
106
141
"""Writes string directly to the client, then flushes the buffer,
107
142
unless flush is 0."""
109
# TODO: Prepare the IVLE HTTP and HTML headers before the first write.
144
if not self.headers_written:
145
self.headers_written = True
146
# Prepare the HTTP and HTML headers before the first write is made
147
if self.content_type != None:
148
self.apache_req.content_type = self.content_type
149
self.apache_req.status = self.status
150
if self.location != None:
151
self.apache_req.headers_out['Location'] = self.location
152
if self.write_html_head_foot:
153
# Write the HTML header, pass "self" (request object)
154
self.func_write_html_head(self)
110
156
self.apache_req.write(string, flush)