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