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