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