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