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@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 "/%s" % "/".join(self.trans.postpath) |
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@69 | 119 | t = self.get_path().split("?") |
paulb@69 | 120 | if len(t) == 1: |
paulb@69 | 121 | return "" |
paulb@69 | 122 | else: |
paulb@69 | 123 | |
paulb@69 | 124 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 125 | |
paulb@69 | 126 | return "?".join(t[1:]) |
paulb@69 | 127 | |
paulb@1 | 128 | # Higher level request-related methods. |
paulb@1 | 129 | |
paulb@69 | 130 | def get_fields_from_path(self): |
paulb@69 | 131 | |
paulb@69 | 132 | """ |
paulb@69 | 133 | A framework-specific method which extracts the form fields from the |
paulb@69 | 134 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 135 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 136 | |
paulb@71 | 137 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 138 | single value is associated with any given field name). |
paulb@69 | 139 | """ |
paulb@69 | 140 | |
paulb@71 | 141 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 142 | |
paulb@69 | 143 | def get_fields_from_body(self): |
paulb@69 | 144 | |
paulb@69 | 145 | """ |
paulb@69 | 146 | A framework-specific method which extracts the form fields from the |
paulb@69 | 147 | message body in the transaction. |
paulb@69 | 148 | |
paulb@73 | 149 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 150 | single value is associated with any given field name). |
paulb@69 | 151 | """ |
paulb@69 | 152 | |
paulb@69 | 153 | return self.trans.args |
paulb@1 | 154 | |
paulb@45 | 155 | def get_user(self): |
paulb@1 | 156 | |
paulb@1 | 157 | """ |
paulb@45 | 158 | A framework-specific method which extracts user information from the |
paulb@45 | 159 | transaction. |
paulb@108 | 160 | |
paulb@108 | 161 | Returns a username as a string or None if no user is defined. |
paulb@1 | 162 | """ |
paulb@1 | 163 | |
paulb@128 | 164 | # Twisted makes headers lower case. |
paulb@128 | 165 | |
paulb@128 | 166 | if self.user is not None: |
paulb@128 | 167 | return self.user |
paulb@45 | 168 | |
paulb@49 | 169 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 170 | if auth_header: |
paulb@49 | 171 | return UserInfo(auth_header).username |
paulb@49 | 172 | else: |
paulb@49 | 173 | return None |
paulb@1 | 174 | |
paulb@104 | 175 | def get_cookies(self): |
paulb@104 | 176 | |
paulb@104 | 177 | """ |
paulb@104 | 178 | A framework-specific method which obtains cookie information from the |
paulb@104 | 179 | request. |
paulb@104 | 180 | |
paulb@104 | 181 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@104 | 182 | NOTE: Twisted does not seem to support this operation via methods. Thus, |
paulb@104 | 183 | NOTE: direct access has been employed to get the dictionary. |
paulb@104 | 184 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 185 | NOTE: introduced. |
paulb@104 | 186 | """ |
paulb@104 | 187 | |
paulb@104 | 188 | cookies = {} |
paulb@104 | 189 | for name, value in self.trans.received_cookies.items(): |
paulb@104 | 190 | cookies[name] = Cookie(name, value) |
paulb@104 | 191 | return cookies |
paulb@104 | 192 | |
paulb@104 | 193 | def get_cookie(self, cookie_name): |
paulb@104 | 194 | |
paulb@104 | 195 | """ |
paulb@104 | 196 | A framework-specific method which obtains cookie information from the |
paulb@104 | 197 | request. |
paulb@104 | 198 | |
paulb@104 | 199 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 200 | cookie exists. |
paulb@104 | 201 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 202 | NOTE: introduced. |
paulb@104 | 203 | """ |
paulb@104 | 204 | |
paulb@104 | 205 | return Cookie(cookie_name, self.trans.getCookie(cookie_name)) |
paulb@104 | 206 | |
paulb@1 | 207 | # Response-related methods. |
paulb@1 | 208 | |
paulb@1 | 209 | def get_response_stream(self): |
paulb@1 | 210 | |
paulb@1 | 211 | """ |
paulb@1 | 212 | A framework-specific method which returns the response stream for |
paulb@1 | 213 | the transaction. |
paulb@1 | 214 | """ |
paulb@1 | 215 | |
paulb@5 | 216 | return self.trans |
paulb@1 | 217 | |
paulb@42 | 218 | def get_response_code(self): |
paulb@42 | 219 | |
paulb@42 | 220 | """ |
paulb@42 | 221 | Get the response code associated with the transaction. If no response |
paulb@42 | 222 | code is defined, None is returned. |
paulb@42 | 223 | """ |
paulb@42 | 224 | |
paulb@42 | 225 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 226 | |
paulb@42 | 227 | return self.trans.code |
paulb@42 | 228 | |
paulb@42 | 229 | def set_response_code(self, response_code): |
paulb@42 | 230 | |
paulb@42 | 231 | """ |
paulb@42 | 232 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 233 | specification. |
paulb@42 | 234 | """ |
paulb@42 | 235 | |
paulb@42 | 236 | self.trans.setResponseCode(response_code) |
paulb@42 | 237 | |
paulb@67 | 238 | def set_header_value(self, header, value): |
paulb@49 | 239 | |
paulb@49 | 240 | """ |
paulb@49 | 241 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 242 | """ |
paulb@49 | 243 | |
paulb@49 | 244 | self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 245 | |
paulb@1 | 246 | def set_content_type(self, content_type): |
paulb@1 | 247 | |
paulb@1 | 248 | """ |
paulb@1 | 249 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 250 | response. |
paulb@1 | 251 | """ |
paulb@1 | 252 | |
paulb@5 | 253 | self.trans.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 254 | |
paulb@104 | 255 | # Higher level response-related methods. |
paulb@104 | 256 | |
paulb@104 | 257 | def set_cookie(self, cookie): |
paulb@104 | 258 | |
paulb@104 | 259 | """ |
paulb@104 | 260 | A framework-specific method which stores the given 'cookie' object in |
paulb@104 | 261 | the response. |
paulb@104 | 262 | """ |
paulb@104 | 263 | |
paulb@104 | 264 | self.trans.addCookie(cookie.name, cookie.value, expires=cookie.expires, path=cookie.path) |
paulb@104 | 265 | |
paulb@104 | 266 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@104 | 267 | |
paulb@104 | 268 | """ |
paulb@104 | 269 | A framework-specific method which stores a cookie with the given 'name' |
paulb@104 | 270 | and 'value' in the response. |
paulb@104 | 271 | |
paulb@104 | 272 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@104 | 273 | and the optional 'expires' parameter is a value compatible with the |
paulb@104 | 274 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@104 | 275 | """ |
paulb@104 | 276 | |
paulb@104 | 277 | self.trans.addCookie(self.format_header_value(name), |
paulb@104 | 278 | self.format_header_value(value), expires=expires, path=path) |
paulb@104 | 279 | |
paulb@104 | 280 | def delete_cookie(self, cookie_name): |
paulb@104 | 281 | |
paulb@104 | 282 | """ |
paulb@104 | 283 | A framework-specific method which adds to the response a request that |
paulb@104 | 284 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@104 | 285 | client. |
paulb@104 | 286 | """ |
paulb@104 | 287 | |
paulb@108 | 288 | # Create a special cookie, given that we do not know whether the browser |
paulb@108 | 289 | # has been sent the cookie or not. |
paulb@108 | 290 | # NOTE: Magic discovered in Webware. |
paulb@108 | 291 | |
paulb@108 | 292 | self.trans.addCookie(cookie_name, "", expires=0, path="/", max_age=0) |
paulb@104 | 293 | |
paulb@128 | 294 | # Application-specific methods. |
paulb@128 | 295 | |
paulb@128 | 296 | def set_user(self, username): |
paulb@128 | 297 | |
paulb@128 | 298 | """ |
paulb@128 | 299 | An application-specific method which sets the user information with |
paulb@128 | 300 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 301 | 'get_user'. |
paulb@128 | 302 | """ |
paulb@128 | 303 | |
paulb@128 | 304 | self.user = username |
paulb@128 | 305 | |
paulb@1 | 306 | # vim: tabstop=4 expandtab shiftwidth=4 |