paulb@28 | 1 | #!/usr/bin/env python |
paulb@28 | 2 | |
paulb@28 | 3 | """ |
paulb@28 | 4 | BaseHTTPRequestHandler classes. |
paulb@28 | 5 | """ |
paulb@28 | 6 | |
paulb@28 | 7 | import Generic |
paulb@45 | 8 | from Helpers.Request import MessageBodyStream |
paulb@49 | 9 | from Helpers.Auth import UserInfo |
paulb@71 | 10 | from cgi import parse_qs, FieldStorage |
paulb@115 | 11 | import Cookie |
paulb@28 | 12 | from StringIO import StringIO |
paulb@28 | 13 | |
paulb@28 | 14 | class Transaction(Generic.Transaction): |
paulb@28 | 15 | |
paulb@28 | 16 | """ |
paulb@28 | 17 | BaseHTTPRequestHandler transaction interface. |
paulb@28 | 18 | """ |
paulb@28 | 19 | |
paulb@28 | 20 | def __init__(self, trans): |
paulb@28 | 21 | |
paulb@28 | 22 | """ |
paulb@28 | 23 | Initialise the transaction using the BaseHTTPRequestHandler instance |
paulb@28 | 24 | 'trans'. |
paulb@28 | 25 | """ |
paulb@28 | 26 | |
paulb@28 | 27 | self.trans = trans |
paulb@28 | 28 | |
paulb@28 | 29 | # Other attributes of interest in instances of this class. |
paulb@28 | 30 | |
paulb@28 | 31 | self.content_type = None |
paulb@45 | 32 | self.response_code = 200 |
paulb@28 | 33 | self.content = StringIO() |
paulb@115 | 34 | self.headers_out = {} |
paulb@115 | 35 | self.cookies_out = Cookie.SimpleCookie() |
paulb@115 | 36 | |
paulb@115 | 37 | # Define the incoming cookies. |
paulb@115 | 38 | |
paulb@115 | 39 | self.cookies_in = Cookie.SimpleCookie(self.get_headers().get("cookie")) |
paulb@28 | 40 | |
paulb@28 | 41 | def commit(self): |
paulb@28 | 42 | |
paulb@28 | 43 | """ |
paulb@28 | 44 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 45 | objects. |
paulb@28 | 46 | """ |
paulb@28 | 47 | |
paulb@28 | 48 | self.trans.send_response(self.response_code) |
paulb@45 | 49 | if self.content_type is not None: |
paulb@45 | 50 | self.trans.send_header("Content-Type", self.format_content_type(self.content_type)) |
paulb@115 | 51 | |
paulb@115 | 52 | for header, value in self.headers_out.items(): |
paulb@49 | 53 | self.trans.send_header(self.format_header_value(header), self.format_header_value(value)) |
paulb@115 | 54 | |
paulb@115 | 55 | # NOTE: May not be using the appropriate method. |
paulb@115 | 56 | |
paulb@115 | 57 | for morsel in self.cookies_out.values(): |
paulb@115 | 58 | self.trans.send_header("Set-Cookie", morsel.OutputString()) |
paulb@115 | 59 | |
paulb@28 | 60 | self.trans.end_headers() |
paulb@28 | 61 | self.content.seek(0) |
paulb@28 | 62 | self.trans.wfile.write(self.content.read()) |
paulb@28 | 63 | |
paulb@28 | 64 | # Request-related methods. |
paulb@28 | 65 | |
paulb@28 | 66 | def get_request_stream(self): |
paulb@28 | 67 | |
paulb@28 | 68 | """ |
paulb@28 | 69 | A framework-specific method which returns the request stream for |
paulb@28 | 70 | the transaction. |
paulb@28 | 71 | """ |
paulb@28 | 72 | |
paulb@45 | 73 | return MessageBodyStream(self.trans.rfile, self.get_headers()) |
paulb@28 | 74 | |
paulb@28 | 75 | def get_request_method(self): |
paulb@28 | 76 | |
paulb@28 | 77 | """ |
paulb@28 | 78 | A framework-specific method which gets the request method. |
paulb@28 | 79 | """ |
paulb@28 | 80 | |
paulb@28 | 81 | return self.trans.command |
paulb@28 | 82 | |
paulb@28 | 83 | def get_headers(self): |
paulb@28 | 84 | |
paulb@28 | 85 | """ |
paulb@108 | 86 | A framework-specific method which returns all request headers as a |
paulb@108 | 87 | dictionary-like object mapping header names to values. |
paulb@91 | 88 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 89 | NOTE: need to change. |
paulb@28 | 90 | """ |
paulb@28 | 91 | |
paulb@28 | 92 | return self.trans.headers |
paulb@28 | 93 | |
paulb@65 | 94 | def get_header_values(self, key): |
paulb@65 | 95 | |
paulb@65 | 96 | """ |
paulb@65 | 97 | A framework-specific method which returns a list of all request header |
paulb@65 | 98 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 99 | 'key' is treated as a case-insensitive string. |
paulb@65 | 100 | """ |
paulb@65 | 101 | |
paulb@65 | 102 | return self.convert_to_list(self.trans.headers.get(key)) |
paulb@65 | 103 | |
paulb@28 | 104 | def get_content_type(self): |
paulb@28 | 105 | |
paulb@28 | 106 | """ |
paulb@28 | 107 | A framework-specific method which gets the content type specified on the |
paulb@28 | 108 | request, along with the charset employed. |
paulb@28 | 109 | """ |
paulb@28 | 110 | |
paulb@115 | 111 | return self.parse_content_type(self.trans.headers.get("content-type")) |
paulb@28 | 112 | |
paulb@28 | 113 | def get_content_charsets(self): |
paulb@28 | 114 | |
paulb@28 | 115 | """ |
paulb@28 | 116 | Returns the character set preferences. |
paulb@28 | 117 | """ |
paulb@28 | 118 | |
paulb@115 | 119 | return self.parse_content_preferences(self.trans.headers.get("accept-charset")) |
paulb@28 | 120 | |
paulb@28 | 121 | def get_content_languages(self): |
paulb@28 | 122 | |
paulb@28 | 123 | """ |
paulb@28 | 124 | A framework-specific method which extracts language information from |
paulb@28 | 125 | the transaction. |
paulb@28 | 126 | """ |
paulb@28 | 127 | |
paulb@115 | 128 | return self.parse_content_preferences(self.trans.headers.get("accept-language")) |
paulb@28 | 129 | |
paulb@28 | 130 | def get_path(self): |
paulb@28 | 131 | |
paulb@28 | 132 | """ |
paulb@28 | 133 | A framework-specific method which gets the entire path from the request. |
paulb@28 | 134 | """ |
paulb@28 | 135 | |
paulb@28 | 136 | return self.trans.path |
paulb@28 | 137 | |
paulb@28 | 138 | def get_path_info(self): |
paulb@28 | 139 | |
paulb@28 | 140 | """ |
paulb@28 | 141 | A framework-specific method which gets the "path info" (the part of the |
paulb@28 | 142 | URL after the resource name handling the current request) from the |
paulb@28 | 143 | request. |
paulb@28 | 144 | """ |
paulb@28 | 145 | |
paulb@71 | 146 | # Remove the query string from the end of the path. |
paulb@28 | 147 | |
paulb@71 | 148 | return self.trans.path.split("?")[0] |
paulb@28 | 149 | |
paulb@69 | 150 | def get_query_string(self): |
paulb@69 | 151 | |
paulb@69 | 152 | """ |
paulb@69 | 153 | A framework-specific method which gets the query string from the path in |
paulb@69 | 154 | the request. |
paulb@69 | 155 | """ |
paulb@69 | 156 | |
paulb@69 | 157 | t = self.trans.path.split("?") |
paulb@69 | 158 | if len(t) == 1: |
paulb@69 | 159 | return "" |
paulb@69 | 160 | else: |
paulb@69 | 161 | |
paulb@69 | 162 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 163 | |
paulb@69 | 164 | return "?".join(t[1:]) |
paulb@69 | 165 | |
paulb@28 | 166 | # Higher level request-related methods. |
paulb@28 | 167 | |
paulb@69 | 168 | def get_fields_from_path(self): |
paulb@69 | 169 | |
paulb@69 | 170 | """ |
paulb@69 | 171 | A framework-specific method which extracts the form fields from the |
paulb@69 | 172 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 173 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 174 | |
paulb@71 | 175 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 176 | single value is associated with any given field name). |
paulb@69 | 177 | """ |
paulb@69 | 178 | |
paulb@71 | 179 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 180 | |
paulb@69 | 181 | def get_fields_from_body(self): |
paulb@69 | 182 | |
paulb@69 | 183 | """ |
paulb@69 | 184 | A framework-specific method which extracts the form fields from the |
paulb@69 | 185 | message body in the transaction. |
paulb@69 | 186 | |
paulb@73 | 187 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 188 | single value is associated with any given field name). |
paulb@69 | 189 | """ |
paulb@69 | 190 | |
paulb@73 | 191 | storage = FieldStorage(fp=self.get_request_stream(), headers=self.get_headers(), |
paulb@73 | 192 | environ={"REQUEST_METHOD" : self.get_request_method()}, keep_blank_values=1) |
paulb@69 | 193 | |
paulb@75 | 194 | # Avoid strange design issues with FieldStorage by checking the internal |
paulb@75 | 195 | # field list directly. |
paulb@28 | 196 | |
paulb@73 | 197 | fields = {} |
paulb@75 | 198 | if storage.list is not None: |
paulb@75 | 199 | |
paulb@75 | 200 | # Traverse the storage, finding each field value. |
paulb@75 | 201 | |
paulb@75 | 202 | for field_name in storage.keys(): |
paulb@75 | 203 | fields[field_name] = storage.getlist(field_name) |
paulb@73 | 204 | return fields |
paulb@28 | 205 | |
paulb@45 | 206 | def get_user(self): |
paulb@28 | 207 | |
paulb@28 | 208 | """ |
paulb@45 | 209 | A framework-specific method which extracts user information from the |
paulb@45 | 210 | transaction. |
paulb@108 | 211 | |
paulb@108 | 212 | Returns a username as a string or None if no user is defined. |
paulb@28 | 213 | """ |
paulb@28 | 214 | |
paulb@115 | 215 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 216 | if auth_header: |
paulb@49 | 217 | return UserInfo(auth_header).username |
paulb@49 | 218 | else: |
paulb@49 | 219 | return None |
paulb@28 | 220 | |
paulb@115 | 221 | def get_cookies(self): |
paulb@115 | 222 | |
paulb@115 | 223 | """ |
paulb@115 | 224 | A framework-specific method which obtains cookie information from the |
paulb@115 | 225 | request. |
paulb@115 | 226 | |
paulb@115 | 227 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 228 | """ |
paulb@115 | 229 | |
paulb@115 | 230 | return self.cookies_in |
paulb@115 | 231 | |
paulb@115 | 232 | def get_cookie(self, cookie_name): |
paulb@115 | 233 | |
paulb@115 | 234 | """ |
paulb@115 | 235 | A framework-specific method which obtains cookie information from the |
paulb@115 | 236 | request. |
paulb@115 | 237 | |
paulb@115 | 238 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 239 | cookie exists. |
paulb@115 | 240 | """ |
paulb@115 | 241 | |
paulb@115 | 242 | return self.cookies_in.get(cookie_name) |
paulb@115 | 243 | |
paulb@28 | 244 | # Response-related methods. |
paulb@28 | 245 | |
paulb@28 | 246 | def get_response_stream(self): |
paulb@28 | 247 | |
paulb@28 | 248 | """ |
paulb@28 | 249 | A framework-specific method which returns the response stream for |
paulb@28 | 250 | the transaction. |
paulb@28 | 251 | """ |
paulb@28 | 252 | |
paulb@28 | 253 | # Return a stream which is later emptied into the real stream. |
paulb@28 | 254 | |
paulb@28 | 255 | return self.content |
paulb@28 | 256 | |
paulb@42 | 257 | def get_response_code(self): |
paulb@42 | 258 | |
paulb@42 | 259 | """ |
paulb@42 | 260 | Get the response code associated with the transaction. If no response |
paulb@42 | 261 | code is defined, None is returned. |
paulb@42 | 262 | """ |
paulb@42 | 263 | |
paulb@42 | 264 | return self.response_code |
paulb@42 | 265 | |
paulb@42 | 266 | def set_response_code(self, response_code): |
paulb@42 | 267 | |
paulb@42 | 268 | """ |
paulb@42 | 269 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 270 | specification. |
paulb@42 | 271 | """ |
paulb@42 | 272 | |
paulb@42 | 273 | self.response_code = response_code |
paulb@42 | 274 | |
paulb@67 | 275 | def set_header_value(self, header, value): |
paulb@49 | 276 | |
paulb@49 | 277 | """ |
paulb@49 | 278 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 279 | """ |
paulb@49 | 280 | |
paulb@49 | 281 | # The header is not written out immediately due to the buffering in use. |
paulb@49 | 282 | |
paulb@115 | 283 | self.headers_out[header] = value |
paulb@49 | 284 | |
paulb@28 | 285 | def set_content_type(self, content_type): |
paulb@28 | 286 | |
paulb@28 | 287 | """ |
paulb@28 | 288 | A framework-specific method which sets the 'content_type' for the |
paulb@28 | 289 | response. |
paulb@28 | 290 | """ |
paulb@28 | 291 | |
paulb@28 | 292 | # The content type has to be written as a header, before actual content, |
paulb@28 | 293 | # but after the response line. This means that some kind of buffering is |
paulb@28 | 294 | # required. Hence, we don't write the header out immediately. |
paulb@28 | 295 | |
paulb@28 | 296 | self.content_type = content_type |
paulb@28 | 297 | |
paulb@115 | 298 | def set_cookie(self, cookie): |
paulb@115 | 299 | |
paulb@115 | 300 | """ |
paulb@115 | 301 | A framework-specific method which stores the given 'cookie' object in |
paulb@115 | 302 | the response. |
paulb@115 | 303 | """ |
paulb@115 | 304 | |
paulb@115 | 305 | # NOTE: If multiple cookies of the same name could be specified, this |
paulb@115 | 306 | # NOTE: could need changing. |
paulb@115 | 307 | |
paulb@115 | 308 | self.cookies_out[cookie.name] = cookie.value |
paulb@115 | 309 | |
paulb@115 | 310 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 311 | |
paulb@115 | 312 | """ |
paulb@115 | 313 | A framework-specific method which stores a cookie with the given 'name' |
paulb@115 | 314 | and 'value' in the response. |
paulb@115 | 315 | |
paulb@115 | 316 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 317 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 318 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 319 | """ |
paulb@115 | 320 | |
paulb@115 | 321 | self.cookies_out[name] = value |
paulb@115 | 322 | if path is not None: |
paulb@115 | 323 | self.cookies_out[name]["path"] = path |
paulb@115 | 324 | if expires is not None: |
paulb@115 | 325 | self.cookies_out[name]["expires"] = expires |
paulb@115 | 326 | |
paulb@115 | 327 | def delete_cookie(self, cookie_name): |
paulb@115 | 328 | |
paulb@115 | 329 | """ |
paulb@115 | 330 | A framework-specific method which adds to the response a request that |
paulb@115 | 331 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@115 | 332 | client. |
paulb@115 | 333 | """ |
paulb@115 | 334 | |
paulb@115 | 335 | # Create a special cookie, given that we do not know whether the browser |
paulb@115 | 336 | # has been sent the cookie or not. |
paulb@115 | 337 | # NOTE: Magic discovered in Webware. |
paulb@115 | 338 | |
paulb@115 | 339 | self.cookies_out[cookie_name] = "" |
paulb@115 | 340 | self.cookies_out[cookie_name]["path"] = "/" |
paulb@115 | 341 | self.cookies_out[cookie_name]["expires"] = 0 |
paulb@115 | 342 | self.cookies_out[cookie_name]["max-age"] = 0 |
paulb@115 | 343 | |
paulb@28 | 344 | # vim: tabstop=4 expandtab shiftwidth=4 |