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@353 | 141 | |
paulb@353 | 142 | """ |
paulb@353 | 143 | Parse the given 'content_type_field' - a value found comparable to that |
paulb@353 | 144 | found in an HTTP request header for "Content-Type". |
paulb@353 | 145 | """ |
paulb@353 | 146 | |
paulb@238 | 147 | return self.parse_header_value(ContentType, content_type_field) |
paulb@5 | 148 | |
paulb@49 | 149 | def format_header_value(self, value): |
paulb@49 | 150 | |
paulb@49 | 151 | """ |
paulb@49 | 152 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 153 | of US-ASCII. |
paulb@49 | 154 | """ |
paulb@49 | 155 | |
paulb@49 | 156 | return value.encode("US-ASCII") |
paulb@49 | 157 | |
paulb@258 | 158 | def encode_cookie_value(self, value): |
paulb@258 | 159 | |
paulb@258 | 160 | """ |
paulb@258 | 161 | Encode the given cookie 'value'. This ensures the usage of US-ASCII |
paulb@258 | 162 | through the encoding of Unicode objects as URL-encoded UTF-8 text. |
paulb@258 | 163 | """ |
paulb@258 | 164 | |
paulb@258 | 165 | return urllib.quote(value.encode("UTF-8")).encode("US-ASCII") |
paulb@258 | 166 | |
paulb@258 | 167 | def decode_cookie_value(self, value): |
paulb@258 | 168 | |
paulb@258 | 169 | """ |
paulb@258 | 170 | Decode the given cookie 'value'. |
paulb@258 | 171 | """ |
paulb@258 | 172 | |
paulb@258 | 173 | return unicode(urllib.unquote(value), "UTF-8") |
paulb@258 | 174 | |
paulb@258 | 175 | def process_cookies(self, cookie_dict, using_strings=0): |
paulb@258 | 176 | |
paulb@258 | 177 | """ |
paulb@258 | 178 | Process the given 'cookie_dict', returning a dictionary mapping cookie names |
paulb@258 | 179 | to cookie objects where the names and values have been decoded from the form |
paulb@258 | 180 | used in the cookies retrieved from the request. |
paulb@258 | 181 | |
paulb@258 | 182 | The optional 'using_strings', if set to 1, treats the 'cookie_dict' as a |
paulb@258 | 183 | mapping of cookie names to values. |
paulb@258 | 184 | """ |
paulb@258 | 185 | |
paulb@258 | 186 | cookies = {} |
paulb@258 | 187 | for name in cookie_dict.keys(): |
paulb@258 | 188 | if using_strings: |
paulb@258 | 189 | value = cookie_dict[name] |
paulb@258 | 190 | else: |
paulb@258 | 191 | cookie = cookie_dict[name] |
paulb@258 | 192 | value = cookie.value |
paulb@258 | 193 | cookie_name = self.decode_cookie_value(name) |
paulb@258 | 194 | cookie_value = self.decode_cookie_value(value) |
paulb@258 | 195 | cookies[cookie_name] = Cookie(cookie_name, cookie_value) |
paulb@258 | 196 | return cookies |
paulb@258 | 197 | |
paulb@1 | 198 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 199 | |
paulb@1 | 200 | """ |
paulb@1 | 201 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 202 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 203 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 204 | criteria are ignored. |
paulb@1 | 205 | |
paulb@1 | 206 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 207 | preferences are appropriate. |
paulb@1 | 208 | """ |
paulb@1 | 209 | |
paulb@108 | 210 | if accept_preference is None: |
paulb@108 | 211 | return [] |
paulb@108 | 212 | |
paulb@1 | 213 | accept_defs = accept_preference.split(",") |
paulb@1 | 214 | accept_prefs = [] |
paulb@1 | 215 | for accept_def in accept_defs: |
paulb@1 | 216 | t = accept_def.split(";") |
paulb@1 | 217 | if len(t) >= 1: |
paulb@1 | 218 | accept_prefs.append(t[0].strip()) |
paulb@1 | 219 | return accept_prefs |
paulb@1 | 220 | |
paulb@65 | 221 | def convert_to_list(self, value): |
paulb@65 | 222 | |
paulb@65 | 223 | """ |
paulb@65 | 224 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 225 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 226 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 227 | then an empty list is returned. |
paulb@65 | 228 | """ |
paulb@65 | 229 | |
paulb@65 | 230 | if type(value) == type([]): |
paulb@65 | 231 | return value |
paulb@65 | 232 | elif type(value) == type(()): |
paulb@65 | 233 | return list(value) |
paulb@65 | 234 | elif value is None: |
paulb@65 | 235 | return [] |
paulb@65 | 236 | else: |
paulb@65 | 237 | return [value] |
paulb@65 | 238 | |
paulb@338 | 239 | # Server-related methods. |
paulb@338 | 240 | |
paulb@338 | 241 | def get_server_name(self): |
paulb@338 | 242 | |
paulb@338 | 243 | "Returns the server name." |
paulb@338 | 244 | |
paulb@338 | 245 | raise NotImplementedError, "get_server_name" |
paulb@338 | 246 | |
paulb@338 | 247 | def get_server_port(self): |
paulb@338 | 248 | |
paulb@338 | 249 | "Returns the server port as a string." |
paulb@338 | 250 | |
paulb@338 | 251 | raise NotImplementedError, "get_server_port" |
paulb@338 | 252 | |
paulb@1 | 253 | # Request-related methods. |
paulb@1 | 254 | |
paulb@1 | 255 | def get_request_stream(self): |
paulb@1 | 256 | |
paulb@1 | 257 | """ |
paulb@186 | 258 | Returns the request stream for the transaction. |
paulb@1 | 259 | """ |
paulb@1 | 260 | |
paulb@1 | 261 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 262 | |
paulb@1 | 263 | def get_request_method(self): |
paulb@1 | 264 | |
paulb@1 | 265 | """ |
paulb@186 | 266 | Returns the request method. |
paulb@1 | 267 | """ |
paulb@1 | 268 | |
paulb@1 | 269 | raise NotImplementedError, "get_request_method" |
paulb@1 | 270 | |
paulb@1 | 271 | def get_headers(self): |
paulb@1 | 272 | |
paulb@1 | 273 | """ |
paulb@186 | 274 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 275 | names to values. |
paulb@1 | 276 | """ |
paulb@1 | 277 | |
paulb@1 | 278 | raise NotImplementedError, "get_headers" |
paulb@1 | 279 | |
paulb@65 | 280 | def get_header_values(self, key): |
paulb@65 | 281 | |
paulb@65 | 282 | """ |
paulb@186 | 283 | Returns a list of all request header values associated with the given |
paulb@186 | 284 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 285 | case-insensitive string. |
paulb@65 | 286 | """ |
paulb@65 | 287 | |
paulb@65 | 288 | raise NotImplementedError, "get_header_values" |
paulb@65 | 289 | |
paulb@1 | 290 | def get_content_type(self): |
paulb@1 | 291 | |
paulb@1 | 292 | """ |
paulb@186 | 293 | Returns the content type specified on the request, along with the |
paulb@186 | 294 | charset employed. |
paulb@1 | 295 | """ |
paulb@1 | 296 | |
paulb@1 | 297 | raise NotImplementedError, "get_content_type" |
paulb@1 | 298 | |
paulb@1 | 299 | def get_content_charsets(self): |
paulb@1 | 300 | |
paulb@1 | 301 | """ |
paulb@1 | 302 | Returns the character set preferences. |
paulb@1 | 303 | """ |
paulb@1 | 304 | |
paulb@1 | 305 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 306 | |
paulb@1 | 307 | def get_content_languages(self): |
paulb@1 | 308 | |
paulb@1 | 309 | """ |
paulb@186 | 310 | Returns extracted language information from the transaction. |
paulb@1 | 311 | """ |
paulb@1 | 312 | |
paulb@1 | 313 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 314 | |
paulb@5 | 315 | def get_path(self): |
paulb@5 | 316 | |
paulb@5 | 317 | """ |
paulb@186 | 318 | Returns the entire path from the request. |
paulb@5 | 319 | """ |
paulb@5 | 320 | |
paulb@5 | 321 | raise NotImplementedError, "get_path" |
paulb@5 | 322 | |
paulb@162 | 323 | def get_path_without_query(self): |
paulb@162 | 324 | |
paulb@162 | 325 | """ |
paulb@186 | 326 | Returns the entire path from the request minus the query string. |
paulb@162 | 327 | """ |
paulb@162 | 328 | |
paulb@162 | 329 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 330 | |
paulb@1 | 331 | def get_path_info(self): |
paulb@1 | 332 | |
paulb@1 | 333 | """ |
paulb@186 | 334 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 335 | handling the current request) from the request. |
paulb@1 | 336 | """ |
paulb@1 | 337 | |
paulb@1 | 338 | raise NotImplementedError, "get_path_info" |
paulb@1 | 339 | |
paulb@69 | 340 | def get_query_string(self): |
paulb@69 | 341 | |
paulb@69 | 342 | """ |
paulb@186 | 343 | Returns the query string from the path in the request. |
paulb@69 | 344 | """ |
paulb@69 | 345 | |
paulb@69 | 346 | raise NotImplementedError, "get_query_string" |
paulb@69 | 347 | |
paulb@69 | 348 | # Higher level request-related methods. |
paulb@69 | 349 | |
paulb@69 | 350 | def get_fields_from_path(self): |
paulb@69 | 351 | |
paulb@69 | 352 | """ |
paulb@249 | 353 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 354 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 355 | the path if handling a POST transaction. |
paulb@69 | 356 | |
paulb@71 | 357 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 358 | single value is associated with any given field name). |
paulb@69 | 359 | """ |
paulb@69 | 360 | |
paulb@69 | 361 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 362 | |
paulb@167 | 363 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 364 | |
paulb@69 | 365 | """ |
paulb@249 | 366 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 367 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 368 | encoding of the message body for cases where no such information is |
paulb@249 | 369 | available, but where the default encoding is to be overridden. |
paulb@69 | 370 | |
paulb@73 | 371 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 372 | single value is associated with any given field name). Each value is |
paulb@193 | 373 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 374 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 375 | """ |
paulb@69 | 376 | |
paulb@69 | 377 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 378 | |
paulb@249 | 379 | def get_fields(self, encoding=None): |
paulb@249 | 380 | |
paulb@249 | 381 | """ |
paulb@249 | 382 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 383 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 384 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 385 | where no such information is available, but where the default encoding |
paulb@249 | 386 | is to be overridden. |
paulb@249 | 387 | |
paulb@249 | 388 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 389 | single value is associated with any given field name). Each value is |
paulb@249 | 390 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 391 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 392 | |
paulb@249 | 393 | Where a given field name is used in both the path and message body to |
paulb@249 | 394 | specify values, the values from both sources will be combined into a |
paulb@249 | 395 | single list associated with that field name. |
paulb@249 | 396 | """ |
paulb@249 | 397 | |
paulb@249 | 398 | raise NotImplementedError, "get_fields" |
paulb@249 | 399 | |
paulb@45 | 400 | def get_user(self): |
paulb@1 | 401 | |
paulb@1 | 402 | """ |
paulb@186 | 403 | Extracts user information from the transaction. |
paulb@115 | 404 | |
paulb@115 | 405 | Returns a username as a string or None if no user is defined. |
paulb@1 | 406 | """ |
paulb@1 | 407 | |
paulb@45 | 408 | raise NotImplementedError, "get_user" |
paulb@1 | 409 | |
paulb@98 | 410 | def get_cookies(self): |
paulb@98 | 411 | |
paulb@98 | 412 | """ |
paulb@186 | 413 | Obtains cookie information from the request. |
paulb@98 | 414 | |
paulb@98 | 415 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 416 | """ |
paulb@98 | 417 | |
paulb@98 | 418 | raise NotImplementedError, "get_cookies" |
paulb@98 | 419 | |
paulb@104 | 420 | def get_cookie(self, cookie_name): |
paulb@104 | 421 | |
paulb@104 | 422 | """ |
paulb@186 | 423 | Obtains cookie information from the request. |
paulb@104 | 424 | |
paulb@104 | 425 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 426 | cookie exists. |
paulb@104 | 427 | """ |
paulb@104 | 428 | |
paulb@104 | 429 | raise NotImplementedError, "get_cookie" |
paulb@104 | 430 | |
paulb@1 | 431 | # Response-related methods. |
paulb@1 | 432 | |
paulb@1 | 433 | def get_response_stream(self): |
paulb@1 | 434 | |
paulb@1 | 435 | """ |
paulb@186 | 436 | Returns the response stream for the transaction. |
paulb@1 | 437 | """ |
paulb@1 | 438 | |
paulb@1 | 439 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 440 | |
paulb@252 | 441 | def get_response_stream_encoding(self): |
paulb@252 | 442 | |
paulb@252 | 443 | """ |
paulb@252 | 444 | Returns the response stream encoding. |
paulb@252 | 445 | """ |
paulb@252 | 446 | |
paulb@252 | 447 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 448 | |
paulb@42 | 449 | def get_response_code(self): |
paulb@42 | 450 | |
paulb@42 | 451 | """ |
paulb@42 | 452 | Get the response code associated with the transaction. If no response |
paulb@42 | 453 | code is defined, None is returned. |
paulb@42 | 454 | """ |
paulb@42 | 455 | |
paulb@42 | 456 | raise NotImplementedError, "get_response_code" |
paulb@42 | 457 | |
paulb@42 | 458 | def set_response_code(self, response_code): |
paulb@42 | 459 | |
paulb@42 | 460 | """ |
paulb@42 | 461 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 462 | specification. |
paulb@42 | 463 | """ |
paulb@42 | 464 | |
paulb@42 | 465 | raise NotImplementedError, "set_response_code" |
paulb@42 | 466 | |
paulb@67 | 467 | def set_header_value(self, header, value): |
paulb@49 | 468 | |
paulb@49 | 469 | """ |
paulb@49 | 470 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 471 | """ |
paulb@49 | 472 | |
paulb@67 | 473 | raise NotImplementedError, "set_header_value" |
paulb@49 | 474 | |
paulb@1 | 475 | def set_content_type(self, content_type): |
paulb@1 | 476 | |
paulb@1 | 477 | """ |
paulb@186 | 478 | Sets the 'content_type' for the response. |
paulb@1 | 479 | """ |
paulb@1 | 480 | |
paulb@1 | 481 | raise NotImplementedError, "set_content_type" |
paulb@1 | 482 | |
paulb@98 | 483 | # Higher level response-related methods. |
paulb@98 | 484 | |
paulb@98 | 485 | def set_cookie(self, cookie): |
paulb@98 | 486 | |
paulb@98 | 487 | """ |
paulb@186 | 488 | Stores the given 'cookie' object in the response. |
paulb@98 | 489 | """ |
paulb@98 | 490 | |
paulb@98 | 491 | raise NotImplementedError, "set_cookie" |
paulb@98 | 492 | |
paulb@98 | 493 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 494 | |
paulb@98 | 495 | """ |
paulb@186 | 496 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 497 | |
paulb@98 | 498 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 499 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 500 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 501 | """ |
paulb@98 | 502 | |
paulb@98 | 503 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 504 | |
paulb@98 | 505 | def delete_cookie(self, cookie_name): |
paulb@98 | 506 | |
paulb@98 | 507 | """ |
paulb@186 | 508 | Adds to the response a request that the cookie with the given |
paulb@186 | 509 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 510 | """ |
paulb@98 | 511 | |
paulb@98 | 512 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 513 | |
paulb@183 | 514 | # Session-related methods. |
paulb@183 | 515 | |
paulb@183 | 516 | def get_session(self, create=1): |
paulb@183 | 517 | |
paulb@183 | 518 | """ |
paulb@186 | 519 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 520 | transaction. |
paulb@183 | 521 | |
paulb@183 | 522 | If no session has yet been established according to information |
paulb@183 | 523 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 524 | determines whether a new session will be established. |
paulb@183 | 525 | |
paulb@183 | 526 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 527 | then None is returned. In all other cases, a session object is created |
paulb@183 | 528 | (where appropriate) and returned. |
paulb@183 | 529 | """ |
paulb@183 | 530 | |
paulb@183 | 531 | raise NotImplementedError, "get_session" |
paulb@183 | 532 | |
paulb@183 | 533 | def expire_session(self): |
paulb@183 | 534 | |
paulb@183 | 535 | """ |
paulb@186 | 536 | Expires any session established according to information provided in the |
paulb@186 | 537 | transaction. |
paulb@183 | 538 | """ |
paulb@183 | 539 | |
paulb@183 | 540 | raise NotImplementedError, "expire_session" |
paulb@183 | 541 | |
paulb@128 | 542 | # Application-specific methods. |
paulb@128 | 543 | |
paulb@128 | 544 | def set_user(self, username): |
paulb@128 | 545 | |
paulb@128 | 546 | """ |
paulb@128 | 547 | An application-specific method which sets the user information with |
paulb@128 | 548 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 549 | 'get_user'. |
paulb@128 | 550 | """ |
paulb@128 | 551 | |
paulb@311 | 552 | self.user = username |
paulb@311 | 553 | |
paulb@316 | 554 | def set_virtual_path_info(self, path_info): |
paulb@311 | 555 | |
paulb@311 | 556 | """ |
paulb@311 | 557 | An application-specific method which sets the 'path_info' in the |
paulb@316 | 558 | transaction. This affects subsequent calls to 'get_virtual_path_info'. |
paulb@311 | 559 | """ |
paulb@311 | 560 | |
paulb@311 | 561 | self.path_info = path_info |
paulb@128 | 562 | |
paulb@316 | 563 | def get_virtual_path_info(self): |
paulb@316 | 564 | |
paulb@316 | 565 | """ |
paulb@316 | 566 | An application-specific method which either returns path info set in the |
paulb@316 | 567 | 'set_virtual_path_info' method, or the normal path info found in the |
paulb@316 | 568 | request. |
paulb@316 | 569 | """ |
paulb@316 | 570 | |
paulb@399 | 571 | if self.path_info is not None: |
paulb@399 | 572 | return self.path_info |
paulb@399 | 573 | else: |
paulb@399 | 574 | return self.get_path_info() |
paulb@316 | 575 | |
paulb@1 | 576 | class Resource: |
paulb@1 | 577 | |
paulb@1 | 578 | "A generic resource interface." |
paulb@1 | 579 | |
paulb@3 | 580 | def respond(self, trans): |
paulb@3 | 581 | |
paulb@3 | 582 | """ |
paulb@3 | 583 | An application-specific method which performs activities on the basis of |
paulb@3 | 584 | the transaction object 'trans'. |
paulb@3 | 585 | """ |
paulb@3 | 586 | |
paulb@3 | 587 | raise NotImplementedError, "respond" |
paulb@1 | 588 | |
paulb@75 | 589 | class Authenticator: |
paulb@75 | 590 | |
paulb@75 | 591 | "A generic authentication component." |
paulb@75 | 592 | |
paulb@75 | 593 | def authenticate(self, trans): |
paulb@75 | 594 | |
paulb@75 | 595 | """ |
paulb@75 | 596 | An application-specific method which authenticates the sender of the |
paulb@75 | 597 | request described by the transaction object 'trans'. This method should |
paulb@75 | 598 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 599 | the transaction. |
paulb@75 | 600 | |
paulb@75 | 601 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 602 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 603 | evaluates to false. |
paulb@75 | 604 | """ |
paulb@75 | 605 | |
paulb@75 | 606 | raise NotImplementedError, "authenticate" |
paulb@75 | 607 | |
paulb@75 | 608 | def get_auth_type(self): |
paulb@75 | 609 | |
paulb@75 | 610 | """ |
paulb@75 | 611 | An application-specific method which returns the authentication type to |
paulb@75 | 612 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 613 | authentication. |
paulb@75 | 614 | """ |
paulb@75 | 615 | |
paulb@75 | 616 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 617 | |
paulb@75 | 618 | def get_realm(self): |
paulb@75 | 619 | |
paulb@75 | 620 | """ |
paulb@75 | 621 | An application-specific method which returns the name of the realm for |
paulb@75 | 622 | which authentication is taking place. |
paulb@75 | 623 | """ |
paulb@75 | 624 | |
paulb@75 | 625 | raise NotImplementedError, "get_realm" |
paulb@75 | 626 | |
paulb@1 | 627 | # vim: tabstop=4 expandtab shiftwidth=4 |