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@108 | 10 | from Helpers import Environment |
paulb@115 | 11 | from Helpers.Request import Cookie |
paulb@1 | 12 | |
paulb@1 | 13 | class Transaction(Generic.Transaction): |
paulb@1 | 14 | |
paulb@1 | 15 | """ |
paulb@1 | 16 | Webware transaction interface. |
paulb@1 | 17 | """ |
paulb@1 | 18 | |
paulb@1 | 19 | def __init__(self, trans): |
paulb@1 | 20 | |
paulb@1 | 21 | "Initialise the transaction using the Webware transaction 'trans'." |
paulb@1 | 22 | |
paulb@1 | 23 | self.trans = trans |
paulb@128 | 24 | self.user = None |
paulb@1 | 25 | |
paulb@1 | 26 | # Request-related methods. |
paulb@1 | 27 | |
paulb@1 | 28 | def get_request_stream(self): |
paulb@1 | 29 | |
paulb@1 | 30 | """ |
paulb@1 | 31 | A framework-specific method which returns the request stream for |
paulb@1 | 32 | the transaction. |
paulb@1 | 33 | """ |
paulb@1 | 34 | |
paulb@85 | 35 | request = self.trans.request() |
paulb@85 | 36 | try: |
paulb@85 | 37 | stream = request.rawInput(rewind=1) |
paulb@85 | 38 | if stream is None: |
paulb@85 | 39 | return StringIO.StringIO("") |
paulb@85 | 40 | |
paulb@85 | 41 | # NOTE: Dubious catch-all situation, but it is difficult to control |
paulb@85 | 42 | # NOTE: cases where Webware's internals themselves fail. |
paulb@85 | 43 | |
paulb@85 | 44 | except: |
paulb@1 | 45 | return StringIO.StringIO("") |
paulb@1 | 46 | |
paulb@1 | 47 | return stream |
paulb@1 | 48 | |
paulb@1 | 49 | def get_request_method(self): |
paulb@1 | 50 | |
paulb@1 | 51 | """ |
paulb@1 | 52 | A framework-specific method which gets the request method. |
paulb@1 | 53 | """ |
paulb@1 | 54 | |
paulb@1 | 55 | return self.trans.request().method() |
paulb@1 | 56 | |
paulb@1 | 57 | def get_headers(self): |
paulb@1 | 58 | |
paulb@1 | 59 | """ |
paulb@108 | 60 | A framework-specific method which returns all request headers as a |
paulb@108 | 61 | dictionary-like object mapping header names to values. |
paulb@91 | 62 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 63 | NOTE: need to change. |
paulb@1 | 64 | """ |
paulb@1 | 65 | |
paulb@91 | 66 | # Use the Webware environment and some assumptions about variable names. |
paulb@91 | 67 | # NOTE: Using lower case for the header names. |
paulb@1 | 68 | |
paulb@91 | 69 | env = self.trans.request().environ() |
paulb@108 | 70 | return Environment.get_headers(env) |
paulb@1 | 71 | |
paulb@65 | 72 | def get_header_values(self, key): |
paulb@65 | 73 | |
paulb@65 | 74 | """ |
paulb@65 | 75 | A framework-specific method which returns a list of all request header |
paulb@65 | 76 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 77 | 'key' is treated as a case-insensitive string. |
paulb@65 | 78 | """ |
paulb@65 | 79 | |
paulb@91 | 80 | # Use the Webware environment and some assumptions about variable names. |
paulb@65 | 81 | |
paulb@91 | 82 | env = self.trans.request().environ() |
paulb@91 | 83 | cgi_key = "HTTP_" + key.replace("-", "_").upper() |
paulb@91 | 84 | if env.has_key(cgi_key): |
paulb@91 | 85 | return [env[cgi_key]] |
paulb@91 | 86 | else: |
paulb@91 | 87 | return [] |
paulb@65 | 88 | |
paulb@1 | 89 | def get_content_type(self): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@1 | 92 | A framework-specific method which gets the content type specified on the |
paulb@1 | 93 | request, along with the charset employed. |
paulb@1 | 94 | """ |
paulb@1 | 95 | |
paulb@1 | 96 | return self.parse_content_type(self.trans.request().contentType()) |
paulb@1 | 97 | |
paulb@1 | 98 | def get_content_charsets(self): |
paulb@1 | 99 | |
paulb@1 | 100 | """ |
paulb@1 | 101 | Returns the character set preferences. |
paulb@1 | 102 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 103 | """ |
paulb@1 | 104 | |
paulb@1 | 105 | return self.trans.request().contentCharsets() |
paulb@1 | 106 | |
paulb@1 | 107 | def get_content_languages(self): |
paulb@1 | 108 | |
paulb@1 | 109 | """ |
paulb@1 | 110 | A framework-specific method which extracts language information from |
paulb@1 | 111 | the transaction. |
paulb@1 | 112 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 113 | """ |
paulb@1 | 114 | |
paulb@1 | 115 | return self.trans.request().contentLanguages() |
paulb@1 | 116 | |
paulb@5 | 117 | def get_path(self): |
paulb@5 | 118 | |
paulb@5 | 119 | """ |
paulb@5 | 120 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 121 | """ |
paulb@5 | 122 | |
paulb@14 | 123 | return self.trans.request().uri() |
paulb@5 | 124 | |
paulb@1 | 125 | def get_path_info(self): |
paulb@1 | 126 | |
paulb@1 | 127 | """ |
paulb@1 | 128 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 129 | URL after the resource name handling the current request) from the |
paulb@1 | 130 | request. |
paulb@1 | 131 | """ |
paulb@1 | 132 | |
paulb@91 | 133 | return self.trans.request().extraURLPath() |
paulb@1 | 134 | |
paulb@69 | 135 | def get_query_string(self): |
paulb@69 | 136 | |
paulb@69 | 137 | """ |
paulb@69 | 138 | A framework-specific method which gets the query string from the path in |
paulb@69 | 139 | the request. |
paulb@69 | 140 | """ |
paulb@69 | 141 | |
paulb@69 | 142 | return self.trans.request().queryString() |
paulb@69 | 143 | |
paulb@1 | 144 | # Higher level request-related methods. |
paulb@1 | 145 | |
paulb@69 | 146 | def get_fields_from_path(self): |
paulb@69 | 147 | |
paulb@69 | 148 | """ |
paulb@69 | 149 | A framework-specific method which extracts the form fields from the |
paulb@69 | 150 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 151 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 152 | |
paulb@71 | 153 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 154 | single value is associated with any given field name). |
paulb@69 | 155 | """ |
paulb@69 | 156 | |
paulb@71 | 157 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 158 | |
paulb@69 | 159 | def get_fields_from_body(self): |
paulb@69 | 160 | |
paulb@69 | 161 | """ |
paulb@69 | 162 | A framework-specific method which extracts the form fields from the |
paulb@69 | 163 | message body in the transaction. |
paulb@69 | 164 | |
paulb@73 | 165 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 166 | single value is associated with any given field name). |
paulb@69 | 167 | """ |
paulb@69 | 168 | |
paulb@73 | 169 | # Fix the non-list results. |
paulb@1 | 170 | |
paulb@73 | 171 | fields = {} |
paulb@73 | 172 | for field_name, field_value in self.trans.request().fields().items(): |
paulb@73 | 173 | if type(field_value) == type([]): |
paulb@73 | 174 | fields[field_name] = field_value |
paulb@73 | 175 | else: |
paulb@73 | 176 | fields[field_name] = [field_value] |
paulb@73 | 177 | return fields |
paulb@1 | 178 | |
paulb@45 | 179 | def get_user(self): |
paulb@1 | 180 | |
paulb@1 | 181 | """ |
paulb@45 | 182 | A framework-specific method which extracts user information from the |
paulb@45 | 183 | transaction. |
paulb@108 | 184 | |
paulb@108 | 185 | Returns a username as a string or None if no user is defined. |
paulb@1 | 186 | """ |
paulb@1 | 187 | |
paulb@49 | 188 | # NOTE: Webware relies entirely on a CGI-style environment where the |
paulb@49 | 189 | # NOTE: actual headers are not available. Therefore, the Web server must |
paulb@49 | 190 | # NOTE: itself be set up to provide user support. |
paulb@49 | 191 | |
paulb@128 | 192 | if self.user is not None: |
paulb@128 | 193 | return self.user |
paulb@128 | 194 | |
paulb@49 | 195 | try: |
paulb@49 | 196 | return self.trans.request().remoteUser() |
paulb@49 | 197 | except KeyError, exc: |
paulb@49 | 198 | return None |
paulb@1 | 199 | |
paulb@115 | 200 | def get_cookies(self): |
paulb@115 | 201 | |
paulb@115 | 202 | """ |
paulb@115 | 203 | A framework-specific method which obtains cookie information from the |
paulb@115 | 204 | request. |
paulb@115 | 205 | |
paulb@115 | 206 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 207 | """ |
paulb@115 | 208 | |
paulb@115 | 209 | cookies = {} |
paulb@115 | 210 | for name, value in self.trans.request().cookies().items(): |
paulb@115 | 211 | cookies[name] = Cookie(name, value) |
paulb@115 | 212 | return cookies |
paulb@115 | 213 | |
paulb@115 | 214 | def get_cookie(self, cookie_name): |
paulb@115 | 215 | |
paulb@115 | 216 | """ |
paulb@115 | 217 | A framework-specific method which obtains cookie information from the |
paulb@115 | 218 | request. |
paulb@115 | 219 | |
paulb@115 | 220 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 221 | cookie exists. |
paulb@115 | 222 | """ |
paulb@115 | 223 | |
paulb@115 | 224 | try: |
paulb@115 | 225 | return Cookie(cookie_name, self.trans.request().cookie(cookie_name)) |
paulb@115 | 226 | except KeyError: |
paulb@115 | 227 | return None |
paulb@115 | 228 | |
paulb@1 | 229 | # Response-related methods. |
paulb@1 | 230 | |
paulb@1 | 231 | def get_response_stream(self): |
paulb@1 | 232 | |
paulb@1 | 233 | """ |
paulb@1 | 234 | A framework-specific method which returns the response stream for |
paulb@1 | 235 | the transaction. |
paulb@1 | 236 | """ |
paulb@1 | 237 | |
paulb@1 | 238 | return self.trans.response() |
paulb@1 | 239 | |
paulb@42 | 240 | def get_response_code(self): |
paulb@42 | 241 | |
paulb@42 | 242 | """ |
paulb@42 | 243 | Get the response code associated with the transaction. If no response |
paulb@42 | 244 | code is defined, None is returned. |
paulb@42 | 245 | """ |
paulb@42 | 246 | |
paulb@42 | 247 | # NOTE: Webware treats the response code as just another header. |
paulb@42 | 248 | |
paulb@42 | 249 | status = self.trans.response().header("Status", None) |
paulb@42 | 250 | try: |
paulb@42 | 251 | if status is not None: |
paulb@42 | 252 | return int(status) |
paulb@42 | 253 | else: |
paulb@42 | 254 | return None |
paulb@42 | 255 | except ValueError: |
paulb@42 | 256 | return None |
paulb@42 | 257 | |
paulb@42 | 258 | def set_response_code(self, response_code): |
paulb@42 | 259 | |
paulb@42 | 260 | """ |
paulb@42 | 261 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 262 | specification. |
paulb@42 | 263 | """ |
paulb@42 | 264 | |
paulb@42 | 265 | self.trans.response().setStatus(response_code) |
paulb@42 | 266 | |
paulb@67 | 267 | def set_header_value(self, header, value): |
paulb@49 | 268 | |
paulb@49 | 269 | """ |
paulb@49 | 270 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 271 | """ |
paulb@49 | 272 | |
paulb@49 | 273 | self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 274 | |
paulb@1 | 275 | def set_content_type(self, content_type): |
paulb@1 | 276 | |
paulb@1 | 277 | """ |
paulb@1 | 278 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 279 | response. |
paulb@1 | 280 | """ |
paulb@1 | 281 | |
paulb@5 | 282 | return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 283 | |
paulb@115 | 284 | # Higher level response-related methods. |
paulb@115 | 285 | |
paulb@115 | 286 | def set_cookie(self, cookie): |
paulb@115 | 287 | |
paulb@115 | 288 | """ |
paulb@115 | 289 | A framework-specific method which stores the given 'cookie' object in |
paulb@115 | 290 | the response. |
paulb@115 | 291 | """ |
paulb@115 | 292 | |
paulb@115 | 293 | self.trans.response().addCookie(cookie) |
paulb@115 | 294 | |
paulb@115 | 295 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 296 | |
paulb@115 | 297 | """ |
paulb@115 | 298 | A framework-specific method which stores a cookie with the given 'name' |
paulb@115 | 299 | and 'value' in the response. |
paulb@115 | 300 | |
paulb@115 | 301 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 302 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 303 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 304 | """ |
paulb@115 | 305 | |
paulb@115 | 306 | self.trans.response().setCookie(name, value, path, expires) |
paulb@115 | 307 | |
paulb@115 | 308 | def delete_cookie(self, cookie_name): |
paulb@115 | 309 | |
paulb@115 | 310 | """ |
paulb@115 | 311 | A framework-specific method which adds to the response a request that |
paulb@115 | 312 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@115 | 313 | client. |
paulb@115 | 314 | """ |
paulb@115 | 315 | |
paulb@115 | 316 | self.trans.response().delCookie(cookie_name) |
paulb@115 | 317 | |
paulb@128 | 318 | # Application-specific methods. |
paulb@128 | 319 | |
paulb@128 | 320 | def set_user(self, username): |
paulb@128 | 321 | |
paulb@128 | 322 | """ |
paulb@128 | 323 | An application-specific method which sets the user information with |
paulb@128 | 324 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 325 | 'get_user'. |
paulb@128 | 326 | """ |
paulb@128 | 327 | |
paulb@128 | 328 | self.user = username |
paulb@128 | 329 | |
paulb@1 | 330 | # vim: tabstop=4 expandtab shiftwidth=4 |