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