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