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