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