paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Generic Web framework interfaces. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@403 | 21 | |
paulb@403 | 22 | -------- |
paulb@403 | 23 | |
paulb@3 | 24 | The WebStack architecture consists of the following layers: |
paulb@3 | 25 | |
paulb@3 | 26 | * Framework - The underlying Web framework implementation. |
paulb@3 | 27 | * Adapter - Code operating under the particular framework which creates |
paulb@3 | 28 | WebStack abstractions and issues them to the application. |
paulb@3 | 29 | * Resources - Units of functionality operating within the hosted Web |
paulb@3 | 30 | application. |
paulb@3 | 31 | |
paulb@3 | 32 | Resources can act as both content producers within an application and as request |
paulb@3 | 33 | dispatchers to other resources; in the latter role, they may be referred to as |
paulb@3 | 34 | directors. |
paulb@1 | 35 | """ |
paulb@1 | 36 | |
paulb@258 | 37 | import urllib |
paulb@258 | 38 | from WebStack.Helpers.Request import Cookie |
paulb@258 | 39 | |
paulb@296 | 40 | class EndOfResponse(Exception): |
paulb@296 | 41 | |
paulb@296 | 42 | "An exception which signals the end of a response." |
paulb@296 | 43 | |
paulb@296 | 44 | pass |
paulb@296 | 45 | |
paulb@238 | 46 | class HeaderValue: |
paulb@238 | 47 | |
paulb@238 | 48 | "A container for header information." |
paulb@238 | 49 | |
paulb@238 | 50 | def __init__(self, principal_value, **attributes): |
paulb@238 | 51 | |
paulb@238 | 52 | """ |
paulb@238 | 53 | Initialise the container with the given 'principal_value' and optional |
paulb@238 | 54 | keyword attributes representing the key=value pairs which accompany the |
paulb@238 | 55 | 'principal_value'. |
paulb@238 | 56 | """ |
paulb@238 | 57 | |
paulb@238 | 58 | self.principal_value = principal_value |
paulb@238 | 59 | self.attributes = attributes |
paulb@238 | 60 | |
paulb@238 | 61 | def __getattr__(self, name): |
paulb@238 | 62 | if self.attributes.has_key(name): |
paulb@238 | 63 | return self.attributes[name] |
paulb@238 | 64 | else: |
paulb@238 | 65 | raise AttributeError, name |
paulb@238 | 66 | |
paulb@238 | 67 | def __str__(self): |
paulb@238 | 68 | |
paulb@238 | 69 | """ |
paulb@238 | 70 | Format the header value object, producing a string suitable for the |
paulb@238 | 71 | response header field. |
paulb@238 | 72 | """ |
paulb@238 | 73 | |
paulb@238 | 74 | l = [] |
paulb@238 | 75 | if self.principal_value: |
paulb@238 | 76 | l.append(self.principal_value) |
paulb@238 | 77 | for name, value in self.attributes.items(): |
paulb@238 | 78 | l.append("; ") |
paulb@238 | 79 | l.append("%s=%s" % (name, value)) |
paulb@238 | 80 | |
paulb@238 | 81 | # Make sure that only ASCII is used. |
paulb@238 | 82 | |
paulb@238 | 83 | return "".join(l).encode("US-ASCII") |
paulb@238 | 84 | |
paulb@238 | 85 | class ContentType(HeaderValue): |
paulb@1 | 86 | |
paulb@1 | 87 | "A container for content type information." |
paulb@1 | 88 | |
paulb@224 | 89 | def __init__(self, media_type, charset=None, **attributes): |
paulb@224 | 90 | |
paulb@224 | 91 | """ |
paulb@224 | 92 | Initialise the container with the given 'media_type', an optional |
paulb@224 | 93 | 'charset', and optional keyword attributes representing the key=value |
paulb@224 | 94 | pairs which qualify content types. |
paulb@224 | 95 | """ |
paulb@224 | 96 | |
paulb@238 | 97 | if charset is not None: |
paulb@238 | 98 | attributes["charset"] = charset |
paulb@238 | 99 | HeaderValue.__init__(self, media_type, **attributes) |
paulb@219 | 100 | |
paulb@238 | 101 | def __getattr__(self, name): |
paulb@238 | 102 | if name == "media_type": |
paulb@238 | 103 | return self.principal_value |
paulb@238 | 104 | elif name == "charset": |
paulb@238 | 105 | return self.attributes.get("charset") |
paulb@238 | 106 | elif self.attributes.has_key(name): |
paulb@238 | 107 | return self.attributes[name] |
paulb@238 | 108 | else: |
paulb@238 | 109 | raise AttributeError, name |
paulb@1 | 110 | |
paulb@1 | 111 | class Transaction: |
paulb@1 | 112 | |
paulb@1 | 113 | """ |
paulb@1 | 114 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 115 | overridden. |
paulb@1 | 116 | """ |
paulb@1 | 117 | |
paulb@224 | 118 | # The default charset ties output together with body field interpretation. |
paulb@224 | 119 | |
paulb@224 | 120 | default_charset = "iso-8859-1" |
paulb@224 | 121 | |
paulb@28 | 122 | def commit(self): |
paulb@28 | 123 | |
paulb@28 | 124 | """ |
paulb@28 | 125 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 126 | objects. |
paulb@28 | 127 | """ |
paulb@28 | 128 | |
paulb@28 | 129 | pass |
paulb@28 | 130 | |
paulb@1 | 131 | # Utility methods. |
paulb@1 | 132 | |
paulb@238 | 133 | def parse_header_value(self, header_class, header_value_str): |
paulb@1 | 134 | |
paulb@1 | 135 | """ |
paulb@238 | 136 | Create an object of the given 'header_class' by determining the details |
paulb@238 | 137 | of the given 'header_value_str' - a string containing the value of a |
paulb@238 | 138 | particular header. |
paulb@1 | 139 | """ |
paulb@1 | 140 | |
paulb@238 | 141 | if header_value_str is None: |
paulb@238 | 142 | return header_class(None) |
paulb@1 | 143 | |
paulb@238 | 144 | l = header_value_str.split(";") |
paulb@224 | 145 | attributes = {} |
paulb@193 | 146 | |
paulb@238 | 147 | # Find the attributes. |
paulb@193 | 148 | |
paulb@238 | 149 | principal_value, attributes_str = l[0].strip(), l[1:] |
paulb@5 | 150 | |
paulb@224 | 151 | for attribute_str in attributes_str: |
paulb@224 | 152 | t = attribute_str.split("=") |
paulb@224 | 153 | if len(t) > 1: |
paulb@224 | 154 | name, value = t[0].strip(), t[1].strip() |
paulb@238 | 155 | attributes[name] = value |
paulb@5 | 156 | |
paulb@238 | 157 | return header_class(principal_value, **attributes) |
paulb@238 | 158 | |
paulb@238 | 159 | def parse_content_type(self, content_type_field): |
paulb@353 | 160 | |
paulb@353 | 161 | """ |
paulb@353 | 162 | Parse the given 'content_type_field' - a value found comparable to that |
paulb@353 | 163 | found in an HTTP request header for "Content-Type". |
paulb@353 | 164 | """ |
paulb@353 | 165 | |
paulb@238 | 166 | return self.parse_header_value(ContentType, content_type_field) |
paulb@5 | 167 | |
paulb@49 | 168 | def format_header_value(self, value): |
paulb@49 | 169 | |
paulb@49 | 170 | """ |
paulb@49 | 171 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 172 | of US-ASCII. |
paulb@49 | 173 | """ |
paulb@49 | 174 | |
paulb@49 | 175 | return value.encode("US-ASCII") |
paulb@49 | 176 | |
paulb@258 | 177 | def encode_cookie_value(self, value): |
paulb@258 | 178 | |
paulb@258 | 179 | """ |
paulb@258 | 180 | Encode the given cookie 'value'. This ensures the usage of US-ASCII |
paulb@258 | 181 | through the encoding of Unicode objects as URL-encoded UTF-8 text. |
paulb@258 | 182 | """ |
paulb@258 | 183 | |
paulb@258 | 184 | return urllib.quote(value.encode("UTF-8")).encode("US-ASCII") |
paulb@258 | 185 | |
paulb@258 | 186 | def decode_cookie_value(self, value): |
paulb@258 | 187 | |
paulb@258 | 188 | """ |
paulb@258 | 189 | Decode the given cookie 'value'. |
paulb@258 | 190 | """ |
paulb@258 | 191 | |
paulb@258 | 192 | return unicode(urllib.unquote(value), "UTF-8") |
paulb@258 | 193 | |
paulb@258 | 194 | def process_cookies(self, cookie_dict, using_strings=0): |
paulb@258 | 195 | |
paulb@258 | 196 | """ |
paulb@258 | 197 | Process the given 'cookie_dict', returning a dictionary mapping cookie names |
paulb@258 | 198 | to cookie objects where the names and values have been decoded from the form |
paulb@258 | 199 | used in the cookies retrieved from the request. |
paulb@258 | 200 | |
paulb@258 | 201 | The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a |
paulb@258 | 202 | mapping of cookie names to values. |
paulb@258 | 203 | """ |
paulb@258 | 204 | |
paulb@258 | 205 | cookies = {} |
paulb@258 | 206 | for name in cookie_dict.keys(): |
paulb@258 | 207 | if using_strings: |
paulb@258 | 208 | value = cookie_dict[name] |
paulb@258 | 209 | else: |
paulb@258 | 210 | cookie = cookie_dict[name] |
paulb@258 | 211 | value = cookie.value |
paulb@258 | 212 | cookie_name = self.decode_cookie_value(name) |
paulb@258 | 213 | cookie_value = self.decode_cookie_value(value) |
paulb@258 | 214 | cookies[cookie_name] = Cookie(cookie_name, cookie_value) |
paulb@258 | 215 | return cookies |
paulb@258 | 216 | |
paulb@1 | 217 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 218 | |
paulb@1 | 219 | """ |
paulb@1 | 220 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 221 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 222 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 223 | criteria are ignored. |
paulb@1 | 224 | |
paulb@1 | 225 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 226 | preferences are appropriate. |
paulb@1 | 227 | """ |
paulb@1 | 228 | |
paulb@108 | 229 | if accept_preference is None: |
paulb@108 | 230 | return [] |
paulb@108 | 231 | |
paulb@1 | 232 | accept_defs = accept_preference.split(",") |
paulb@1 | 233 | accept_prefs = [] |
paulb@1 | 234 | for accept_def in accept_defs: |
paulb@1 | 235 | t = accept_def.split(";") |
paulb@1 | 236 | if len(t) >= 1: |
paulb@1 | 237 | accept_prefs.append(t[0].strip()) |
paulb@1 | 238 | return accept_prefs |
paulb@1 | 239 | |
paulb@65 | 240 | def convert_to_list(self, value): |
paulb@65 | 241 | |
paulb@65 | 242 | """ |
paulb@65 | 243 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 244 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 245 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 246 | then an empty list is returned. |
paulb@65 | 247 | """ |
paulb@65 | 248 | |
paulb@65 | 249 | if type(value) == type([]): |
paulb@65 | 250 | return value |
paulb@65 | 251 | elif type(value) == type(()): |
paulb@65 | 252 | return list(value) |
paulb@65 | 253 | elif value is None: |
paulb@65 | 254 | return [] |
paulb@65 | 255 | else: |
paulb@65 | 256 | return [value] |
paulb@65 | 257 | |
paulb@338 | 258 | # Server-related methods. |
paulb@338 | 259 | |
paulb@338 | 260 | def get_server_name(self): |
paulb@338 | 261 | |
paulb@338 | 262 | "Returns the server name." |
paulb@338 | 263 | |
paulb@338 | 264 | raise NotImplementedError, "get_server_name" |
paulb@338 | 265 | |
paulb@338 | 266 | def get_server_port(self): |
paulb@338 | 267 | |
paulb@338 | 268 | "Returns the server port as a string." |
paulb@338 | 269 | |
paulb@338 | 270 | raise NotImplementedError, "get_server_port" |
paulb@338 | 271 | |
paulb@1 | 272 | # Request-related methods. |
paulb@1 | 273 | |
paulb@1 | 274 | def get_request_stream(self): |
paulb@1 | 275 | |
paulb@1 | 276 | """ |
paulb@186 | 277 | Returns the request stream for the transaction. |
paulb@1 | 278 | """ |
paulb@1 | 279 | |
paulb@1 | 280 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 281 | |
paulb@1 | 282 | def get_request_method(self): |
paulb@1 | 283 | |
paulb@1 | 284 | """ |
paulb@186 | 285 | Returns the request method. |
paulb@1 | 286 | """ |
paulb@1 | 287 | |
paulb@1 | 288 | raise NotImplementedError, "get_request_method" |
paulb@1 | 289 | |
paulb@1 | 290 | def get_headers(self): |
paulb@1 | 291 | |
paulb@1 | 292 | """ |
paulb@186 | 293 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 294 | names to values. |
paulb@1 | 295 | """ |
paulb@1 | 296 | |
paulb@1 | 297 | raise NotImplementedError, "get_headers" |
paulb@1 | 298 | |
paulb@65 | 299 | def get_header_values(self, key): |
paulb@65 | 300 | |
paulb@65 | 301 | """ |
paulb@186 | 302 | Returns a list of all request header values associated with the given |
paulb@186 | 303 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 304 | case-insensitive string. |
paulb@65 | 305 | """ |
paulb@65 | 306 | |
paulb@65 | 307 | raise NotImplementedError, "get_header_values" |
paulb@65 | 308 | |
paulb@1 | 309 | def get_content_type(self): |
paulb@1 | 310 | |
paulb@1 | 311 | """ |
paulb@186 | 312 | Returns the content type specified on the request, along with the |
paulb@186 | 313 | charset employed. |
paulb@1 | 314 | """ |
paulb@1 | 315 | |
paulb@1 | 316 | raise NotImplementedError, "get_content_type" |
paulb@1 | 317 | |
paulb@1 | 318 | def get_content_charsets(self): |
paulb@1 | 319 | |
paulb@1 | 320 | """ |
paulb@1 | 321 | Returns the character set preferences. |
paulb@1 | 322 | """ |
paulb@1 | 323 | |
paulb@1 | 324 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 325 | |
paulb@1 | 326 | def get_content_languages(self): |
paulb@1 | 327 | |
paulb@1 | 328 | """ |
paulb@186 | 329 | Returns extracted language information from the transaction. |
paulb@1 | 330 | """ |
paulb@1 | 331 | |
paulb@1 | 332 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 333 | |
paulb@5 | 334 | def get_path(self): |
paulb@5 | 335 | |
paulb@5 | 336 | """ |
paulb@186 | 337 | Returns the entire path from the request. |
paulb@5 | 338 | """ |
paulb@5 | 339 | |
paulb@5 | 340 | raise NotImplementedError, "get_path" |
paulb@5 | 341 | |
paulb@162 | 342 | def get_path_without_query(self): |
paulb@162 | 343 | |
paulb@162 | 344 | """ |
paulb@186 | 345 | Returns the entire path from the request minus the query string. |
paulb@162 | 346 | """ |
paulb@162 | 347 | |
paulb@162 | 348 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 349 | |
paulb@1 | 350 | def get_path_info(self): |
paulb@1 | 351 | |
paulb@1 | 352 | """ |
paulb@186 | 353 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 354 | handling the current request) from the request. |
paulb@1 | 355 | """ |
paulb@1 | 356 | |
paulb@1 | 357 | raise NotImplementedError, "get_path_info" |
paulb@1 | 358 | |
paulb@69 | 359 | def get_query_string(self): |
paulb@69 | 360 | |
paulb@69 | 361 | """ |
paulb@186 | 362 | Returns the query string from the path in the request. |
paulb@69 | 363 | """ |
paulb@69 | 364 | |
paulb@69 | 365 | raise NotImplementedError, "get_query_string" |
paulb@69 | 366 | |
paulb@69 | 367 | # Higher level request-related methods. |
paulb@69 | 368 | |
paulb@69 | 369 | def get_fields_from_path(self): |
paulb@69 | 370 | |
paulb@69 | 371 | """ |
paulb@249 | 372 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 373 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 374 | the path if handling a POST transaction. |
paulb@69 | 375 | |
paulb@71 | 376 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 377 | single value is associated with any given field name). |
paulb@69 | 378 | """ |
paulb@69 | 379 | |
paulb@69 | 380 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 381 | |
paulb@167 | 382 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 383 | |
paulb@69 | 384 | """ |
paulb@249 | 385 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 386 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 387 | encoding of the message body for cases where no such information is |
paulb@249 | 388 | available, but where the default encoding is to be overridden. |
paulb@69 | 389 | |
paulb@73 | 390 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 391 | single value is associated with any given field name). Each value is |
paulb@193 | 392 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 393 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 394 | """ |
paulb@69 | 395 | |
paulb@69 | 396 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 397 | |
paulb@249 | 398 | def get_fields(self, encoding=None): |
paulb@249 | 399 | |
paulb@249 | 400 | """ |
paulb@249 | 401 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 402 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 403 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 404 | where no such information is available, but where the default encoding |
paulb@249 | 405 | is to be overridden. |
paulb@249 | 406 | |
paulb@249 | 407 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 408 | single value is associated with any given field name). Each value is |
paulb@249 | 409 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 410 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 411 | |
paulb@249 | 412 | Where a given field name is used in both the path and message body to |
paulb@249 | 413 | specify values, the values from both sources will be combined into a |
paulb@249 | 414 | single list associated with that field name. |
paulb@249 | 415 | """ |
paulb@249 | 416 | |
paulb@249 | 417 | raise NotImplementedError, "get_fields" |
paulb@249 | 418 | |
paulb@45 | 419 | def get_user(self): |
paulb@1 | 420 | |
paulb@1 | 421 | """ |
paulb@186 | 422 | Extracts user information from the transaction. |
paulb@115 | 423 | |
paulb@115 | 424 | Returns a username as a string or None if no user is defined. |
paulb@1 | 425 | """ |
paulb@1 | 426 | |
paulb@45 | 427 | raise NotImplementedError, "get_user" |
paulb@1 | 428 | |
paulb@98 | 429 | def get_cookies(self): |
paulb@98 | 430 | |
paulb@98 | 431 | """ |
paulb@186 | 432 | Obtains cookie information from the request. |
paulb@98 | 433 | |
paulb@98 | 434 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 435 | """ |
paulb@98 | 436 | |
paulb@98 | 437 | raise NotImplementedError, "get_cookies" |
paulb@98 | 438 | |
paulb@104 | 439 | def get_cookie(self, cookie_name): |
paulb@104 | 440 | |
paulb@104 | 441 | """ |
paulb@186 | 442 | Obtains cookie information from the request. |
paulb@104 | 443 | |
paulb@104 | 444 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 445 | cookie exists. |
paulb@104 | 446 | """ |
paulb@104 | 447 | |
paulb@104 | 448 | raise NotImplementedError, "get_cookie" |
paulb@104 | 449 | |
paulb@1 | 450 | # Response-related methods. |
paulb@1 | 451 | |
paulb@1 | 452 | def get_response_stream(self): |
paulb@1 | 453 | |
paulb@1 | 454 | """ |
paulb@186 | 455 | Returns the response stream for the transaction. |
paulb@1 | 456 | """ |
paulb@1 | 457 | |
paulb@1 | 458 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 459 | |
paulb@252 | 460 | def get_response_stream_encoding(self): |
paulb@252 | 461 | |
paulb@252 | 462 | """ |
paulb@252 | 463 | Returns the response stream encoding. |
paulb@252 | 464 | """ |
paulb@252 | 465 | |
paulb@252 | 466 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 467 | |
paulb@42 | 468 | def get_response_code(self): |
paulb@42 | 469 | |
paulb@42 | 470 | """ |
paulb@42 | 471 | Get the response code associated with the transaction. If no response |
paulb@42 | 472 | code is defined, None is returned. |
paulb@42 | 473 | """ |
paulb@42 | 474 | |
paulb@42 | 475 | raise NotImplementedError, "get_response_code" |
paulb@42 | 476 | |
paulb@42 | 477 | def set_response_code(self, response_code): |
paulb@42 | 478 | |
paulb@42 | 479 | """ |
paulb@42 | 480 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 481 | specification. |
paulb@42 | 482 | """ |
paulb@42 | 483 | |
paulb@42 | 484 | raise NotImplementedError, "set_response_code" |
paulb@42 | 485 | |
paulb@67 | 486 | def set_header_value(self, header, value): |
paulb@49 | 487 | |
paulb@49 | 488 | """ |
paulb@49 | 489 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 490 | """ |
paulb@49 | 491 | |
paulb@67 | 492 | raise NotImplementedError, "set_header_value" |
paulb@49 | 493 | |
paulb@1 | 494 | def set_content_type(self, content_type): |
paulb@1 | 495 | |
paulb@1 | 496 | """ |
paulb@186 | 497 | Sets the 'content_type' for the response. |
paulb@1 | 498 | """ |
paulb@1 | 499 | |
paulb@1 | 500 | raise NotImplementedError, "set_content_type" |
paulb@1 | 501 | |
paulb@98 | 502 | # Higher level response-related methods. |
paulb@98 | 503 | |
paulb@98 | 504 | def set_cookie(self, cookie): |
paulb@98 | 505 | |
paulb@98 | 506 | """ |
paulb@186 | 507 | Stores the given 'cookie' object in the response. |
paulb@98 | 508 | """ |
paulb@98 | 509 | |
paulb@98 | 510 | raise NotImplementedError, "set_cookie" |
paulb@98 | 511 | |
paulb@98 | 512 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 513 | |
paulb@98 | 514 | """ |
paulb@186 | 515 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 516 | |
paulb@98 | 517 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 518 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 519 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 520 | """ |
paulb@98 | 521 | |
paulb@98 | 522 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 523 | |
paulb@98 | 524 | def delete_cookie(self, cookie_name): |
paulb@98 | 525 | |
paulb@98 | 526 | """ |
paulb@186 | 527 | Adds to the response a request that the cookie with the given |
paulb@186 | 528 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 529 | """ |
paulb@98 | 530 | |
paulb@98 | 531 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 532 | |
paulb@183 | 533 | # Session-related methods. |
paulb@183 | 534 | |
paulb@183 | 535 | def get_session(self, create=1): |
paulb@183 | 536 | |
paulb@183 | 537 | """ |
paulb@186 | 538 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 539 | transaction. |
paulb@183 | 540 | |
paulb@183 | 541 | If no session has yet been established according to information |
paulb@183 | 542 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 543 | determines whether a new session will be established. |
paulb@183 | 544 | |
paulb@183 | 545 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 546 | then None is returned. In all other cases, a session object is created |
paulb@183 | 547 | (where appropriate) and returned. |
paulb@183 | 548 | """ |
paulb@183 | 549 | |
paulb@183 | 550 | raise NotImplementedError, "get_session" |
paulb@183 | 551 | |
paulb@183 | 552 | def expire_session(self): |
paulb@183 | 553 | |
paulb@183 | 554 | """ |
paulb@186 | 555 | Expires any session established according to information provided in the |
paulb@186 | 556 | transaction. |
paulb@183 | 557 | """ |
paulb@183 | 558 | |
paulb@183 | 559 | raise NotImplementedError, "expire_session" |
paulb@183 | 560 | |
paulb@128 | 561 | # Application-specific methods. |
paulb@128 | 562 | |
paulb@128 | 563 | def set_user(self, username): |
paulb@128 | 564 | |
paulb@128 | 565 | """ |
paulb@128 | 566 | An application-specific method which sets the user information with |
paulb@128 | 567 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 568 | 'get_user'. |
paulb@128 | 569 | """ |
paulb@128 | 570 | |
paulb@311 | 571 | self.user = username |
paulb@311 | 572 | |
paulb@316 | 573 | def set_virtual_path_info(self, path_info): |
paulb@311 | 574 | |
paulb@311 | 575 | """ |
paulb@311 | 576 | An application-specific method which sets the 'path_info' in the |
paulb@316 | 577 | transaction. This affects subsequent calls to 'get_virtual_path_info'. |
paulb@421 | 578 | |
paulb@421 | 579 | Note that the virtual path info should either be an empty string, or it |
paulb@421 | 580 | should begin with "/" and then (optionally) include other details. |
paulb@421 | 581 | Virtual path info strings which omit the leading "/" - ie. containing |
paulb@421 | 582 | things like "xxx" or even "xxx/yyy" - do not really make sense and may |
paulb@421 | 583 | not be handled correctly by various WebStack components. |
paulb@311 | 584 | """ |
paulb@311 | 585 | |
paulb@311 | 586 | self.path_info = path_info |
paulb@128 | 587 | |
paulb@316 | 588 | def get_virtual_path_info(self): |
paulb@316 | 589 | |
paulb@316 | 590 | """ |
paulb@316 | 591 | An application-specific method which either returns path info set in the |
paulb@316 | 592 | 'set_virtual_path_info' method, or the normal path info found in the |
paulb@316 | 593 | request. |
paulb@316 | 594 | """ |
paulb@316 | 595 | |
paulb@399 | 596 | if self.path_info is not None: |
paulb@399 | 597 | return self.path_info |
paulb@399 | 598 | else: |
paulb@399 | 599 | return self.get_path_info() |
paulb@316 | 600 | |
paulb@421 | 601 | def get_processed_virtual_path_info(self): |
paulb@421 | 602 | |
paulb@421 | 603 | """ |
paulb@421 | 604 | An application-specific method which returns the virtual path info that |
paulb@421 | 605 | is considered "processed"; that is, the part of the path info which is |
paulb@421 | 606 | not included in the virtual path info. |
paulb@421 | 607 | |
paulb@421 | 608 | Where the virtual path info is identical to the path info, an empty |
paulb@421 | 609 | string is returned. |
paulb@421 | 610 | |
paulb@421 | 611 | Where the virtual path info is a substring of the path info, the path |
paulb@421 | 612 | info preceding that substring is returned. |
paulb@421 | 613 | |
paulb@421 | 614 | Where the virtual path info is either an empty string or not a substring |
paulb@421 | 615 | of the path info, the entire path info is returned. |
paulb@421 | 616 | """ |
paulb@421 | 617 | |
paulb@421 | 618 | real_path_info = self.get_path_info() |
paulb@421 | 619 | virtual_path_info = self.get_virtual_path_info() |
paulb@421 | 620 | |
paulb@421 | 621 | if virtual_path_info == "": |
paulb@421 | 622 | return real_path_info |
paulb@421 | 623 | |
paulb@421 | 624 | i = real_path_info.find(virtual_path_info) |
paulb@421 | 625 | if i == -1: |
paulb@421 | 626 | return real_path_info |
paulb@421 | 627 | else: |
paulb@421 | 628 | return real_path_info[:i] |
paulb@421 | 629 | |
paulb@1 | 630 | class Resource: |
paulb@1 | 631 | |
paulb@1 | 632 | "A generic resource interface." |
paulb@1 | 633 | |
paulb@3 | 634 | def respond(self, trans): |
paulb@3 | 635 | |
paulb@3 | 636 | """ |
paulb@3 | 637 | An application-specific method which performs activities on the basis of |
paulb@3 | 638 | the transaction object 'trans'. |
paulb@3 | 639 | """ |
paulb@3 | 640 | |
paulb@3 | 641 | raise NotImplementedError, "respond" |
paulb@1 | 642 | |
paulb@75 | 643 | class Authenticator: |
paulb@75 | 644 | |
paulb@75 | 645 | "A generic authentication component." |
paulb@75 | 646 | |
paulb@75 | 647 | def authenticate(self, trans): |
paulb@75 | 648 | |
paulb@75 | 649 | """ |
paulb@75 | 650 | An application-specific method which authenticates the sender of the |
paulb@75 | 651 | request described by the transaction object 'trans'. This method should |
paulb@75 | 652 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 653 | the transaction. |
paulb@75 | 654 | |
paulb@75 | 655 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 656 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 657 | evaluates to false. |
paulb@75 | 658 | """ |
paulb@75 | 659 | |
paulb@75 | 660 | raise NotImplementedError, "authenticate" |
paulb@75 | 661 | |
paulb@75 | 662 | def get_auth_type(self): |
paulb@75 | 663 | |
paulb@75 | 664 | """ |
paulb@75 | 665 | An application-specific method which returns the authentication type to |
paulb@75 | 666 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 667 | authentication. |
paulb@75 | 668 | """ |
paulb@75 | 669 | |
paulb@75 | 670 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 671 | |
paulb@75 | 672 | def get_realm(self): |
paulb@75 | 673 | |
paulb@75 | 674 | """ |
paulb@75 | 675 | An application-specific method which returns the name of the realm for |
paulb@75 | 676 | which authentication is taking place. |
paulb@75 | 677 | """ |
paulb@75 | 678 | |
paulb@75 | 679 | raise NotImplementedError, "get_realm" |
paulb@75 | 680 | |
paulb@1 | 681 | # vim: tabstop=4 expandtab shiftwidth=4 |