1 #!/usr/bin/env python 2 3 """ 4 CGI adapter. 5 """ 6 7 import WebStack.CGI 8 from WebStack.Generic import EndOfResponse 9 import sys, os 10 11 def deploy(resource, authenticator=None, input=None, output=None, env=None, handle_errors=1): 12 13 """ 14 Dispatch to the root application-specific 'resource'. Employ the optional 15 'authenticator' to control access to the resource. If the optional 'input' 16 stream, 'output' stream or environment 'env' are specified, use them instead 17 of the defaults: standard input, standard output and the operating system 18 environment respectively. Note that 'env' must evaluate to true for it to 19 replace the default. The optional 'handle_errors' parameter (if true) causes 20 handlers to deal with uncaught exceptions cleanly. 21 """ 22 23 trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, 24 env or os.environ) 25 26 try: 27 if authenticator is None or authenticator.authenticate(trans): 28 try: 29 resource.respond(trans) 30 except EndOfResponse: 31 pass 32 except: 33 if self.handle_errors: 34 trans.set_response_code(500) # Internal error 35 else: 36 raise 37 else: 38 trans.set_response_code(401) # Unauthorized 39 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 40 authenticator.get_auth_type(), authenticator.get_realm())) 41 finally: 42 trans.commit() 43 44 # vim: tabstop=4 expandtab shiftwidth=4