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