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@425 | 258 | # Public utility methods. |
paulb@425 | 259 | |
paulb@430 | 260 | def decode_path(self, path, encoding=None): |
paulb@425 | 261 | |
paulb@425 | 262 | """ |
paulb@430 | 263 | From the given 'path', use the optional 'encoding' (if specified) to decode the |
paulb@430 | 264 | information and convert it to Unicode. Upon failure for a specified 'encoding' |
paulb@430 | 265 | or where 'encoding' is not specified, use the default character encoding to |
paulb@430 | 266 | perform the conversion. |
paulb@430 | 267 | |
paulb@430 | 268 | Returns the 'path' as a Unicode value without "URL encoded" character values. |
paulb@425 | 269 | """ |
paulb@425 | 270 | |
paulb@430 | 271 | unquoted_path = urllib.unquote(path) |
paulb@430 | 272 | if encoding is not None: |
paulb@430 | 273 | try: |
paulb@439 | 274 | return unicode(unquoted_path, encoding) |
paulb@430 | 275 | except UnicodeError: |
paulb@430 | 276 | pass |
paulb@439 | 277 | return unicode(unquoted_path, self.default_charset) |
paulb@430 | 278 | |
paulb@430 | 279 | def encode_path(self, path, encoding=None): |
paulb@430 | 280 | |
paulb@430 | 281 | """ |
paulb@430 | 282 | Encode the given 'path', using the optional 'encoding' (if specified) or the |
paulb@430 | 283 | default encoding where 'encoding' is not specified, and produce a suitable "URL |
paulb@430 | 284 | encoded" string. |
paulb@430 | 285 | """ |
paulb@430 | 286 | |
paulb@430 | 287 | if encoding is not None: |
paulb@430 | 288 | return urllib.quote(path.encode(encoding)) |
paulb@430 | 289 | else: |
paulb@430 | 290 | return urllib.quote(path.encode(self.default_charset)) |
paulb@425 | 291 | |
paulb@338 | 292 | # Server-related methods. |
paulb@338 | 293 | |
paulb@338 | 294 | def get_server_name(self): |
paulb@338 | 295 | |
paulb@338 | 296 | "Returns the server name." |
paulb@338 | 297 | |
paulb@338 | 298 | raise NotImplementedError, "get_server_name" |
paulb@338 | 299 | |
paulb@338 | 300 | def get_server_port(self): |
paulb@338 | 301 | |
paulb@338 | 302 | "Returns the server port as a string." |
paulb@338 | 303 | |
paulb@338 | 304 | raise NotImplementedError, "get_server_port" |
paulb@338 | 305 | |
paulb@1 | 306 | # Request-related methods. |
paulb@1 | 307 | |
paulb@1 | 308 | def get_request_stream(self): |
paulb@1 | 309 | |
paulb@1 | 310 | """ |
paulb@186 | 311 | Returns the request stream for the transaction. |
paulb@1 | 312 | """ |
paulb@1 | 313 | |
paulb@1 | 314 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 315 | |
paulb@1 | 316 | def get_request_method(self): |
paulb@1 | 317 | |
paulb@1 | 318 | """ |
paulb@186 | 319 | Returns the request method. |
paulb@1 | 320 | """ |
paulb@1 | 321 | |
paulb@1 | 322 | raise NotImplementedError, "get_request_method" |
paulb@1 | 323 | |
paulb@1 | 324 | def get_headers(self): |
paulb@1 | 325 | |
paulb@1 | 326 | """ |
paulb@186 | 327 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 328 | names to values. |
paulb@1 | 329 | """ |
paulb@1 | 330 | |
paulb@1 | 331 | raise NotImplementedError, "get_headers" |
paulb@1 | 332 | |
paulb@65 | 333 | def get_header_values(self, key): |
paulb@65 | 334 | |
paulb@65 | 335 | """ |
paulb@186 | 336 | Returns a list of all request header values associated with the given |
paulb@186 | 337 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 338 | case-insensitive string. |
paulb@65 | 339 | """ |
paulb@65 | 340 | |
paulb@65 | 341 | raise NotImplementedError, "get_header_values" |
paulb@65 | 342 | |
paulb@1 | 343 | def get_content_type(self): |
paulb@1 | 344 | |
paulb@1 | 345 | """ |
paulb@186 | 346 | Returns the content type specified on the request, along with the |
paulb@186 | 347 | charset employed. |
paulb@1 | 348 | """ |
paulb@1 | 349 | |
paulb@1 | 350 | raise NotImplementedError, "get_content_type" |
paulb@1 | 351 | |
paulb@1 | 352 | def get_content_charsets(self): |
paulb@1 | 353 | |
paulb@1 | 354 | """ |
paulb@1 | 355 | Returns the character set preferences. |
paulb@1 | 356 | """ |
paulb@1 | 357 | |
paulb@1 | 358 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 359 | |
paulb@1 | 360 | def get_content_languages(self): |
paulb@1 | 361 | |
paulb@1 | 362 | """ |
paulb@186 | 363 | Returns extracted language information from the transaction. |
paulb@1 | 364 | """ |
paulb@1 | 365 | |
paulb@1 | 366 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 367 | |
paulb@434 | 368 | def get_path(self, encoding=None): |
paulb@5 | 369 | |
paulb@5 | 370 | """ |
paulb@434 | 371 | Returns the entire path from the request as a Unicode object. Any "URL |
paulb@434 | 372 | encoded" character values in the part of the path before the query |
paulb@434 | 373 | string will be decoded and presented as genuine characters; the query |
paulb@434 | 374 | string will remain "URL encoded", however. |
paulb@434 | 375 | |
paulb@434 | 376 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 377 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 378 | character values. |
paulb@5 | 379 | """ |
paulb@5 | 380 | |
paulb@5 | 381 | raise NotImplementedError, "get_path" |
paulb@5 | 382 | |
paulb@434 | 383 | def get_path_without_query(self, encoding=None): |
paulb@162 | 384 | |
paulb@162 | 385 | """ |
paulb@434 | 386 | Returns the entire path from the request minus the query string as a |
paulb@434 | 387 | Unicode object containing genuine characters (as opposed to "URL |
paulb@434 | 388 | encoded" character values). |
paulb@434 | 389 | |
paulb@434 | 390 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 391 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 392 | character values. |
paulb@162 | 393 | """ |
paulb@162 | 394 | |
paulb@162 | 395 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 396 | |
paulb@434 | 397 | def get_path_info(self, encoding=None): |
paulb@1 | 398 | |
paulb@1 | 399 | """ |
paulb@186 | 400 | Returns the "path info" (the part of the URL after the resource name |
paulb@434 | 401 | handling the current request) from the request as a Unicode object |
paulb@434 | 402 | containing genuine characters (as opposed to "URL encoded" character |
paulb@434 | 403 | values). |
paulb@434 | 404 | |
paulb@434 | 405 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 406 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 407 | character values. |
paulb@1 | 408 | """ |
paulb@1 | 409 | |
paulb@1 | 410 | raise NotImplementedError, "get_path_info" |
paulb@1 | 411 | |
paulb@69 | 412 | def get_query_string(self): |
paulb@69 | 413 | |
paulb@69 | 414 | """ |
paulb@186 | 415 | Returns the query string from the path in the request. |
paulb@69 | 416 | """ |
paulb@69 | 417 | |
paulb@69 | 418 | raise NotImplementedError, "get_query_string" |
paulb@69 | 419 | |
paulb@69 | 420 | # Higher level request-related methods. |
paulb@69 | 421 | |
paulb@430 | 422 | def get_fields_from_path(self, encoding=None): |
paulb@69 | 423 | |
paulb@69 | 424 | """ |
paulb@249 | 425 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 426 | transaction. The underlying framework may refuse to supply fields from |
paulb@430 | 427 | the path if handling a POST transaction. The optional 'encoding' |
paulb@430 | 428 | parameter specifies the character encoding of the query string for cases |
paulb@430 | 429 | where the default encoding is to be overridden. |
paulb@69 | 430 | |
paulb@71 | 431 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 432 | single value is associated with any given field name). |
paulb@69 | 433 | """ |
paulb@69 | 434 | |
paulb@69 | 435 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 436 | |
paulb@167 | 437 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 438 | |
paulb@69 | 439 | """ |
paulb@249 | 440 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 441 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 442 | encoding of the message body for cases where no such information is |
paulb@249 | 443 | available, but where the default encoding is to be overridden. |
paulb@69 | 444 | |
paulb@73 | 445 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 446 | single value is associated with any given field name). Each value is |
paulb@193 | 447 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 448 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 449 | """ |
paulb@69 | 450 | |
paulb@69 | 451 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 452 | |
paulb@249 | 453 | def get_fields(self, encoding=None): |
paulb@249 | 454 | |
paulb@249 | 455 | """ |
paulb@249 | 456 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 457 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 458 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 459 | where no such information is available, but where the default encoding |
paulb@249 | 460 | is to be overridden. |
paulb@249 | 461 | |
paulb@249 | 462 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 463 | single value is associated with any given field name). Each value is |
paulb@249 | 464 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 465 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 466 | |
paulb@249 | 467 | Where a given field name is used in both the path and message body to |
paulb@249 | 468 | specify values, the values from both sources will be combined into a |
paulb@249 | 469 | single list associated with that field name. |
paulb@249 | 470 | """ |
paulb@249 | 471 | |
paulb@249 | 472 | raise NotImplementedError, "get_fields" |
paulb@249 | 473 | |
paulb@45 | 474 | def get_user(self): |
paulb@1 | 475 | |
paulb@1 | 476 | """ |
paulb@186 | 477 | Extracts user information from the transaction. |
paulb@115 | 478 | |
paulb@115 | 479 | Returns a username as a string or None if no user is defined. |
paulb@1 | 480 | """ |
paulb@1 | 481 | |
paulb@45 | 482 | raise NotImplementedError, "get_user" |
paulb@1 | 483 | |
paulb@98 | 484 | def get_cookies(self): |
paulb@98 | 485 | |
paulb@98 | 486 | """ |
paulb@186 | 487 | Obtains cookie information from the request. |
paulb@98 | 488 | |
paulb@98 | 489 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 490 | """ |
paulb@98 | 491 | |
paulb@98 | 492 | raise NotImplementedError, "get_cookies" |
paulb@98 | 493 | |
paulb@104 | 494 | def get_cookie(self, cookie_name): |
paulb@104 | 495 | |
paulb@104 | 496 | """ |
paulb@186 | 497 | Obtains cookie information from the request. |
paulb@104 | 498 | |
paulb@104 | 499 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 500 | cookie exists. |
paulb@104 | 501 | """ |
paulb@104 | 502 | |
paulb@104 | 503 | raise NotImplementedError, "get_cookie" |
paulb@104 | 504 | |
paulb@1 | 505 | # Response-related methods. |
paulb@1 | 506 | |
paulb@1 | 507 | def get_response_stream(self): |
paulb@1 | 508 | |
paulb@1 | 509 | """ |
paulb@186 | 510 | Returns the response stream for the transaction. |
paulb@1 | 511 | """ |
paulb@1 | 512 | |
paulb@1 | 513 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 514 | |
paulb@252 | 515 | def get_response_stream_encoding(self): |
paulb@252 | 516 | |
paulb@252 | 517 | """ |
paulb@252 | 518 | Returns the response stream encoding. |
paulb@252 | 519 | """ |
paulb@252 | 520 | |
paulb@252 | 521 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 522 | |
paulb@42 | 523 | def get_response_code(self): |
paulb@42 | 524 | |
paulb@42 | 525 | """ |
paulb@42 | 526 | Get the response code associated with the transaction. If no response |
paulb@42 | 527 | code is defined, None is returned. |
paulb@42 | 528 | """ |
paulb@42 | 529 | |
paulb@42 | 530 | raise NotImplementedError, "get_response_code" |
paulb@42 | 531 | |
paulb@42 | 532 | def set_response_code(self, response_code): |
paulb@42 | 533 | |
paulb@42 | 534 | """ |
paulb@42 | 535 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 536 | specification. |
paulb@42 | 537 | """ |
paulb@42 | 538 | |
paulb@42 | 539 | raise NotImplementedError, "set_response_code" |
paulb@42 | 540 | |
paulb@67 | 541 | def set_header_value(self, header, value): |
paulb@49 | 542 | |
paulb@49 | 543 | """ |
paulb@49 | 544 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 545 | """ |
paulb@49 | 546 | |
paulb@67 | 547 | raise NotImplementedError, "set_header_value" |
paulb@49 | 548 | |
paulb@1 | 549 | def set_content_type(self, content_type): |
paulb@1 | 550 | |
paulb@1 | 551 | """ |
paulb@186 | 552 | Sets the 'content_type' for the response. |
paulb@1 | 553 | """ |
paulb@1 | 554 | |
paulb@1 | 555 | raise NotImplementedError, "set_content_type" |
paulb@1 | 556 | |
paulb@98 | 557 | # Higher level response-related methods. |
paulb@98 | 558 | |
paulb@98 | 559 | def set_cookie(self, cookie): |
paulb@98 | 560 | |
paulb@98 | 561 | """ |
paulb@186 | 562 | Stores the given 'cookie' object in the response. |
paulb@98 | 563 | """ |
paulb@98 | 564 | |
paulb@98 | 565 | raise NotImplementedError, "set_cookie" |
paulb@98 | 566 | |
paulb@98 | 567 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 568 | |
paulb@98 | 569 | """ |
paulb@186 | 570 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 571 | |
paulb@98 | 572 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 573 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 574 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 575 | """ |
paulb@98 | 576 | |
paulb@98 | 577 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 578 | |
paulb@98 | 579 | def delete_cookie(self, cookie_name): |
paulb@98 | 580 | |
paulb@98 | 581 | """ |
paulb@186 | 582 | Adds to the response a request that the cookie with the given |
paulb@186 | 583 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 584 | """ |
paulb@98 | 585 | |
paulb@98 | 586 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 587 | |
paulb@183 | 588 | # Session-related methods. |
paulb@183 | 589 | |
paulb@183 | 590 | def get_session(self, create=1): |
paulb@183 | 591 | |
paulb@183 | 592 | """ |
paulb@186 | 593 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 594 | transaction. |
paulb@183 | 595 | |
paulb@183 | 596 | If no session has yet been established according to information |
paulb@183 | 597 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 598 | determines whether a new session will be established. |
paulb@183 | 599 | |
paulb@183 | 600 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 601 | then None is returned. In all other cases, a session object is created |
paulb@183 | 602 | (where appropriate) and returned. |
paulb@183 | 603 | """ |
paulb@183 | 604 | |
paulb@183 | 605 | raise NotImplementedError, "get_session" |
paulb@183 | 606 | |
paulb@183 | 607 | def expire_session(self): |
paulb@183 | 608 | |
paulb@183 | 609 | """ |
paulb@186 | 610 | Expires any session established according to information provided in the |
paulb@186 | 611 | transaction. |
paulb@183 | 612 | """ |
paulb@183 | 613 | |
paulb@183 | 614 | raise NotImplementedError, "expire_session" |
paulb@183 | 615 | |
paulb@128 | 616 | # Application-specific methods. |
paulb@128 | 617 | |
paulb@128 | 618 | def set_user(self, username): |
paulb@128 | 619 | |
paulb@128 | 620 | """ |
paulb@128 | 621 | An application-specific method which sets the user information with |
paulb@128 | 622 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 623 | 'get_user'. |
paulb@128 | 624 | """ |
paulb@128 | 625 | |
paulb@311 | 626 | self.user = username |
paulb@311 | 627 | |
paulb@316 | 628 | def set_virtual_path_info(self, path_info): |
paulb@311 | 629 | |
paulb@311 | 630 | """ |
paulb@311 | 631 | An application-specific method which sets the 'path_info' in the |
paulb@316 | 632 | transaction. This affects subsequent calls to 'get_virtual_path_info'. |
paulb@421 | 633 | |
paulb@421 | 634 | Note that the virtual path info should either be an empty string, or it |
paulb@421 | 635 | should begin with "/" and then (optionally) include other details. |
paulb@421 | 636 | Virtual path info strings which omit the leading "/" - ie. containing |
paulb@421 | 637 | things like "xxx" or even "xxx/yyy" - do not really make sense and may |
paulb@421 | 638 | not be handled correctly by various WebStack components. |
paulb@311 | 639 | """ |
paulb@311 | 640 | |
paulb@311 | 641 | self.path_info = path_info |
paulb@128 | 642 | |
paulb@446 | 643 | def get_virtual_path_info(self, encoding=None): |
paulb@316 | 644 | |
paulb@316 | 645 | """ |
paulb@316 | 646 | An application-specific method which either returns path info set in the |
paulb@316 | 647 | 'set_virtual_path_info' method, or the normal path info found in the |
paulb@316 | 648 | request. |
paulb@446 | 649 | |
paulb@446 | 650 | If the optional 'encoding' is set, use that in preference to the default |
paulb@446 | 651 | encoding to convert the path into a form not containing "URL encoded" |
paulb@446 | 652 | character values. |
paulb@316 | 653 | """ |
paulb@316 | 654 | |
paulb@399 | 655 | if self.path_info is not None: |
paulb@399 | 656 | return self.path_info |
paulb@399 | 657 | else: |
paulb@446 | 658 | return self.get_path_info(encoding) |
paulb@316 | 659 | |
paulb@446 | 660 | def get_processed_virtual_path_info(self, encoding=None): |
paulb@421 | 661 | |
paulb@421 | 662 | """ |
paulb@421 | 663 | An application-specific method which returns the virtual path info that |
paulb@421 | 664 | is considered "processed"; that is, the part of the path info which is |
paulb@421 | 665 | not included in the virtual path info. |
paulb@421 | 666 | |
paulb@446 | 667 | If the optional 'encoding' is set, use that in preference to the default |
paulb@446 | 668 | encoding to convert the path into a form not containing "URL encoded" |
paulb@446 | 669 | character values. |
paulb@446 | 670 | |
paulb@421 | 671 | Where the virtual path info is identical to the path info, an empty |
paulb@421 | 672 | string is returned. |
paulb@421 | 673 | |
paulb@421 | 674 | Where the virtual path info is a substring of the path info, the path |
paulb@421 | 675 | info preceding that substring is returned. |
paulb@421 | 676 | |
paulb@421 | 677 | Where the virtual path info is either an empty string or not a substring |
paulb@421 | 678 | of the path info, the entire path info is returned. |
paulb@421 | 679 | """ |
paulb@421 | 680 | |
paulb@446 | 681 | real_path_info = self.get_path_info(encoding) |
paulb@446 | 682 | virtual_path_info = self.get_virtual_path_info(encoding) |
paulb@421 | 683 | |
paulb@421 | 684 | if virtual_path_info == "": |
paulb@421 | 685 | return real_path_info |
paulb@421 | 686 | |
paulb@421 | 687 | i = real_path_info.find(virtual_path_info) |
paulb@421 | 688 | if i == -1: |
paulb@421 | 689 | return real_path_info |
paulb@421 | 690 | else: |
paulb@421 | 691 | return real_path_info[:i] |
paulb@421 | 692 | |
paulb@1 | 693 | class Resource: |
paulb@1 | 694 | |
paulb@1 | 695 | "A generic resource interface." |
paulb@1 | 696 | |
paulb@3 | 697 | def respond(self, trans): |
paulb@3 | 698 | |
paulb@3 | 699 | """ |
paulb@3 | 700 | An application-specific method which performs activities on the basis of |
paulb@3 | 701 | the transaction object 'trans'. |
paulb@3 | 702 | """ |
paulb@3 | 703 | |
paulb@3 | 704 | raise NotImplementedError, "respond" |
paulb@1 | 705 | |
paulb@75 | 706 | class Authenticator: |
paulb@75 | 707 | |
paulb@75 | 708 | "A generic authentication component." |
paulb@75 | 709 | |
paulb@75 | 710 | def authenticate(self, trans): |
paulb@75 | 711 | |
paulb@75 | 712 | """ |
paulb@75 | 713 | An application-specific method which authenticates the sender of the |
paulb@75 | 714 | request described by the transaction object 'trans'. This method should |
paulb@75 | 715 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 716 | the transaction. |
paulb@75 | 717 | |
paulb@75 | 718 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 719 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 720 | evaluates to false. |
paulb@75 | 721 | """ |
paulb@75 | 722 | |
paulb@75 | 723 | raise NotImplementedError, "authenticate" |
paulb@75 | 724 | |
paulb@75 | 725 | def get_auth_type(self): |
paulb@75 | 726 | |
paulb@75 | 727 | """ |
paulb@75 | 728 | An application-specific method which returns the authentication type to |
paulb@75 | 729 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 730 | authentication. |
paulb@75 | 731 | """ |
paulb@75 | 732 | |
paulb@75 | 733 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 734 | |
paulb@75 | 735 | def get_realm(self): |
paulb@75 | 736 | |
paulb@75 | 737 | """ |
paulb@75 | 738 | An application-specific method which returns the name of the realm for |
paulb@75 | 739 | which authentication is taking place. |
paulb@75 | 740 | """ |
paulb@75 | 741 | |
paulb@75 | 742 | raise NotImplementedError, "get_realm" |
paulb@75 | 743 | |
paulb@1 | 744 | # vim: tabstop=4 expandtab shiftwidth=4 |