paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Twisted classes. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import Generic |
paulb@49 | 8 | from Helpers.Auth import UserInfo |
paulb@104 | 9 | from Helpers.Request import Cookie |
paulb@71 | 10 | from cgi import parse_qs |
paulb@1 | 11 | |
paulb@1 | 12 | class Transaction(Generic.Transaction): |
paulb@1 | 13 | |
paulb@1 | 14 | """ |
paulb@1 | 15 | Twisted transaction interface. |
paulb@1 | 16 | """ |
paulb@1 | 17 | |
paulb@1 | 18 | def __init__(self, trans): |
paulb@1 | 19 | |
paulb@1 | 20 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 21 | |
paulb@1 | 22 | self.trans = trans |
paulb@128 | 23 | self.user = None |
paulb@1 | 24 | |
paulb@1 | 25 | # Request-related methods. |
paulb@1 | 26 | |
paulb@1 | 27 | def get_request_stream(self): |
paulb@1 | 28 | |
paulb@1 | 29 | """ |
paulb@1 | 30 | A framework-specific method which returns the request stream for |
paulb@1 | 31 | the transaction. |
paulb@1 | 32 | """ |
paulb@1 | 33 | |
paulb@5 | 34 | return self.trans.content |
paulb@1 | 35 | |
paulb@1 | 36 | def get_request_method(self): |
paulb@1 | 37 | |
paulb@1 | 38 | """ |
paulb@1 | 39 | A framework-specific method which gets the request method. |
paulb@1 | 40 | """ |
paulb@1 | 41 | |
paulb@1 | 42 | return self.trans.method |
paulb@1 | 43 | |
paulb@1 | 44 | def get_headers(self): |
paulb@1 | 45 | |
paulb@1 | 46 | """ |
paulb@108 | 47 | A framework-specific method which returns all request headers as a |
paulb@108 | 48 | dictionary-like object mapping header names to values. |
paulb@91 | 49 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 50 | NOTE: need to change. |
paulb@1 | 51 | """ |
paulb@1 | 52 | |
paulb@65 | 53 | return self.trans.received_headers |
paulb@65 | 54 | |
paulb@65 | 55 | def get_header_values(self, key): |
paulb@1 | 56 | |
paulb@65 | 57 | """ |
paulb@65 | 58 | A framework-specific method which returns a list of all request header |
paulb@65 | 59 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 60 | 'key' is treated as a case-insensitive string. |
paulb@65 | 61 | """ |
paulb@65 | 62 | |
paulb@65 | 63 | # Twisted does not convert the header key to lower case (which is the |
paulb@65 | 64 | # stored representation). |
paulb@65 | 65 | |
paulb@65 | 66 | return self.convert_to_list(self.trans.received_headers.get(key.lower())) |
paulb@1 | 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.getHeader("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.getHeader("Accept-Language")) |
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.getHeader("Accept-Charset")) |
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@69 | 100 | return self.trans.uri |
paulb@5 | 101 | |
paulb@162 | 102 | def get_path_without_query(self): |
paulb@162 | 103 | |
paulb@162 | 104 | """ |
paulb@162 | 105 | A framework-specific method which gets the entire path from the request |
paulb@162 | 106 | minus the query string. |
paulb@162 | 107 | """ |
paulb@162 | 108 | |
paulb@162 | 109 | return self.get_path().split("?")[0] |
paulb@162 | 110 | |
paulb@1 | 111 | def get_path_info(self): |
paulb@1 | 112 | |
paulb@1 | 113 | """ |
paulb@1 | 114 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 115 | URL after the resource name handling the current request) from the |
paulb@1 | 116 | request. |
paulb@1 | 117 | """ |
paulb@1 | 118 | |
paulb@10 | 119 | return "/%s" % "/".join(self.trans.postpath) |
paulb@1 | 120 | |
paulb@69 | 121 | def get_query_string(self): |
paulb@69 | 122 | |
paulb@69 | 123 | """ |
paulb@69 | 124 | A framework-specific method which gets the query string from the path in |
paulb@69 | 125 | the request. |
paulb@69 | 126 | """ |
paulb@69 | 127 | |
paulb@69 | 128 | t = self.get_path().split("?") |
paulb@69 | 129 | if len(t) == 1: |
paulb@69 | 130 | return "" |
paulb@69 | 131 | else: |
paulb@69 | 132 | |
paulb@69 | 133 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 134 | |
paulb@69 | 135 | return "?".join(t[1:]) |
paulb@69 | 136 | |
paulb@1 | 137 | # Higher level request-related methods. |
paulb@1 | 138 | |
paulb@69 | 139 | def get_fields_from_path(self): |
paulb@69 | 140 | |
paulb@69 | 141 | """ |
paulb@69 | 142 | A framework-specific method which extracts the form fields from the |
paulb@69 | 143 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 144 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 145 | |
paulb@71 | 146 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 147 | single value is associated with any given field name). |
paulb@69 | 148 | """ |
paulb@69 | 149 | |
paulb@71 | 150 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 151 | |
paulb@69 | 152 | def get_fields_from_body(self): |
paulb@69 | 153 | |
paulb@69 | 154 | """ |
paulb@69 | 155 | A framework-specific method which extracts the form fields from the |
paulb@69 | 156 | message body in the transaction. |
paulb@69 | 157 | |
paulb@73 | 158 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 159 | single value is associated with any given field name). |
paulb@69 | 160 | """ |
paulb@69 | 161 | |
paulb@69 | 162 | return self.trans.args |
paulb@1 | 163 | |
paulb@45 | 164 | def get_user(self): |
paulb@1 | 165 | |
paulb@1 | 166 | """ |
paulb@45 | 167 | A framework-specific method which extracts user information from the |
paulb@45 | 168 | transaction. |
paulb@108 | 169 | |
paulb@108 | 170 | Returns a username as a string or None if no user is defined. |
paulb@1 | 171 | """ |
paulb@1 | 172 | |
paulb@128 | 173 | # Twisted makes headers lower case. |
paulb@128 | 174 | |
paulb@128 | 175 | if self.user is not None: |
paulb@128 | 176 | return self.user |
paulb@45 | 177 | |
paulb@49 | 178 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 179 | if auth_header: |
paulb@49 | 180 | return UserInfo(auth_header).username |
paulb@49 | 181 | else: |
paulb@49 | 182 | return None |
paulb@1 | 183 | |
paulb@104 | 184 | def get_cookies(self): |
paulb@104 | 185 | |
paulb@104 | 186 | """ |
paulb@104 | 187 | A framework-specific method which obtains cookie information from the |
paulb@104 | 188 | request. |
paulb@104 | 189 | |
paulb@104 | 190 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@104 | 191 | NOTE: Twisted does not seem to support this operation via methods. Thus, |
paulb@104 | 192 | NOTE: direct access has been employed to get the dictionary. |
paulb@104 | 193 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 194 | NOTE: introduced. |
paulb@104 | 195 | """ |
paulb@104 | 196 | |
paulb@104 | 197 | cookies = {} |
paulb@104 | 198 | for name, value in self.trans.received_cookies.items(): |
paulb@104 | 199 | cookies[name] = Cookie(name, value) |
paulb@104 | 200 | return cookies |
paulb@104 | 201 | |
paulb@104 | 202 | def get_cookie(self, cookie_name): |
paulb@104 | 203 | |
paulb@104 | 204 | """ |
paulb@104 | 205 | A framework-specific method which obtains cookie information from the |
paulb@104 | 206 | request. |
paulb@104 | 207 | |
paulb@104 | 208 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 209 | cookie exists. |
paulb@104 | 210 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 211 | NOTE: introduced. |
paulb@104 | 212 | """ |
paulb@104 | 213 | |
paulb@104 | 214 | return Cookie(cookie_name, self.trans.getCookie(cookie_name)) |
paulb@104 | 215 | |
paulb@1 | 216 | # Response-related methods. |
paulb@1 | 217 | |
paulb@1 | 218 | def get_response_stream(self): |
paulb@1 | 219 | |
paulb@1 | 220 | """ |
paulb@1 | 221 | A framework-specific method which returns the response stream for |
paulb@1 | 222 | the transaction. |
paulb@1 | 223 | """ |
paulb@1 | 224 | |
paulb@5 | 225 | return self.trans |
paulb@1 | 226 | |
paulb@42 | 227 | def get_response_code(self): |
paulb@42 | 228 | |
paulb@42 | 229 | """ |
paulb@42 | 230 | Get the response code associated with the transaction. If no response |
paulb@42 | 231 | code is defined, None is returned. |
paulb@42 | 232 | """ |
paulb@42 | 233 | |
paulb@42 | 234 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 235 | |
paulb@42 | 236 | return self.trans.code |
paulb@42 | 237 | |
paulb@42 | 238 | def set_response_code(self, response_code): |
paulb@42 | 239 | |
paulb@42 | 240 | """ |
paulb@42 | 241 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 242 | specification. |
paulb@42 | 243 | """ |
paulb@42 | 244 | |
paulb@42 | 245 | self.trans.setResponseCode(response_code) |
paulb@42 | 246 | |
paulb@67 | 247 | def set_header_value(self, header, value): |
paulb@49 | 248 | |
paulb@49 | 249 | """ |
paulb@49 | 250 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 251 | """ |
paulb@49 | 252 | |
paulb@49 | 253 | self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 254 | |
paulb@1 | 255 | def set_content_type(self, content_type): |
paulb@1 | 256 | |
paulb@1 | 257 | """ |
paulb@1 | 258 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 259 | response. |
paulb@1 | 260 | """ |
paulb@1 | 261 | |
paulb@5 | 262 | self.trans.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 263 | |
paulb@104 | 264 | # Higher level response-related methods. |
paulb@104 | 265 | |
paulb@104 | 266 | def set_cookie(self, cookie): |
paulb@104 | 267 | |
paulb@104 | 268 | """ |
paulb@104 | 269 | A framework-specific method which stores the given 'cookie' object in |
paulb@104 | 270 | the response. |
paulb@104 | 271 | """ |
paulb@104 | 272 | |
paulb@104 | 273 | self.trans.addCookie(cookie.name, cookie.value, expires=cookie.expires, path=cookie.path) |
paulb@104 | 274 | |
paulb@104 | 275 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@104 | 276 | |
paulb@104 | 277 | """ |
paulb@104 | 278 | A framework-specific method which stores a cookie with the given 'name' |
paulb@104 | 279 | and 'value' in the response. |
paulb@104 | 280 | |
paulb@104 | 281 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@104 | 282 | and the optional 'expires' parameter is a value compatible with the |
paulb@104 | 283 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@104 | 284 | """ |
paulb@104 | 285 | |
paulb@104 | 286 | self.trans.addCookie(self.format_header_value(name), |
paulb@104 | 287 | self.format_header_value(value), expires=expires, path=path) |
paulb@104 | 288 | |
paulb@104 | 289 | def delete_cookie(self, cookie_name): |
paulb@104 | 290 | |
paulb@104 | 291 | """ |
paulb@104 | 292 | A framework-specific method which adds to the response a request that |
paulb@104 | 293 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@104 | 294 | client. |
paulb@104 | 295 | """ |
paulb@104 | 296 | |
paulb@108 | 297 | # Create a special cookie, given that we do not know whether the browser |
paulb@108 | 298 | # has been sent the cookie or not. |
paulb@108 | 299 | # NOTE: Magic discovered in Webware. |
paulb@108 | 300 | |
paulb@108 | 301 | self.trans.addCookie(cookie_name, "", expires=0, path="/", max_age=0) |
paulb@104 | 302 | |
paulb@128 | 303 | # Application-specific methods. |
paulb@128 | 304 | |
paulb@128 | 305 | def set_user(self, username): |
paulb@128 | 306 | |
paulb@128 | 307 | """ |
paulb@128 | 308 | An application-specific method which sets the user information with |
paulb@128 | 309 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 310 | 'get_user'. |
paulb@128 | 311 | """ |
paulb@128 | 312 | |
paulb@128 | 313 | self.user = username |
paulb@128 | 314 | |
paulb@1 | 315 | # vim: tabstop=4 expandtab shiftwidth=4 |