~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 02:21:50 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:38
dispatch: Request now fully handles all attributes by setting the correct
    headers when it writes, and calling the given HTML function.
    New module html which provides functions to write HTML header and footer.
    __init__ uses this more formally, abstracting the "test application"
        function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
class Request:
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."""
29
 
 
30
 
    # Special code for "everything's OK" (somehow different to 200).
 
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
 
 
34
        status (write)
 
35
            Int. Response status number. Use one of the status codes defined
 
36
            in class Request.
 
37
        content_type (write)
 
38
            String. The Content-Type (mime type) header value.
 
39
        location (write)
 
40
            String. Response "Location" header value. Used with HTTP redirect
 
41
            responses.
 
42
        title (write)
 
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.  
 
51
 
 
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.
 
57
    """
 
58
 
 
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.
31
62
    OK  = 0
32
63
 
33
64
    # HTTP status codes
81
112
    HTTP_INSUFFICIENT_STORAGE         = 507
82
113
    HTTP_NOT_EXTENDED                 = 510
83
114
 
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.
88
119
 
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.
90
123
        """
91
124
 
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
94
129
 
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."""
108
143
 
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)
 
155
 
110
156
        self.apache_req.write(string, flush)
111
157
 
112
158
    def flush(self):