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