paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Generic Web framework interfaces. |
paulb@403 | 5 | |
paulb@562 | 6 | Copyright (C) 2004, 2005, 2006 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@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@463 | 38 | from WebStack.Helpers.Request import Cookie, parse_header_value, ContentType, HeaderValue |
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@1 | 46 | class Transaction: |
paulb@1 | 47 | |
paulb@1 | 48 | """ |
paulb@1 | 49 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 50 | overridden. |
paulb@1 | 51 | """ |
paulb@1 | 52 | |
paulb@224 | 53 | # The default charset ties output together with body field interpretation. |
paulb@224 | 54 | |
paulb@224 | 55 | default_charset = "iso-8859-1" |
paulb@224 | 56 | |
paulb@543 | 57 | # The default path info is provided here, although the manipulated virtual |
paulb@543 | 58 | # path info is an instance attribute set through instances of subclasses of |
paulb@543 | 59 | # this class. |
paulb@543 | 60 | |
paulb@543 | 61 | path_info = None |
paulb@543 | 62 | |
paulb@543 | 63 | # The default user is provided here, although the manipulated user is an |
paulb@543 | 64 | # instance attribute set through instances of subclasses of this class. |
paulb@543 | 65 | |
paulb@543 | 66 | user = None |
paulb@543 | 67 | |
paulb@28 | 68 | def commit(self): |
paulb@28 | 69 | |
paulb@28 | 70 | """ |
paulb@28 | 71 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 72 | objects. |
paulb@28 | 73 | """ |
paulb@28 | 74 | |
paulb@28 | 75 | pass |
paulb@28 | 76 | |
paulb@593 | 77 | def rollback(self): |
paulb@593 | 78 | |
paulb@593 | 79 | """ |
paulb@593 | 80 | A special method, partially synchronising the transaction with |
paulb@593 | 81 | framework-specific objects, but discarding previously emitted content |
paulb@593 | 82 | that is to be replaced by an error message. |
paulb@593 | 83 | """ |
paulb@593 | 84 | |
paulb@593 | 85 | pass |
paulb@593 | 86 | |
paulb@1 | 87 | # Utility methods. |
paulb@1 | 88 | |
paulb@238 | 89 | def parse_header_value(self, header_class, header_value_str): |
paulb@1 | 90 | |
paulb@1 | 91 | """ |
paulb@238 | 92 | Create an object of the given 'header_class' by determining the details |
paulb@238 | 93 | of the given 'header_value_str' - a string containing the value of a |
paulb@238 | 94 | particular header. |
paulb@1 | 95 | """ |
paulb@1 | 96 | |
paulb@463 | 97 | # Now uses the WebStack.Helpers.Request function of the same name. |
paulb@193 | 98 | |
paulb@463 | 99 | return parse_header_value(header_class, header_value_str) |
paulb@238 | 100 | |
paulb@238 | 101 | def parse_content_type(self, content_type_field): |
paulb@353 | 102 | |
paulb@353 | 103 | """ |
paulb@353 | 104 | Parse the given 'content_type_field' - a value found comparable to that |
paulb@353 | 105 | found in an HTTP request header for "Content-Type". |
paulb@353 | 106 | """ |
paulb@353 | 107 | |
paulb@238 | 108 | return self.parse_header_value(ContentType, content_type_field) |
paulb@5 | 109 | |
paulb@49 | 110 | def format_header_value(self, value): |
paulb@49 | 111 | |
paulb@49 | 112 | """ |
paulb@49 | 113 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 114 | of US-ASCII. |
paulb@49 | 115 | """ |
paulb@49 | 116 | |
paulb@49 | 117 | return value.encode("US-ASCII") |
paulb@49 | 118 | |
paulb@258 | 119 | def encode_cookie_value(self, value): |
paulb@258 | 120 | |
paulb@258 | 121 | """ |
paulb@258 | 122 | Encode the given cookie 'value'. This ensures the usage of US-ASCII |
paulb@258 | 123 | through the encoding of Unicode objects as URL-encoded UTF-8 text. |
paulb@258 | 124 | """ |
paulb@258 | 125 | |
paulb@258 | 126 | return urllib.quote(value.encode("UTF-8")).encode("US-ASCII") |
paulb@258 | 127 | |
paulb@258 | 128 | def decode_cookie_value(self, value): |
paulb@258 | 129 | |
paulb@258 | 130 | """ |
paulb@258 | 131 | Decode the given cookie 'value'. |
paulb@258 | 132 | """ |
paulb@258 | 133 | |
paulb@258 | 134 | return unicode(urllib.unquote(value), "UTF-8") |
paulb@258 | 135 | |
paulb@258 | 136 | def process_cookies(self, cookie_dict, using_strings=0): |
paulb@258 | 137 | |
paulb@258 | 138 | """ |
paulb@258 | 139 | Process the given 'cookie_dict', returning a dictionary mapping cookie names |
paulb@258 | 140 | to cookie objects where the names and values have been decoded from the form |
paulb@258 | 141 | used in the cookies retrieved from the request. |
paulb@258 | 142 | |
paulb@258 | 143 | The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a |
paulb@258 | 144 | mapping of cookie names to values. |
paulb@258 | 145 | """ |
paulb@258 | 146 | |
paulb@258 | 147 | cookies = {} |
paulb@258 | 148 | for name in cookie_dict.keys(): |
paulb@258 | 149 | if using_strings: |
paulb@258 | 150 | value = cookie_dict[name] |
paulb@258 | 151 | else: |
paulb@258 | 152 | cookie = cookie_dict[name] |
paulb@258 | 153 | value = cookie.value |
paulb@258 | 154 | cookie_name = self.decode_cookie_value(name) |
paulb@258 | 155 | cookie_value = self.decode_cookie_value(value) |
paulb@258 | 156 | cookies[cookie_name] = Cookie(cookie_name, cookie_value) |
paulb@258 | 157 | return cookies |
paulb@258 | 158 | |
paulb@1 | 159 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 160 | |
paulb@1 | 161 | """ |
paulb@1 | 162 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 163 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 164 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 165 | criteria are ignored. |
paulb@1 | 166 | |
paulb@1 | 167 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 168 | preferences are appropriate. |
paulb@1 | 169 | """ |
paulb@1 | 170 | |
paulb@108 | 171 | if accept_preference is None: |
paulb@108 | 172 | return [] |
paulb@108 | 173 | |
paulb@1 | 174 | accept_defs = accept_preference.split(",") |
paulb@1 | 175 | accept_prefs = [] |
paulb@1 | 176 | for accept_def in accept_defs: |
paulb@1 | 177 | t = accept_def.split(";") |
paulb@1 | 178 | if len(t) >= 1: |
paulb@1 | 179 | accept_prefs.append(t[0].strip()) |
paulb@1 | 180 | return accept_prefs |
paulb@1 | 181 | |
paulb@65 | 182 | def convert_to_list(self, value): |
paulb@65 | 183 | |
paulb@65 | 184 | """ |
paulb@65 | 185 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 186 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 187 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 188 | then an empty list is returned. |
paulb@65 | 189 | """ |
paulb@65 | 190 | |
paulb@65 | 191 | if type(value) == type([]): |
paulb@65 | 192 | return value |
paulb@65 | 193 | elif type(value) == type(()): |
paulb@65 | 194 | return list(value) |
paulb@65 | 195 | elif value is None: |
paulb@65 | 196 | return [] |
paulb@65 | 197 | else: |
paulb@65 | 198 | return [value] |
paulb@65 | 199 | |
paulb@425 | 200 | # Public utility methods. |
paulb@425 | 201 | |
paulb@430 | 202 | def decode_path(self, path, encoding=None): |
paulb@425 | 203 | |
paulb@425 | 204 | """ |
paulb@430 | 205 | From the given 'path', use the optional 'encoding' (if specified) to decode the |
paulb@430 | 206 | information and convert it to Unicode. Upon failure for a specified 'encoding' |
paulb@430 | 207 | or where 'encoding' is not specified, use the default character encoding to |
paulb@430 | 208 | perform the conversion. |
paulb@430 | 209 | |
paulb@430 | 210 | Returns the 'path' as a Unicode value without "URL encoded" character values. |
paulb@425 | 211 | """ |
paulb@425 | 212 | |
paulb@430 | 213 | unquoted_path = urllib.unquote(path) |
paulb@430 | 214 | if encoding is not None: |
paulb@430 | 215 | try: |
paulb@439 | 216 | return unicode(unquoted_path, encoding) |
paulb@430 | 217 | except UnicodeError: |
paulb@430 | 218 | pass |
paulb@439 | 219 | return unicode(unquoted_path, self.default_charset) |
paulb@430 | 220 | |
paulb@430 | 221 | def encode_path(self, path, encoding=None): |
paulb@430 | 222 | |
paulb@430 | 223 | """ |
paulb@430 | 224 | Encode the given 'path', using the optional 'encoding' (if specified) or the |
paulb@430 | 225 | default encoding where 'encoding' is not specified, and produce a suitable "URL |
paulb@430 | 226 | encoded" string. |
paulb@430 | 227 | """ |
paulb@430 | 228 | |
paulb@430 | 229 | if encoding is not None: |
paulb@430 | 230 | return urllib.quote(path.encode(encoding)) |
paulb@430 | 231 | else: |
paulb@430 | 232 | return urllib.quote(path.encode(self.default_charset)) |
paulb@425 | 233 | |
paulb@338 | 234 | # Server-related methods. |
paulb@338 | 235 | |
paulb@338 | 236 | def get_server_name(self): |
paulb@338 | 237 | |
paulb@338 | 238 | "Returns the server name." |
paulb@338 | 239 | |
paulb@338 | 240 | raise NotImplementedError, "get_server_name" |
paulb@338 | 241 | |
paulb@338 | 242 | def get_server_port(self): |
paulb@338 | 243 | |
paulb@338 | 244 | "Returns the server port as a string." |
paulb@338 | 245 | |
paulb@338 | 246 | raise NotImplementedError, "get_server_port" |
paulb@338 | 247 | |
paulb@1 | 248 | # Request-related methods. |
paulb@1 | 249 | |
paulb@1 | 250 | def get_request_stream(self): |
paulb@1 | 251 | |
paulb@1 | 252 | """ |
paulb@186 | 253 | Returns the request stream for the transaction. |
paulb@1 | 254 | """ |
paulb@1 | 255 | |
paulb@1 | 256 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 257 | |
paulb@1 | 258 | def get_request_method(self): |
paulb@1 | 259 | |
paulb@1 | 260 | """ |
paulb@186 | 261 | Returns the request method. |
paulb@1 | 262 | """ |
paulb@1 | 263 | |
paulb@1 | 264 | raise NotImplementedError, "get_request_method" |
paulb@1 | 265 | |
paulb@1 | 266 | def get_headers(self): |
paulb@1 | 267 | |
paulb@1 | 268 | """ |
paulb@186 | 269 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 270 | names to values. |
paulb@1 | 271 | """ |
paulb@1 | 272 | |
paulb@1 | 273 | raise NotImplementedError, "get_headers" |
paulb@1 | 274 | |
paulb@65 | 275 | def get_header_values(self, key): |
paulb@65 | 276 | |
paulb@65 | 277 | """ |
paulb@186 | 278 | Returns a list of all request header values associated with the given |
paulb@186 | 279 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 280 | case-insensitive string. |
paulb@65 | 281 | """ |
paulb@65 | 282 | |
paulb@65 | 283 | raise NotImplementedError, "get_header_values" |
paulb@65 | 284 | |
paulb@1 | 285 | def get_content_type(self): |
paulb@1 | 286 | |
paulb@1 | 287 | """ |
paulb@186 | 288 | Returns the content type specified on the request, along with the |
paulb@186 | 289 | charset employed. |
paulb@1 | 290 | """ |
paulb@1 | 291 | |
paulb@1 | 292 | raise NotImplementedError, "get_content_type" |
paulb@1 | 293 | |
paulb@1 | 294 | def get_content_charsets(self): |
paulb@1 | 295 | |
paulb@1 | 296 | """ |
paulb@1 | 297 | Returns the character set preferences. |
paulb@1 | 298 | """ |
paulb@1 | 299 | |
paulb@1 | 300 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 301 | |
paulb@1 | 302 | def get_content_languages(self): |
paulb@1 | 303 | |
paulb@1 | 304 | """ |
paulb@186 | 305 | Returns extracted language information from the transaction. |
paulb@1 | 306 | """ |
paulb@1 | 307 | |
paulb@1 | 308 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 309 | |
paulb@434 | 310 | def get_path(self, encoding=None): |
paulb@5 | 311 | |
paulb@5 | 312 | """ |
paulb@434 | 313 | Returns the entire path from the request as a Unicode object. Any "URL |
paulb@434 | 314 | encoded" character values in the part of the path before the query |
paulb@434 | 315 | string will be decoded and presented as genuine characters; the query |
paulb@434 | 316 | string will remain "URL encoded", however. |
paulb@434 | 317 | |
paulb@434 | 318 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 319 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 320 | character values. |
paulb@5 | 321 | """ |
paulb@5 | 322 | |
paulb@5 | 323 | raise NotImplementedError, "get_path" |
paulb@5 | 324 | |
paulb@434 | 325 | def get_path_without_query(self, encoding=None): |
paulb@162 | 326 | |
paulb@162 | 327 | """ |
paulb@434 | 328 | Returns the entire path from the request minus the query string as a |
paulb@434 | 329 | Unicode object containing genuine characters (as opposed to "URL |
paulb@434 | 330 | encoded" character values). |
paulb@434 | 331 | |
paulb@434 | 332 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 333 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 334 | character values. |
paulb@162 | 335 | """ |
paulb@162 | 336 | |
paulb@162 | 337 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 338 | |
paulb@434 | 339 | def get_path_info(self, encoding=None): |
paulb@1 | 340 | |
paulb@1 | 341 | """ |
paulb@186 | 342 | Returns the "path info" (the part of the URL after the resource name |
paulb@434 | 343 | handling the current request) from the request as a Unicode object |
paulb@434 | 344 | containing genuine characters (as opposed to "URL encoded" character |
paulb@434 | 345 | values). |
paulb@434 | 346 | |
paulb@434 | 347 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 348 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 349 | character values. |
paulb@1 | 350 | """ |
paulb@1 | 351 | |
paulb@1 | 352 | raise NotImplementedError, "get_path_info" |
paulb@1 | 353 | |
paulb@505 | 354 | def get_path_without_info(self, encoding=None): |
paulb@505 | 355 | |
paulb@505 | 356 | """ |
paulb@505 | 357 | Returns the entire path from the request minus the query string and the |
paulb@505 | 358 | "path info" as a Unicode object containing genuine characters (as |
paulb@505 | 359 | opposed to "URL encoded" character values). |
paulb@505 | 360 | |
paulb@505 | 361 | If the optional 'encoding' is set, use that in preference to the default |
paulb@505 | 362 | encoding to convert the path into a form not containing "URL encoded" |
paulb@505 | 363 | character values. |
paulb@505 | 364 | """ |
paulb@505 | 365 | |
paulb@505 | 366 | entire_path = self.get_path_without_query(encoding) |
paulb@505 | 367 | path_info = self.get_path_info(encoding) |
paulb@505 | 368 | return entire_path[:-len(path_info)] |
paulb@505 | 369 | |
paulb@69 | 370 | def get_query_string(self): |
paulb@69 | 371 | |
paulb@69 | 372 | """ |
paulb@186 | 373 | Returns the query string from the path in the request. |
paulb@69 | 374 | """ |
paulb@69 | 375 | |
paulb@69 | 376 | raise NotImplementedError, "get_query_string" |
paulb@69 | 377 | |
paulb@69 | 378 | # Higher level request-related methods. |
paulb@69 | 379 | |
paulb@430 | 380 | def get_fields_from_path(self, encoding=None): |
paulb@69 | 381 | |
paulb@69 | 382 | """ |
paulb@249 | 383 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 384 | transaction. The underlying framework may refuse to supply fields from |
paulb@430 | 385 | the path if handling a POST transaction. The optional 'encoding' |
paulb@430 | 386 | parameter specifies the character encoding of the query string for cases |
paulb@430 | 387 | where the default encoding is to be overridden. |
paulb@69 | 388 | |
paulb@71 | 389 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 390 | single value is associated with any given field name). |
paulb@69 | 391 | """ |
paulb@69 | 392 | |
paulb@69 | 393 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 394 | |
paulb@167 | 395 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 396 | |
paulb@69 | 397 | """ |
paulb@249 | 398 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 399 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 400 | encoding of the message body for cases where no such information is |
paulb@249 | 401 | available, but where the default encoding is to be overridden. |
paulb@69 | 402 | |
paulb@73 | 403 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 404 | single value is associated with any given field name). Each value is |
paulb@193 | 405 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 406 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 407 | upload form field). |
paulb@69 | 408 | """ |
paulb@69 | 409 | |
paulb@69 | 410 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 411 | |
paulb@249 | 412 | def get_fields(self, encoding=None): |
paulb@249 | 413 | |
paulb@249 | 414 | """ |
paulb@249 | 415 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 416 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 417 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 418 | where no such information is available, but where the default encoding |
paulb@249 | 419 | is to be overridden. |
paulb@249 | 420 | |
paulb@249 | 421 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 422 | single value is associated with any given field name). Each value is |
paulb@249 | 423 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 424 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 425 | upload form field). |
paulb@249 | 426 | |
paulb@249 | 427 | Where a given field name is used in both the path and message body to |
paulb@249 | 428 | specify values, the values from both sources will be combined into a |
paulb@249 | 429 | single list associated with that field name. |
paulb@249 | 430 | """ |
paulb@249 | 431 | |
paulb@249 | 432 | raise NotImplementedError, "get_fields" |
paulb@249 | 433 | |
paulb@45 | 434 | def get_user(self): |
paulb@1 | 435 | |
paulb@1 | 436 | """ |
paulb@186 | 437 | Extracts user information from the transaction. |
paulb@115 | 438 | |
paulb@115 | 439 | Returns a username as a string or None if no user is defined. |
paulb@1 | 440 | """ |
paulb@1 | 441 | |
paulb@45 | 442 | raise NotImplementedError, "get_user" |
paulb@1 | 443 | |
paulb@98 | 444 | def get_cookies(self): |
paulb@98 | 445 | |
paulb@98 | 446 | """ |
paulb@186 | 447 | Obtains cookie information from the request. |
paulb@98 | 448 | |
paulb@98 | 449 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 450 | """ |
paulb@98 | 451 | |
paulb@98 | 452 | raise NotImplementedError, "get_cookies" |
paulb@98 | 453 | |
paulb@104 | 454 | def get_cookie(self, cookie_name): |
paulb@104 | 455 | |
paulb@104 | 456 | """ |
paulb@186 | 457 | Obtains cookie information from the request. |
paulb@104 | 458 | |
paulb@104 | 459 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 460 | cookie exists. |
paulb@104 | 461 | """ |
paulb@104 | 462 | |
paulb@104 | 463 | raise NotImplementedError, "get_cookie" |
paulb@104 | 464 | |
paulb@1 | 465 | # Response-related methods. |
paulb@1 | 466 | |
paulb@1 | 467 | def get_response_stream(self): |
paulb@1 | 468 | |
paulb@1 | 469 | """ |
paulb@186 | 470 | Returns the response stream for the transaction. |
paulb@1 | 471 | """ |
paulb@1 | 472 | |
paulb@1 | 473 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 474 | |
paulb@252 | 475 | def get_response_stream_encoding(self): |
paulb@252 | 476 | |
paulb@252 | 477 | """ |
paulb@252 | 478 | Returns the response stream encoding. |
paulb@252 | 479 | """ |
paulb@252 | 480 | |
paulb@252 | 481 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 482 | |
paulb@42 | 483 | def get_response_code(self): |
paulb@42 | 484 | |
paulb@42 | 485 | """ |
paulb@42 | 486 | Get the response code associated with the transaction. If no response |
paulb@42 | 487 | code is defined, None is returned. |
paulb@42 | 488 | """ |
paulb@42 | 489 | |
paulb@42 | 490 | raise NotImplementedError, "get_response_code" |
paulb@42 | 491 | |
paulb@42 | 492 | def set_response_code(self, response_code): |
paulb@42 | 493 | |
paulb@42 | 494 | """ |
paulb@42 | 495 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 496 | specification. |
paulb@42 | 497 | """ |
paulb@42 | 498 | |
paulb@42 | 499 | raise NotImplementedError, "set_response_code" |
paulb@42 | 500 | |
paulb@67 | 501 | def set_header_value(self, header, value): |
paulb@49 | 502 | |
paulb@49 | 503 | """ |
paulb@49 | 504 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 505 | """ |
paulb@49 | 506 | |
paulb@67 | 507 | raise NotImplementedError, "set_header_value" |
paulb@49 | 508 | |
paulb@1 | 509 | def set_content_type(self, content_type): |
paulb@1 | 510 | |
paulb@1 | 511 | """ |
paulb@186 | 512 | Sets the 'content_type' for the response. |
paulb@1 | 513 | """ |
paulb@1 | 514 | |
paulb@1 | 515 | raise NotImplementedError, "set_content_type" |
paulb@1 | 516 | |
paulb@98 | 517 | # Higher level response-related methods. |
paulb@98 | 518 | |
paulb@98 | 519 | def set_cookie(self, cookie): |
paulb@98 | 520 | |
paulb@98 | 521 | """ |
paulb@186 | 522 | Stores the given 'cookie' object in the response. |
paulb@98 | 523 | """ |
paulb@98 | 524 | |
paulb@98 | 525 | raise NotImplementedError, "set_cookie" |
paulb@98 | 526 | |
paulb@98 | 527 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 528 | |
paulb@98 | 529 | """ |
paulb@186 | 530 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 531 | |
paulb@98 | 532 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 533 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 534 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 535 | """ |
paulb@98 | 536 | |
paulb@98 | 537 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 538 | |
paulb@98 | 539 | def delete_cookie(self, cookie_name): |
paulb@98 | 540 | |
paulb@98 | 541 | """ |
paulb@186 | 542 | Adds to the response a request that the cookie with the given |
paulb@186 | 543 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 544 | """ |
paulb@98 | 545 | |
paulb@98 | 546 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 547 | |
paulb@183 | 548 | # Session-related methods. |
paulb@183 | 549 | |
paulb@183 | 550 | def get_session(self, create=1): |
paulb@183 | 551 | |
paulb@183 | 552 | """ |
paulb@186 | 553 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 554 | transaction. |
paulb@183 | 555 | |
paulb@183 | 556 | If no session has yet been established according to information |
paulb@183 | 557 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 558 | determines whether a new session will be established. |
paulb@183 | 559 | |
paulb@183 | 560 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 561 | then None is returned. In all other cases, a session object is created |
paulb@183 | 562 | (where appropriate) and returned. |
paulb@183 | 563 | """ |
paulb@183 | 564 | |
paulb@183 | 565 | raise NotImplementedError, "get_session" |
paulb@183 | 566 | |
paulb@183 | 567 | def expire_session(self): |
paulb@183 | 568 | |
paulb@183 | 569 | """ |
paulb@186 | 570 | Expires any session established according to information provided in the |
paulb@186 | 571 | transaction. |
paulb@183 | 572 | """ |
paulb@183 | 573 | |
paulb@183 | 574 | raise NotImplementedError, "expire_session" |
paulb@183 | 575 | |
paulb@128 | 576 | # Application-specific methods. |
paulb@128 | 577 | |
paulb@128 | 578 | def set_user(self, username): |
paulb@128 | 579 | |
paulb@128 | 580 | """ |
paulb@128 | 581 | An application-specific method which sets the user information with |
paulb@128 | 582 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 583 | 'get_user'. |
paulb@128 | 584 | """ |
paulb@128 | 585 | |
paulb@311 | 586 | self.user = username |
paulb@311 | 587 | |
paulb@316 | 588 | def set_virtual_path_info(self, path_info): |
paulb@311 | 589 | |
paulb@311 | 590 | """ |
paulb@311 | 591 | An application-specific method which sets the 'path_info' in the |
paulb@316 | 592 | transaction. This affects subsequent calls to 'get_virtual_path_info'. |
paulb@421 | 593 | |
paulb@421 | 594 | Note that the virtual path info should either be an empty string, or it |
paulb@421 | 595 | should begin with "/" and then (optionally) include other details. |
paulb@421 | 596 | Virtual path info strings which omit the leading "/" - ie. containing |
paulb@421 | 597 | things like "xxx" or even "xxx/yyy" - do not really make sense and may |
paulb@421 | 598 | not be handled correctly by various WebStack components. |
paulb@311 | 599 | """ |
paulb@311 | 600 | |
paulb@311 | 601 | self.path_info = path_info |
paulb@128 | 602 | |
paulb@446 | 603 | def get_virtual_path_info(self, encoding=None): |
paulb@316 | 604 | |
paulb@316 | 605 | """ |
paulb@316 | 606 | An application-specific method which either returns path info set in the |
paulb@316 | 607 | 'set_virtual_path_info' method, or the normal path info found in the |
paulb@316 | 608 | request. |
paulb@446 | 609 | |
paulb@446 | 610 | If the optional 'encoding' is set, use that in preference to the default |
paulb@446 | 611 | encoding to convert the path into a form not containing "URL encoded" |
paulb@446 | 612 | character values. |
paulb@316 | 613 | """ |
paulb@316 | 614 | |
paulb@399 | 615 | if self.path_info is not None: |
paulb@399 | 616 | return self.path_info |
paulb@399 | 617 | else: |
paulb@446 | 618 | return self.get_path_info(encoding) |
paulb@316 | 619 | |
paulb@446 | 620 | def get_processed_virtual_path_info(self, encoding=None): |
paulb@421 | 621 | |
paulb@421 | 622 | """ |
paulb@421 | 623 | An application-specific method which returns the virtual path info that |
paulb@421 | 624 | is considered "processed"; that is, the part of the path info which is |
paulb@421 | 625 | not included in the virtual path info. |
paulb@421 | 626 | |
paulb@446 | 627 | If the optional 'encoding' is set, use that in preference to the default |
paulb@446 | 628 | encoding to convert the path into a form not containing "URL encoded" |
paulb@446 | 629 | character values. |
paulb@446 | 630 | |
paulb@421 | 631 | Where the virtual path info is identical to the path info, an empty |
paulb@421 | 632 | string is returned. |
paulb@421 | 633 | |
paulb@421 | 634 | Where the virtual path info is a substring of the path info, the path |
paulb@421 | 635 | info preceding that substring is returned. |
paulb@421 | 636 | |
paulb@421 | 637 | Where the virtual path info is either an empty string or not a substring |
paulb@421 | 638 | of the path info, the entire path info is returned. |
paulb@528 | 639 | |
paulb@528 | 640 | Generally, one should expect the following relationship between the path |
paulb@528 | 641 | info, virtual path info and processed virtual path info: |
paulb@528 | 642 | |
paulb@528 | 643 | path info == processed virtual path info + virtual path info |
paulb@421 | 644 | """ |
paulb@421 | 645 | |
paulb@446 | 646 | real_path_info = self.get_path_info(encoding) |
paulb@446 | 647 | virtual_path_info = self.get_virtual_path_info(encoding) |
paulb@421 | 648 | |
paulb@421 | 649 | if virtual_path_info == "": |
paulb@421 | 650 | return real_path_info |
paulb@421 | 651 | |
paulb@528 | 652 | i = real_path_info.rfind(virtual_path_info) |
paulb@421 | 653 | if i == -1: |
paulb@421 | 654 | return real_path_info |
paulb@421 | 655 | else: |
paulb@421 | 656 | return real_path_info[:i] |
paulb@421 | 657 | |
paulb@527 | 658 | def get_attributes(self): |
paulb@527 | 659 | |
paulb@527 | 660 | """ |
paulb@527 | 661 | An application-specific method which obtains a dictionary mapping names |
paulb@527 | 662 | to attribute values that can be used to store arbitrary information. |
paulb@527 | 663 | |
paulb@527 | 664 | Since the dictionary of attributes is retained by the transaction during |
paulb@527 | 665 | its lifetime, such a dictionary can be used to store information that an |
paulb@527 | 666 | application wishes to communicate amongst its components and resources |
paulb@527 | 667 | without having to pass objects other than the transaction between them. |
paulb@527 | 668 | |
paulb@527 | 669 | The returned dictionary can be modified using normal dictionary-like |
paulb@527 | 670 | methods. If no attributes existed previously, a new dictionary is |
paulb@527 | 671 | created and associated with the transaction. |
paulb@527 | 672 | """ |
paulb@527 | 673 | |
paulb@527 | 674 | if not hasattr(self, "_attributes"): |
paulb@527 | 675 | self._attributes = {} |
paulb@527 | 676 | return self._attributes |
paulb@527 | 677 | |
paulb@505 | 678 | # Utility methods. |
paulb@505 | 679 | |
paulb@598 | 680 | def traverse_path(self, encoding=None): |
paulb@598 | 681 | |
paulb@598 | 682 | """ |
paulb@598 | 683 | Traverse the path, updating the virtual path info and thus the processed |
paulb@598 | 684 | virtual path info accordingly. Return the traversed virtual path info |
paulb@598 | 685 | fragment. |
paulb@598 | 686 | """ |
paulb@598 | 687 | |
paulb@598 | 688 | vp = self.get_virtual_path_info(encoding).split("/") |
paulb@598 | 689 | self.set_virtual_path_info("/" + "/".join(vp[2:])) |
paulb@598 | 690 | return vp[1] |
paulb@598 | 691 | |
paulb@505 | 692 | def update_path(self, path, relative_path): |
paulb@505 | 693 | |
paulb@505 | 694 | """ |
paulb@505 | 695 | Transform the given 'path' using the specified 'relative_path'. For |
paulb@576 | 696 | example, a simple identifier replaces the last component from 'path': |
paulb@505 | 697 | |
paulb@505 | 698 | trans.update_path("/parent/node", "other") -> "/parent/other" |
paulb@524 | 699 | |
paulb@576 | 700 | If the last component is empty, the effect is similar to an append |
paulb@576 | 701 | operation: |
paulb@576 | 702 | |
paulb@505 | 703 | trans.update_path("/parent/node/", "other") -> "/parent/node/other" |
paulb@524 | 704 | |
paulb@576 | 705 | Where 'relative_path' is empty, the result is 'path' with the last |
paulb@576 | 706 | component erased (but still present): |
paulb@576 | 707 | |
paulb@576 | 708 | trans.update_path("/parent/node", "") -> "/parent/" |
paulb@576 | 709 | |
paulb@576 | 710 | trans.update_path("/parent/node/", "") -> "/parent/node/" |
paulb@576 | 711 | |
paulb@576 | 712 | Where 'relative_path' contains ".", the component is regarded as being |
paulb@576 | 713 | empty: |
paulb@576 | 714 | |
paulb@576 | 715 | trans.update_path("/parent/node", "other/./more") -> "/parent/other/more" |
paulb@576 | 716 | |
paulb@576 | 717 | trans.update_path("/parent/node/", "other/./more") -> "/parent/node/other/more" |
paulb@576 | 718 | |
paulb@576 | 719 | However, at the start of 'relative_path', "." can remove one component: |
paulb@576 | 720 | |
paulb@576 | 721 | trans.update_path("/parent/node", ".") -> "/parent" |
paulb@576 | 722 | |
paulb@576 | 723 | trans.update_path("/parent/node/", ".") -> "/parent/node" |
paulb@576 | 724 | |
paulb@576 | 725 | Adding "/" immediately afterwards restores any removed "/": |
paulb@576 | 726 | |
paulb@576 | 727 | trans.update_path("/parent/node/", "./") -> "/parent/node/" |
paulb@576 | 728 | |
paulb@576 | 729 | trans.update_path("/parent/node", "./") -> "/parent/" |
paulb@576 | 730 | |
paulb@576 | 731 | Following components add to the effect of "./": |
paulb@576 | 732 | |
paulb@576 | 733 | trans.update_path("/parent/node", "./other/more") -> "/parent/other/more" |
paulb@576 | 734 | |
paulb@576 | 735 | trans.update_path("/parent/node/", "./other/more") -> "/parent/node/other/more" |
paulb@576 | 736 | |
paulb@576 | 737 | Where 'relative_path' contains "..", two components are removed from the |
paulb@576 | 738 | resulting path: |
paulb@576 | 739 | |
paulb@576 | 740 | trans.update_path("/parent/node/", "..") -> "/parent" |
paulb@576 | 741 | |
paulb@576 | 742 | trans.update_path("/parent/node/", "../other") -> "/parent/other" |
paulb@576 | 743 | |
paulb@576 | 744 | trans.update_path("/parent/node", "..") -> "/" |
paulb@576 | 745 | |
paulb@505 | 746 | trans.update_path("/parent/node", "../other") -> "/other" |
paulb@524 | 747 | |
paulb@576 | 748 | Where fewer components exist than are to be removed, the path is reset: |
paulb@576 | 749 | |
paulb@576 | 750 | trans.update_path("/parent/node", "../..") -> "/" |
paulb@576 | 751 | |
paulb@576 | 752 | Subsequent components are applied to the reset path: |
paulb@524 | 753 | |
paulb@505 | 754 | trans.update_path("/parent/node", "../../other") -> "/other" |
paulb@524 | 755 | |
paulb@505 | 756 | trans.update_path("/parent/node/", "../../other") -> "/other" |
paulb@505 | 757 | |
paulb@505 | 758 | Where 'relative_path' begins with "/", the 'path' is reset to "/" and |
paulb@505 | 759 | the components of the 'relative_path' are then applied to that new path: |
paulb@505 | 760 | |
paulb@505 | 761 | trans.update_path("/parent/node", "/other") -> "/other" |
paulb@505 | 762 | |
paulb@505 | 763 | Where 'relative_path' ends with "/", the final "/" is added to the |
paulb@505 | 764 | result: |
paulb@505 | 765 | |
paulb@505 | 766 | trans.update_path("/parent/node", "other/") -> "/parent/other/" |
paulb@505 | 767 | """ |
paulb@505 | 768 | |
paulb@505 | 769 | rparts = relative_path.split("/") |
paulb@505 | 770 | |
paulb@505 | 771 | if relative_path.startswith("/"): |
paulb@505 | 772 | parts = [""] |
paulb@505 | 773 | del rparts[0] |
paulb@505 | 774 | elif relative_path == "": |
paulb@505 | 775 | parts = path.split("/") |
paulb@505 | 776 | parts[-1] = "" |
paulb@505 | 777 | del rparts[0] |
paulb@505 | 778 | else: |
paulb@505 | 779 | parts = path.split("/") |
paulb@505 | 780 | del parts[-1] |
paulb@505 | 781 | |
paulb@505 | 782 | for rpart in rparts: |
paulb@505 | 783 | if rpart == ".": |
paulb@505 | 784 | continue |
paulb@505 | 785 | elif rpart == "..": |
paulb@505 | 786 | if len(parts) > 1: |
paulb@505 | 787 | parts = parts[:-1] |
paulb@505 | 788 | else: |
paulb@505 | 789 | parts.append(rpart) |
paulb@505 | 790 | |
paulb@531 | 791 | return "/" + "/".join(parts[1:]) |
paulb@505 | 792 | |
paulb@505 | 793 | def redirect(self, path, code=302): |
paulb@505 | 794 | |
paulb@505 | 795 | """ |
paulb@505 | 796 | Send a redirect response to the client, providing the given 'path' as |
paulb@505 | 797 | the suggested location of a resource. The optional 'code' (set to 302 by |
paulb@505 | 798 | default) may be used to change the exact meaning of the response |
paulb@505 | 799 | according to the HTTP specifications. |
paulb@505 | 800 | |
paulb@505 | 801 | Note that 'path' should be a plain string suitable for header output. |
paulb@505 | 802 | Use the 'encode_path' method to convert Unicode objects into such |
paulb@505 | 803 | strings. |
paulb@505 | 804 | """ |
paulb@505 | 805 | |
paulb@505 | 806 | self.set_response_code(code) |
paulb@505 | 807 | self.set_header_value("Location", path) |
paulb@505 | 808 | raise EndOfResponse |
paulb@505 | 809 | |
paulb@1 | 810 | class Resource: |
paulb@1 | 811 | |
paulb@1 | 812 | "A generic resource interface." |
paulb@1 | 813 | |
paulb@3 | 814 | def respond(self, trans): |
paulb@3 | 815 | |
paulb@3 | 816 | """ |
paulb@3 | 817 | An application-specific method which performs activities on the basis of |
paulb@3 | 818 | the transaction object 'trans'. |
paulb@3 | 819 | """ |
paulb@3 | 820 | |
paulb@3 | 821 | raise NotImplementedError, "respond" |
paulb@1 | 822 | |
paulb@75 | 823 | class Authenticator: |
paulb@75 | 824 | |
paulb@75 | 825 | "A generic authentication component." |
paulb@75 | 826 | |
paulb@75 | 827 | def authenticate(self, trans): |
paulb@75 | 828 | |
paulb@75 | 829 | """ |
paulb@75 | 830 | An application-specific method which authenticates the sender of the |
paulb@75 | 831 | request described by the transaction object 'trans'. This method should |
paulb@75 | 832 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 833 | the transaction. |
paulb@75 | 834 | |
paulb@75 | 835 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 836 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 837 | evaluates to false. |
paulb@75 | 838 | """ |
paulb@75 | 839 | |
paulb@75 | 840 | raise NotImplementedError, "authenticate" |
paulb@75 | 841 | |
paulb@75 | 842 | def get_auth_type(self): |
paulb@75 | 843 | |
paulb@75 | 844 | """ |
paulb@75 | 845 | An application-specific method which returns the authentication type to |
paulb@75 | 846 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 847 | authentication. |
paulb@75 | 848 | """ |
paulb@75 | 849 | |
paulb@75 | 850 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 851 | |
paulb@75 | 852 | def get_realm(self): |
paulb@75 | 853 | |
paulb@75 | 854 | """ |
paulb@75 | 855 | An application-specific method which returns the name of the realm for |
paulb@75 | 856 | which authentication is taking place. |
paulb@75 | 857 | """ |
paulb@75 | 858 | |
paulb@75 | 859 | raise NotImplementedError, "get_realm" |
paulb@75 | 860 | |
paulb@1 | 861 | # vim: tabstop=4 expandtab shiftwidth=4 |