1 #!/usr/bin/env python 2 3 """ 4 CGI adapter. 5 6 Copyright (C) 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 21 """ 22 23 import WebStack.CGI 24 from WebStack.Generic import EndOfResponse 25 import sys, os 26 from WebStack.Adapters.Helpers.Error import ErrorResource 27 28 def deploy(resource, authenticator=None, input=None, output=None, env=None, 29 address=None, handle_errors=1, error_resource=None): 30 31 """ 32 Dispatch to the root application-specific 'resource'. Employ the optional 33 'authenticator' to control access to the resource. If the optional 'input' 34 stream, 'output' stream or environment 'env' are specified, use them instead 35 of the defaults: standard input, standard output and the operating system 36 environment respectively. Note that 'env' must evaluate to true for it to 37 replace the default. The optional 'handle_errors' parameter (if true) causes 38 handlers to deal with uncaught exceptions cleanly, and the optional 39 'error_resource' specifies an alternative error message generation resource, 40 if desired. 41 42 The optional 'address' parameter is deliberately ignored. 43 """ 44 45 error_resource = error_resource or ErrorResource() 46 47 trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, 48 env or os.environ) 49 50 try: 51 if authenticator is None or authenticator.authenticate(trans): 52 try: 53 resource.respond(trans) 54 except EndOfResponse: 55 pass 56 except: 57 if handle_errors: 58 trans.rollback() 59 trans.set_response_code(500) # Internal error 60 error_resource.respond(trans) 61 else: 62 raise 63 else: 64 trans.set_response_code(401) # Unauthorized 65 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 66 authenticator.get_auth_type(), authenticator.get_realm())) 67 finally: 68 trans.commit() 69 70 # vim: tabstop=4 expandtab shiftwidth=4