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@71 | 8 | from mod_python.util import parse_qs, FieldStorage |
paulb@42 | 9 | from mod_python import apache |
paulb@98 | 10 | try: |
paulb@98 | 11 | from mod_python import Cookie |
paulb@98 | 12 | except ImportError: |
paulb@98 | 13 | # NOTE: Should provide an alternative implementation. |
paulb@98 | 14 | Cookie = None |
paulb@1 | 15 | |
paulb@1 | 16 | class Transaction(Generic.Transaction): |
paulb@1 | 17 | |
paulb@1 | 18 | """ |
paulb@1 | 19 | mod_python transaction interface. |
paulb@1 | 20 | """ |
paulb@1 | 21 | |
paulb@1 | 22 | def __init__(self, trans): |
paulb@1 | 23 | |
paulb@1 | 24 | "Initialise the transaction using the mod_python transaction 'trans'." |
paulb@1 | 25 | |
paulb@1 | 26 | self.trans = trans |
paulb@42 | 27 | self.response_code = apache.OK |
paulb@128 | 28 | self.user = None |
paulb@1 | 29 | |
paulb@133 | 30 | # Cached information. |
paulb@133 | 31 | |
paulb@133 | 32 | self.storage_body = None |
paulb@133 | 33 | |
paulb@1 | 34 | # Request-related methods. |
paulb@1 | 35 | |
paulb@1 | 36 | def get_request_stream(self): |
paulb@1 | 37 | |
paulb@1 | 38 | """ |
paulb@1 | 39 | A framework-specific method which returns the request stream for |
paulb@1 | 40 | the transaction. |
paulb@1 | 41 | """ |
paulb@1 | 42 | |
paulb@1 | 43 | return self.trans |
paulb@1 | 44 | |
paulb@1 | 45 | def get_request_method(self): |
paulb@1 | 46 | |
paulb@1 | 47 | """ |
paulb@1 | 48 | A framework-specific method which gets the request method. |
paulb@1 | 49 | """ |
paulb@1 | 50 | |
paulb@1 | 51 | return self.trans.method |
paulb@1 | 52 | |
paulb@1 | 53 | def get_headers(self): |
paulb@1 | 54 | |
paulb@1 | 55 | """ |
paulb@108 | 56 | A framework-specific method which returns all request headers as a |
paulb@108 | 57 | dictionary-like object mapping header names to values. |
paulb@91 | 58 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 59 | NOTE: need to change. |
paulb@1 | 60 | """ |
paulb@1 | 61 | |
paulb@1 | 62 | return self.trans.headers_in |
paulb@1 | 63 | |
paulb@65 | 64 | def get_header_values(self, key): |
paulb@65 | 65 | |
paulb@65 | 66 | """ |
paulb@65 | 67 | A framework-specific method which returns a list of all request header |
paulb@65 | 68 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 69 | 'key' is treated as a case-insensitive string. |
paulb@65 | 70 | """ |
paulb@65 | 71 | |
paulb@65 | 72 | return self.convert_to_list(self.trans.headers_in.get(key)) |
paulb@65 | 73 | |
paulb@1 | 74 | def get_content_type(self): |
paulb@1 | 75 | |
paulb@1 | 76 | """ |
paulb@1 | 77 | A framework-specific method which gets the content type specified on the |
paulb@1 | 78 | request, along with the charset employed. |
paulb@1 | 79 | """ |
paulb@1 | 80 | |
paulb@1 | 81 | return self.parse_content_type(self.trans.content_type) |
paulb@1 | 82 | |
paulb@1 | 83 | def get_content_charsets(self): |
paulb@1 | 84 | |
paulb@1 | 85 | """ |
paulb@1 | 86 | Returns the character set preferences. |
paulb@1 | 87 | """ |
paulb@1 | 88 | |
paulb@108 | 89 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Charset")) |
paulb@1 | 90 | |
paulb@1 | 91 | def get_content_languages(self): |
paulb@1 | 92 | |
paulb@1 | 93 | """ |
paulb@1 | 94 | A framework-specific method which extracts language information from |
paulb@1 | 95 | the transaction. |
paulb@1 | 96 | """ |
paulb@1 | 97 | |
paulb@108 | 98 | return self.parse_content_preferences(self.trans.headers_in.get("Accept-Language")) |
paulb@1 | 99 | |
paulb@5 | 100 | def get_path(self): |
paulb@5 | 101 | |
paulb@5 | 102 | """ |
paulb@5 | 103 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 104 | """ |
paulb@5 | 105 | |
paulb@144 | 106 | query_string = self.get_query_string() |
paulb@144 | 107 | if query_string: |
paulb@144 | 108 | return self.trans.uri + "?" + query_string |
paulb@144 | 109 | else: |
paulb@144 | 110 | return self.trans.uri |
paulb@5 | 111 | |
paulb@162 | 112 | def get_path_without_query(self): |
paulb@162 | 113 | |
paulb@162 | 114 | """ |
paulb@162 | 115 | A framework-specific method which gets the entire path from the request |
paulb@162 | 116 | minus the query string. |
paulb@162 | 117 | """ |
paulb@162 | 118 | |
paulb@162 | 119 | return self.trans.uri |
paulb@162 | 120 | |
paulb@1 | 121 | def get_path_info(self): |
paulb@1 | 122 | |
paulb@1 | 123 | """ |
paulb@1 | 124 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 125 | URL after the resource name handling the current request) from the |
paulb@1 | 126 | request. |
paulb@1 | 127 | """ |
paulb@1 | 128 | |
paulb@10 | 129 | return self.trans.path_info |
paulb@1 | 130 | |
paulb@69 | 131 | def get_query_string(self): |
paulb@69 | 132 | |
paulb@69 | 133 | """ |
paulb@69 | 134 | A framework-specific method which gets the query string from the path in |
paulb@69 | 135 | the request. |
paulb@69 | 136 | """ |
paulb@69 | 137 | |
paulb@73 | 138 | return self.trans.args or "" |
paulb@69 | 139 | |
paulb@1 | 140 | # Higher level request-related methods. |
paulb@1 | 141 | |
paulb@69 | 142 | def get_fields_from_path(self): |
paulb@69 | 143 | |
paulb@69 | 144 | """ |
paulb@69 | 145 | A framework-specific method which extracts the form fields from the |
paulb@69 | 146 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 147 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 148 | |
paulb@71 | 149 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 150 | single value is associated with any given field name). |
paulb@69 | 151 | """ |
paulb@69 | 152 | |
paulb@71 | 153 | return parse_qs(self.get_query_string(), 1) # keep_blank_values=1 |
paulb@69 | 154 | |
paulb@69 | 155 | def get_fields_from_body(self): |
paulb@69 | 156 | |
paulb@69 | 157 | """ |
paulb@69 | 158 | A framework-specific method which extracts the form fields from the |
paulb@69 | 159 | message body in the transaction. |
paulb@69 | 160 | |
paulb@73 | 161 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 162 | single value is associated with any given field name). |
paulb@69 | 163 | |
paulb@69 | 164 | The mod_python.util.FieldStorage class may augment the fields from the |
paulb@69 | 165 | body with fields found in the path. |
paulb@69 | 166 | """ |
paulb@69 | 167 | |
paulb@133 | 168 | if self.storage_body is None: |
paulb@133 | 169 | self.storage_body = FieldStorage(self.trans, keep_blank_values=1) |
paulb@69 | 170 | |
paulb@73 | 171 | # Traverse the storage, finding each field value. |
paulb@1 | 172 | |
paulb@73 | 173 | fields = {} |
paulb@133 | 174 | for field in self.storage_body.list: |
paulb@73 | 175 | if not fields.has_key(field.name): |
paulb@73 | 176 | fields[field.name] = [] |
paulb@73 | 177 | fields[field.name].append(field.value) |
paulb@73 | 178 | return fields |
paulb@1 | 179 | |
paulb@45 | 180 | def get_user(self): |
paulb@1 | 181 | |
paulb@1 | 182 | """ |
paulb@45 | 183 | A framework-specific method which extracts user information from the |
paulb@45 | 184 | transaction. |
paulb@108 | 185 | |
paulb@108 | 186 | Returns a username as a string or None if no user is defined. |
paulb@1 | 187 | """ |
paulb@1 | 188 | |
paulb@128 | 189 | if self.user is not None: |
paulb@128 | 190 | return self.user |
paulb@128 | 191 | else: |
paulb@128 | 192 | return self.trans.user |
paulb@1 | 193 | |
paulb@98 | 194 | def get_cookies(self): |
paulb@98 | 195 | |
paulb@98 | 196 | """ |
paulb@98 | 197 | A framework-specific method which obtains cookie information from the |
paulb@98 | 198 | request. |
paulb@98 | 199 | |
paulb@98 | 200 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@98 | 201 | |
paulb@98 | 202 | NOTE: No additional information is passed to the underlying API despite |
paulb@98 | 203 | NOTE: support for enhanced cookies in mod_python. |
paulb@98 | 204 | """ |
paulb@98 | 205 | |
paulb@137 | 206 | if Cookie: |
paulb@137 | 207 | return Cookie.get_cookies(self.trans) |
paulb@137 | 208 | else: |
paulb@137 | 209 | return None |
paulb@98 | 210 | |
paulb@104 | 211 | def get_cookie(self, cookie_name): |
paulb@104 | 212 | |
paulb@104 | 213 | """ |
paulb@104 | 214 | A framework-specific method which obtains cookie information from the |
paulb@104 | 215 | request. |
paulb@104 | 216 | |
paulb@104 | 217 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 218 | cookie exists. |
paulb@104 | 219 | """ |
paulb@104 | 220 | |
paulb@104 | 221 | return self.get_cookies().get(cookie_name) |
paulb@104 | 222 | |
paulb@1 | 223 | # Response-related methods. |
paulb@1 | 224 | |
paulb@1 | 225 | def get_response_stream(self): |
paulb@1 | 226 | |
paulb@1 | 227 | """ |
paulb@1 | 228 | A framework-specific method which returns the response stream for |
paulb@1 | 229 | the transaction. |
paulb@1 | 230 | """ |
paulb@1 | 231 | |
paulb@1 | 232 | return self.trans |
paulb@1 | 233 | |
paulb@42 | 234 | def get_response_code(self): |
paulb@42 | 235 | |
paulb@42 | 236 | """ |
paulb@42 | 237 | Get the response code associated with the transaction. If no response |
paulb@42 | 238 | code is defined, None is returned. |
paulb@42 | 239 | """ |
paulb@42 | 240 | |
paulb@42 | 241 | return self.response_code |
paulb@42 | 242 | |
paulb@42 | 243 | def set_response_code(self, response_code): |
paulb@42 | 244 | |
paulb@42 | 245 | """ |
paulb@42 | 246 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 247 | specification. |
paulb@42 | 248 | """ |
paulb@42 | 249 | |
paulb@42 | 250 | self.response_code = response_code |
paulb@42 | 251 | |
paulb@67 | 252 | def set_header_value(self, header, value): |
paulb@49 | 253 | |
paulb@49 | 254 | """ |
paulb@49 | 255 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 256 | """ |
paulb@49 | 257 | |
paulb@49 | 258 | self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) |
paulb@49 | 259 | |
paulb@1 | 260 | def set_content_type(self, content_type): |
paulb@1 | 261 | |
paulb@1 | 262 | """ |
paulb@1 | 263 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 264 | response. |
paulb@1 | 265 | """ |
paulb@1 | 266 | |
paulb@5 | 267 | self.trans.content_type = self.format_content_type(content_type) |
paulb@1 | 268 | |
paulb@98 | 269 | def set_cookie(self, cookie): |
paulb@98 | 270 | |
paulb@98 | 271 | """ |
paulb@98 | 272 | A framework-specific method which stores the given 'cookie' object in |
paulb@98 | 273 | the response. |
paulb@98 | 274 | """ |
paulb@98 | 275 | |
paulb@98 | 276 | if Cookie: |
paulb@98 | 277 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 278 | else: |
paulb@98 | 279 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 280 | pass |
paulb@98 | 281 | |
paulb@98 | 282 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@98 | 283 | |
paulb@98 | 284 | """ |
paulb@98 | 285 | A framework-specific method which stores a cookie with the given 'name' |
paulb@98 | 286 | and 'value' in the response. |
paulb@98 | 287 | |
paulb@98 | 288 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@98 | 289 | and the optional 'expires' parameter is a value compatible with the |
paulb@98 | 290 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@98 | 291 | """ |
paulb@98 | 292 | |
paulb@104 | 293 | # NOTE: We just hope that Cookie converts Unicode arguments to US-ASCII. |
paulb@104 | 294 | |
paulb@98 | 295 | if Cookie: |
paulb@98 | 296 | cookie = Cookie.Cookie(name, value) |
paulb@98 | 297 | if expires is not None: |
paulb@98 | 298 | cookie.expires = expires |
paulb@98 | 299 | if path is not None: |
paulb@98 | 300 | cookie.path = path |
paulb@98 | 301 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 302 | else: |
paulb@98 | 303 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 304 | pass |
paulb@98 | 305 | |
paulb@98 | 306 | def delete_cookie(self, cookie_name): |
paulb@98 | 307 | |
paulb@98 | 308 | """ |
paulb@98 | 309 | A framework-specific method which adds to the response a request that |
paulb@98 | 310 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@98 | 311 | client. |
paulb@98 | 312 | """ |
paulb@98 | 313 | |
paulb@98 | 314 | # Create a special cookie, given that we do not know whether the browser |
paulb@98 | 315 | # has been sent the cookie or not. |
paulb@98 | 316 | # NOTE: Magic discovered in Webware. |
paulb@98 | 317 | |
paulb@98 | 318 | if Cookie: |
paulb@98 | 319 | cookie = Cookie.Cookie(cookie_name, "") |
paulb@98 | 320 | cookie.path = "/" |
paulb@98 | 321 | cookie.expires = 0 |
paulb@98 | 322 | cookie.max_age = 0 |
paulb@98 | 323 | Cookie.add_cookie(self.trans, cookie) |
paulb@98 | 324 | else: |
paulb@98 | 325 | # NOTE: Should raise an exception or provide an implementation. |
paulb@98 | 326 | pass |
paulb@98 | 327 | |
paulb@128 | 328 | # Application-specific methods. |
paulb@128 | 329 | |
paulb@128 | 330 | def set_user(self, username): |
paulb@128 | 331 | |
paulb@128 | 332 | """ |
paulb@128 | 333 | An application-specific method which sets the user information with |
paulb@128 | 334 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 335 | 'get_user'. |
paulb@128 | 336 | """ |
paulb@128 | 337 | |
paulb@128 | 338 | self.user = username |
paulb@128 | 339 | |
paulb@1 | 340 | # vim: tabstop=4 expandtab shiftwidth=4 |