paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Generic Web framework interfaces. |
paulb@3 | 5 | The WebStack architecture consists of the following layers: |
paulb@3 | 6 | |
paulb@3 | 7 | * Framework - The underlying Web framework implementation. |
paulb@3 | 8 | * Adapter - Code operating under the particular framework which creates |
paulb@3 | 9 | WebStack abstractions and issues them to the application. |
paulb@3 | 10 | * Resources - Units of functionality operating within the hosted Web |
paulb@3 | 11 | application. |
paulb@3 | 12 | |
paulb@3 | 13 | Resources can act as both content producers within an application and as request |
paulb@3 | 14 | dispatchers to other resources; in the latter role, they may be referred to as |
paulb@3 | 15 | directors. |
paulb@1 | 16 | """ |
paulb@1 | 17 | |
paulb@258 | 18 | import urllib |
paulb@258 | 19 | from WebStack.Helpers.Request import Cookie |
paulb@258 | 20 | |
paulb@296 | 21 | class EndOfResponse(Exception): |
paulb@296 | 22 | |
paulb@296 | 23 | "An exception which signals the end of a response." |
paulb@296 | 24 | |
paulb@296 | 25 | pass |
paulb@296 | 26 | |
paulb@238 | 27 | class HeaderValue: |
paulb@238 | 28 | |
paulb@238 | 29 | "A container for header information." |
paulb@238 | 30 | |
paulb@238 | 31 | def __init__(self, principal_value, **attributes): |
paulb@238 | 32 | |
paulb@238 | 33 | """ |
paulb@238 | 34 | Initialise the container with the given 'principal_value' and optional |
paulb@238 | 35 | keyword attributes representing the key=value pairs which accompany the |
paulb@238 | 36 | 'principal_value'. |
paulb@238 | 37 | """ |
paulb@238 | 38 | |
paulb@238 | 39 | self.principal_value = principal_value |
paulb@238 | 40 | self.attributes = attributes |
paulb@238 | 41 | |
paulb@238 | 42 | def __getattr__(self, name): |
paulb@238 | 43 | if self.attributes.has_key(name): |
paulb@238 | 44 | return self.attributes[name] |
paulb@238 | 45 | else: |
paulb@238 | 46 | raise AttributeError, name |
paulb@238 | 47 | |
paulb@238 | 48 | def __str__(self): |
paulb@238 | 49 | |
paulb@238 | 50 | """ |
paulb@238 | 51 | Format the header value object, producing a string suitable for the |
paulb@238 | 52 | response header field. |
paulb@238 | 53 | """ |
paulb@238 | 54 | |
paulb@238 | 55 | l = [] |
paulb@238 | 56 | if self.principal_value: |
paulb@238 | 57 | l.append(self.principal_value) |
paulb@238 | 58 | for name, value in self.attributes.items(): |
paulb@238 | 59 | l.append("; ") |
paulb@238 | 60 | l.append("%s=%s" % (name, value)) |
paulb@238 | 61 | |
paulb@238 | 62 | # Make sure that only ASCII is used. |
paulb@238 | 63 | |
paulb@238 | 64 | return "".join(l).encode("US-ASCII") |
paulb@238 | 65 | |
paulb@238 | 66 | class ContentType(HeaderValue): |
paulb@1 | 67 | |
paulb@1 | 68 | "A container for content type information." |
paulb@1 | 69 | |
paulb@224 | 70 | def __init__(self, media_type, charset=None, **attributes): |
paulb@224 | 71 | |
paulb@224 | 72 | """ |
paulb@224 | 73 | Initialise the container with the given 'media_type', an optional |
paulb@224 | 74 | 'charset', and optional keyword attributes representing the key=value |
paulb@224 | 75 | pairs which qualify content types. |
paulb@224 | 76 | """ |
paulb@224 | 77 | |
paulb@238 | 78 | if charset is not None: |
paulb@238 | 79 | attributes["charset"] = charset |
paulb@238 | 80 | HeaderValue.__init__(self, media_type, **attributes) |
paulb@219 | 81 | |
paulb@238 | 82 | def __getattr__(self, name): |
paulb@238 | 83 | if name == "media_type": |
paulb@238 | 84 | return self.principal_value |
paulb@238 | 85 | elif name == "charset": |
paulb@238 | 86 | return self.attributes.get("charset") |
paulb@238 | 87 | elif self.attributes.has_key(name): |
paulb@238 | 88 | return self.attributes[name] |
paulb@238 | 89 | else: |
paulb@238 | 90 | raise AttributeError, name |
paulb@1 | 91 | |
paulb@1 | 92 | class Transaction: |
paulb@1 | 93 | |
paulb@1 | 94 | """ |
paulb@1 | 95 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 96 | overridden. |
paulb@1 | 97 | """ |
paulb@1 | 98 | |
paulb@224 | 99 | # The default charset ties output together with body field interpretation. |
paulb@224 | 100 | |
paulb@224 | 101 | default_charset = "iso-8859-1" |
paulb@224 | 102 | |
paulb@28 | 103 | def commit(self): |
paulb@28 | 104 | |
paulb@28 | 105 | """ |
paulb@28 | 106 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 107 | objects. |
paulb@28 | 108 | """ |
paulb@28 | 109 | |
paulb@28 | 110 | pass |
paulb@28 | 111 | |
paulb@1 | 112 | # Utility methods. |
paulb@1 | 113 | |
paulb@238 | 114 | def parse_header_value(self, header_class, header_value_str): |
paulb@1 | 115 | |
paulb@1 | 116 | """ |
paulb@238 | 117 | Create an object of the given 'header_class' by determining the details |
paulb@238 | 118 | of the given 'header_value_str' - a string containing the value of a |
paulb@238 | 119 | particular header. |
paulb@1 | 120 | """ |
paulb@1 | 121 | |
paulb@238 | 122 | if header_value_str is None: |
paulb@238 | 123 | return header_class(None) |
paulb@1 | 124 | |
paulb@238 | 125 | l = header_value_str.split(";") |
paulb@224 | 126 | attributes = {} |
paulb@193 | 127 | |
paulb@238 | 128 | # Find the attributes. |
paulb@193 | 129 | |
paulb@238 | 130 | principal_value, attributes_str = l[0].strip(), l[1:] |
paulb@5 | 131 | |
paulb@224 | 132 | for attribute_str in attributes_str: |
paulb@224 | 133 | t = attribute_str.split("=") |
paulb@224 | 134 | if len(t) > 1: |
paulb@224 | 135 | name, value = t[0].strip(), t[1].strip() |
paulb@238 | 136 | attributes[name] = value |
paulb@5 | 137 | |
paulb@238 | 138 | return header_class(principal_value, **attributes) |
paulb@238 | 139 | |
paulb@238 | 140 | def parse_content_type(self, content_type_field): |
paulb@238 | 141 | return self.parse_header_value(ContentType, content_type_field) |
paulb@5 | 142 | |
paulb@49 | 143 | def format_header_value(self, value): |
paulb@49 | 144 | |
paulb@49 | 145 | """ |
paulb@49 | 146 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 147 | of US-ASCII. |
paulb@49 | 148 | """ |
paulb@49 | 149 | |
paulb@49 | 150 | return value.encode("US-ASCII") |
paulb@49 | 151 | |
paulb@258 | 152 | def encode_cookie_value(self, value): |
paulb@258 | 153 | |
paulb@258 | 154 | """ |
paulb@258 | 155 | Encode the given cookie 'value'. This ensures the usage of US-ASCII |
paulb@258 | 156 | through the encoding of Unicode objects as URL-encoded UTF-8 text. |
paulb@258 | 157 | """ |
paulb@258 | 158 | |
paulb@258 | 159 | return urllib.quote(value.encode("UTF-8")).encode("US-ASCII") |
paulb@258 | 160 | |
paulb@258 | 161 | def decode_cookie_value(self, value): |
paulb@258 | 162 | |
paulb@258 | 163 | """ |
paulb@258 | 164 | Decode the given cookie 'value'. |
paulb@258 | 165 | """ |
paulb@258 | 166 | |
paulb@258 | 167 | return unicode(urllib.unquote(value), "UTF-8") |
paulb@258 | 168 | |
paulb@258 | 169 | def process_cookies(self, cookie_dict, using_strings=0): |
paulb@258 | 170 | |
paulb@258 | 171 | """ |
paulb@258 | 172 | Process the given 'cookie_dict', returning a dictionary mapping cookie names |
paulb@258 | 173 | to cookie objects where the names and values have been decoded from the form |
paulb@258 | 174 | used in the cookies retrieved from the request. |
paulb@258 | 175 | |
paulb@258 | 176 | The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a |
paulb@258 | 177 | mapping of cookie names to values. |
paulb@258 | 178 | """ |
paulb@258 | 179 | |
paulb@258 | 180 | cookies = {} |
paulb@258 | 181 | for name in cookie_dict.keys(): |
paulb@258 | 182 | if using_strings: |
paulb@258 | 183 | value = cookie_dict[name] |
paulb@258 | 184 | else: |
paulb@258 | 185 | cookie = cookie_dict[name] |
paulb@258 | 186 | value = cookie.value |
paulb@258 | 187 | cookie_name = self.decode_cookie_value(name) |
paulb@258 | 188 | cookie_value = self.decode_cookie_value(value) |
paulb@258 | 189 | cookies[cookie_name] = Cookie(cookie_name, cookie_value) |
paulb@258 | 190 | return cookies |
paulb@258 | 191 | |
paulb@1 | 192 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 193 | |
paulb@1 | 194 | """ |
paulb@1 | 195 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 196 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 197 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 198 | criteria are ignored. |
paulb@1 | 199 | |
paulb@1 | 200 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 201 | preferences are appropriate. |
paulb@1 | 202 | """ |
paulb@1 | 203 | |
paulb@108 | 204 | if accept_preference is None: |
paulb@108 | 205 | return [] |
paulb@108 | 206 | |
paulb@1 | 207 | accept_defs = accept_preference.split(",") |
paulb@1 | 208 | accept_prefs = [] |
paulb@1 | 209 | for accept_def in accept_defs: |
paulb@1 | 210 | t = accept_def.split(";") |
paulb@1 | 211 | if len(t) >= 1: |
paulb@1 | 212 | accept_prefs.append(t[0].strip()) |
paulb@1 | 213 | return accept_prefs |
paulb@1 | 214 | |
paulb@65 | 215 | def convert_to_list(self, value): |
paulb@65 | 216 | |
paulb@65 | 217 | """ |
paulb@65 | 218 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 219 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 220 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 221 | then an empty list is returned. |
paulb@65 | 222 | """ |
paulb@65 | 223 | |
paulb@65 | 224 | if type(value) == type([]): |
paulb@65 | 225 | return value |
paulb@65 | 226 | elif type(value) == type(()): |
paulb@65 | 227 | return list(value) |
paulb@65 | 228 | elif value is None: |
paulb@65 | 229 | return [] |
paulb@65 | 230 | else: |
paulb@65 | 231 | return [value] |
paulb@65 | 232 | |
paulb@1 | 233 | # Request-related methods. |
paulb@1 | 234 | |
paulb@1 | 235 | def get_request_stream(self): |
paulb@1 | 236 | |
paulb@1 | 237 | """ |
paulb@186 | 238 | Returns the request stream for the transaction. |
paulb@1 | 239 | """ |
paulb@1 | 240 | |
paulb@1 | 241 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 242 | |
paulb@1 | 243 | def get_request_method(self): |
paulb@1 | 244 | |
paulb@1 | 245 | """ |
paulb@186 | 246 | Returns the request method. |
paulb@1 | 247 | """ |
paulb@1 | 248 | |
paulb@1 | 249 | raise NotImplementedError, "get_request_method" |
paulb@1 | 250 | |
paulb@1 | 251 | def get_headers(self): |
paulb@1 | 252 | |
paulb@1 | 253 | """ |
paulb@186 | 254 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 255 | names to values. |
paulb@1 | 256 | """ |
paulb@1 | 257 | |
paulb@1 | 258 | raise NotImplementedError, "get_headers" |
paulb@1 | 259 | |
paulb@65 | 260 | def get_header_values(self, key): |
paulb@65 | 261 | |
paulb@65 | 262 | """ |
paulb@186 | 263 | Returns a list of all request header values associated with the given |
paulb@186 | 264 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 265 | case-insensitive string. |
paulb@65 | 266 | """ |
paulb@65 | 267 | |
paulb@65 | 268 | raise NotImplementedError, "get_header_values" |
paulb@65 | 269 | |
paulb@1 | 270 | def get_content_type(self): |
paulb@1 | 271 | |
paulb@1 | 272 | """ |
paulb@186 | 273 | Returns the content type specified on the request, along with the |
paulb@186 | 274 | charset employed. |
paulb@1 | 275 | """ |
paulb@1 | 276 | |
paulb@1 | 277 | raise NotImplementedError, "get_content_type" |
paulb@1 | 278 | |
paulb@1 | 279 | def get_content_charsets(self): |
paulb@1 | 280 | |
paulb@1 | 281 | """ |
paulb@1 | 282 | Returns the character set preferences. |
paulb@1 | 283 | """ |
paulb@1 | 284 | |
paulb@1 | 285 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 286 | |
paulb@1 | 287 | def get_content_languages(self): |
paulb@1 | 288 | |
paulb@1 | 289 | """ |
paulb@186 | 290 | Returns extracted language information from the transaction. |
paulb@1 | 291 | """ |
paulb@1 | 292 | |
paulb@1 | 293 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 294 | |
paulb@5 | 295 | def get_path(self): |
paulb@5 | 296 | |
paulb@5 | 297 | """ |
paulb@186 | 298 | Returns the entire path from the request. |
paulb@5 | 299 | """ |
paulb@5 | 300 | |
paulb@5 | 301 | raise NotImplementedError, "get_path" |
paulb@5 | 302 | |
paulb@162 | 303 | def get_path_without_query(self): |
paulb@162 | 304 | |
paulb@162 | 305 | """ |
paulb@186 | 306 | Returns the entire path from the request minus the query string. |
paulb@162 | 307 | """ |
paulb@162 | 308 | |
paulb@162 | 309 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 310 | |
paulb@1 | 311 | def get_path_info(self): |
paulb@1 | 312 | |
paulb@1 | 313 | """ |
paulb@186 | 314 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 315 | handling the current request) from the request. |
paulb@1 | 316 | """ |
paulb@1 | 317 | |
paulb@1 | 318 | raise NotImplementedError, "get_path_info" |
paulb@1 | 319 | |
paulb@69 | 320 | def get_query_string(self): |
paulb@69 | 321 | |
paulb@69 | 322 | """ |
paulb@186 | 323 | Returns the query string from the path in the request. |
paulb@69 | 324 | """ |
paulb@69 | 325 | |
paulb@69 | 326 | raise NotImplementedError, "get_query_string" |
paulb@69 | 327 | |
paulb@69 | 328 | # Higher level request-related methods. |
paulb@69 | 329 | |
paulb@69 | 330 | def get_fields_from_path(self): |
paulb@69 | 331 | |
paulb@69 | 332 | """ |
paulb@249 | 333 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 334 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 335 | the path if handling a POST transaction. |
paulb@69 | 336 | |
paulb@71 | 337 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 338 | single value is associated with any given field name). |
paulb@69 | 339 | """ |
paulb@69 | 340 | |
paulb@69 | 341 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 342 | |
paulb@167 | 343 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 344 | |
paulb@69 | 345 | """ |
paulb@249 | 346 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 347 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 348 | encoding of the message body for cases where no such information is |
paulb@249 | 349 | available, but where the default encoding is to be overridden. |
paulb@69 | 350 | |
paulb@73 | 351 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 352 | single value is associated with any given field name). Each value is |
paulb@193 | 353 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 354 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 355 | """ |
paulb@69 | 356 | |
paulb@69 | 357 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 358 | |
paulb@249 | 359 | def get_fields(self, encoding=None): |
paulb@249 | 360 | |
paulb@249 | 361 | """ |
paulb@249 | 362 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 363 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 364 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 365 | where no such information is available, but where the default encoding |
paulb@249 | 366 | is to be overridden. |
paulb@249 | 367 | |
paulb@249 | 368 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 369 | single value is associated with any given field name). Each value is |
paulb@249 | 370 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 371 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 372 | |
paulb@249 | 373 | Where a given field name is used in both the path and message body to |
paulb@249 | 374 | specify values, the values from both sources will be combined into a |
paulb@249 | 375 | single list associated with that field name. |
paulb@249 | 376 | """ |
paulb@249 | 377 | |
paulb@249 | 378 | raise NotImplementedError, "get_fields" |
paulb@249 | 379 | |
paulb@45 | 380 | def get_user(self): |
paulb@1 | 381 | |
paulb@1 | 382 | """ |
paulb@186 | 383 | Extracts user information from the transaction. |
paulb@115 | 384 | |
paulb@115 | 385 | Returns a username as a string or None if no user is defined. |
paulb@1 | 386 | """ |
paulb@1 | 387 | |
paulb@45 | 388 | raise NotImplementedError, "get_user" |
paulb@1 | 389 | |
paulb@98 | 390 | def get_cookies(self): |
paulb@98 | 391 | |
paulb@98 | 392 | """ |
paulb@186 | 393 | Obtains cookie information from the request. |
paulb@98 | 394 | |
paulb@98 | 395 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 396 | """ |
paulb@98 | 397 | |
paulb@98 | 398 | raise NotImplementedError, "get_cookies" |
paulb@98 | 399 | |
paulb@104 | 400 | def get_cookie(self, cookie_name): |
paulb@104 | 401 | |
paulb@104 | 402 | """ |
paulb@186 | 403 | Obtains cookie information from the request. |
paulb@104 | 404 | |
paulb@104 | 405 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 406 | cookie exists. |
paulb@104 | 407 | """ |
paulb@104 | 408 | |
paulb@104 | 409 | raise NotImplementedError, "get_cookie" |
paulb@104 | 410 | |
paulb@1 | 411 | # Response-related methods. |
paulb@1 | 412 | |
paulb@1 | 413 | def get_response_stream(self): |
paulb@1 | 414 | |
paulb@1 | 415 | """ |
paulb@186 | 416 | Returns the response stream for the transaction. |
paulb@1 | 417 | """ |
paulb@1 | 418 | |
paulb@1 | 419 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 420 | |
paulb@252 | 421 | def get_response_stream_encoding(self): |
paulb@252 | 422 | |
paulb@252 | 423 | """ |
paulb@252 | 424 | Returns the response stream encoding. |
paulb@252 | 425 | """ |
paulb@252 | 426 | |
paulb@252 | 427 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 428 | |
paulb@42 | 429 | def get_response_code(self): |
paulb@42 | 430 | |
paulb@42 | 431 | """ |
paulb@42 | 432 | Get the response code associated with the transaction. If no response |
paulb@42 | 433 | code is defined, None is returned. |
paulb@42 | 434 | """ |
paulb@42 | 435 | |
paulb@42 | 436 | raise NotImplementedError, "get_response_code" |
paulb@42 | 437 | |
paulb@42 | 438 | def set_response_code(self, response_code): |
paulb@42 | 439 | |
paulb@42 | 440 | """ |
paulb@42 | 441 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 442 | specification. |
paulb@42 | 443 | """ |
paulb@42 | 444 | |
paulb@42 | 445 | raise NotImplementedError, "set_response_code" |
paulb@42 | 446 | |
paulb@67 | 447 | def set_header_value(self, header, value): |
paulb@49 | 448 | |
paulb@49 | 449 | """ |
paulb@49 | 450 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 451 | """ |
paulb@49 | 452 | |
paulb@67 | 453 | raise NotImplementedError, "set_header_value" |
paulb@49 | 454 | |
paulb@1 | 455 | def set_content_type(self, content_type): |
paulb@1 | 456 | |
paulb@1 | 457 | """ |
paulb@186 | 458 | Sets the 'content_type' for the response. |
paulb@1 | 459 | """ |
paulb@1 | 460 | |
paulb@1 | 461 | raise NotImplementedError, "set_content_type" |
paulb@1 | 462 | |
paulb@98 | 463 | # Higher level response-related methods. |
paulb@98 | 464 | |
paulb@98 | 465 | def set_cookie(self, cookie): |
paulb@98 | 466 | |
paulb@98 | 467 | """ |
paulb@186 | 468 | Stores the given 'cookie' object in the response. |
paulb@98 | 469 | """ |
paulb@98 | 470 | |
paulb@98 | 471 | raise NotImplementedError, "set_cookie" |
paulb@98 | 472 | |
paulb@98 | 473 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 474 | |
paulb@98 | 475 | """ |
paulb@186 | 476 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 477 | |
paulb@98 | 478 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 479 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 480 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 481 | """ |
paulb@98 | 482 | |
paulb@98 | 483 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 484 | |
paulb@98 | 485 | def delete_cookie(self, cookie_name): |
paulb@98 | 486 | |
paulb@98 | 487 | """ |
paulb@186 | 488 | Adds to the response a request that the cookie with the given |
paulb@186 | 489 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 490 | """ |
paulb@98 | 491 | |
paulb@98 | 492 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 493 | |
paulb@183 | 494 | # Session-related methods. |
paulb@183 | 495 | |
paulb@183 | 496 | def get_session(self, create=1): |
paulb@183 | 497 | |
paulb@183 | 498 | """ |
paulb@186 | 499 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 500 | transaction. |
paulb@183 | 501 | |
paulb@183 | 502 | If no session has yet been established according to information |
paulb@183 | 503 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 504 | determines whether a new session will be established. |
paulb@183 | 505 | |
paulb@183 | 506 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 507 | then None is returned. In all other cases, a session object is created |
paulb@183 | 508 | (where appropriate) and returned. |
paulb@183 | 509 | """ |
paulb@183 | 510 | |
paulb@183 | 511 | raise NotImplementedError, "get_session" |
paulb@183 | 512 | |
paulb@183 | 513 | def expire_session(self): |
paulb@183 | 514 | |
paulb@183 | 515 | """ |
paulb@186 | 516 | Expires any session established according to information provided in the |
paulb@186 | 517 | transaction. |
paulb@183 | 518 | """ |
paulb@183 | 519 | |
paulb@183 | 520 | raise NotImplementedError, "expire_session" |
paulb@183 | 521 | |
paulb@128 | 522 | # Application-specific methods. |
paulb@128 | 523 | |
paulb@128 | 524 | def set_user(self, username): |
paulb@128 | 525 | |
paulb@128 | 526 | """ |
paulb@128 | 527 | An application-specific method which sets the user information with |
paulb@128 | 528 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 529 | 'get_user'. |
paulb@128 | 530 | """ |
paulb@128 | 531 | |
paulb@128 | 532 | raise NotImplementedError, "set_user" |
paulb@128 | 533 | |
paulb@1 | 534 | class Resource: |
paulb@1 | 535 | |
paulb@1 | 536 | "A generic resource interface." |
paulb@1 | 537 | |
paulb@3 | 538 | def respond(self, trans): |
paulb@3 | 539 | |
paulb@3 | 540 | """ |
paulb@3 | 541 | An application-specific method which performs activities on the basis of |
paulb@3 | 542 | the transaction object 'trans'. |
paulb@3 | 543 | """ |
paulb@3 | 544 | |
paulb@3 | 545 | raise NotImplementedError, "respond" |
paulb@1 | 546 | |
paulb@75 | 547 | class Authenticator: |
paulb@75 | 548 | |
paulb@75 | 549 | "A generic authentication component." |
paulb@75 | 550 | |
paulb@75 | 551 | def authenticate(self, trans): |
paulb@75 | 552 | |
paulb@75 | 553 | """ |
paulb@75 | 554 | An application-specific method which authenticates the sender of the |
paulb@75 | 555 | request described by the transaction object 'trans'. This method should |
paulb@75 | 556 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 557 | the transaction. |
paulb@75 | 558 | |
paulb@75 | 559 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 560 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 561 | evaluates to false. |
paulb@75 | 562 | """ |
paulb@75 | 563 | |
paulb@75 | 564 | raise NotImplementedError, "authenticate" |
paulb@75 | 565 | |
paulb@75 | 566 | def get_auth_type(self): |
paulb@75 | 567 | |
paulb@75 | 568 | """ |
paulb@75 | 569 | An application-specific method which returns the authentication type to |
paulb@75 | 570 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 571 | authentication. |
paulb@75 | 572 | """ |
paulb@75 | 573 | |
paulb@75 | 574 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 575 | |
paulb@75 | 576 | def get_realm(self): |
paulb@75 | 577 | |
paulb@75 | 578 | """ |
paulb@75 | 579 | An application-specific method which returns the name of the realm for |
paulb@75 | 580 | which authentication is taking place. |
paulb@75 | 581 | """ |
paulb@75 | 582 | |
paulb@75 | 583 | raise NotImplementedError, "get_realm" |
paulb@75 | 584 | |
paulb@1 | 585 | # vim: tabstop=4 expandtab shiftwidth=4 |