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@338 | 233 | # Server-related methods. |
paulb@338 | 234 | |
paulb@338 | 235 | def get_server_name(self): |
paulb@338 | 236 | |
paulb@338 | 237 | "Returns the server name." |
paulb@338 | 238 | |
paulb@338 | 239 | raise NotImplementedError, "get_server_name" |
paulb@338 | 240 | |
paulb@338 | 241 | def get_server_port(self): |
paulb@338 | 242 | |
paulb@338 | 243 | "Returns the server port as a string." |
paulb@338 | 244 | |
paulb@338 | 245 | raise NotImplementedError, "get_server_port" |
paulb@338 | 246 | |
paulb@1 | 247 | # Request-related methods. |
paulb@1 | 248 | |
paulb@1 | 249 | def get_request_stream(self): |
paulb@1 | 250 | |
paulb@1 | 251 | """ |
paulb@186 | 252 | Returns the request stream for the transaction. |
paulb@1 | 253 | """ |
paulb@1 | 254 | |
paulb@1 | 255 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 256 | |
paulb@1 | 257 | def get_request_method(self): |
paulb@1 | 258 | |
paulb@1 | 259 | """ |
paulb@186 | 260 | Returns the request method. |
paulb@1 | 261 | """ |
paulb@1 | 262 | |
paulb@1 | 263 | raise NotImplementedError, "get_request_method" |
paulb@1 | 264 | |
paulb@1 | 265 | def get_headers(self): |
paulb@1 | 266 | |
paulb@1 | 267 | """ |
paulb@186 | 268 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 269 | names to values. |
paulb@1 | 270 | """ |
paulb@1 | 271 | |
paulb@1 | 272 | raise NotImplementedError, "get_headers" |
paulb@1 | 273 | |
paulb@65 | 274 | def get_header_values(self, key): |
paulb@65 | 275 | |
paulb@65 | 276 | """ |
paulb@186 | 277 | Returns a list of all request header values associated with the given |
paulb@186 | 278 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 279 | case-insensitive string. |
paulb@65 | 280 | """ |
paulb@65 | 281 | |
paulb@65 | 282 | raise NotImplementedError, "get_header_values" |
paulb@65 | 283 | |
paulb@1 | 284 | def get_content_type(self): |
paulb@1 | 285 | |
paulb@1 | 286 | """ |
paulb@186 | 287 | Returns the content type specified on the request, along with the |
paulb@186 | 288 | charset employed. |
paulb@1 | 289 | """ |
paulb@1 | 290 | |
paulb@1 | 291 | raise NotImplementedError, "get_content_type" |
paulb@1 | 292 | |
paulb@1 | 293 | def get_content_charsets(self): |
paulb@1 | 294 | |
paulb@1 | 295 | """ |
paulb@1 | 296 | Returns the character set preferences. |
paulb@1 | 297 | """ |
paulb@1 | 298 | |
paulb@1 | 299 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 300 | |
paulb@1 | 301 | def get_content_languages(self): |
paulb@1 | 302 | |
paulb@1 | 303 | """ |
paulb@186 | 304 | Returns extracted language information from the transaction. |
paulb@1 | 305 | """ |
paulb@1 | 306 | |
paulb@1 | 307 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 308 | |
paulb@5 | 309 | def get_path(self): |
paulb@5 | 310 | |
paulb@5 | 311 | """ |
paulb@186 | 312 | Returns the entire path from the request. |
paulb@5 | 313 | """ |
paulb@5 | 314 | |
paulb@5 | 315 | raise NotImplementedError, "get_path" |
paulb@5 | 316 | |
paulb@162 | 317 | def get_path_without_query(self): |
paulb@162 | 318 | |
paulb@162 | 319 | """ |
paulb@186 | 320 | Returns the entire path from the request minus the query string. |
paulb@162 | 321 | """ |
paulb@162 | 322 | |
paulb@162 | 323 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 324 | |
paulb@1 | 325 | def get_path_info(self): |
paulb@1 | 326 | |
paulb@1 | 327 | """ |
paulb@186 | 328 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 329 | handling the current request) from the request. |
paulb@1 | 330 | """ |
paulb@1 | 331 | |
paulb@1 | 332 | raise NotImplementedError, "get_path_info" |
paulb@1 | 333 | |
paulb@69 | 334 | def get_query_string(self): |
paulb@69 | 335 | |
paulb@69 | 336 | """ |
paulb@186 | 337 | Returns the query string from the path in the request. |
paulb@69 | 338 | """ |
paulb@69 | 339 | |
paulb@69 | 340 | raise NotImplementedError, "get_query_string" |
paulb@69 | 341 | |
paulb@69 | 342 | # Higher level request-related methods. |
paulb@69 | 343 | |
paulb@69 | 344 | def get_fields_from_path(self): |
paulb@69 | 345 | |
paulb@69 | 346 | """ |
paulb@249 | 347 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 348 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 349 | the path if handling a POST transaction. |
paulb@69 | 350 | |
paulb@71 | 351 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 352 | single value is associated with any given field name). |
paulb@69 | 353 | """ |
paulb@69 | 354 | |
paulb@69 | 355 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 356 | |
paulb@167 | 357 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 358 | |
paulb@69 | 359 | """ |
paulb@249 | 360 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 361 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 362 | encoding of the message body for cases where no such information is |
paulb@249 | 363 | available, but where the default encoding is to be overridden. |
paulb@69 | 364 | |
paulb@73 | 365 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 366 | single value is associated with any given field name). Each value is |
paulb@193 | 367 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 368 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 369 | """ |
paulb@69 | 370 | |
paulb@69 | 371 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 372 | |
paulb@249 | 373 | def get_fields(self, encoding=None): |
paulb@249 | 374 | |
paulb@249 | 375 | """ |
paulb@249 | 376 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 377 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 378 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 379 | where no such information is available, but where the default encoding |
paulb@249 | 380 | is to be overridden. |
paulb@249 | 381 | |
paulb@249 | 382 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 383 | single value is associated with any given field name). Each value is |
paulb@249 | 384 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 385 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 386 | |
paulb@249 | 387 | Where a given field name is used in both the path and message body to |
paulb@249 | 388 | specify values, the values from both sources will be combined into a |
paulb@249 | 389 | single list associated with that field name. |
paulb@249 | 390 | """ |
paulb@249 | 391 | |
paulb@249 | 392 | raise NotImplementedError, "get_fields" |
paulb@249 | 393 | |
paulb@45 | 394 | def get_user(self): |
paulb@1 | 395 | |
paulb@1 | 396 | """ |
paulb@186 | 397 | Extracts user information from the transaction. |
paulb@115 | 398 | |
paulb@115 | 399 | Returns a username as a string or None if no user is defined. |
paulb@1 | 400 | """ |
paulb@1 | 401 | |
paulb@45 | 402 | raise NotImplementedError, "get_user" |
paulb@1 | 403 | |
paulb@98 | 404 | def get_cookies(self): |
paulb@98 | 405 | |
paulb@98 | 406 | """ |
paulb@186 | 407 | Obtains cookie information from the request. |
paulb@98 | 408 | |
paulb@98 | 409 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 410 | """ |
paulb@98 | 411 | |
paulb@98 | 412 | raise NotImplementedError, "get_cookies" |
paulb@98 | 413 | |
paulb@104 | 414 | def get_cookie(self, cookie_name): |
paulb@104 | 415 | |
paulb@104 | 416 | """ |
paulb@186 | 417 | Obtains cookie information from the request. |
paulb@104 | 418 | |
paulb@104 | 419 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 420 | cookie exists. |
paulb@104 | 421 | """ |
paulb@104 | 422 | |
paulb@104 | 423 | raise NotImplementedError, "get_cookie" |
paulb@104 | 424 | |
paulb@1 | 425 | # Response-related methods. |
paulb@1 | 426 | |
paulb@1 | 427 | def get_response_stream(self): |
paulb@1 | 428 | |
paulb@1 | 429 | """ |
paulb@186 | 430 | Returns the response stream for the transaction. |
paulb@1 | 431 | """ |
paulb@1 | 432 | |
paulb@1 | 433 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 434 | |
paulb@252 | 435 | def get_response_stream_encoding(self): |
paulb@252 | 436 | |
paulb@252 | 437 | """ |
paulb@252 | 438 | Returns the response stream encoding. |
paulb@252 | 439 | """ |
paulb@252 | 440 | |
paulb@252 | 441 | raise NotImplementedError, "get_response_stream_encoding" |
paulb@252 | 442 | |
paulb@42 | 443 | def get_response_code(self): |
paulb@42 | 444 | |
paulb@42 | 445 | """ |
paulb@42 | 446 | Get the response code associated with the transaction. If no response |
paulb@42 | 447 | code is defined, None is returned. |
paulb@42 | 448 | """ |
paulb@42 | 449 | |
paulb@42 | 450 | raise NotImplementedError, "get_response_code" |
paulb@42 | 451 | |
paulb@42 | 452 | def set_response_code(self, response_code): |
paulb@42 | 453 | |
paulb@42 | 454 | """ |
paulb@42 | 455 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 456 | specification. |
paulb@42 | 457 | """ |
paulb@42 | 458 | |
paulb@42 | 459 | raise NotImplementedError, "set_response_code" |
paulb@42 | 460 | |
paulb@67 | 461 | def set_header_value(self, header, value): |
paulb@49 | 462 | |
paulb@49 | 463 | """ |
paulb@49 | 464 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 465 | """ |
paulb@49 | 466 | |
paulb@67 | 467 | raise NotImplementedError, "set_header_value" |
paulb@49 | 468 | |
paulb@1 | 469 | def set_content_type(self, content_type): |
paulb@1 | 470 | |
paulb@1 | 471 | """ |
paulb@186 | 472 | Sets the 'content_type' for the response. |
paulb@1 | 473 | """ |
paulb@1 | 474 | |
paulb@1 | 475 | raise NotImplementedError, "set_content_type" |
paulb@1 | 476 | |
paulb@98 | 477 | # Higher level response-related methods. |
paulb@98 | 478 | |
paulb@98 | 479 | def set_cookie(self, cookie): |
paulb@98 | 480 | |
paulb@98 | 481 | """ |
paulb@186 | 482 | Stores the given 'cookie' object in the response. |
paulb@98 | 483 | """ |
paulb@98 | 484 | |
paulb@98 | 485 | raise NotImplementedError, "set_cookie" |
paulb@98 | 486 | |
paulb@98 | 487 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 488 | |
paulb@98 | 489 | """ |
paulb@186 | 490 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 491 | |
paulb@98 | 492 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 493 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 494 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 495 | """ |
paulb@98 | 496 | |
paulb@98 | 497 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 498 | |
paulb@98 | 499 | def delete_cookie(self, cookie_name): |
paulb@98 | 500 | |
paulb@98 | 501 | """ |
paulb@186 | 502 | Adds to the response a request that the cookie with the given |
paulb@186 | 503 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 504 | """ |
paulb@98 | 505 | |
paulb@98 | 506 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 507 | |
paulb@183 | 508 | # Session-related methods. |
paulb@183 | 509 | |
paulb@183 | 510 | def get_session(self, create=1): |
paulb@183 | 511 | |
paulb@183 | 512 | """ |
paulb@186 | 513 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 514 | transaction. |
paulb@183 | 515 | |
paulb@183 | 516 | If no session has yet been established according to information |
paulb@183 | 517 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 518 | determines whether a new session will be established. |
paulb@183 | 519 | |
paulb@183 | 520 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 521 | then None is returned. In all other cases, a session object is created |
paulb@183 | 522 | (where appropriate) and returned. |
paulb@183 | 523 | """ |
paulb@183 | 524 | |
paulb@183 | 525 | raise NotImplementedError, "get_session" |
paulb@183 | 526 | |
paulb@183 | 527 | def expire_session(self): |
paulb@183 | 528 | |
paulb@183 | 529 | """ |
paulb@186 | 530 | Expires any session established according to information provided in the |
paulb@186 | 531 | transaction. |
paulb@183 | 532 | """ |
paulb@183 | 533 | |
paulb@183 | 534 | raise NotImplementedError, "expire_session" |
paulb@183 | 535 | |
paulb@128 | 536 | # Application-specific methods. |
paulb@128 | 537 | |
paulb@128 | 538 | def set_user(self, username): |
paulb@128 | 539 | |
paulb@128 | 540 | """ |
paulb@128 | 541 | An application-specific method which sets the user information with |
paulb@128 | 542 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 543 | 'get_user'. |
paulb@128 | 544 | """ |
paulb@128 | 545 | |
paulb@311 | 546 | self.user = username |
paulb@311 | 547 | |
paulb@316 | 548 | def set_virtual_path_info(self, path_info): |
paulb@311 | 549 | |
paulb@311 | 550 | """ |
paulb@311 | 551 | An application-specific method which sets the 'path_info' in the |
paulb@316 | 552 | transaction. This affects subsequent calls to 'get_virtual_path_info'. |
paulb@311 | 553 | """ |
paulb@311 | 554 | |
paulb@311 | 555 | self.path_info = path_info |
paulb@128 | 556 | |
paulb@316 | 557 | def get_virtual_path_info(self): |
paulb@316 | 558 | |
paulb@316 | 559 | """ |
paulb@316 | 560 | An application-specific method which either returns path info set in the |
paulb@316 | 561 | 'set_virtual_path_info' method, or the normal path info found in the |
paulb@316 | 562 | request. |
paulb@316 | 563 | """ |
paulb@316 | 564 | |
paulb@316 | 565 | return self.path_info or self.get_path_info() |
paulb@316 | 566 | |
paulb@1 | 567 | class Resource: |
paulb@1 | 568 | |
paulb@1 | 569 | "A generic resource interface." |
paulb@1 | 570 | |
paulb@3 | 571 | def respond(self, trans): |
paulb@3 | 572 | |
paulb@3 | 573 | """ |
paulb@3 | 574 | An application-specific method which performs activities on the basis of |
paulb@3 | 575 | the transaction object 'trans'. |
paulb@3 | 576 | """ |
paulb@3 | 577 | |
paulb@3 | 578 | raise NotImplementedError, "respond" |
paulb@1 | 579 | |
paulb@75 | 580 | class Authenticator: |
paulb@75 | 581 | |
paulb@75 | 582 | "A generic authentication component." |
paulb@75 | 583 | |
paulb@75 | 584 | def authenticate(self, trans): |
paulb@75 | 585 | |
paulb@75 | 586 | """ |
paulb@75 | 587 | An application-specific method which authenticates the sender of the |
paulb@75 | 588 | request described by the transaction object 'trans'. This method should |
paulb@75 | 589 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 590 | the transaction. |
paulb@75 | 591 | |
paulb@75 | 592 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 593 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 594 | evaluates to false. |
paulb@75 | 595 | """ |
paulb@75 | 596 | |
paulb@75 | 597 | raise NotImplementedError, "authenticate" |
paulb@75 | 598 | |
paulb@75 | 599 | def get_auth_type(self): |
paulb@75 | 600 | |
paulb@75 | 601 | """ |
paulb@75 | 602 | An application-specific method which returns the authentication type to |
paulb@75 | 603 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 604 | authentication. |
paulb@75 | 605 | """ |
paulb@75 | 606 | |
paulb@75 | 607 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 608 | |
paulb@75 | 609 | def get_realm(self): |
paulb@75 | 610 | |
paulb@75 | 611 | """ |
paulb@75 | 612 | An application-specific method which returns the name of the realm for |
paulb@75 | 613 | which authentication is taking place. |
paulb@75 | 614 | """ |
paulb@75 | 615 | |
paulb@75 | 616 | raise NotImplementedError, "get_realm" |
paulb@75 | 617 | |
paulb@1 | 618 | # vim: tabstop=4 expandtab shiftwidth=4 |