paulb@109 | 1 | #!/usr/bin/env python |
paulb@109 | 2 | |
paulb@109 | 3 | """ |
paulb@109 | 4 | CGI adapter. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 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@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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@109 | 26 | |
paulb@321 | 27 | def deploy(resource, authenticator=None, input=None, output=None, env=None, handle_errors=1): |
paulb@109 | 28 | |
paulb@109 | 29 | """ |
paulb@109 | 30 | Dispatch to the root application-specific 'resource'. Employ the optional |
paulb@134 | 31 | 'authenticator' to control access to the resource. If the optional 'input' |
paulb@134 | 32 | stream, 'output' stream or environment 'env' are specified, use them instead |
paulb@134 | 33 | of the defaults: standard input, standard output and the operating system |
paulb@134 | 34 | environment respectively. Note that 'env' must evaluate to true for it to |
paulb@310 | 35 | replace the default. The optional 'handle_errors' parameter (if true) causes |
paulb@310 | 36 | handlers to deal with uncaught exceptions cleanly. |
paulb@109 | 37 | """ |
paulb@109 | 38 | |
paulb@134 | 39 | trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, |
paulb@134 | 40 | env or os.environ) |
paulb@109 | 41 | |
paulb@109 | 42 | try: |
paulb@109 | 43 | if authenticator is None or authenticator.authenticate(trans): |
paulb@297 | 44 | try: |
paulb@297 | 45 | resource.respond(trans) |
paulb@297 | 46 | except EndOfResponse: |
paulb@297 | 47 | pass |
paulb@310 | 48 | except: |
paulb@337 | 49 | if handle_errors: |
paulb@310 | 50 | trans.set_response_code(500) # Internal error |
paulb@310 | 51 | else: |
paulb@310 | 52 | raise |
paulb@109 | 53 | else: |
paulb@109 | 54 | trans.set_response_code(401) # Unauthorized |
paulb@109 | 55 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@109 | 56 | authenticator.get_auth_type(), authenticator.get_realm())) |
paulb@109 | 57 | finally: |
paulb@109 | 58 | trans.commit() |
paulb@109 | 59 | |
paulb@109 | 60 | # vim: tabstop=4 expandtab shiftwidth=4 |