paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | mod_python classes. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import Generic |
paulb@249 | 8 | from Helpers.Request import get_body_field, filter_fields |
paulb@167 | 9 | from Helpers.Response import ConvertingStream |
paulb@71 | 10 | from mod_python.util import parse_qs, FieldStorage |
paulb@42 | 11 | from mod_python import apache |
paulb@183 | 12 | |
paulb@245 | 13 | # Provide alternative implementations. |
paulb@245 | 14 | # The alternative session support requires cookie support of some kind. |
paulb@183 | 15 | |
paulb@245 | 16 | try: |
paulb@245 | 17 | from mod_python import Cookie |
paulb@245 | 18 | except ImportError: |
paulb@245 | 19 | from Cookie import SimpleCookie |
paulb@245 | 20 | Cookie = None |
paulb@239 | 21 | try: |
paulb@239 | 22 | from mod_python import Session |
paulb@239 | 23 | except ImportError: |
paulb@239 | 24 | from Helpers.Session import SessionStore |
paulb@239 | 25 | import os |
paulb@239 | 26 | Session = None |
paulb@1 | 27 | |
paulb@1 | 28 | class Transaction(Generic.Transaction): |
paulb@1 | 29 | |
paulb@1 | 30 | """ |
paulb@1 | 31 | mod_python transaction interface. |
paulb@1 | 32 | """ |
paulb@1 | 33 | |
paulb@1 | 34 | def __init__(self, trans): |
paulb@1 | 35 | |
paulb@1 | 36 | "Initialise the transaction using the mod_python transaction 'trans'." |
paulb@1 | 37 | |
paulb@1 | 38 | self.trans = trans |
paulb@42 | 39 | self.response_code = apache.OK |
paulb@128 | 40 | self.user = None |
paulb@167 | 41 | self.content_type = None |
paulb@1 | 42 | |
paulb@245 | 43 | # Support non-framework cookies. |
paulb@245 | 44 | |
paulb@245 | 45 | if Cookie is None: |
paulb@245 | 46 | |
paulb@245 | 47 | # Define the incoming cookies. |
paulb@245 | 48 | |
paulb@245 | 49 | self.cookies_in = SimpleCookie(self.get_headers().get("cookie")) |
paulb@245 | 50 | |
paulb@133 | 51 | # Cached information. |
paulb@133 | 52 | |
paulb@133 | 53 | self.storage_body = None |
paulb@133 | 54 | |
paulb@239 | 55 | # Special objects retained throughout the transaction. |
paulb@239 | 56 | |
paulb@239 | 57 | self.session_store = None |
paulb@239 | 58 | |
paulb@239 | 59 | def commit(self): |
paulb@239 | 60 | |
paulb@239 | 61 | """ |
paulb@239 | 62 | A special method, synchronising the transaction with framework-specific |
paulb@239 | 63 | objects. |
paulb@239 | 64 | """ |
paulb@239 | 65 | |
paulb@239 | 66 | # Close the session store. |
paulb@239 | 67 | |
paulb@239 | 68 | if self.session_store is not None: |
paulb@239 | 69 | self.session_store.close() |
paulb@239 | 70 | |
paulb@1 | 71 | # Request-related methods. |
paulb@1 | 72 | |
paulb@1 | 73 | def get_request_stream(self): |
paulb@1 | 74 | |
paulb@1 | 75 | """ |
paulb@186 | 76 | Returns the request stream for the transaction. |
paulb@1 | 77 | """ |
paulb@1 | 78 | |
paulb@1 | 79 | return self.trans |
paulb@1 | 80 | |
paulb@1 | 81 | def get_request_method(self): |
paulb@1 | 82 | |
paulb@1 | 83 | """ |
paulb@186 | 84 | Returns the request method. |
paulb@1 | 85 | """ |
paulb@1 | 86 | |
paulb@1 | 87 | return self.trans.method |
paulb@1 | 88 | |
paulb@1 | 89 | def get_headers(self): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@186 | 92 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 93 | names to values. |
paulb@186 | 94 | |
paulb@91 | 95 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 96 | NOTE: need to change. |
paulb@1 | 97 | """ |
paulb@1 | 98 | |
paulb@1 | 99 | return self.trans.headers_in |
paulb@1 | 100 | |
paulb@65 | 101 | def get_header_values(self, key): |
paulb@65 | 102 | |
paulb@65 | 103 | """ |
paulb@186 | 104 | Returns a list of all request header values associated with the given |
paulb@186 | 105 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 106 | case-insensitive string. |
paulb@65 | 107 | """ |
paulb@65 | 108 | |
paulb@65 | 109 | return self.convert_to_list(self.trans.headers_in.get(key)) |
paulb@65 | 110 | |
paulb@1 | 111 | def get_content_type(self): |
paulb@1 | 112 | |
paulb@1 | 113 | """ |
paulb@186 | 114 | Returns the content type specified on the request, along with the |
paulb@186 | 115 | charset employed. |
paulb@1 | 116 | """ |
paulb@1 | 117 | |
paulb@1 | 118 | return self.parse_content_type(self.trans.content_type) |
paulb@1 | 119 | |
paulb@1 | 120 | def get_content_charsets(self): |
paulb@1 | 121 | |
paulb@1 | 122 | """ |
paulb@1 | 123 | Returns the character set preferences. |
paulb@1 | 124 | """ |
paulb@1 | 125 | |
paulb@108 | 126 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Charset")) |
paulb@1 | 127 | |
paulb@1 | 128 | def get_content_languages(self): |
paulb@1 | 129 | |
paulb@1 | 130 | """ |
paulb@186 | 131 | Returns extracted language information from the transaction. |
paulb@1 | 132 | """ |
paulb@1 | 133 | |
paulb@108 | 134 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Language")) |
paulb@1 | 135 | |
paulb@5 | 136 | def get_path(self): |
paulb@5 | 137 | |
paulb@5 | 138 | """ |
paulb@186 | 139 | Returns the entire path from the request. |
paulb@5 | 140 | """ |
paulb@5 | 141 | |
paulb@144 | 142 | query_string = self.get_query_string() |
paulb@144 | 143 | if query_string: |
paulb@144 | 144 | return self.trans.uri + "?" + query_string |
paulb@144 | 145 | else: |
paulb@144 | 146 | return self.trans.uri |
paulb@5 | 147 | |
paulb@162 | 148 | def get_path_without_query(self): |
paulb@162 | 149 | |
paulb@162 | 150 | """ |
paulb@186 | 151 | Returns the entire path from the request minus the query string. |
paulb@162 | 152 | """ |
paulb@162 | 153 | |
paulb@162 | 154 | return self.trans.uri |
paulb@162 | 155 | |
paulb@1 | 156 | def get_path_info(self): |
paulb@1 | 157 | |
paulb@1 | 158 | """ |
paulb@186 | 159 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 160 | handling the current request) from the request. |
paulb@1 | 161 | """ |
paulb@1 | 162 | |
paulb@10 | 163 | return self.trans.path_info |
paulb@1 | 164 | |
paulb@69 | 165 | def get_query_string(self): |
paulb@69 | 166 | |
paulb@69 | 167 | """ |
paulb@186 | 168 | Returns the query string from the path in the request. |
paulb@69 | 169 | """ |
paulb@69 | 170 | |
paulb@73 | 171 | return self.trans.args or "" |
paulb@69 | 172 | |
paulb@1 | 173 | # Higher level request-related methods. |
paulb@1 | 174 | |
paulb@69 | 175 | def get_fields_from_path(self): |
paulb@69 | 176 | |
paulb@69 | 177 | """ |
paulb@249 | 178 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 179 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 180 | the path if handling a POST transaction. |
paulb@69 | 181 | |
paulb@71 | 182 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 183 | single value is associated with any given field name). |
paulb@69 | 184 | """ |
paulb@69 | 185 | |
paulb@249 | 186 | # NOTE: Support at best ISO-8859-1 values. |
paulb@249 | 187 | |
paulb@249 | 188 | fields = {} |
paulb@249 | 189 | for name, values in parse_qs(self.get_query_string(), 1).items(): # keep_blank_values=1 |
paulb@249 | 190 | fields[name] = [] |
paulb@249 | 191 | for value in values: |
paulb@249 | 192 | fields[name].append(unicode(value, "iso-8859-1")) |
paulb@249 | 193 | return fields |
paulb@69 | 194 | |
paulb@167 | 195 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 196 | |
paulb@69 | 197 | """ |
paulb@249 | 198 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 199 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 200 | encoding of the message body for cases where no such information is |
paulb@249 | 201 | available, but where the default encoding is to be overridden. |
paulb@69 | 202 | |
paulb@73 | 203 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 204 | single value is associated with any given field name). Each value is |
paulb@193 | 205 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 206 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 207 | |
paulb@69 | 208 | The mod_python.util.FieldStorage class may augment the fields from the |
paulb@69 | 209 | body with fields found in the path. |
paulb@69 | 210 | """ |
paulb@69 | 211 | |
paulb@249 | 212 | all_fields = self._get_fields(encoding) |
paulb@249 | 213 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 214 | return filter_fields(all_fields, fields_from_path) |
paulb@249 | 215 | |
paulb@249 | 216 | def _get_fields(self, encoding=None): |
paulb@224 | 217 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@167 | 218 | |
paulb@133 | 219 | if self.storage_body is None: |
paulb@133 | 220 | self.storage_body = FieldStorage(self.trans, keep_blank_values=1) |
paulb@69 | 221 | |
paulb@73 | 222 | # Traverse the storage, finding each field value. |
paulb@1 | 223 | |
paulb@73 | 224 | fields = {} |
paulb@133 | 225 | for field in self.storage_body.list: |
paulb@73 | 226 | if not fields.has_key(field.name): |
paulb@73 | 227 | fields[field.name] = [] |
paulb@198 | 228 | fields[field.name].append(get_body_field(field.value, encoding)) |
paulb@73 | 229 | return fields |
paulb@1 | 230 | |
paulb@249 | 231 | def get_fields(self, encoding=None): |
paulb@249 | 232 | |
paulb@249 | 233 | """ |
paulb@249 | 234 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 235 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 236 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 237 | where no such information is available, but where the default encoding |
paulb@249 | 238 | is to be overridden. |
paulb@249 | 239 | |
paulb@249 | 240 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 241 | single value is associated with any given field name). Each value is |
paulb@249 | 242 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 243 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 244 | |
paulb@249 | 245 | Where a given field name is used in both the path and message body to |
paulb@249 | 246 | specify values, the values from both sources will be combined into a |
paulb@249 | 247 | single list associated with that field name. |
paulb@249 | 248 | """ |
paulb@249 | 249 | |
paulb@249 | 250 | return self._get_fields(encoding) |
paulb@249 | 251 | |
paulb@45 | 252 | def get_user(self): |
paulb@1 | 253 | |
paulb@1 | 254 | """ |
paulb@186 | 255 | Extracts user information from the transaction. |
paulb@108 | 256 | |
paulb@108 | 257 | Returns a username as a string or None if no user is defined. |
paulb@1 | 258 | """ |
paulb@1 | 259 | |
paulb@128 | 260 | if self.user is not None: |
paulb@128 | 261 | return self.user |
paulb@128 | 262 | else: |
paulb@128 | 263 | return self.trans.user |
paulb@1 | 264 | |
paulb@98 | 265 | def get_cookies(self): |
paulb@98 | 266 | |
paulb@98 | 267 | """ |
paulb@186 | 268 | Obtains cookie information from the request. |
paulb@98 | 269 | |
paulb@98 | 270 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 271 | |
paulb@98 | 272 | NOTE: No additional information is passed to the underlying API despite |
paulb@98 | 273 | NOTE: support for enhanced cookies in mod_python. |
paulb@98 | 274 | """ |
paulb@98 | 275 | |
paulb@137 | 276 | if Cookie: |
paulb@137 | 277 | return Cookie.get_cookies(self.trans) |
paulb@137 | 278 | else: |
paulb@245 | 279 | return self.cookies_in |
paulb@98 | 280 | |
paulb@104 | 281 | def get_cookie(self, cookie_name): |
paulb@104 | 282 | |
paulb@104 | 283 | """ |
paulb@186 | 284 | Obtains cookie information from the request. |
paulb@104 | 285 | |
paulb@104 | 286 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 287 | cookie exists. |
paulb@104 | 288 | """ |
paulb@104 | 289 | |
paulb@104 | 290 | return self.get_cookies().get(cookie_name) |
paulb@104 | 291 | |
paulb@1 | 292 | # Response-related methods. |
paulb@1 | 293 | |
paulb@1 | 294 | def get_response_stream(self): |
paulb@1 | 295 | |
paulb@1 | 296 | """ |
paulb@186 | 297 | Returns the response stream for the transaction. |
paulb@1 | 298 | """ |
paulb@1 | 299 | |
paulb@224 | 300 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 301 | # or a default encoding. |
paulb@167 | 302 | |
paulb@252 | 303 | encoding = self.get_response_stream_encoding() |
paulb@252 | 304 | return ConvertingStream(self.trans, encoding) |
paulb@252 | 305 | |
paulb@252 | 306 | def get_response_stream_encoding(self): |
paulb@252 | 307 | |
paulb@252 | 308 | """ |
paulb@252 | 309 | Returns the response stream encoding. |
paulb@252 | 310 | """ |
paulb@252 | 311 | |
paulb@167 | 312 | if self.content_type: |
paulb@224 | 313 | encoding = self.content_type.charset |
paulb@252 | 314 | else: |
paulb@252 | 315 | encoding = None |
paulb@252 | 316 | return encoding or self.default_charset |
paulb@1 | 317 | |
paulb@42 | 318 | def get_response_code(self): |
paulb@42 | 319 | |
paulb@42 | 320 | """ |
paulb@42 | 321 | Get the response code associated with the transaction. If no response |
paulb@42 | 322 | code is defined, None is returned. |
paulb@42 | 323 | """ |
paulb@42 | 324 | |
paulb@42 | 325 | return self.response_code |
paulb@42 | 326 | |
paulb@42 | 327 | def set_response_code(self, response_code): |
paulb@42 | 328 | |
paulb@42 | 329 | """ |
paulb@42 | 330 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 331 | specification. |
paulb@42 | 332 | """ |
paulb@42 | 333 | |
paulb@42 | 334 | self.response_code = response_code |
paulb@42 | 335 | |
paulb@67 | 336 | def set_header_value(self, header, value): |
paulb@49 | 337 | |
paulb@49 | 338 | """ |
paulb@49 | 339 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 340 | """ |
paulb@49 | 341 | |
paulb@49 | 342 | self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) |
paulb@49 | 343 | |
paulb@1 | 344 | def set_content_type(self, content_type): |
paulb@1 | 345 | |
paulb@1 | 346 | """ |
paulb@186 | 347 | Sets the 'content_type' for the response. |
paulb@1 | 348 | """ |
paulb@1 | 349 | |
paulb@167 | 350 | # Remember the content type for encoding purposes later. |
paulb@167 | 351 | |
paulb@167 | 352 | self.content_type = content_type |
paulb@224 | 353 | self.trans.content_type = str(content_type) |
paulb@1 | 354 | |
paulb@186 | 355 | # Higher level response-related methods. |
paulb@186 | 356 | |
paulb@98 | 357 | def set_cookie(self, cookie): |
paulb@98 | 358 | |
paulb@98 | 359 | """ |
paulb@186 | 360 | Stores the given 'cookie' object in the response. |
paulb@98 | 361 | """ |
paulb@98 | 362 | |
paulb@98 | 363 | if Cookie: |
paulb@98 | 364 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 365 | else: |
paulb@245 | 366 | # NOTE: If multiple cookies of the same name could be specified, this |
paulb@245 | 367 | # NOTE: could need changing. |
paulb@245 | 368 | |
paulb@245 | 369 | cookie_out = SimpleCookie() |
paulb@245 | 370 | cookie_out[cookie.name] = cookie.value |
paulb@245 | 371 | self._write_cookie(cookie_out) |
paulb@98 | 372 | |
paulb@98 | 373 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 374 | |
paulb@98 | 375 | """ |
paulb@186 | 376 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 377 | |
paulb@98 | 378 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 379 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 380 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 381 | """ |
paulb@98 | 382 | |
paulb@104 | 383 | # NOTE: We just hope that Cookie converts Unicode arguments to US-ASCII. |
paulb@104 | 384 | |
paulb@98 | 385 | if Cookie: |
paulb@98 | 386 | cookie = Cookie.Cookie(name, value) |
paulb@98 | 387 | if expires is not None: |
paulb@98 | 388 | cookie.expires = expires |
paulb@98 | 389 | if path is not None: |
paulb@98 | 390 | cookie.path = path |
paulb@98 | 391 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 392 | else: |
paulb@245 | 393 | cookie_out = SimpleCookie() |
paulb@245 | 394 | cookie_out[name] = value |
paulb@245 | 395 | if path is not None: |
paulb@245 | 396 | cookie_out[name]["path"] = path |
paulb@245 | 397 | if expires is not None: |
paulb@245 | 398 | cookie_out[name]["expires"] = expires |
paulb@245 | 399 | self._write_cookie(cookie_out) |
paulb@98 | 400 | |
paulb@98 | 401 | def delete_cookie(self, cookie_name): |
paulb@98 | 402 | |
paulb@98 | 403 | """ |
paulb@186 | 404 | Adds to the response a request that the cookie with the given |
paulb@186 | 405 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 406 | """ |
paulb@98 | 407 | |
paulb@98 | 408 | # Create a special cookie, given that we do not know whether the browser |
paulb@98 | 409 | # has been sent the cookie or not. |
paulb@98 | 410 | # NOTE: Magic discovered in Webware. |
paulb@98 | 411 | |
paulb@98 | 412 | if Cookie: |
paulb@98 | 413 | cookie = Cookie.Cookie(cookie_name, "") |
paulb@98 | 414 | cookie.path = "/" |
paulb@98 | 415 | cookie.expires = 0 |
paulb@98 | 416 | cookie.max_age = 0 |
paulb@98 | 417 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 418 | else: |
paulb@245 | 419 | cookie_out = SimpleCookie() |
paulb@245 | 420 | cookie_out[cookie_name] = "" |
paulb@245 | 421 | cookie_out[cookie_name]["path"] = "/" |
paulb@245 | 422 | cookie_out[cookie_name]["expires"] = 0 |
paulb@245 | 423 | cookie_out[cookie_name]["max-age"] = 0 |
paulb@245 | 424 | self._write_cookie(cookie_out) |
paulb@245 | 425 | |
paulb@245 | 426 | def _write_cookie(self, cookie): |
paulb@245 | 427 | |
paulb@245 | 428 | "An internal method adding the given 'cookie' to the headers." |
paulb@245 | 429 | |
paulb@245 | 430 | # NOTE: May not be using the appropriate method. |
paulb@245 | 431 | |
paulb@245 | 432 | for morsel in cookie.values(): |
paulb@245 | 433 | self.set_header_value("Set-Cookie", morsel.OutputString()) |
paulb@98 | 434 | |
paulb@183 | 435 | # Session-related methods. |
paulb@183 | 436 | |
paulb@183 | 437 | def get_session(self, create=1): |
paulb@183 | 438 | |
paulb@183 | 439 | """ |
paulb@186 | 440 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 441 | transaction. |
paulb@183 | 442 | |
paulb@183 | 443 | If no session has yet been established according to information |
paulb@183 | 444 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 445 | determines whether a new session will be established. |
paulb@183 | 446 | |
paulb@183 | 447 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 448 | then None is returned. In all other cases, a session object is created |
paulb@183 | 449 | (where appropriate) and returned. |
paulb@183 | 450 | """ |
paulb@183 | 451 | |
paulb@183 | 452 | if Session: |
paulb@183 | 453 | # NOTE: Not exposing all functionality. |
paulb@183 | 454 | return Session.Session(self.trans) |
paulb@183 | 455 | else: |
paulb@239 | 456 | # NOTE: Requires configuration. |
paulb@239 | 457 | |
paulb@239 | 458 | if self.session_store is None: |
paulb@239 | 459 | self.session_store = SessionStore(self, os.path.join(apache.server_root(), "WebStack-sessions")) |
paulb@239 | 460 | return self.session_store.get_session(create) |
paulb@183 | 461 | |
paulb@183 | 462 | def expire_session(self): |
paulb@183 | 463 | |
paulb@183 | 464 | """ |
paulb@186 | 465 | Expires any session established according to information provided in the |
paulb@186 | 466 | transaction. |
paulb@183 | 467 | """ |
paulb@183 | 468 | |
paulb@239 | 469 | if Session: |
paulb@239 | 470 | session = self.get_session(create=0) |
paulb@239 | 471 | if session: |
paulb@239 | 472 | session.invalidate() |
paulb@239 | 473 | else: |
paulb@239 | 474 | # NOTE: Requires configuration. |
paulb@239 | 475 | |
paulb@239 | 476 | if self.session_store is None: |
paulb@239 | 477 | self.session_store = SessionStore(self, os.path.join(apache.server_root(), "WebStack-sessions")) |
paulb@239 | 478 | self.session_store.expire_session() |
paulb@183 | 479 | |
paulb@128 | 480 | # Application-specific methods. |
paulb@128 | 481 | |
paulb@128 | 482 | def set_user(self, username): |
paulb@128 | 483 | |
paulb@128 | 484 | """ |
paulb@128 | 485 | An application-specific method which sets the user information with |
paulb@128 | 486 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 487 | 'get_user'. |
paulb@128 | 488 | """ |
paulb@128 | 489 | |
paulb@128 | 490 | self.user = username |
paulb@128 | 491 | |
paulb@1 | 492 | # vim: tabstop=4 expandtab shiftwidth=4 |