paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | mod_python classes. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import Generic |
paulb@198 | 8 | from Helpers.Request import get_body_field |
paulb@167 | 9 | from Helpers.Response import ConvertingStream |
paulb@71 | 10 | from mod_python.util import parse_qs, FieldStorage |
paulb@42 | 11 | from mod_python import apache |
paulb@183 | 12 | |
paulb@239 | 13 | # NOTE: Provide an alternative implementation for the cookie support. |
paulb@239 | 14 | # NOTE: The alternative session support requires cookie support. |
paulb@183 | 15 | |
paulb@183 | 16 | try: from mod_python import Cookie |
paulb@183 | 17 | except ImportError: Cookie = None |
paulb@239 | 18 | try: |
paulb@239 | 19 | from mod_python import Session |
paulb@239 | 20 | except ImportError: |
paulb@239 | 21 | from Helpers.Session import SessionStore |
paulb@239 | 22 | import os |
paulb@239 | 23 | Session = None |
paulb@1 | 24 | |
paulb@1 | 25 | class Transaction(Generic.Transaction): |
paulb@1 | 26 | |
paulb@1 | 27 | """ |
paulb@1 | 28 | mod_python transaction interface. |
paulb@1 | 29 | """ |
paulb@1 | 30 | |
paulb@1 | 31 | def __init__(self, trans): |
paulb@1 | 32 | |
paulb@1 | 33 | "Initialise the transaction using the mod_python transaction 'trans'." |
paulb@1 | 34 | |
paulb@1 | 35 | self.trans = trans |
paulb@42 | 36 | self.response_code = apache.OK |
paulb@128 | 37 | self.user = None |
paulb@167 | 38 | self.content_type = None |
paulb@1 | 39 | |
paulb@133 | 40 | # Cached information. |
paulb@133 | 41 | |
paulb@133 | 42 | self.storage_body = None |
paulb@133 | 43 | |
paulb@239 | 44 | # Special objects retained throughout the transaction. |
paulb@239 | 45 | |
paulb@239 | 46 | self.session_store = None |
paulb@239 | 47 | |
paulb@239 | 48 | def commit(self): |
paulb@239 | 49 | |
paulb@239 | 50 | """ |
paulb@239 | 51 | A special method, synchronising the transaction with framework-specific |
paulb@239 | 52 | objects. |
paulb@239 | 53 | """ |
paulb@239 | 54 | |
paulb@239 | 55 | # Close the session store. |
paulb@239 | 56 | |
paulb@239 | 57 | if self.session_store is not None: |
paulb@239 | 58 | self.session_store.close() |
paulb@239 | 59 | |
paulb@1 | 60 | # Request-related methods. |
paulb@1 | 61 | |
paulb@1 | 62 | def get_request_stream(self): |
paulb@1 | 63 | |
paulb@1 | 64 | """ |
paulb@186 | 65 | Returns the request stream for the transaction. |
paulb@1 | 66 | """ |
paulb@1 | 67 | |
paulb@1 | 68 | return self.trans |
paulb@1 | 69 | |
paulb@1 | 70 | def get_request_method(self): |
paulb@1 | 71 | |
paulb@1 | 72 | """ |
paulb@186 | 73 | Returns the request method. |
paulb@1 | 74 | """ |
paulb@1 | 75 | |
paulb@1 | 76 | return self.trans.method |
paulb@1 | 77 | |
paulb@1 | 78 | def get_headers(self): |
paulb@1 | 79 | |
paulb@1 | 80 | """ |
paulb@186 | 81 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 82 | names to values. |
paulb@186 | 83 | |
paulb@91 | 84 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 85 | NOTE: need to change. |
paulb@1 | 86 | """ |
paulb@1 | 87 | |
paulb@1 | 88 | return self.trans.headers_in |
paulb@1 | 89 | |
paulb@65 | 90 | def get_header_values(self, key): |
paulb@65 | 91 | |
paulb@65 | 92 | """ |
paulb@186 | 93 | Returns a list of all request header values associated with the given |
paulb@186 | 94 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 95 | case-insensitive string. |
paulb@65 | 96 | """ |
paulb@65 | 97 | |
paulb@65 | 98 | return self.convert_to_list(self.trans.headers_in.get(key)) |
paulb@65 | 99 | |
paulb@1 | 100 | def get_content_type(self): |
paulb@1 | 101 | |
paulb@1 | 102 | """ |
paulb@186 | 103 | Returns the content type specified on the request, along with the |
paulb@186 | 104 | charset employed. |
paulb@1 | 105 | """ |
paulb@1 | 106 | |
paulb@1 | 107 | return self.parse_content_type(self.trans.content_type) |
paulb@1 | 108 | |
paulb@1 | 109 | def get_content_charsets(self): |
paulb@1 | 110 | |
paulb@1 | 111 | """ |
paulb@1 | 112 | Returns the character set preferences. |
paulb@1 | 113 | """ |
paulb@1 | 114 | |
paulb@108 | 115 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Charset")) |
paulb@1 | 116 | |
paulb@1 | 117 | def get_content_languages(self): |
paulb@1 | 118 | |
paulb@1 | 119 | """ |
paulb@186 | 120 | Returns extracted language information from the transaction. |
paulb@1 | 121 | """ |
paulb@1 | 122 | |
paulb@108 | 123 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Language")) |
paulb@1 | 124 | |
paulb@5 | 125 | def get_path(self): |
paulb@5 | 126 | |
paulb@5 | 127 | """ |
paulb@186 | 128 | Returns the entire path from the request. |
paulb@5 | 129 | """ |
paulb@5 | 130 | |
paulb@144 | 131 | query_string = self.get_query_string() |
paulb@144 | 132 | if query_string: |
paulb@144 | 133 | return self.trans.uri + "?" + query_string |
paulb@144 | 134 | else: |
paulb@144 | 135 | return self.trans.uri |
paulb@5 | 136 | |
paulb@162 | 137 | def get_path_without_query(self): |
paulb@162 | 138 | |
paulb@162 | 139 | """ |
paulb@186 | 140 | Returns the entire path from the request minus the query string. |
paulb@162 | 141 | """ |
paulb@162 | 142 | |
paulb@162 | 143 | return self.trans.uri |
paulb@162 | 144 | |
paulb@1 | 145 | def get_path_info(self): |
paulb@1 | 146 | |
paulb@1 | 147 | """ |
paulb@186 | 148 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 149 | handling the current request) from the request. |
paulb@1 | 150 | """ |
paulb@1 | 151 | |
paulb@10 | 152 | return self.trans.path_info |
paulb@1 | 153 | |
paulb@69 | 154 | def get_query_string(self): |
paulb@69 | 155 | |
paulb@69 | 156 | """ |
paulb@186 | 157 | Returns the query string from the path in the request. |
paulb@69 | 158 | """ |
paulb@69 | 159 | |
paulb@73 | 160 | return self.trans.args or "" |
paulb@69 | 161 | |
paulb@1 | 162 | # Higher level request-related methods. |
paulb@1 | 163 | |
paulb@69 | 164 | def get_fields_from_path(self): |
paulb@69 | 165 | |
paulb@69 | 166 | """ |
paulb@186 | 167 | Extracts the form fields from the path specified in the transaction. The |
paulb@186 | 168 | underlying framework may refuse to supply fields from the path if |
paulb@186 | 169 | handling a POST transaction. |
paulb@69 | 170 | |
paulb@71 | 171 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 172 | single value is associated with any given field name). |
paulb@69 | 173 | """ |
paulb@69 | 174 | |
paulb@71 | 175 | return parse_qs(self.get_query_string(), 1) # keep_blank_values=1 |
paulb@69 | 176 | |
paulb@167 | 177 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 178 | |
paulb@69 | 179 | """ |
paulb@186 | 180 | Extracts the form fields from the message body in the transaction. The |
paulb@186 | 181 | optional 'encoding' parameter specifies the character encoding of the |
paulb@186 | 182 | message body for cases where no such information is available, but where |
paulb@186 | 183 | the default encoding is to be overridden. |
paulb@69 | 184 | |
paulb@73 | 185 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 186 | single value is associated with any given field name). Each value is |
paulb@193 | 187 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 188 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 189 | |
paulb@69 | 190 | The mod_python.util.FieldStorage class may augment the fields from the |
paulb@69 | 191 | body with fields found in the path. |
paulb@69 | 192 | """ |
paulb@69 | 193 | |
paulb@224 | 194 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@167 | 195 | |
paulb@133 | 196 | if self.storage_body is None: |
paulb@133 | 197 | self.storage_body = FieldStorage(self.trans, keep_blank_values=1) |
paulb@69 | 198 | |
paulb@73 | 199 | # Traverse the storage, finding each field value. |
paulb@1 | 200 | |
paulb@73 | 201 | fields = {} |
paulb@133 | 202 | for field in self.storage_body.list: |
paulb@73 | 203 | if not fields.has_key(field.name): |
paulb@73 | 204 | fields[field.name] = [] |
paulb@198 | 205 | fields[field.name].append(get_body_field(field.value, encoding)) |
paulb@73 | 206 | return fields |
paulb@1 | 207 | |
paulb@45 | 208 | def get_user(self): |
paulb@1 | 209 | |
paulb@1 | 210 | """ |
paulb@186 | 211 | Extracts user information from the transaction. |
paulb@108 | 212 | |
paulb@108 | 213 | Returns a username as a string or None if no user is defined. |
paulb@1 | 214 | """ |
paulb@1 | 215 | |
paulb@128 | 216 | if self.user is not None: |
paulb@128 | 217 | return self.user |
paulb@128 | 218 | else: |
paulb@128 | 219 | return self.trans.user |
paulb@1 | 220 | |
paulb@98 | 221 | def get_cookies(self): |
paulb@98 | 222 | |
paulb@98 | 223 | """ |
paulb@186 | 224 | Obtains cookie information from the request. |
paulb@98 | 225 | |
paulb@98 | 226 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 227 | |
paulb@98 | 228 | NOTE: No additional information is passed to the underlying API despite |
paulb@98 | 229 | NOTE: support for enhanced cookies in mod_python. |
paulb@98 | 230 | """ |
paulb@98 | 231 | |
paulb@137 | 232 | if Cookie: |
paulb@137 | 233 | return Cookie.get_cookies(self.trans) |
paulb@137 | 234 | else: |
paulb@137 | 235 | return None |
paulb@98 | 236 | |
paulb@104 | 237 | def get_cookie(self, cookie_name): |
paulb@104 | 238 | |
paulb@104 | 239 | """ |
paulb@186 | 240 | Obtains cookie information from the request. |
paulb@104 | 241 | |
paulb@104 | 242 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 243 | cookie exists. |
paulb@104 | 244 | """ |
paulb@104 | 245 | |
paulb@104 | 246 | return self.get_cookies().get(cookie_name) |
paulb@104 | 247 | |
paulb@1 | 248 | # Response-related methods. |
paulb@1 | 249 | |
paulb@1 | 250 | def get_response_stream(self): |
paulb@1 | 251 | |
paulb@1 | 252 | """ |
paulb@186 | 253 | Returns the response stream for the transaction. |
paulb@1 | 254 | """ |
paulb@1 | 255 | |
paulb@224 | 256 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 257 | # or a default encoding. |
paulb@167 | 258 | |
paulb@167 | 259 | if self.content_type: |
paulb@224 | 260 | encoding = self.content_type.charset |
paulb@224 | 261 | encoding = encoding or self.default_charset |
paulb@167 | 262 | return ConvertingStream(self.trans, encoding) |
paulb@1 | 263 | |
paulb@42 | 264 | def get_response_code(self): |
paulb@42 | 265 | |
paulb@42 | 266 | """ |
paulb@42 | 267 | Get the response code associated with the transaction. If no response |
paulb@42 | 268 | code is defined, None is returned. |
paulb@42 | 269 | """ |
paulb@42 | 270 | |
paulb@42 | 271 | return self.response_code |
paulb@42 | 272 | |
paulb@42 | 273 | def set_response_code(self, response_code): |
paulb@42 | 274 | |
paulb@42 | 275 | """ |
paulb@42 | 276 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 277 | specification. |
paulb@42 | 278 | """ |
paulb@42 | 279 | |
paulb@42 | 280 | self.response_code = response_code |
paulb@42 | 281 | |
paulb@67 | 282 | def set_header_value(self, header, value): |
paulb@49 | 283 | |
paulb@49 | 284 | """ |
paulb@49 | 285 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 286 | """ |
paulb@49 | 287 | |
paulb@49 | 288 | self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) |
paulb@49 | 289 | |
paulb@1 | 290 | def set_content_type(self, content_type): |
paulb@1 | 291 | |
paulb@1 | 292 | """ |
paulb@186 | 293 | Sets the 'content_type' for the response. |
paulb@1 | 294 | """ |
paulb@1 | 295 | |
paulb@167 | 296 | # Remember the content type for encoding purposes later. |
paulb@167 | 297 | |
paulb@167 | 298 | self.content_type = content_type |
paulb@224 | 299 | self.trans.content_type = str(content_type) |
paulb@1 | 300 | |
paulb@186 | 301 | # Higher level response-related methods. |
paulb@186 | 302 | |
paulb@98 | 303 | def set_cookie(self, cookie): |
paulb@98 | 304 | |
paulb@98 | 305 | """ |
paulb@186 | 306 | Stores the given 'cookie' object in the response. |
paulb@98 | 307 | """ |
paulb@98 | 308 | |
paulb@98 | 309 | if Cookie: |
paulb@98 | 310 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 311 | else: |
paulb@98 | 312 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 313 | pass |
paulb@98 | 314 | |
paulb@98 | 315 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 316 | |
paulb@98 | 317 | """ |
paulb@186 | 318 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@98 | 319 | |
paulb@98 | 320 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 321 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 322 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 323 | """ |
paulb@98 | 324 | |
paulb@104 | 325 | # NOTE: We just hope that Cookie converts Unicode arguments to US-ASCII. |
paulb@104 | 326 | |
paulb@98 | 327 | if Cookie: |
paulb@98 | 328 | cookie = Cookie.Cookie(name, value) |
paulb@98 | 329 | if expires is not None: |
paulb@98 | 330 | cookie.expires = expires |
paulb@98 | 331 | if path is not None: |
paulb@98 | 332 | cookie.path = path |
paulb@98 | 333 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 334 | else: |
paulb@98 | 335 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 336 | pass |
paulb@98 | 337 | |
paulb@98 | 338 | def delete_cookie(self, cookie_name): |
paulb@98 | 339 | |
paulb@98 | 340 | """ |
paulb@186 | 341 | Adds to the response a request that the cookie with the given |
paulb@186 | 342 | 'cookie_name' be deleted/discarded by the client. |
paulb@98 | 343 | """ |
paulb@98 | 344 | |
paulb@98 | 345 | # Create a special cookie, given that we do not know whether the browser |
paulb@98 | 346 | # has been sent the cookie or not. |
paulb@98 | 347 | # NOTE: Magic discovered in Webware. |
paulb@98 | 348 | |
paulb@98 | 349 | if Cookie: |
paulb@98 | 350 | cookie = Cookie.Cookie(cookie_name, "") |
paulb@98 | 351 | cookie.path = "/" |
paulb@98 | 352 | cookie.expires = 0 |
paulb@98 | 353 | cookie.max_age = 0 |
paulb@98 | 354 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 355 | else: |
paulb@98 | 356 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 357 | pass |
paulb@98 | 358 | |
paulb@183 | 359 | # Session-related methods. |
paulb@183 | 360 | |
paulb@183 | 361 | def get_session(self, create=1): |
paulb@183 | 362 | |
paulb@183 | 363 | """ |
paulb@186 | 364 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 365 | transaction. |
paulb@183 | 366 | |
paulb@183 | 367 | If no session has yet been established according to information |
paulb@183 | 368 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 369 | determines whether a new session will be established. |
paulb@183 | 370 | |
paulb@183 | 371 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 372 | then None is returned. In all other cases, a session object is created |
paulb@183 | 373 | (where appropriate) and returned. |
paulb@183 | 374 | """ |
paulb@183 | 375 | |
paulb@183 | 376 | if Session: |
paulb@183 | 377 | # NOTE: Not exposing all functionality. |
paulb@183 | 378 | return Session.Session(self.trans) |
paulb@183 | 379 | else: |
paulb@239 | 380 | # NOTE: Requires configuration. |
paulb@239 | 381 | |
paulb@239 | 382 | if self.session_store is None: |
paulb@239 | 383 | self.session_store = SessionStore(self, os.path.join(apache.server_root(), "WebStack-sessions")) |
paulb@239 | 384 | return self.session_store.get_session(create) |
paulb@183 | 385 | |
paulb@183 | 386 | def expire_session(self): |
paulb@183 | 387 | |
paulb@183 | 388 | """ |
paulb@186 | 389 | Expires any session established according to information provided in the |
paulb@186 | 390 | transaction. |
paulb@183 | 391 | """ |
paulb@183 | 392 | |
paulb@239 | 393 | if Session: |
paulb@239 | 394 | session = self.get_session(create=0) |
paulb@239 | 395 | if session: |
paulb@239 | 396 | session.invalidate() |
paulb@239 | 397 | else: |
paulb@239 | 398 | # NOTE: Requires configuration. |
paulb@239 | 399 | |
paulb@239 | 400 | if self.session_store is None: |
paulb@239 | 401 | self.session_store = SessionStore(self, os.path.join(apache.server_root(), "WebStack-sessions")) |
paulb@239 | 402 | self.session_store.expire_session() |
paulb@183 | 403 | |
paulb@128 | 404 | # Application-specific methods. |
paulb@128 | 405 | |
paulb@128 | 406 | def set_user(self, username): |
paulb@128 | 407 | |
paulb@128 | 408 | """ |
paulb@128 | 409 | An application-specific method which sets the user information with |
paulb@128 | 410 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 411 | 'get_user'. |
paulb@128 | 412 | """ |
paulb@128 | 413 | |
paulb@128 | 414 | self.user = username |
paulb@128 | 415 | |
paulb@1 | 416 | # vim: tabstop=4 expandtab shiftwidth=4 |