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