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