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

« back to all changes in this revision

Viewing changes to src/dispatch/request.py

  • Committer: mattgiuca
  • Date: 2007-12-12 04:22:22 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:41
Moved Tom's notes from README (application doc) to notes/session.txt
(development doc).

Show diffs side-by-side

added added

removed removed

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