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