paulb@109 | 1 | #!/usr/bin/env python |
paulb@109 | 2 | |
paulb@109 | 3 | """ |
paulb@109 | 4 | CGI adapter. |
paulb@403 | 5 | |
paulb@562 | 6 | Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@109 | 21 | """ |
paulb@109 | 22 | |
paulb@109 | 23 | import WebStack.CGI |
paulb@297 | 24 | from WebStack.Generic import EndOfResponse |
paulb@109 | 25 | import sys, os |
paulb@594 | 26 | from WebStack.Adapters.Helpers.Error import ErrorResource |
paulb@109 | 27 | |
paulb@554 | 28 | def deploy(resource, authenticator=None, input=None, output=None, env=None, |
paulb@594 | 29 | address=None, handle_errors=1, error_resource=None): |
paulb@109 | 30 | |
paulb@109 | 31 | """ |
paulb@109 | 32 | Dispatch to the root application-specific 'resource'. Employ the optional |
paulb@134 | 33 | 'authenticator' to control access to the resource. If the optional 'input' |
paulb@134 | 34 | stream, 'output' stream or environment 'env' are specified, use them instead |
paulb@134 | 35 | of the defaults: standard input, standard output and the operating system |
paulb@134 | 36 | environment respectively. Note that 'env' must evaluate to true for it to |
paulb@310 | 37 | replace the default. The optional 'handle_errors' parameter (if true) causes |
paulb@594 | 38 | handlers to deal with uncaught exceptions cleanly, and the optional |
paulb@594 | 39 | 'error_resource' specifies an alternative error message generation resource, |
paulb@594 | 40 | if desired. |
paulb@554 | 41 | |
paulb@554 | 42 | The optional 'address' parameter is deliberately ignored. |
paulb@109 | 43 | """ |
paulb@109 | 44 | |
paulb@594 | 45 | error_resource = error_resource or ErrorResource() |
paulb@594 | 46 | |
paulb@134 | 47 | trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, |
paulb@134 | 48 | env or os.environ) |
paulb@109 | 49 | |
paulb@109 | 50 | try: |
paulb@109 | 51 | if authenticator is None or authenticator.authenticate(trans): |
paulb@297 | 52 | try: |
paulb@297 | 53 | resource.respond(trans) |
paulb@297 | 54 | except EndOfResponse: |
paulb@297 | 55 | pass |
paulb@310 | 56 | except: |
paulb@337 | 57 | if handle_errors: |
paulb@594 | 58 | trans.rollback() |
paulb@310 | 59 | trans.set_response_code(500) # Internal error |
paulb@594 | 60 | error_resource.respond(trans) |
paulb@310 | 61 | else: |
paulb@310 | 62 | raise |
paulb@109 | 63 | else: |
paulb@109 | 64 | trans.set_response_code(401) # Unauthorized |
paulb@109 | 65 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@109 | 66 | authenticator.get_auth_type(), authenticator.get_realm())) |
paulb@109 | 67 | finally: |
paulb@109 | 68 | trans.commit() |
paulb@109 | 69 | |
paulb@109 | 70 | # vim: tabstop=4 expandtab shiftwidth=4 |