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