paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Twisted classes. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@1 | 21 | """ |
paulb@1 | 22 | |
paulb@1 | 23 | import Generic |
paulb@49 | 24 | from Helpers.Auth import UserInfo |
paulb@249 | 25 | from Helpers.Request import Cookie, get_body_field, filter_fields |
paulb@167 | 26 | from Helpers.Response import ConvertingStream |
paulb@239 | 27 | from Helpers.Session import SessionStore |
paulb@71 | 28 | from cgi import parse_qs |
paulb@1 | 29 | |
paulb@1 | 30 | class Transaction(Generic.Transaction): |
paulb@1 | 31 | |
paulb@1 | 32 | """ |
paulb@1 | 33 | Twisted transaction interface. |
paulb@1 | 34 | """ |
paulb@1 | 35 | |
paulb@1 | 36 | def __init__(self, trans): |
paulb@1 | 37 | |
paulb@1 | 38 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 39 | |
paulb@1 | 40 | self.trans = trans |
paulb@311 | 41 | self.content_type = None |
paulb@128 | 42 | self.user = None |
paulb@311 | 43 | self.path_info = None |
paulb@1 | 44 | |
paulb@239 | 45 | # Special objects retained throughout the transaction. |
paulb@239 | 46 | |
paulb@239 | 47 | self.session_store = None |
paulb@239 | 48 | |
paulb@239 | 49 | def commit(self): |
paulb@239 | 50 | |
paulb@239 | 51 | """ |
paulb@239 | 52 | A special method, synchronising the transaction with framework-specific |
paulb@239 | 53 | objects. |
paulb@239 | 54 | """ |
paulb@239 | 55 | |
paulb@239 | 56 | # Close the session store. |
paulb@239 | 57 | |
paulb@239 | 58 | if self.session_store is not None: |
paulb@239 | 59 | self.session_store.close() |
paulb@239 | 60 | |
paulb@338 | 61 | # Server-related methods. |
paulb@338 | 62 | |
paulb@338 | 63 | def get_server_name(self): |
paulb@338 | 64 | |
paulb@338 | 65 | "Returns the server name." |
paulb@338 | 66 | |
paulb@338 | 67 | return self.trans.getRequestHostname() |
paulb@338 | 68 | |
paulb@338 | 69 | def get_server_port(self): |
paulb@338 | 70 | |
paulb@338 | 71 | "Returns the server port as a string." |
paulb@338 | 72 | |
paulb@345 | 73 | return str(self.trans.getHost()[2]) |
paulb@338 | 74 | |
paulb@1 | 75 | # Request-related methods. |
paulb@1 | 76 | |
paulb@1 | 77 | def get_request_stream(self): |
paulb@1 | 78 | |
paulb@1 | 79 | """ |
paulb@186 | 80 | Returns the request stream for the transaction. |
paulb@1 | 81 | """ |
paulb@1 | 82 | |
paulb@5 | 83 | return self.trans.content |
paulb@1 | 84 | |
paulb@1 | 85 | def get_request_method(self): |
paulb@1 | 86 | |
paulb@1 | 87 | """ |
paulb@186 | 88 | Returns the request method. |
paulb@1 | 89 | """ |
paulb@1 | 90 | |
paulb@1 | 91 | return self.trans.method |
paulb@1 | 92 | |
paulb@1 | 93 | def get_headers(self): |
paulb@1 | 94 | |
paulb@1 | 95 | """ |
paulb@186 | 96 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 97 | names to values. |
paulb@186 | 98 | |
paulb@91 | 99 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 100 | NOTE: need to change. |
paulb@1 | 101 | """ |
paulb@1 | 102 | |
paulb@65 | 103 | return self.trans.received_headers |
paulb@65 | 104 | |
paulb@65 | 105 | def get_header_values(self, key): |
paulb@1 | 106 | |
paulb@65 | 107 | """ |
paulb@186 | 108 | Returns a list of all request header values associated with the given |
paulb@186 | 109 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 110 | case-insensitive string. |
paulb@65 | 111 | """ |
paulb@65 | 112 | |
paulb@65 | 113 | # Twisted does not convert the header key to lower case (which is the |
paulb@65 | 114 | # stored representation). |
paulb@65 | 115 | |
paulb@65 | 116 | return self.convert_to_list(self.trans.received_headers.get(key.lower())) |
paulb@1 | 117 | |
paulb@1 | 118 | def get_content_type(self): |
paulb@1 | 119 | |
paulb@1 | 120 | """ |
paulb@186 | 121 | Returns the content type specified on the request, along with the |
paulb@186 | 122 | charset employed. |
paulb@1 | 123 | """ |
paulb@1 | 124 | |
paulb@1 | 125 | return self.parse_content_type(self.trans.getHeader("Content-Type")) |
paulb@1 | 126 | |
paulb@1 | 127 | def get_content_charsets(self): |
paulb@1 | 128 | |
paulb@1 | 129 | """ |
paulb@1 | 130 | Returns the character set preferences. |
paulb@1 | 131 | """ |
paulb@1 | 132 | |
paulb@1 | 133 | return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) |
paulb@1 | 134 | |
paulb@1 | 135 | def get_content_languages(self): |
paulb@1 | 136 | |
paulb@1 | 137 | """ |
paulb@186 | 138 | Returns extracted language information from the transaction. |
paulb@1 | 139 | """ |
paulb@1 | 140 | |
paulb@1 | 141 | return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) |
paulb@1 | 142 | |
paulb@5 | 143 | def get_path(self): |
paulb@5 | 144 | |
paulb@5 | 145 | """ |
paulb@186 | 146 | Returns the entire path from the request. |
paulb@5 | 147 | """ |
paulb@5 | 148 | |
paulb@69 | 149 | return self.trans.uri |
paulb@5 | 150 | |
paulb@162 | 151 | def get_path_without_query(self): |
paulb@162 | 152 | |
paulb@162 | 153 | """ |
paulb@186 | 154 | Returns the entire path from the request minus the query string. |
paulb@162 | 155 | """ |
paulb@162 | 156 | |
paulb@162 | 157 | return self.get_path().split("?")[0] |
paulb@162 | 158 | |
paulb@1 | 159 | def get_path_info(self): |
paulb@1 | 160 | |
paulb@1 | 161 | """ |
paulb@186 | 162 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 163 | handling the current request) from the request. |
paulb@1 | 164 | """ |
paulb@1 | 165 | |
paulb@316 | 166 | return "/%s" % "/".join(self.trans.postpath) |
paulb@1 | 167 | |
paulb@69 | 168 | def get_query_string(self): |
paulb@69 | 169 | |
paulb@69 | 170 | """ |
paulb@186 | 171 | Returns the query string from the path in the request. |
paulb@69 | 172 | """ |
paulb@69 | 173 | |
paulb@69 | 174 | t = self.get_path().split("?") |
paulb@69 | 175 | if len(t) == 1: |
paulb@69 | 176 | return "" |
paulb@69 | 177 | else: |
paulb@69 | 178 | |
paulb@69 | 179 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 180 | |
paulb@69 | 181 | return "?".join(t[1:]) |
paulb@69 | 182 | |
paulb@1 | 183 | # Higher level request-related methods. |
paulb@1 | 184 | |
paulb@69 | 185 | def get_fields_from_path(self): |
paulb@69 | 186 | |
paulb@69 | 187 | """ |
paulb@249 | 188 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 189 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 190 | the path if handling a POST transaction. |
paulb@69 | 191 | |
paulb@71 | 192 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 193 | single value is associated with any given field name). |
paulb@69 | 194 | """ |
paulb@69 | 195 | |
paulb@249 | 196 | # NOTE: Support at best ISO-8859-1 values. |
paulb@249 | 197 | |
paulb@249 | 198 | fields = {} |
paulb@249 | 199 | for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): |
paulb@249 | 200 | fields[name] = [] |
paulb@249 | 201 | for value in values: |
paulb@249 | 202 | fields[name].append(unicode(value, "iso-8859-1")) |
paulb@249 | 203 | return fields |
paulb@69 | 204 | |
paulb@167 | 205 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 206 | |
paulb@69 | 207 | """ |
paulb@249 | 208 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 209 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 210 | encoding of the message body for cases where no such information is |
paulb@249 | 211 | available, but where the default encoding is to be overridden. |
paulb@69 | 212 | |
paulb@73 | 213 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 214 | single value is associated with any given field name). Each value is |
paulb@193 | 215 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 216 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 217 | """ |
paulb@69 | 218 | |
paulb@249 | 219 | # There may not be a reliable means of extracting only the fields |
paulb@249 | 220 | # the message body using the API. Remove fields originating from the |
paulb@249 | 221 | # path in the mixture provided by the API. |
paulb@228 | 222 | |
paulb@249 | 223 | all_fields = self._get_fields(encoding) |
paulb@228 | 224 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 225 | return filter_fields(all_fields, fields_from_path) |
paulb@224 | 226 | |
paulb@249 | 227 | def _get_fields(self, encoding=None): |
paulb@224 | 228 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@167 | 229 | fields = {} |
paulb@167 | 230 | for field_name, field_values in self.trans.args.items(): |
paulb@228 | 231 | |
paulb@228 | 232 | # Find the body values. |
paulb@228 | 233 | |
paulb@198 | 234 | if type(field_values) == type([]): |
paulb@198 | 235 | fields[field_name] = [] |
paulb@198 | 236 | |
paulb@198 | 237 | # Twisted stores plain strings. |
paulb@198 | 238 | |
paulb@198 | 239 | for field_str in field_values: |
paulb@249 | 240 | fields[field_name].append(get_body_field(field_str, encoding)) |
paulb@198 | 241 | else: |
paulb@249 | 242 | fields[field_name] = get_body_field(field_values, encoding) |
paulb@228 | 243 | |
paulb@167 | 244 | return fields |
paulb@1 | 245 | |
paulb@249 | 246 | def get_fields(self, encoding=None): |
paulb@249 | 247 | |
paulb@249 | 248 | """ |
paulb@249 | 249 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 250 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 251 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 252 | where no such information is available, but where the default encoding |
paulb@249 | 253 | is to be overridden. |
paulb@249 | 254 | |
paulb@249 | 255 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 256 | single value is associated with any given field name). Each value is |
paulb@249 | 257 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 258 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 259 | |
paulb@249 | 260 | Where a given field name is used in both the path and message body to |
paulb@249 | 261 | specify values, the values from both sources will be combined into a |
paulb@249 | 262 | single list associated with that field name. |
paulb@249 | 263 | """ |
paulb@249 | 264 | |
paulb@249 | 265 | return self._get_fields(encoding) |
paulb@249 | 266 | |
paulb@45 | 267 | def get_user(self): |
paulb@1 | 268 | |
paulb@1 | 269 | """ |
paulb@186 | 270 | Extracts user information from the transaction. |
paulb@108 | 271 | |
paulb@108 | 272 | Returns a username as a string or None if no user is defined. |
paulb@1 | 273 | """ |
paulb@1 | 274 | |
paulb@128 | 275 | # Twisted makes headers lower case. |
paulb@128 | 276 | |
paulb@128 | 277 | if self.user is not None: |
paulb@128 | 278 | return self.user |
paulb@45 | 279 | |
paulb@49 | 280 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 281 | if auth_header: |
paulb@49 | 282 | return UserInfo(auth_header).username |
paulb@49 | 283 | else: |
paulb@49 | 284 | return None |
paulb@1 | 285 | |
paulb@104 | 286 | def get_cookies(self): |
paulb@104 | 287 | |
paulb@104 | 288 | """ |
paulb@186 | 289 | Obtains cookie information from the request. |
paulb@104 | 290 | |
paulb@104 | 291 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@104 | 292 | NOTE: Twisted does not seem to support this operation via methods. Thus, |
paulb@104 | 293 | NOTE: direct access has been employed to get the dictionary. |
paulb@104 | 294 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 295 | NOTE: introduced. |
paulb@104 | 296 | """ |
paulb@104 | 297 | |
paulb@258 | 298 | return self.process_cookies(self.trans.received_cookies, using_strings=1) |
paulb@104 | 299 | |
paulb@104 | 300 | def get_cookie(self, cookie_name): |
paulb@104 | 301 | |
paulb@104 | 302 | """ |
paulb@186 | 303 | Obtains cookie information from the request. |
paulb@104 | 304 | |
paulb@104 | 305 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 306 | cookie exists. |
paulb@104 | 307 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 308 | NOTE: introduced. |
paulb@104 | 309 | """ |
paulb@104 | 310 | |
paulb@258 | 311 | value = self.trans.getCookie(self.encode_cookie_value(cookie_name)) |
paulb@258 | 312 | if value is not None: |
paulb@258 | 313 | return Cookie(cookie_name, self.decode_cookie_value(value)) |
paulb@258 | 314 | else: |
paulb@258 | 315 | return None |
paulb@104 | 316 | |
paulb@1 | 317 | # Response-related methods. |
paulb@1 | 318 | |
paulb@1 | 319 | def get_response_stream(self): |
paulb@1 | 320 | |
paulb@1 | 321 | """ |
paulb@186 | 322 | Returns the response stream for the transaction. |
paulb@1 | 323 | """ |
paulb@1 | 324 | |
paulb@224 | 325 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 326 | # or a default encoding. |
paulb@167 | 327 | |
paulb@252 | 328 | encoding = self.get_response_stream_encoding() |
paulb@252 | 329 | return ConvertingStream(self.trans, encoding) |
paulb@252 | 330 | |
paulb@252 | 331 | def get_response_stream_encoding(self): |
paulb@252 | 332 | |
paulb@252 | 333 | """ |
paulb@252 | 334 | Returns the response stream encoding. |
paulb@252 | 335 | """ |
paulb@252 | 336 | |
paulb@167 | 337 | if self.content_type: |
paulb@224 | 338 | encoding = self.content_type.charset |
paulb@252 | 339 | else: |
paulb@252 | 340 | encoding = None |
paulb@252 | 341 | return encoding or self.default_charset |
paulb@1 | 342 | |
paulb@42 | 343 | def get_response_code(self): |
paulb@42 | 344 | |
paulb@42 | 345 | """ |
paulb@42 | 346 | Get the response code associated with the transaction. If no response |
paulb@42 | 347 | code is defined, None is returned. |
paulb@42 | 348 | """ |
paulb@42 | 349 | |
paulb@42 | 350 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 351 | |
paulb@42 | 352 | return self.trans.code |
paulb@42 | 353 | |
paulb@42 | 354 | def set_response_code(self, response_code): |
paulb@42 | 355 | |
paulb@42 | 356 | """ |
paulb@42 | 357 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 358 | specification. |
paulb@42 | 359 | """ |
paulb@42 | 360 | |
paulb@42 | 361 | self.trans.setResponseCode(response_code) |
paulb@42 | 362 | |
paulb@67 | 363 | def set_header_value(self, header, value): |
paulb@49 | 364 | |
paulb@49 | 365 | """ |
paulb@49 | 366 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 367 | """ |
paulb@49 | 368 | |
paulb@49 | 369 | self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 370 | |
paulb@1 | 371 | def set_content_type(self, content_type): |
paulb@1 | 372 | |
paulb@1 | 373 | """ |
paulb@186 | 374 | Sets the 'content_type' for the response. |
paulb@1 | 375 | """ |
paulb@1 | 376 | |
paulb@167 | 377 | # Remember the content type for encoding purposes later. |
paulb@167 | 378 | |
paulb@167 | 379 | self.content_type = content_type |
paulb@224 | 380 | self.trans.setHeader("Content-Type", str(content_type)) |
paulb@1 | 381 | |
paulb@104 | 382 | # Higher level response-related methods. |
paulb@104 | 383 | |
paulb@104 | 384 | def set_cookie(self, cookie): |
paulb@104 | 385 | |
paulb@104 | 386 | """ |
paulb@186 | 387 | Stores the given 'cookie' object in the response. |
paulb@104 | 388 | """ |
paulb@104 | 389 | |
paulb@258 | 390 | self.set_cookie_value(cookie.name, cookie.value, path=cookie.path, expires=cookie.expires) |
paulb@104 | 391 | |
paulb@104 | 392 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@104 | 393 | |
paulb@104 | 394 | """ |
paulb@186 | 395 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@104 | 396 | |
paulb@104 | 397 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@104 | 398 | and the optional 'expires' parameter is a value compatible with the |
paulb@104 | 399 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@104 | 400 | """ |
paulb@104 | 401 | |
paulb@258 | 402 | self.trans.addCookie(self.encode_cookie_value(name), |
paulb@258 | 403 | self.encode_cookie_value(value), expires=expires, path=path) |
paulb@104 | 404 | |
paulb@104 | 405 | def delete_cookie(self, cookie_name): |
paulb@104 | 406 | |
paulb@104 | 407 | """ |
paulb@186 | 408 | Adds to the response a request that the cookie with the given |
paulb@186 | 409 | 'cookie_name' be deleted/discarded by the client. |
paulb@104 | 410 | """ |
paulb@104 | 411 | |
paulb@108 | 412 | # Create a special cookie, given that we do not know whether the browser |
paulb@108 | 413 | # has been sent the cookie or not. |
paulb@108 | 414 | # NOTE: Magic discovered in Webware. |
paulb@108 | 415 | |
paulb@258 | 416 | self.trans.addCookie(self.encode_cookie_value(cookie_name), "", expires=0, path="/", max_age=0) |
paulb@104 | 417 | |
paulb@239 | 418 | # Session-related methods. |
paulb@239 | 419 | |
paulb@239 | 420 | def get_session(self, create=1): |
paulb@239 | 421 | |
paulb@239 | 422 | """ |
paulb@239 | 423 | Gets a session corresponding to an identifier supplied in the |
paulb@239 | 424 | transaction. |
paulb@239 | 425 | |
paulb@239 | 426 | If no session has yet been established according to information |
paulb@239 | 427 | provided in the transaction then the optional 'create' parameter |
paulb@239 | 428 | determines whether a new session will be established. |
paulb@239 | 429 | |
paulb@239 | 430 | Where no session has been established and where 'create' is set to 0 |
paulb@239 | 431 | then None is returned. In all other cases, a session object is created |
paulb@239 | 432 | (where appropriate) and returned. |
paulb@239 | 433 | """ |
paulb@239 | 434 | |
paulb@239 | 435 | # NOTE: Requires configuration. |
paulb@239 | 436 | |
paulb@239 | 437 | if self.session_store is None: |
paulb@239 | 438 | self.session_store = SessionStore(self, "WebStack-sessions") |
paulb@239 | 439 | return self.session_store.get_session(create) |
paulb@239 | 440 | |
paulb@239 | 441 | def expire_session(self): |
paulb@239 | 442 | |
paulb@239 | 443 | """ |
paulb@239 | 444 | Expires any session established according to information provided in the |
paulb@239 | 445 | transaction. |
paulb@239 | 446 | """ |
paulb@239 | 447 | |
paulb@239 | 448 | # NOTE: Requires configuration. |
paulb@239 | 449 | |
paulb@239 | 450 | if self.session_store is None: |
paulb@239 | 451 | self.session_store = SessionStore(self, "WebStack-sessions") |
paulb@239 | 452 | self.session_store.expire_session() |
paulb@239 | 453 | |
paulb@1 | 454 | # vim: tabstop=4 expandtab shiftwidth=4 |