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@71 | 9 | from cgi import parse_qs |
paulb@1 | 10 | |
paulb@1 | 11 | class Transaction(Generic.Transaction): |
paulb@1 | 12 | |
paulb@1 | 13 | """ |
paulb@1 | 14 | Twisted transaction interface. |
paulb@1 | 15 | """ |
paulb@1 | 16 | |
paulb@1 | 17 | def __init__(self, trans): |
paulb@1 | 18 | |
paulb@1 | 19 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 20 | |
paulb@1 | 21 | self.trans = trans |
paulb@1 | 22 | |
paulb@1 | 23 | # Request-related methods. |
paulb@1 | 24 | |
paulb@1 | 25 | def get_request_stream(self): |
paulb@1 | 26 | |
paulb@1 | 27 | """ |
paulb@1 | 28 | A framework-specific method which returns the request stream for |
paulb@1 | 29 | the transaction. |
paulb@1 | 30 | """ |
paulb@1 | 31 | |
paulb@5 | 32 | return self.trans.content |
paulb@1 | 33 | |
paulb@1 | 34 | def get_request_method(self): |
paulb@1 | 35 | |
paulb@1 | 36 | """ |
paulb@1 | 37 | A framework-specific method which gets the request method. |
paulb@1 | 38 | """ |
paulb@1 | 39 | |
paulb@1 | 40 | return self.trans.method |
paulb@1 | 41 | |
paulb@1 | 42 | def get_headers(self): |
paulb@1 | 43 | |
paulb@1 | 44 | """ |
paulb@65 | 45 | A framework-specific method which returns all request headers. |
paulb@1 | 46 | """ |
paulb@1 | 47 | |
paulb@65 | 48 | return self.trans.received_headers |
paulb@65 | 49 | |
paulb@65 | 50 | def get_header_values(self, key): |
paulb@1 | 51 | |
paulb@65 | 52 | """ |
paulb@65 | 53 | A framework-specific method which returns a list of all request header |
paulb@65 | 54 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 55 | 'key' is treated as a case-insensitive string. |
paulb@65 | 56 | """ |
paulb@65 | 57 | |
paulb@65 | 58 | # Twisted does not convert the header key to lower case (which is the |
paulb@65 | 59 | # stored representation). |
paulb@65 | 60 | |
paulb@65 | 61 | return self.convert_to_list(self.trans.received_headers.get(key.lower())) |
paulb@1 | 62 | |
paulb@1 | 63 | def get_content_type(self): |
paulb@1 | 64 | |
paulb@1 | 65 | """ |
paulb@1 | 66 | A framework-specific method which gets the content type specified on the |
paulb@1 | 67 | request, along with the charset employed. |
paulb@1 | 68 | """ |
paulb@1 | 69 | |
paulb@1 | 70 | return self.parse_content_type(self.trans.getHeader("Content-Type")) |
paulb@1 | 71 | |
paulb@1 | 72 | def get_content_charsets(self): |
paulb@1 | 73 | |
paulb@1 | 74 | """ |
paulb@1 | 75 | Returns the character set preferences. |
paulb@1 | 76 | """ |
paulb@1 | 77 | |
paulb@1 | 78 | return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) |
paulb@1 | 79 | |
paulb@1 | 80 | def get_content_languages(self): |
paulb@1 | 81 | |
paulb@1 | 82 | """ |
paulb@1 | 83 | A framework-specific method which extracts language information from |
paulb@1 | 84 | the transaction. |
paulb@1 | 85 | """ |
paulb@1 | 86 | |
paulb@1 | 87 | return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) |
paulb@1 | 88 | |
paulb@5 | 89 | def get_path(self): |
paulb@5 | 90 | |
paulb@5 | 91 | """ |
paulb@5 | 92 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 93 | """ |
paulb@5 | 94 | |
paulb@69 | 95 | return self.trans.uri |
paulb@5 | 96 | |
paulb@1 | 97 | def get_path_info(self): |
paulb@1 | 98 | |
paulb@1 | 99 | """ |
paulb@1 | 100 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 101 | URL after the resource name handling the current request) from the |
paulb@1 | 102 | request. |
paulb@1 | 103 | """ |
paulb@1 | 104 | |
paulb@10 | 105 | return "/%s" % "/".join(self.trans.postpath) |
paulb@1 | 106 | |
paulb@69 | 107 | def get_query_string(self): |
paulb@69 | 108 | |
paulb@69 | 109 | """ |
paulb@69 | 110 | A framework-specific method which gets the query string from the path in |
paulb@69 | 111 | the request. |
paulb@69 | 112 | """ |
paulb@69 | 113 | |
paulb@69 | 114 | t = self.get_path().split("?") |
paulb@69 | 115 | if len(t) == 1: |
paulb@69 | 116 | return "" |
paulb@69 | 117 | else: |
paulb@69 | 118 | |
paulb@69 | 119 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 120 | |
paulb@69 | 121 | return "?".join(t[1:]) |
paulb@69 | 122 | |
paulb@1 | 123 | # Higher level request-related methods. |
paulb@1 | 124 | |
paulb@69 | 125 | def get_fields_from_path(self): |
paulb@69 | 126 | |
paulb@69 | 127 | """ |
paulb@69 | 128 | A framework-specific method which extracts the form fields from the |
paulb@69 | 129 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 130 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 131 | |
paulb@71 | 132 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 133 | single value is associated with any given field name). |
paulb@69 | 134 | """ |
paulb@69 | 135 | |
paulb@71 | 136 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 137 | |
paulb@69 | 138 | def get_fields_from_body(self): |
paulb@69 | 139 | |
paulb@69 | 140 | """ |
paulb@69 | 141 | A framework-specific method which extracts the form fields from the |
paulb@69 | 142 | message body in the transaction. |
paulb@69 | 143 | |
paulb@73 | 144 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 145 | single value is associated with any given field name). |
paulb@69 | 146 | """ |
paulb@69 | 147 | |
paulb@69 | 148 | return self.trans.args |
paulb@1 | 149 | |
paulb@45 | 150 | def get_user(self): |
paulb@1 | 151 | |
paulb@1 | 152 | """ |
paulb@45 | 153 | A framework-specific method which extracts user information from the |
paulb@45 | 154 | transaction. |
paulb@1 | 155 | """ |
paulb@1 | 156 | |
paulb@49 | 157 | # NOTE: Twisted makes headers lower case, for some reason. |
paulb@45 | 158 | |
paulb@49 | 159 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 160 | if auth_header: |
paulb@49 | 161 | return UserInfo(auth_header).username |
paulb@49 | 162 | else: |
paulb@49 | 163 | return None |
paulb@1 | 164 | |
paulb@1 | 165 | # Response-related methods. |
paulb@1 | 166 | |
paulb@1 | 167 | def get_response_stream(self): |
paulb@1 | 168 | |
paulb@1 | 169 | """ |
paulb@1 | 170 | A framework-specific method which returns the response stream for |
paulb@1 | 171 | the transaction. |
paulb@1 | 172 | """ |
paulb@1 | 173 | |
paulb@5 | 174 | return self.trans |
paulb@1 | 175 | |
paulb@42 | 176 | def get_response_code(self): |
paulb@42 | 177 | |
paulb@42 | 178 | """ |
paulb@42 | 179 | Get the response code associated with the transaction. If no response |
paulb@42 | 180 | code is defined, None is returned. |
paulb@42 | 181 | """ |
paulb@42 | 182 | |
paulb@42 | 183 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 184 | |
paulb@42 | 185 | return self.trans.code |
paulb@42 | 186 | |
paulb@42 | 187 | def set_response_code(self, response_code): |
paulb@42 | 188 | |
paulb@42 | 189 | """ |
paulb@42 | 190 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 191 | specification. |
paulb@42 | 192 | """ |
paulb@42 | 193 | |
paulb@42 | 194 | self.trans.setResponseCode(response_code) |
paulb@42 | 195 | |
paulb@67 | 196 | def set_header_value(self, header, value): |
paulb@49 | 197 | |
paulb@49 | 198 | """ |
paulb@49 | 199 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 200 | """ |
paulb@49 | 201 | |
paulb@49 | 202 | self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 203 | |
paulb@1 | 204 | def set_content_type(self, content_type): |
paulb@1 | 205 | |
paulb@1 | 206 | """ |
paulb@1 | 207 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 208 | response. |
paulb@1 | 209 | """ |
paulb@1 | 210 | |
paulb@5 | 211 | self.trans.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 212 | |
paulb@1 | 213 | # vim: tabstop=4 expandtab shiftwidth=4 |