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