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 respond(resource, authenticator=None, input=None, output=None, env=None): 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. 20 """ 21 22 trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, 23 env or os.environ) 24 25 try: 26 if authenticator is None or authenticator.authenticate(trans): 27 try: 28 resource.respond(trans) 29 except EndOfResponse: 30 pass 31 else: 32 trans.set_response_code(401) # Unauthorized 33 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 34 authenticator.get_auth_type(), authenticator.get_realm())) 35 finally: 36 trans.commit() 37 38 # vim: tabstop=4 expandtab shiftwidth=4