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