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