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