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@198 | 9 | from Helpers.Request import Cookie, get_body_field |
paulb@167 | 10 | from Helpers.Response import ConvertingStream |
paulb@71 | 11 | from cgi import parse_qs |
paulb@1 | 12 | |
paulb@1 | 13 | class Transaction(Generic.Transaction): |
paulb@1 | 14 | |
paulb@1 | 15 | """ |
paulb@1 | 16 | Twisted transaction interface. |
paulb@1 | 17 | """ |
paulb@1 | 18 | |
paulb@1 | 19 | def __init__(self, trans): |
paulb@1 | 20 | |
paulb@1 | 21 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 22 | |
paulb@1 | 23 | self.trans = trans |
paulb@128 | 24 | self.user = None |
paulb@167 | 25 | self.content_type = None |
paulb@1 | 26 | |
paulb@1 | 27 | # Request-related methods. |
paulb@1 | 28 | |
paulb@1 | 29 | def get_request_stream(self): |
paulb@1 | 30 | |
paulb@1 | 31 | """ |
paulb@186 | 32 | Returns the request stream for the transaction. |
paulb@1 | 33 | """ |
paulb@1 | 34 | |
paulb@5 | 35 | return self.trans.content |
paulb@1 | 36 | |
paulb@1 | 37 | def get_request_method(self): |
paulb@1 | 38 | |
paulb@1 | 39 | """ |
paulb@186 | 40 | Returns the request method. |
paulb@1 | 41 | """ |
paulb@1 | 42 | |
paulb@1 | 43 | return self.trans.method |
paulb@1 | 44 | |
paulb@1 | 45 | def get_headers(self): |
paulb@1 | 46 | |
paulb@1 | 47 | """ |
paulb@186 | 48 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 49 | names to values. |
paulb@186 | 50 | |
paulb@91 | 51 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 52 | NOTE: need to change. |
paulb@1 | 53 | """ |
paulb@1 | 54 | |
paulb@65 | 55 | return self.trans.received_headers |
paulb@65 | 56 | |
paulb@65 | 57 | def get_header_values(self, key): |
paulb@1 | 58 | |
paulb@65 | 59 | """ |
paulb@186 | 60 | Returns a list of all request header values associated with the given |
paulb@186 | 61 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 62 | case-insensitive string. |
paulb@65 | 63 | """ |
paulb@65 | 64 | |
paulb@65 | 65 | # Twisted does not convert the header key to lower case (which is the |
paulb@65 | 66 | # stored representation). |
paulb@65 | 67 | |
paulb@65 | 68 | return self.convert_to_list(self.trans.received_headers.get(key.lower())) |
paulb@1 | 69 | |
paulb@1 | 70 | def get_content_type(self): |
paulb@1 | 71 | |
paulb@1 | 72 | """ |
paulb@186 | 73 | Returns the content type specified on the request, along with the |
paulb@186 | 74 | charset employed. |
paulb@1 | 75 | """ |
paulb@1 | 76 | |
paulb@1 | 77 | return self.parse_content_type(self.trans.getHeader("Content-Type")) |
paulb@1 | 78 | |
paulb@1 | 79 | def get_content_charsets(self): |
paulb@1 | 80 | |
paulb@1 | 81 | """ |
paulb@1 | 82 | Returns the character set preferences. |
paulb@1 | 83 | """ |
paulb@1 | 84 | |
paulb@1 | 85 | return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) |
paulb@1 | 86 | |
paulb@1 | 87 | def get_content_languages(self): |
paulb@1 | 88 | |
paulb@1 | 89 | """ |
paulb@186 | 90 | Returns extracted language information from the transaction. |
paulb@1 | 91 | """ |
paulb@1 | 92 | |
paulb@1 | 93 | return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) |
paulb@1 | 94 | |
paulb@5 | 95 | def get_path(self): |
paulb@5 | 96 | |
paulb@5 | 97 | """ |
paulb@186 | 98 | Returns the entire path from the request. |
paulb@5 | 99 | """ |
paulb@5 | 100 | |
paulb@69 | 101 | return self.trans.uri |
paulb@5 | 102 | |
paulb@162 | 103 | def get_path_without_query(self): |
paulb@162 | 104 | |
paulb@162 | 105 | """ |
paulb@186 | 106 | Returns the entire path from the request 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@186 | 114 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 115 | handling the current request) from the request. |
paulb@1 | 116 | """ |
paulb@1 | 117 | |
paulb@10 | 118 | return "/%s" % "/".join(self.trans.postpath) |
paulb@1 | 119 | |
paulb@69 | 120 | def get_query_string(self): |
paulb@69 | 121 | |
paulb@69 | 122 | """ |
paulb@186 | 123 | Returns the query string from the path in the request. |
paulb@69 | 124 | """ |
paulb@69 | 125 | |
paulb@69 | 126 | t = self.get_path().split("?") |
paulb@69 | 127 | if len(t) == 1: |
paulb@69 | 128 | return "" |
paulb@69 | 129 | else: |
paulb@69 | 130 | |
paulb@69 | 131 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 132 | |
paulb@69 | 133 | return "?".join(t[1:]) |
paulb@69 | 134 | |
paulb@1 | 135 | # Higher level request-related methods. |
paulb@1 | 136 | |
paulb@69 | 137 | def get_fields_from_path(self): |
paulb@69 | 138 | |
paulb@69 | 139 | """ |
paulb@186 | 140 | Extracts the form fields from the path specified in the transaction. The |
paulb@186 | 141 | underlying framework may refuse to supply fields from the path if |
paulb@186 | 142 | handling a POST transaction. |
paulb@69 | 143 | |
paulb@71 | 144 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 145 | single value is associated with any given field name). |
paulb@69 | 146 | """ |
paulb@69 | 147 | |
paulb@71 | 148 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 149 | |
paulb@167 | 150 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 151 | |
paulb@69 | 152 | """ |
paulb@186 | 153 | Extracts the form fields from the message body in the transaction. The |
paulb@186 | 154 | optional 'encoding' parameter specifies the character encoding of the |
paulb@186 | 155 | message body for cases where no such information is available, but where |
paulb@186 | 156 | the default encoding is to be overridden. |
paulb@69 | 157 | |
paulb@73 | 158 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 159 | single value is associated with any given field name). Each value is |
paulb@193 | 160 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 161 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 162 | """ |
paulb@69 | 163 | |
paulb@224 | 164 | # NOTE: Fix the inclusion of path fields since this prevents Unicode conversion. |
paulb@224 | 165 | |
paulb@224 | 166 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@167 | 167 | fields = {} |
paulb@167 | 168 | for field_name, field_values in self.trans.args.items(): |
paulb@198 | 169 | if type(field_values) == type([]): |
paulb@198 | 170 | fields[field_name] = [] |
paulb@198 | 171 | |
paulb@198 | 172 | # Twisted stores plain strings. |
paulb@198 | 173 | |
paulb@198 | 174 | for field_str in field_values: |
paulb@198 | 175 | fields[field_name].append(get_body_field(field_str, encoding)) |
paulb@198 | 176 | else: |
paulb@198 | 177 | fields[field_name] = get_body_field(field_values, encoding) |
paulb@167 | 178 | return fields |
paulb@1 | 179 | |
paulb@45 | 180 | def get_user(self): |
paulb@1 | 181 | |
paulb@1 | 182 | """ |
paulb@186 | 183 | Extracts user information from the transaction. |
paulb@108 | 184 | |
paulb@108 | 185 | Returns a username as a string or None if no user is defined. |
paulb@1 | 186 | """ |
paulb@1 | 187 | |
paulb@128 | 188 | # Twisted makes headers lower case. |
paulb@128 | 189 | |
paulb@128 | 190 | if self.user is not None: |
paulb@128 | 191 | return self.user |
paulb@45 | 192 | |
paulb@49 | 193 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 194 | if auth_header: |
paulb@49 | 195 | return UserInfo(auth_header).username |
paulb@49 | 196 | else: |
paulb@49 | 197 | return None |
paulb@1 | 198 | |
paulb@104 | 199 | def get_cookies(self): |
paulb@104 | 200 | |
paulb@104 | 201 | """ |
paulb@186 | 202 | Obtains cookie information from the request. |
paulb@104 | 203 | |
paulb@104 | 204 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@104 | 205 | NOTE: Twisted does not seem to support this operation via methods. Thus, |
paulb@104 | 206 | NOTE: direct access has been employed to get the dictionary. |
paulb@104 | 207 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 208 | NOTE: introduced. |
paulb@104 | 209 | """ |
paulb@104 | 210 | |
paulb@104 | 211 | cookies = {} |
paulb@104 | 212 | for name, value in self.trans.received_cookies.items(): |
paulb@104 | 213 | cookies[name] = Cookie(name, value) |
paulb@104 | 214 | return cookies |
paulb@104 | 215 | |
paulb@104 | 216 | def get_cookie(self, cookie_name): |
paulb@104 | 217 | |
paulb@104 | 218 | """ |
paulb@186 | 219 | Obtains cookie information from the request. |
paulb@104 | 220 | |
paulb@104 | 221 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 222 | cookie exists. |
paulb@104 | 223 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 224 | NOTE: introduced. |
paulb@104 | 225 | """ |
paulb@104 | 226 | |
paulb@104 | 227 | return Cookie(cookie_name, self.trans.getCookie(cookie_name)) |
paulb@104 | 228 | |
paulb@1 | 229 | # Response-related methods. |
paulb@1 | 230 | |
paulb@1 | 231 | def get_response_stream(self): |
paulb@1 | 232 | |
paulb@1 | 233 | """ |
paulb@186 | 234 | Returns the response stream for the transaction. |
paulb@1 | 235 | """ |
paulb@1 | 236 | |
paulb@224 | 237 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 238 | # or a default encoding. |
paulb@167 | 239 | |
paulb@167 | 240 | if self.content_type: |
paulb@224 | 241 | encoding = self.content_type.charset |
paulb@224 | 242 | encoding = encoding or self.default_charset |
paulb@167 | 243 | return ConvertingStream(self.trans, encoding) |
paulb@1 | 244 | |
paulb@42 | 245 | def get_response_code(self): |
paulb@42 | 246 | |
paulb@42 | 247 | """ |
paulb@42 | 248 | Get the response code associated with the transaction. If no response |
paulb@42 | 249 | code is defined, None is returned. |
paulb@42 | 250 | """ |
paulb@42 | 251 | |
paulb@42 | 252 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 253 | |
paulb@42 | 254 | return self.trans.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.trans.setResponseCode(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.setHeader(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@186 | 276 | Sets the 'content_type' for the response. |
paulb@1 | 277 | """ |
paulb@1 | 278 | |
paulb@167 | 279 | # Remember the content type for encoding purposes later. |
paulb@167 | 280 | |
paulb@167 | 281 | self.content_type = content_type |
paulb@224 | 282 | self.trans.setHeader("Content-Type", str(content_type)) |
paulb@1 | 283 | |
paulb@104 | 284 | # Higher level response-related methods. |
paulb@104 | 285 | |
paulb@104 | 286 | def set_cookie(self, cookie): |
paulb@104 | 287 | |
paulb@104 | 288 | """ |
paulb@186 | 289 | Stores the given 'cookie' object in the response. |
paulb@104 | 290 | """ |
paulb@104 | 291 | |
paulb@104 | 292 | self.trans.addCookie(cookie.name, cookie.value, expires=cookie.expires, path=cookie.path) |
paulb@104 | 293 | |
paulb@104 | 294 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@104 | 295 | |
paulb@104 | 296 | """ |
paulb@186 | 297 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@104 | 298 | |
paulb@104 | 299 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@104 | 300 | and the optional 'expires' parameter is a value compatible with the |
paulb@104 | 301 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@104 | 302 | """ |
paulb@104 | 303 | |
paulb@104 | 304 | self.trans.addCookie(self.format_header_value(name), |
paulb@104 | 305 | self.format_header_value(value), expires=expires, path=path) |
paulb@104 | 306 | |
paulb@104 | 307 | def delete_cookie(self, cookie_name): |
paulb@104 | 308 | |
paulb@104 | 309 | """ |
paulb@186 | 310 | Adds to the response a request that the cookie with the given |
paulb@186 | 311 | 'cookie_name' be deleted/discarded by the client. |
paulb@104 | 312 | """ |
paulb@104 | 313 | |
paulb@108 | 314 | # Create a special cookie, given that we do not know whether the browser |
paulb@108 | 315 | # has been sent the cookie or not. |
paulb@108 | 316 | # NOTE: Magic discovered in Webware. |
paulb@108 | 317 | |
paulb@108 | 318 | self.trans.addCookie(cookie_name, "", expires=0, path="/", max_age=0) |
paulb@104 | 319 | |
paulb@128 | 320 | # Application-specific methods. |
paulb@128 | 321 | |
paulb@128 | 322 | def set_user(self, username): |
paulb@128 | 323 | |
paulb@128 | 324 | """ |
paulb@128 | 325 | An application-specific method which sets the user information with |
paulb@128 | 326 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 327 | 'get_user'. |
paulb@128 | 328 | """ |
paulb@128 | 329 | |
paulb@128 | 330 | self.user = username |
paulb@128 | 331 | |
paulb@1 | 332 | # vim: tabstop=4 expandtab shiftwidth=4 |