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