paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | """ |
paulb@4 | 4 | mod_python adapter. |
paulb@4 | 5 | """ |
paulb@4 | 6 | |
paulb@4 | 7 | import WebStack.ModPython |
paulb@297 | 8 | from WebStack.Generic import EndOfResponse |
paulb@6 | 9 | from mod_python import apache |
paulb@4 | 10 | |
paulb@76 | 11 | def respond(request, resource, debug=0): |
paulb@6 | 12 | |
paulb@11 | 13 | """ |
paulb@11 | 14 | Dispatch the given 'request' to the root application-specific 'resource'. |
paulb@11 | 15 | The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in |
paulb@11 | 16 | the browser. |
paulb@11 | 17 | """ |
paulb@6 | 18 | |
paulb@6 | 19 | trans = WebStack.ModPython.Transaction(request) |
paulb@11 | 20 | |
paulb@11 | 21 | # NOTE: Resource pooling may be appropriate. |
paulb@11 | 22 | |
paulb@6 | 23 | try: |
paulb@297 | 24 | try: |
paulb@297 | 25 | resource.respond(trans) |
paulb@297 | 26 | except EndOfResponse: |
paulb@297 | 27 | pass |
paulb@29 | 28 | trans.commit() |
paulb@43 | 29 | return trans.get_response_code() |
paulb@6 | 30 | except: |
paulb@6 | 31 | |
paulb@6 | 32 | # NOTE: Error conditions should be investigated further, along with |
paulb@6 | 33 | # NOTE: other response states. |
paulb@6 | 34 | |
paulb@11 | 35 | if not debug: |
paulb@11 | 36 | return apache.HTTP_INTERNAL_SERVER_ERROR |
paulb@11 | 37 | else: |
paulb@11 | 38 | raise |
paulb@4 | 39 | |
paulb@76 | 40 | def authenticate(request, authenticator, debug=0): |
paulb@76 | 41 | |
paulb@76 | 42 | """ |
paulb@76 | 43 | Dispatch the given 'request' to the application-specific 'authenticator'. |
paulb@76 | 44 | The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in |
paulb@76 | 45 | the browser. |
paulb@76 | 46 | """ |
paulb@76 | 47 | |
paulb@76 | 48 | trans = WebStack.ModPython.Transaction(request) |
paulb@76 | 49 | |
paulb@76 | 50 | # NOTE: Resource pooling may be appropriate. |
paulb@76 | 51 | # NOTE: Forbidden access is not yet considered here. |
paulb@76 | 52 | |
paulb@76 | 53 | try: |
paulb@76 | 54 | if authenticator.authenticate(trans): |
paulb@76 | 55 | return apache.OK |
paulb@76 | 56 | else: |
paulb@76 | 57 | return apache.HTTP_UNAUTHORIZED |
paulb@76 | 58 | except: |
paulb@76 | 59 | |
paulb@76 | 60 | # NOTE: Error conditions should be investigated further, along with |
paulb@76 | 61 | # NOTE: other response states. |
paulb@76 | 62 | |
paulb@76 | 63 | if not debug: |
paulb@76 | 64 | return apache.HTTP_INTERNAL_SERVER_ERROR |
paulb@76 | 65 | else: |
paulb@76 | 66 | raise |
paulb@76 | 67 | |
paulb@4 | 68 | # vim: tabstop=4 expandtab shiftwidth=4 |