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