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