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@1 | 18 | class ContentType: |
paulb@1 | 19 | |
paulb@1 | 20 | "A container for content type information." |
paulb@1 | 21 | |
paulb@193 | 22 | def __init__(self, content_type, charset=None, attributes=None): |
paulb@219 | 23 | |
paulb@219 | 24 | """ |
paulb@219 | 25 | Initialise the container with the given principal 'content_type', an |
paulb@219 | 26 | optional 'charset' (which otherwise uses "utf-8" as default), and |
paulb@219 | 27 | optional 'attributes' (a list of 2-tuples each representing the |
paulb@219 | 28 | key=value pairs which qualify content types). |
paulb@219 | 29 | """ |
paulb@219 | 30 | |
paulb@1 | 31 | self.content_type = content_type |
paulb@219 | 32 | self.charset = charset or "utf-8" |
paulb@193 | 33 | self.attributes = attributes |
paulb@1 | 34 | |
paulb@1 | 35 | class Transaction: |
paulb@1 | 36 | |
paulb@1 | 37 | """ |
paulb@1 | 38 | A generic transaction interface containing framework-specific methods to be |
paulb@1 | 39 | overridden. |
paulb@1 | 40 | """ |
paulb@1 | 41 | |
paulb@28 | 42 | def commit(self): |
paulb@28 | 43 | |
paulb@28 | 44 | """ |
paulb@28 | 45 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 46 | objects. |
paulb@28 | 47 | """ |
paulb@28 | 48 | |
paulb@28 | 49 | pass |
paulb@28 | 50 | |
paulb@1 | 51 | # Utility methods. |
paulb@1 | 52 | |
paulb@1 | 53 | def parse_content_type(self, content_type_field): |
paulb@1 | 54 | |
paulb@1 | 55 | """ |
paulb@1 | 56 | Determine the content type and charset from the supplied |
paulb@1 | 57 | 'content_type_field' string. |
paulb@1 | 58 | """ |
paulb@1 | 59 | |
paulb@1 | 60 | if content_type_field is None: |
paulb@1 | 61 | return ContentType(None, "iso-8859-1") |
paulb@1 | 62 | |
paulb@193 | 63 | l = content_type_field.split(";") |
paulb@193 | 64 | content_type_attributes = [] |
paulb@193 | 65 | content_type_charset = "iso-8859-1" |
paulb@193 | 66 | |
paulb@193 | 67 | # Find the charset and remember all other attributes. |
paulb@193 | 68 | |
paulb@193 | 69 | content_type_str, attributes = l[0].strip(), l[1:] |
paulb@193 | 70 | |
paulb@193 | 71 | for attribute in attributes: |
paulb@193 | 72 | t = attribute.strip().split("=") |
paulb@193 | 73 | if t[0] == "charset" and len(t) > 1: |
paulb@193 | 74 | content_type_charset = t[1] |
paulb@193 | 75 | else: |
paulb@193 | 76 | content_type_attributes.append(t) |
paulb@193 | 77 | |
paulb@193 | 78 | return ContentType(content_type_str, content_type_charset, content_type_attributes) |
paulb@1 | 79 | |
paulb@5 | 80 | def format_content_type(self, content_type): |
paulb@5 | 81 | |
paulb@5 | 82 | """ |
paulb@5 | 83 | Format the given 'content_type' object, producing a string suitable for |
paulb@5 | 84 | the response header field. |
paulb@5 | 85 | """ |
paulb@5 | 86 | |
paulb@5 | 87 | if content_type.charset: |
paulb@5 | 88 | field = "%s; charset=%s" % (content_type.content_type, content_type.charset) |
paulb@5 | 89 | else: |
paulb@5 | 90 | field = content_type.content_type |
paulb@5 | 91 | |
paulb@5 | 92 | # Make sure that only ASCII is used in the header. |
paulb@5 | 93 | |
paulb@5 | 94 | return field.encode("US-ASCII") |
paulb@5 | 95 | |
paulb@49 | 96 | def format_header_value(self, value): |
paulb@49 | 97 | |
paulb@49 | 98 | """ |
paulb@49 | 99 | Format the given header 'value'. Typically, this just ensures the usage |
paulb@49 | 100 | of US-ASCII. |
paulb@49 | 101 | """ |
paulb@49 | 102 | |
paulb@49 | 103 | return value.encode("US-ASCII") |
paulb@49 | 104 | |
paulb@1 | 105 | def parse_content_preferences(self, accept_preference): |
paulb@1 | 106 | |
paulb@1 | 107 | """ |
paulb@1 | 108 | Returns the preferences as requested by the user agent. The preferences are |
paulb@1 | 109 | returned as a list of codes in the same order as they appeared in the |
paulb@1 | 110 | appropriate environment variable. In other words, the explicit weighting |
paulb@1 | 111 | criteria are ignored. |
paulb@1 | 112 | |
paulb@1 | 113 | As the 'accept_preference' parameter, values for language and charset |
paulb@1 | 114 | preferences are appropriate. |
paulb@1 | 115 | """ |
paulb@1 | 116 | |
paulb@108 | 117 | if accept_preference is None: |
paulb@108 | 118 | return [] |
paulb@108 | 119 | |
paulb@1 | 120 | accept_defs = accept_preference.split(",") |
paulb@1 | 121 | accept_prefs = [] |
paulb@1 | 122 | for accept_def in accept_defs: |
paulb@1 | 123 | t = accept_def.split(";") |
paulb@1 | 124 | if len(t) >= 1: |
paulb@1 | 125 | accept_prefs.append(t[0].strip()) |
paulb@1 | 126 | return accept_prefs |
paulb@1 | 127 | |
paulb@65 | 128 | def convert_to_list(self, value): |
paulb@65 | 129 | |
paulb@65 | 130 | """ |
paulb@65 | 131 | Returns a single element list containing 'value' if it is not itself a list, a |
paulb@65 | 132 | tuple, or None. If 'value' is a list then it is itself returned; if 'value' is a |
paulb@65 | 133 | tuple then a new list containing the same elements is returned; if 'value' is None |
paulb@65 | 134 | then an empty list is returned. |
paulb@65 | 135 | """ |
paulb@65 | 136 | |
paulb@65 | 137 | if type(value) == type([]): |
paulb@65 | 138 | return value |
paulb@65 | 139 | elif type(value) == type(()): |
paulb@65 | 140 | return list(value) |
paulb@65 | 141 | elif value is None: |
paulb@65 | 142 | return [] |
paulb@65 | 143 | else: |
paulb@65 | 144 | return [value] |
paulb@65 | 145 | |
paulb@1 | 146 | # Request-related methods. |
paulb@1 | 147 | |
paulb@1 | 148 | def get_request_stream(self): |
paulb@1 | 149 | |
paulb@1 | 150 | """ |
paulb@186 | 151 | Returns the request stream for the transaction. |
paulb@1 | 152 | """ |
paulb@1 | 153 | |
paulb@1 | 154 | raise NotImplementedError, "get_request_stream" |
paulb@1 | 155 | |
paulb@1 | 156 | def get_request_method(self): |
paulb@1 | 157 | |
paulb@1 | 158 | """ |
paulb@186 | 159 | Returns the request method. |
paulb@1 | 160 | """ |
paulb@1 | 161 | |
paulb@1 | 162 | raise NotImplementedError, "get_request_method" |
paulb@1 | 163 | |
paulb@1 | 164 | def get_headers(self): |
paulb@1 | 165 | |
paulb@1 | 166 | """ |
paulb@186 | 167 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 168 | names to values. |
paulb@1 | 169 | """ |
paulb@1 | 170 | |
paulb@1 | 171 | raise NotImplementedError, "get_headers" |
paulb@1 | 172 | |
paulb@65 | 173 | def get_header_values(self, key): |
paulb@65 | 174 | |
paulb@65 | 175 | """ |
paulb@186 | 176 | Returns a list of all request header values associated with the given |
paulb@186 | 177 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 178 | case-insensitive string. |
paulb@65 | 179 | """ |
paulb@65 | 180 | |
paulb@65 | 181 | raise NotImplementedError, "get_header_values" |
paulb@65 | 182 | |
paulb@1 | 183 | def get_content_type(self): |
paulb@1 | 184 | |
paulb@1 | 185 | """ |
paulb@186 | 186 | Returns the content type specified on the request, along with the |
paulb@186 | 187 | charset employed. |
paulb@1 | 188 | """ |
paulb@1 | 189 | |
paulb@1 | 190 | raise NotImplementedError, "get_content_type" |
paulb@1 | 191 | |
paulb@1 | 192 | def get_content_charsets(self): |
paulb@1 | 193 | |
paulb@1 | 194 | """ |
paulb@1 | 195 | Returns the character set preferences. |
paulb@1 | 196 | """ |
paulb@1 | 197 | |
paulb@1 | 198 | raise NotImplementedError, "get_content_charsets" |
paulb@1 | 199 | |
paulb@1 | 200 | def get_content_languages(self): |
paulb@1 | 201 | |
paulb@1 | 202 | """ |
paulb@186 | 203 | Returns extracted language information from the transaction. |
paulb@1 | 204 | """ |
paulb@1 | 205 | |
paulb@1 | 206 | raise NotImplementedError, "get_content_languages" |
paulb@1 | 207 | |
paulb@5 | 208 | def get_path(self): |
paulb@5 | 209 | |
paulb@5 | 210 | """ |
paulb@186 | 211 | Returns the entire path from the request. |
paulb@5 | 212 | """ |
paulb@5 | 213 | |
paulb@5 | 214 | raise NotImplementedError, "get_path" |
paulb@5 | 215 | |
paulb@162 | 216 | def get_path_without_query(self): |
paulb@162 | 217 | |
paulb@162 | 218 | """ |
paulb@186 | 219 | Returns the entire path from the request minus the query string. |
paulb@162 | 220 | """ |
paulb@162 | 221 | |
paulb@162 | 222 | raise NotImplementedError, "get_path_without_query" |
paulb@162 | 223 | |
paulb@1 | 224 | def get_path_info(self): |
paulb@1 | 225 | |
paulb@1 | 226 | """ |
paulb@186 | 227 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 228 | handling the current request) from the request. |
paulb@1 | 229 | """ |
paulb@1 | 230 | |
paulb@1 | 231 | raise NotImplementedError, "get_path_info" |
paulb@1 | 232 | |
paulb@69 | 233 | def get_query_string(self): |
paulb@69 | 234 | |
paulb@69 | 235 | """ |
paulb@186 | 236 | Returns the query string from the path in the request. |
paulb@69 | 237 | """ |
paulb@69 | 238 | |
paulb@69 | 239 | raise NotImplementedError, "get_query_string" |
paulb@69 | 240 | |
paulb@69 | 241 | # Higher level request-related methods. |
paulb@69 | 242 | |
paulb@69 | 243 | def get_fields_from_path(self): |
paulb@69 | 244 | |
paulb@69 | 245 | """ |
paulb@186 | 246 | Extracts the form fields from the path specified in the transaction. The |
paulb@186 | 247 | underlying framework may refuse to supply fields from the path if |
paulb@186 | 248 | handling a POST transaction. |
paulb@69 | 249 | |
paulb@71 | 250 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 251 | single value is associated with any given field name). |
paulb@69 | 252 | """ |
paulb@69 | 253 | |
paulb@69 | 254 | raise NotImplementedError, "get_fields_from_path" |
paulb@69 | 255 | |
paulb@167 | 256 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 257 | |
paulb@69 | 258 | """ |
paulb@186 | 259 | Extracts the form fields from the message body in the transaction. The |
paulb@186 | 260 | optional 'encoding' parameter specifies the character encoding of the |
paulb@186 | 261 | message body for cases where no such information is available, but where |
paulb@186 | 262 | the default encoding is to be overridden. |
paulb@69 | 263 | |
paulb@73 | 264 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 265 | single value is associated with any given field name). Each value is |
paulb@193 | 266 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 267 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 268 | """ |
paulb@69 | 269 | |
paulb@69 | 270 | raise NotImplementedError, "get_fields_from_body" |
paulb@69 | 271 | |
paulb@45 | 272 | def get_user(self): |
paulb@1 | 273 | |
paulb@1 | 274 | """ |
paulb@186 | 275 | Extracts user information from the transaction. |
paulb@115 | 276 | |
paulb@115 | 277 | Returns a username as a string or None if no user is defined. |
paulb@1 | 278 | """ |
paulb@1 | 279 | |
paulb@45 | 280 | raise NotImplementedError, "get_user" |
paulb@1 | 281 | |
paulb@98 | 282 | def get_cookies(self): |
paulb@98 | 283 | |
paulb@98 | 284 | """ |
paulb@186 | 285 | Obtains cookie information from the request. |
paulb@98 | 286 | |
paulb@98 | 287 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 288 | """ |
paulb@98 | 289 | |
paulb@98 | 290 | raise NotImplementedError, "get_cookies" |
paulb@98 | 291 | |
paulb@104 | 292 | def get_cookie(self, cookie_name): |
paulb@104 | 293 | |
paulb@104 | 294 | """ |
paulb@186 | 295 | Obtains cookie information from the request. |
paulb@104 | 296 | |
paulb@104 | 297 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 298 | cookie exists. |
paulb@104 | 299 | """ |
paulb@104 | 300 | |
paulb@104 | 301 | raise NotImplementedError, "get_cookie" |
paulb@104 | 302 | |
paulb@1 | 303 | # Response-related methods. |
paulb@1 | 304 | |
paulb@1 | 305 | def get_response_stream(self): |
paulb@1 | 306 | |
paulb@1 | 307 | """ |
paulb@186 | 308 | Returns the response stream for the transaction. |
paulb@1 | 309 | """ |
paulb@1 | 310 | |
paulb@1 | 311 | raise NotImplementedError, "get_response_stream" |
paulb@1 | 312 | |
paulb@42 | 313 | def get_response_code(self): |
paulb@42 | 314 | |
paulb@42 | 315 | """ |
paulb@42 | 316 | Get the response code associated with the transaction. If no response |
paulb@42 | 317 | code is defined, None is returned. |
paulb@42 | 318 | """ |
paulb@42 | 319 | |
paulb@42 | 320 | raise NotImplementedError, "get_response_code" |
paulb@42 | 321 | |
paulb@42 | 322 | def set_response_code(self, response_code): |
paulb@42 | 323 | |
paulb@42 | 324 | """ |
paulb@42 | 325 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 326 | specification. |
paulb@42 | 327 | """ |
paulb@42 | 328 | |
paulb@42 | 329 | raise NotImplementedError, "set_response_code" |
paulb@42 | 330 | |
paulb@67 | 331 | def set_header_value(self, header, value): |
paulb@49 | 332 | |
paulb@49 | 333 | """ |
paulb@49 | 334 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 335 | """ |
paulb@49 | 336 | |
paulb@67 | 337 | raise NotImplementedError, "set_header_value" |
paulb@49 | 338 | |
paulb@1 | 339 | def set_content_type(self, content_type): |
paulb@1 | 340 | |
paulb@1 | 341 | """ |
paulb@186 | 342 | Sets the 'content_type' for the response. |
paulb@1 | 343 | """ |
paulb@1 | 344 | |
paulb@1 | 345 | raise NotImplementedError, "set_content_type" |
paulb@1 | 346 | |
paulb@98 | 347 | # Higher level response-related methods. |
paulb@98 | 348 | |
paulb@98 | 349 | def set_cookie(self, cookie): |
paulb@98 | 350 | |
paulb@98 | 351 | """ |
paulb@186 | 352 | Stores the given 'cookie' object in the response. |
paulb@98 | 353 | """ |
paulb@98 | 354 | |
paulb@98 | 355 | raise NotImplementedError, "set_cookie" |
paulb@98 | 356 | |
paulb@98 | 357 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 358 | |
paulb@98 | 359 | """ |
paulb@186 | 360 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 361 | |
paulb@98 | 362 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 363 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 364 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 365 | """ |
paulb@98 | 366 | |
paulb@98 | 367 | raise NotImplementedError, "set_cookie_value" |
paulb@98 | 368 | |
paulb@98 | 369 | def delete_cookie(self, cookie_name): |
paulb@98 | 370 | |
paulb@98 | 371 | """ |
paulb@186 | 372 | Adds to the response a request that the cookie with the given |
paulb@186 | 373 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 374 | """ |
paulb@98 | 375 | |
paulb@98 | 376 | raise NotImplementedError, "delete_cookie" |
paulb@98 | 377 | |
paulb@183 | 378 | # Session-related methods. |
paulb@183 | 379 | |
paulb@183 | 380 | def get_session(self, create=1): |
paulb@183 | 381 | |
paulb@183 | 382 | """ |
paulb@186 | 383 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 384 | transaction. |
paulb@183 | 385 | |
paulb@183 | 386 | If no session has yet been established according to information |
paulb@183 | 387 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 388 | determines whether a new session will be established. |
paulb@183 | 389 | |
paulb@183 | 390 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 391 | then None is returned. In all other cases, a session object is created |
paulb@183 | 392 | (where appropriate) and returned. |
paulb@183 | 393 | """ |
paulb@183 | 394 | |
paulb@183 | 395 | raise NotImplementedError, "get_session" |
paulb@183 | 396 | |
paulb@183 | 397 | def expire_session(self): |
paulb@183 | 398 | |
paulb@183 | 399 | """ |
paulb@186 | 400 | Expires any session established according to information provided in the |
paulb@186 | 401 | transaction. |
paulb@183 | 402 | """ |
paulb@183 | 403 | |
paulb@183 | 404 | raise NotImplementedError, "expire_session" |
paulb@183 | 405 | |
paulb@128 | 406 | # Application-specific methods. |
paulb@128 | 407 | |
paulb@128 | 408 | def set_user(self, username): |
paulb@128 | 409 | |
paulb@128 | 410 | """ |
paulb@128 | 411 | An application-specific method which sets the user information with |
paulb@128 | 412 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 413 | 'get_user'. |
paulb@128 | 414 | """ |
paulb@128 | 415 | |
paulb@128 | 416 | raise NotImplementedError, "set_user" |
paulb@128 | 417 | |
paulb@1 | 418 | class Resource: |
paulb@1 | 419 | |
paulb@1 | 420 | "A generic resource interface." |
paulb@1 | 421 | |
paulb@3 | 422 | def respond(self, trans): |
paulb@3 | 423 | |
paulb@3 | 424 | """ |
paulb@3 | 425 | An application-specific method which performs activities on the basis of |
paulb@3 | 426 | the transaction object 'trans'. |
paulb@3 | 427 | """ |
paulb@3 | 428 | |
paulb@3 | 429 | raise NotImplementedError, "respond" |
paulb@1 | 430 | |
paulb@75 | 431 | class Authenticator: |
paulb@75 | 432 | |
paulb@75 | 433 | "A generic authentication component." |
paulb@75 | 434 | |
paulb@75 | 435 | def authenticate(self, trans): |
paulb@75 | 436 | |
paulb@75 | 437 | """ |
paulb@75 | 438 | An application-specific method which authenticates the sender of the |
paulb@75 | 439 | request described by the transaction object 'trans'. This method should |
paulb@75 | 440 | consider 'trans' to be read-only and not attempt to change the state of |
paulb@75 | 441 | the transaction. |
paulb@75 | 442 | |
paulb@75 | 443 | If the sender of the request is authenticated successfully, the result |
paulb@75 | 444 | of this method evaluates to true; otherwise the result of this method |
paulb@75 | 445 | evaluates to false. |
paulb@75 | 446 | """ |
paulb@75 | 447 | |
paulb@75 | 448 | raise NotImplementedError, "authenticate" |
paulb@75 | 449 | |
paulb@75 | 450 | def get_auth_type(self): |
paulb@75 | 451 | |
paulb@75 | 452 | """ |
paulb@75 | 453 | An application-specific method which returns the authentication type to |
paulb@75 | 454 | be used. An example value is 'Basic' which specifies HTTP basic |
paulb@75 | 455 | authentication. |
paulb@75 | 456 | """ |
paulb@75 | 457 | |
paulb@75 | 458 | raise NotImplementedError, "get_auth_type" |
paulb@75 | 459 | |
paulb@75 | 460 | def get_realm(self): |
paulb@75 | 461 | |
paulb@75 | 462 | """ |
paulb@75 | 463 | An application-specific method which returns the name of the realm for |
paulb@75 | 464 | which authentication is taking place. |
paulb@75 | 465 | """ |
paulb@75 | 466 | |
paulb@75 | 467 | raise NotImplementedError, "get_realm" |
paulb@75 | 468 | |
paulb@1 | 469 | # vim: tabstop=4 expandtab shiftwidth=4 |