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