paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Webware classes. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import Generic |
paulb@71 | 8 | from cgi import parse_qs |
paulb@85 | 9 | import StringIO |
paulb@1 | 10 | |
paulb@1 | 11 | class Transaction(Generic.Transaction): |
paulb@1 | 12 | |
paulb@1 | 13 | """ |
paulb@1 | 14 | Webware transaction interface. |
paulb@1 | 15 | """ |
paulb@1 | 16 | |
paulb@1 | 17 | def __init__(self, trans): |
paulb@1 | 18 | |
paulb@1 | 19 | "Initialise the transaction using the Webware transaction 'trans'." |
paulb@1 | 20 | |
paulb@1 | 21 | self.trans = trans |
paulb@1 | 22 | |
paulb@1 | 23 | # Request-related methods. |
paulb@1 | 24 | |
paulb@1 | 25 | def get_request_stream(self): |
paulb@1 | 26 | |
paulb@1 | 27 | """ |
paulb@1 | 28 | A framework-specific method which returns the request stream for |
paulb@1 | 29 | the transaction. |
paulb@1 | 30 | """ |
paulb@1 | 31 | |
paulb@85 | 32 | request = self.trans.request() |
paulb@85 | 33 | try: |
paulb@85 | 34 | stream = request.rawInput(rewind=1) |
paulb@85 | 35 | if stream is None: |
paulb@85 | 36 | return StringIO.StringIO("") |
paulb@85 | 37 | |
paulb@85 | 38 | # NOTE: Dubious catch-all situation, but it is difficult to control |
paulb@85 | 39 | # NOTE: cases where Webware's internals themselves fail. |
paulb@85 | 40 | |
paulb@85 | 41 | except: |
paulb@1 | 42 | return StringIO.StringIO("") |
paulb@1 | 43 | |
paulb@1 | 44 | return stream |
paulb@1 | 45 | |
paulb@1 | 46 | def get_request_method(self): |
paulb@1 | 47 | |
paulb@1 | 48 | """ |
paulb@1 | 49 | A framework-specific method which gets the request method. |
paulb@1 | 50 | """ |
paulb@1 | 51 | |
paulb@1 | 52 | return self.trans.request().method() |
paulb@1 | 53 | |
paulb@1 | 54 | def get_headers(self): |
paulb@1 | 55 | |
paulb@1 | 56 | """ |
paulb@65 | 57 | A framework-specific method which returns all request headers. |
paulb@1 | 58 | """ |
paulb@1 | 59 | |
paulb@1 | 60 | # NOTE: Webware doesn't really provide access to headers in the request. |
paulb@1 | 61 | |
paulb@1 | 62 | return {} |
paulb@1 | 63 | |
paulb@65 | 64 | def get_header_values(self, key): |
paulb@65 | 65 | |
paulb@65 | 66 | """ |
paulb@65 | 67 | A framework-specific method which returns a list of all request header |
paulb@65 | 68 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 69 | 'key' is treated as a case-insensitive string. |
paulb@65 | 70 | """ |
paulb@65 | 71 | |
paulb@65 | 72 | # NOTE: Webware doesn't really provide access to headers in the request. |
paulb@65 | 73 | |
paulb@65 | 74 | return [] |
paulb@65 | 75 | |
paulb@1 | 76 | def get_content_type(self): |
paulb@1 | 77 | |
paulb@1 | 78 | """ |
paulb@1 | 79 | A framework-specific method which gets the content type specified on the |
paulb@1 | 80 | request, along with the charset employed. |
paulb@1 | 81 | """ |
paulb@1 | 82 | |
paulb@1 | 83 | return self.parse_content_type(self.trans.request().contentType()) |
paulb@1 | 84 | |
paulb@1 | 85 | def get_content_charsets(self): |
paulb@1 | 86 | |
paulb@1 | 87 | """ |
paulb@1 | 88 | Returns the character set preferences. |
paulb@1 | 89 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 90 | """ |
paulb@1 | 91 | |
paulb@1 | 92 | return self.trans.request().contentCharsets() |
paulb@1 | 93 | |
paulb@1 | 94 | def get_content_languages(self): |
paulb@1 | 95 | |
paulb@1 | 96 | """ |
paulb@1 | 97 | A framework-specific method which extracts language information from |
paulb@1 | 98 | the transaction. |
paulb@1 | 99 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 100 | """ |
paulb@1 | 101 | |
paulb@1 | 102 | return self.trans.request().contentLanguages() |
paulb@1 | 103 | |
paulb@5 | 104 | def get_path(self): |
paulb@5 | 105 | |
paulb@5 | 106 | """ |
paulb@5 | 107 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 108 | """ |
paulb@5 | 109 | |
paulb@14 | 110 | return self.trans.request().uri() |
paulb@5 | 111 | |
paulb@1 | 112 | def get_path_info(self): |
paulb@1 | 113 | |
paulb@1 | 114 | """ |
paulb@1 | 115 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 116 | URL after the resource name handling the current request) from the |
paulb@1 | 117 | request. |
paulb@1 | 118 | """ |
paulb@1 | 119 | |
paulb@31 | 120 | # NOTE: Webware does not seem to prefix the "path info" in a way |
paulb@31 | 121 | # NOTE: consistent with the other frameworks. |
paulb@31 | 122 | |
paulb@31 | 123 | return "/" + self.trans.request().pathInfo() |
paulb@1 | 124 | |
paulb@69 | 125 | def get_query_string(self): |
paulb@69 | 126 | |
paulb@69 | 127 | """ |
paulb@69 | 128 | A framework-specific method which gets the query string from the path in |
paulb@69 | 129 | the request. |
paulb@69 | 130 | """ |
paulb@69 | 131 | |
paulb@69 | 132 | return self.trans.request().queryString() |
paulb@69 | 133 | |
paulb@1 | 134 | # Higher level request-related methods. |
paulb@1 | 135 | |
paulb@69 | 136 | def get_fields_from_path(self): |
paulb@69 | 137 | |
paulb@69 | 138 | """ |
paulb@69 | 139 | A framework-specific method which extracts the form fields from the |
paulb@69 | 140 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 141 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 142 | |
paulb@71 | 143 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 144 | single value is associated with any given field name). |
paulb@69 | 145 | """ |
paulb@69 | 146 | |
paulb@71 | 147 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 148 | |
paulb@69 | 149 | def get_fields_from_body(self): |
paulb@69 | 150 | |
paulb@69 | 151 | """ |
paulb@69 | 152 | A framework-specific method which extracts the form fields from the |
paulb@69 | 153 | message body in the transaction. |
paulb@69 | 154 | |
paulb@73 | 155 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 156 | single value is associated with any given field name). |
paulb@69 | 157 | """ |
paulb@69 | 158 | |
paulb@73 | 159 | # Fix the non-list results. |
paulb@1 | 160 | |
paulb@73 | 161 | fields = {} |
paulb@73 | 162 | for field_name, field_value in self.trans.request().fields().items(): |
paulb@73 | 163 | if type(field_value) == type([]): |
paulb@73 | 164 | fields[field_name] = field_value |
paulb@73 | 165 | else: |
paulb@73 | 166 | fields[field_name] = [field_value] |
paulb@73 | 167 | return fields |
paulb@1 | 168 | |
paulb@45 | 169 | def get_user(self): |
paulb@1 | 170 | |
paulb@1 | 171 | """ |
paulb@45 | 172 | A framework-specific method which extracts user information from the |
paulb@45 | 173 | transaction. |
paulb@1 | 174 | """ |
paulb@1 | 175 | |
paulb@49 | 176 | # NOTE: Webware relies entirely on a CGI-style environment where the |
paulb@49 | 177 | # NOTE: actual headers are not available. Therefore, the Web server must |
paulb@49 | 178 | # NOTE: itself be set up to provide user support. |
paulb@49 | 179 | |
paulb@49 | 180 | try: |
paulb@49 | 181 | return self.trans.request().remoteUser() |
paulb@49 | 182 | except KeyError, exc: |
paulb@49 | 183 | return None |
paulb@1 | 184 | |
paulb@1 | 185 | # Response-related methods. |
paulb@1 | 186 | |
paulb@1 | 187 | def get_response_stream(self): |
paulb@1 | 188 | |
paulb@1 | 189 | """ |
paulb@1 | 190 | A framework-specific method which returns the response stream for |
paulb@1 | 191 | the transaction. |
paulb@1 | 192 | """ |
paulb@1 | 193 | |
paulb@1 | 194 | return self.trans.response() |
paulb@1 | 195 | |
paulb@42 | 196 | def get_response_code(self): |
paulb@42 | 197 | |
paulb@42 | 198 | """ |
paulb@42 | 199 | Get the response code associated with the transaction. If no response |
paulb@42 | 200 | code is defined, None is returned. |
paulb@42 | 201 | """ |
paulb@42 | 202 | |
paulb@42 | 203 | # NOTE: Webware treats the response code as just another header. |
paulb@42 | 204 | |
paulb@42 | 205 | status = self.trans.response().header("Status", None) |
paulb@42 | 206 | try: |
paulb@42 | 207 | if status is not None: |
paulb@42 | 208 | return int(status) |
paulb@42 | 209 | else: |
paulb@42 | 210 | return None |
paulb@42 | 211 | except ValueError: |
paulb@42 | 212 | return None |
paulb@42 | 213 | |
paulb@42 | 214 | def set_response_code(self, response_code): |
paulb@42 | 215 | |
paulb@42 | 216 | """ |
paulb@42 | 217 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 218 | specification. |
paulb@42 | 219 | """ |
paulb@42 | 220 | |
paulb@42 | 221 | self.trans.response().setStatus(response_code) |
paulb@42 | 222 | |
paulb@67 | 223 | def set_header_value(self, header, value): |
paulb@49 | 224 | |
paulb@49 | 225 | """ |
paulb@49 | 226 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 227 | """ |
paulb@49 | 228 | |
paulb@49 | 229 | self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 230 | |
paulb@1 | 231 | def set_content_type(self, content_type): |
paulb@1 | 232 | |
paulb@1 | 233 | """ |
paulb@1 | 234 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 235 | response. |
paulb@1 | 236 | """ |
paulb@1 | 237 | |
paulb@5 | 238 | return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 239 | |
paulb@1 | 240 | # vim: tabstop=4 expandtab shiftwidth=4 |