paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Generic Web framework interfaces. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@403 | 21 | |
paulb@403 | 22 | -------- |
paulb@403 | 23 | |
paulb@3 | 24 | The WebStack architecture consists of the following layers: |
paulb@3 | 25 | |
paulb@3 | 26 | * Framework - The underlying Web framework implementation. |
paulb@3 | 27 | * Adapter - Code operating under the particular framework which creates |
paulb@3 | 28 | WebStack abstractions and issues them to the application. |
paulb@3 | 29 | * Resources - Units of functionality operating within the hosted Web |
paulb@3 | 30 | application. |
paulb@3 | 31 | |
paulb@3 | 32 | Resources can act as both content producers within an application and as request |
paulb@3 | 33 | dispatchers to other resources; in the latter role, they may be referred to as |
paulb@3 | 34 | directors. |
paulb@1 | 35 | """ |
paulb@1 | 36 | |
paulb@258 | 37 | import urllib |
paulb@258 | 38 | from WebStack.Helpers.Request import Cookie |
paulb@258 | 39 | |
paulb@296 | 40 | class EndOfResponse(Exception): |
paulb@296 | 41 | |
paulb@296 | 42 | "An exception which signals the end of a response." |
paulb@296 | 43 | |
paulb@296 | 44 | pass |
paulb@296 | 45 | |
paulb@238 | 46 | class HeaderValue: |
paulb@238 | 47 | |
paulb@238 | 48 | "A container for header information." |
paulb@238 | 49 | |
paulb@238 | 50 | def __init__(self, principal_value, **attributes): |
paulb@238 | 51 | |
paulb@238 | 52 | """ |
paulb@238 | 53 | Initialise the container with the given 'principal_value' and optional |
paulb@238 | 54 | keyword attributes representing the key=value pairs which accompany the |
paulb@238 | 55 | 'principal_value'. |
paulb@238 | 56 | """ |
paulb@238 | 57 | |
paulb@238 | 58 | self.principal_value = principal_value |
paulb@238 | 59 | self.attributes = attributes |
paulb@238 | 60 | |
paulb@238 | 61 | def __getattr__(self, name): |
paulb@238 | 62 | if self.attributes.has_key(name): |
paulb@238 | 63 | return self.attributes[name] |
paulb@238 | 64 | else: |
paulb@238 | 65 | raise AttributeError, name |
paulb@238 | 66 | |
paulb@238 | 67 | def __str__(self): |
paulb@238 | 68 | |
paulb@238 | 69 | """ |
paulb@238 | 70 | Format the header value object, producing a string suitable for the |
paulb@238 | 71 | response header field. |
paulb@238 | 72 | """ |
paulb@238 | 73 | |
paulb@238 | 74 | l = [] |
paulb@238 | 75 | if self.principal_value: |
paulb@238 | 76 | l.append(self.principal_value) |
paulb@238 | 77 | for name, value in self.attributes.items(): |
paulb@238 | 78 | l.append("; ") |
paulb@238 | 79 | l.append("%s=%s" % (name, value)) |
paulb@238 | 80 | |
paulb@238 | 81 | # Make sure that only ASCII is used. |
paulb@238 | 82 | |
paulb@238 | 83 | return "".join(l).encode("US-ASCII") |
paulb@238 | 84 | |
paulb@238 | 85 | class ContentType(HeaderValue): |
paulb@1 | 86 | |
paulb@1 | 87 | "A container for content type information." |
paulb@1 | 88 | |
paulb@224 | 89 | def __init__(self, media_type, charset=None, **attributes): |
paulb@224 | 90 | |
paulb@224 | 91 | """ |
paulb@224 | 92 | Initialise the container with the given 'media_type', an optional |
paulb@224 | 93 | 'charset', and optional keyword attributes representing the key=value |
paulb@224 | 94 | pairs which qualify content types. |
paulb@224 | 95 | """ |
paulb@224 | 96 | |
paulb@238 | 97 | if charset is not None: |
paulb@238 | 98 | attributes["charset"] = charset |
paulb@238 | 99 | HeaderValue.__init__(self, media_type, **attributes) |
paulb@219 | 100 | |
paulb@238 | 101 | def __getattr__(self, name): |
paulb@238 | 102 | if name == "media_type": |
paulb@238 | 103 | return self.principal_value |
paulb@238 | 104 | elif name == "charset": |
paulb@238 | 105 | return self.attributes.get("charset") |
paulb@238 | 106 | elif self.attributes.has_key(name): |
paulb@238 | 107 | return self.attributes[name] |
paulb@238 | 108 | else: |
paulb@238 | 109 | raise AttributeError, name |
paulb@1 | 110 | |
paulb@1 | 111 | class Transaction: |
paulb@1 | 112 | |
paulb@1 | 113 | """ |
paulb@1 | 114 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 115 | overridden. |
paulb@1 | 116 | """ |
paulb@1 | 117 | |
paulb@224 | 118 | # The default charset ties output together with body field interpretation. |
paulb@224 | 119 | |
paulb@224 | 120 | default_charset = "iso-8859-1" |
paulb@224 | 121 | |
paulb@28 | 122 | def commit(self): |
paulb@28 | 123 | |
paulb@28 | 124 | """ |
paulb@28 | 125 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 126 | objects. |
paulb@28 | 127 | """ |
paulb@28 | 128 | |
paulb@28 | 129 | pass |
paulb@28 | 130 | |
paulb@1 | 131 | # Utility methods. |
paulb@1 | 132 | |
paulb@238 | 133 | def parse_header_value(self, header_class, header_value_str): |
paulb@1 | 134 | |
paulb@1 | 135 | """ |
paulb@238 | 136 | Create an object of the given 'header_class' by determining the details |
paulb@238 | 137 | of the given 'header_value_str' - a string containing the value of a |
paulb@238 | 138 | particular header. |
paulb@1 | 139 | """ |
paulb@1 | 140 | |
paulb@238 | 141 | if header_value_str is None: |
paulb@238 | 142 | return header_class(None) |
paulb@1 | 143 | |
paulb@238 | 144 | l = header_value_str.split(";") |
paulb@224 | 145 | attributes = {} |
paulb@193 | 146 | |
paulb@238 | 147 | # Find the attributes. |
paulb@193 | 148 | |
paulb@238 | 149 | principal_value, attributes_str = l[0].strip(), l[1:] |
paulb@5 | 150 | |
paulb@224 | 151 | for attribute_str in attributes_str: |
paulb@224 | 152 | t = attribute_str.split("=") |
paulb@224 | 153 | if len(t) > 1: |
paulb@224 | 154 | name, value = t[0].strip(), t[1].strip() |
paulb@238 | 155 | attributes[name] = value |
paulb@5 | 156 | |
paulb@238 | 157 | return header_class(principal_value, **attributes) |
paulb@238 | 158 | |
paulb@238 | 159 | def parse_content_type(self, content_type_field): |
paulb@353 | 160 | |
paulb@353 | 161 | """ |
paulb@353 | 162 | Parse the given 'content_type_field' - a value found comparable to that |
paulb@353 | 163 | found in an HTTP request header for "Content-Type". |
paulb@353 | 164 | """ |
paulb@353 | 165 | |
paulb@238 | 166 | return self.parse_header_value(ContentType, content_type_field) |
paulb@5 | 167 | |
paulb@49 | 168 | def format_header_value(self, value): |
paulb@49 | 169 | |
paulb@49 | 170 | """ |
paulb@49 | 171 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 172 | of US-ASCII. |
paulb@49 | 173 | """ |
paulb@49 | 174 | |
paulb@49 | 175 | return value.encode("US-ASCII") |
paulb@49 | 176 | |
paulb@258 | 177 | def encode_cookie_value(self, value): |
paulb@258 | 178 | |
paulb@258 | 179 | """ |
paulb@258 | 180 | Encode the given cookie 'value'. This ensures the usage of US-ASCII |
paulb@258 | 181 | through the encoding of Unicode objects as URL-encoded UTF-8 text. |
paulb@258 | 182 | """ |
paulb@258 | 183 | |
paulb@258 | 184 | return urllib.quote(value.encode("UTF-8")).encode("US-ASCII") |
paulb@258 | 185 | |
paulb@258 | 186 | def decode_cookie_value(self, value): |
paulb@258 | 187 | |
paulb@258 | 188 | """ |
paulb@258 | 189 | Decode the given cookie 'value'. |
paulb@258 | 190 | """ |
paulb@258 | 191 | |
paulb@258 | 192 | return unicode(urllib.unquote(value), "UTF-8") |
paulb@258 | 193 | |
paulb@258 | 194 | def process_cookies(self, cookie_dict, using_strings=0): |
paulb@258 | 195 | |
paulb@258 | 196 | """ |
paulb@258 | 197 | Process the given 'cookie_dict', returning a dictionary mapping cookie names |
paulb@258 | 198 | to cookie objects where the names and values have been decoded from the form |
paulb@258 | 199 | used in the cookies retrieved from the request. |
paulb@258 | 200 | |
paulb@258 | 201 | The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a |
paulb@258 | 202 | mapping of cookie names to values. |
paulb@258 | 203 | """ |
paulb@258 | 204 | |
paulb@258 | 205 | cookies = {} |
paulb@258 | 206 | for name in cookie_dict.keys(): |
paulb@258 | 207 | if using_strings: |
paulb@258 | 208 | value = cookie_dict[name] |
paulb@258 | 209 | else: |
paulb@258 | 210 | cookie = cookie_dict[name] |
paulb@258 | 211 | value = cookie.value |
paulb@258 | 212 | cookie_name = self.decode_cookie_value(name) |
paulb@258 | 213 | cookie_value = self.decode_cookie_value(value) |
paulb@258 | 214 | cookies[cookie_name] = Cookie(cookie_name, cookie_value) |
paulb@258 | 215 | return cookies |
paulb@258 | 216 | |
paulb@1 | 217 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 218 | |
paulb@1 | 219 | """ |
paulb@1 | 220 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 221 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 222 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 223 | criteria are ignored. |
paulb@1 | 224 | |
paulb@1 | 225 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 226 | preferences are appropriate. |
paulb@1 | 227 | """ |
paulb@1 | 228 | |
paulb@108 | 229 | if accept_preference is None: |
paulb@108 | 230 | return [] |
paulb@108 | 231 | |
paulb@1 | 232 | accept_defs = accept_preference.split(",") |
paulb@1 | 233 | accept_prefs = [] |
paulb@1 | 234 | for accept_def in accept_defs: |
paulb@1 | 235 | t = accept_def.split(";") |
paulb@1 | 236 | if len(t) >= 1: |
paulb@1 | 237 | accept_prefs.append(t[0].strip()) |
paulb@1 | 238 | return accept_prefs |
paulb@1 | 239 | |
paulb@65 | 240 | def convert_to_list(self, value): |
paulb@65 | 241 | |
paulb@65 | 242 | """ |
paulb@65 | 243 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 244 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 245 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 246 | then an empty list is returned. |
paulb@65 | 247 | """ |
paulb@65 | 248 | |
paulb@65 | 249 | if type(value) == type([]): |
paulb@65 | 250 | return value |
paulb@65 | 251 | elif type(value) == type(()): |
paulb@65 | 252 | return list(value) |
paulb@65 | 253 | elif value is None: |
paulb@65 | 254 | return [] |
paulb@65 | 255 | else: |
paulb@65 | 256 | return [value] |
paulb@65 | 257 | |
paulb@425 | 258 | # Public utility methods. |
paulb@425 | 259 | |
paulb@425 | 260 | def get_readable_path(self, path, encoding): |
paulb@425 | 261 | |
paulb@425 | 262 | """ |
paulb@425 | 263 | From the given 'path', use the given 'encoding' to decode the information and |
paulb@425 | 264 | convert it to Unicode. Upon success, return the 'path' as a Unicode value. Upon |
paulb@425 | 265 | failure, return None. |
paulb@425 | 266 | """ |
paulb@425 | 267 | |
paulb@425 | 268 | try: |
paulb@425 | 269 | return urllib.unquote(path).decode(encoding) |
paulb@425 | 270 | except UnicodeError: |
paulb@425 | 271 | return None |
paulb@425 | 272 | |
paulb@338 | 273 | # Server-related methods. |
paulb@338 | 274 | |
paulb@338 | 275 | def get_server_name(self): |
paulb@338 | 276 | |
paulb@338 | 277 | "Returns the server name." |
paulb@338 | 278 | |
paulb@338 | 279 | raise NotImplementedError, "get_server_name" |
paulb@338 | 280 | |
paulb@338 | 281 | def get_server_port(self): |
paulb@338 | 282 | |
paulb@338 | 283 | "Returns the server port as a string." |
paulb@338 | 284 | |
paulb@338 | 285 | raise NotImplementedError, "get_server_port" |
paulb@338 | 286 | |
paulb@1 | 287 | # Request-related methods. |
paulb@1 | 288 | |
paulb@1 | 289 | def get_request_stream(self): |
paulb@1 | 290 | |
paulb@1 | 291 | """ |
paulb@186 | 292 | Returns the request stream for the transaction. |
paulb@1 | 293 | """ |
paulb@1 | 294 | |
paulb@1 | 295 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 296 | |
paulb@1 | 297 | def get_request_method(self): |
paulb@1 | 298 | |
paulb@1 | 299 | """ |
paulb@186 | 300 | Returns the request method. |
paulb@1 | 301 | """ |
paulb@1 | 302 | |
paulb@1 | 303 | raise NotImplementedError, "get_request_method" |
paulb@1 | 304 | |
paulb@1 | 305 | def get_headers(self): |
paulb@1 | 306 | |
paulb@1 | 307 | """ |
paulb@186 | 308 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 309 | names to values. |
paulb@1 | 310 | """ |
paulb@1 | 311 | |
paulb@1 | 312 | raise NotImplementedError, "get_headers" |
paulb@1 | 313 | |
paulb@65 | 314 | def get_header_values(self, key): |
paulb@65 | 315 | |
paulb@65 | 316 | """ |
paulb@186 | 317 | Returns a list of all request header values associated with the given |
paulb@186 | 318 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 319 | case-insensitive string. |
paulb@65 | 320 | """ |
paulb@65 | 321 | |
paulb@65 | 322 | raise NotImplementedError, "get_header_values" |
paulb@65 | 323 | |
paulb@1 | 324 | def get_content_type(self): |
paulb@1 | 325 | |
paulb@1 | 326 | """ |
paulb@186 | 327 | Returns the content type specified on the request, along with the |
paulb@186 | 328 | charset employed. |
paulb@1 | 329 | """ |
paulb@1 | 330 | |
paulb@1 | 331 | raise NotImplementedError, "get_content_type" |
paulb@1 | 332 | |
paulb@1 | 333 | def get_content_charsets(self): |
paulb@1 | 334 | |
paulb@1 | 335 | """ |
paulb@1 | 336 | Returns the character set preferences. |
paulb@1 | 337 | """ |
paulb@1 | 338 | |
paulb@1 | 339 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 340 | |
paulb@1 | 341 | def get_content_languages(self): |
paulb@1 | 342 | |
paulb@1 | 343 | """ |
paulb@186 | 344 | Returns extracted language information from the transaction. |
paulb@1 | 345 | """ |
paulb@1 | 346 | |
paulb@1 | 347 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 348 | |
paulb@5 | 349 | def get_path(self): |
paulb@5 | 350 | |
paulb@5 | 351 | """ |
paulb@186 | 352 | Returns the entire path from the request. |
paulb@5 | 353 | """ |
paulb@5 | 354 | |
paulb@5 | 355 | raise NotImplementedError, "get_path" |
paulb@5 | 356 | |
paulb@162 | 357 | def get_path_without_query(self): |
paulb@162 | 358 | |
paulb@162 | 359 | """ |
paulb@186 | 360 | Returns the entire path from the request minus the query string. |
paulb@162 | 361 | """ |
paulb@162 | 362 | |
paulb@162 | 363 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 364 | |
paulb@1 | 365 | def get_path_info(self): |
paulb@1 | 366 | |
paulb@1 | 367 | """ |
paulb@186 | 368 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 369 | handling the current request) from the request. |
paulb@1 | 370 | """ |
paulb@1 | 371 | |
paulb@1 | 372 | raise NotImplementedError, "get_path_info" |
paulb@1 | 373 | |
paulb@69 | 374 | def get_query_string(self): |
paulb@69 | 375 | |
paulb@69 | 376 | """ |
paulb@186 | 377 | Returns the query string from the path in the request. |
paulb@69 | 378 | """ |
paulb@69 | 379 | |
paulb@69 | 380 | raise NotImplementedError, "get_query_string" |
paulb@69 | 381 | |
paulb@69 | 382 | # Higher level request-related methods. |
paulb@69 | 383 | |
paulb@69 | 384 | def get_fields_from_path(self): |
paulb@69 | 385 | |
paulb@69 | 386 | """ |
paulb@249 | 387 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 388 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 389 | the path if handling a POST transaction. |
paulb@69 | 390 | |
paulb@71 | 391 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 392 | single value is associated with any given field name). |
paulb@69 | 393 | """ |
paulb@69 | 394 | |
paulb@69 | 395 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 396 | |
paulb@167 | 397 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 398 | |
paulb@69 | 399 | """ |
paulb@249 | 400 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 401 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 402 | encoding of the message body for cases where no such information is |
paulb@249 | 403 | available, but where the default encoding is to be overridden. |
paulb@69 | 404 | |
paulb@73 | 405 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 406 | single value is associated with any given field name). Each value is |
paulb@193 | 407 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 408 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 409 | """ |
paulb@69 | 410 | |
paulb@69 | 411 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 412 | |
paulb@249 | 413 | def get_fields(self, encoding=None): |
paulb@249 | 414 | |
paulb@249 | 415 | """ |
paulb@249 | 416 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 417 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 418 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 419 | where no such information is available, but where the default encoding |
paulb@249 | 420 | is to be overridden. |
paulb@249 | 421 | |
paulb@249 | 422 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 423 | single value is associated with any given field name). Each value is |
paulb@249 | 424 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 425 | or a plain string (representing a file upload form field, for example). |
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@316 | 603 | def get_virtual_path_info(self): |
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@316 | 609 | """ |
paulb@316 | 610 | |
paulb@399 | 611 | if self.path_info is not None: |
paulb@399 | 612 | return self.path_info |
paulb@399 | 613 | else: |
paulb@399 | 614 | return self.get_path_info() |
paulb@316 | 615 | |
paulb@421 | 616 | def get_processed_virtual_path_info(self): |
paulb@421 | 617 | |
paulb@421 | 618 | """ |
paulb@421 | 619 | An application-specific method which returns the virtual path info that |
paulb@421 | 620 | is considered "processed"; that is, the part of the path info which is |
paulb@421 | 621 | not included in the virtual path info. |
paulb@421 | 622 | |
paulb@421 | 623 | Where the virtual path info is identical to the path info, an empty |
paulb@421 | 624 | string is returned. |
paulb@421 | 625 | |
paulb@421 | 626 | Where the virtual path info is a substring of the path info, the path |
paulb@421 | 627 | info preceding that substring is returned. |
paulb@421 | 628 | |
paulb@421 | 629 | Where the virtual path info is either an empty string or not a substring |
paulb@421 | 630 | of the path info, the entire path info is returned. |
paulb@421 | 631 | """ |
paulb@421 | 632 | |
paulb@421 | 633 | real_path_info = self.get_path_info() |
paulb@421 | 634 | virtual_path_info = self.get_virtual_path_info() |
paulb@421 | 635 | |
paulb@421 | 636 | if virtual_path_info == "": |
paulb@421 | 637 | return real_path_info |
paulb@421 | 638 | |
paulb@421 | 639 | i = real_path_info.find(virtual_path_info) |
paulb@421 | 640 | if i == -1: |
paulb@421 | 641 | return real_path_info |
paulb@421 | 642 | else: |
paulb@421 | 643 | return real_path_info[:i] |
paulb@421 | 644 | |
paulb@1 | 645 | class Resource: |
paulb@1 | 646 | |
paulb@1 | 647 | "A generic resource interface." |
paulb@1 | 648 | |
paulb@3 | 649 | def respond(self, trans): |
paulb@3 | 650 | |
paulb@3 | 651 | """ |
paulb@3 | 652 | An application-specific method which performs activities on the basis of |
paulb@3 | 653 | the transaction object 'trans'. |
paulb@3 | 654 | """ |
paulb@3 | 655 | |
paulb@3 | 656 | raise NotImplementedError, "respond" |
paulb@1 | 657 | |
paulb@75 | 658 | class Authenticator: |
paulb@75 | 659 | |
paulb@75 | 660 | "A generic authentication component." |
paulb@75 | 661 | |
paulb@75 | 662 | def authenticate(self, trans): |
paulb@75 | 663 | |
paulb@75 | 664 | """ |
paulb@75 | 665 | An application-specific method which authenticates the sender of the |
paulb@75 | 666 | request described by the transaction object 'trans'. This method should |
paulb@75 | 667 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 668 | the transaction. |
paulb@75 | 669 | |
paulb@75 | 670 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 671 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 672 | evaluates to false. |
paulb@75 | 673 | """ |
paulb@75 | 674 | |
paulb@75 | 675 | raise NotImplementedError, "authenticate" |
paulb@75 | 676 | |
paulb@75 | 677 | def get_auth_type(self): |
paulb@75 | 678 | |
paulb@75 | 679 | """ |
paulb@75 | 680 | An application-specific method which returns the authentication type to |
paulb@75 | 681 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 682 | authentication. |
paulb@75 | 683 | """ |
paulb@75 | 684 | |
paulb@75 | 685 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 686 | |
paulb@75 | 687 | def get_realm(self): |
paulb@75 | 688 | |
paulb@75 | 689 | """ |
paulb@75 | 690 | An application-specific method which returns the name of the realm for |
paulb@75 | 691 | which authentication is taking place. |
paulb@75 | 692 | """ |
paulb@75 | 693 | |
paulb@75 | 694 | raise NotImplementedError, "get_realm" |
paulb@75 | 695 | |
paulb@1 | 696 | # vim: tabstop=4 expandtab shiftwidth=4 |