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