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