paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | """ |
paulb@4 | 4 | mod_python adapter. |
paulb@403 | 5 | |
paulb@562 | 6 | Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@4 | 21 | """ |
paulb@4 | 22 | |
paulb@4 | 23 | import WebStack.ModPython |
paulb@297 | 24 | from WebStack.Generic import EndOfResponse |
paulb@6 | 25 | from mod_python import apache |
paulb@4 | 26 | |
paulb@554 | 27 | def deploy(resource, authenticator=None, address=None, handle_errors=1): |
paulb@554 | 28 | |
paulb@554 | 29 | """ |
paulb@554 | 30 | Deploy the given 'resource' and optional 'authenticator'. |
paulb@554 | 31 | The optional 'handle_errors' flag, if set to false, causes tracebacks to be |
paulb@554 | 32 | displayed in the browser. |
paulb@554 | 33 | |
paulb@554 | 34 | The optional 'address' parameter is deliberately ignored. |
paulb@554 | 35 | """ |
paulb@554 | 36 | |
paulb@554 | 37 | def _respond(request): |
paulb@554 | 38 | return respond(request, resource, handle_errors=handle_errors) |
paulb@554 | 39 | def _authenticate(request): |
paulb@554 | 40 | return authenticate(request, authenticator, handle_errors=handle_errors) |
paulb@554 | 41 | return _respond, _authenticate |
paulb@554 | 42 | |
paulb@310 | 43 | def respond(request, resource, handle_errors=1): |
paulb@6 | 44 | |
paulb@11 | 45 | """ |
paulb@11 | 46 | Dispatch the given 'request' to the root application-specific 'resource'. |
paulb@310 | 47 | The optional 'handle_errors' flag, if set to false, causes tracebacks to be |
paulb@310 | 48 | displayed in the browser. |
paulb@11 | 49 | """ |
paulb@6 | 50 | |
paulb@6 | 51 | trans = WebStack.ModPython.Transaction(request) |
paulb@11 | 52 | |
paulb@11 | 53 | # NOTE: Resource pooling may be appropriate. |
paulb@11 | 54 | |
paulb@6 | 55 | try: |
paulb@297 | 56 | try: |
paulb@510 | 57 | try: |
paulb@510 | 58 | resource.respond(trans) |
paulb@510 | 59 | except EndOfResponse: |
paulb@510 | 60 | pass |
paulb@510 | 61 | return trans.get_response_code() |
paulb@510 | 62 | |
paulb@510 | 63 | except: |
paulb@6 | 64 | |
paulb@510 | 65 | # NOTE: Error conditions should be investigated further, along with |
paulb@510 | 66 | # NOTE: other response states. |
paulb@6 | 67 | |
paulb@510 | 68 | if handle_errors: |
paulb@510 | 69 | return apache.HTTP_INTERNAL_SERVER_ERROR |
paulb@510 | 70 | else: |
paulb@510 | 71 | raise |
paulb@510 | 72 | |
paulb@510 | 73 | finally: |
paulb@510 | 74 | trans.commit() |
paulb@4 | 75 | |
paulb@310 | 76 | def authenticate(request, authenticator, handle_errors=1): |
paulb@76 | 77 | |
paulb@76 | 78 | """ |
paulb@76 | 79 | Dispatch the given 'request' to the application-specific 'authenticator'. |
paulb@310 | 80 | The optional 'handle_errors' flag, if set to false, causes tracebacks to be |
paulb@310 | 81 | displayed in the browser. |
paulb@76 | 82 | """ |
paulb@76 | 83 | |
paulb@76 | 84 | trans = WebStack.ModPython.Transaction(request) |
paulb@76 | 85 | |
paulb@76 | 86 | # NOTE: Resource pooling may be appropriate. |
paulb@76 | 87 | # NOTE: Forbidden access is not yet considered here. |
paulb@76 | 88 | |
paulb@76 | 89 | try: |
paulb@76 | 90 | if authenticator.authenticate(trans): |
paulb@76 | 91 | return apache.OK |
paulb@76 | 92 | else: |
paulb@76 | 93 | return apache.HTTP_UNAUTHORIZED |
paulb@76 | 94 | except: |
paulb@76 | 95 | |
paulb@76 | 96 | # NOTE: Error conditions should be investigated further, along with |
paulb@76 | 97 | # NOTE: other response states. |
paulb@76 | 98 | |
paulb@310 | 99 | if handle_errors: |
paulb@76 | 100 | return apache.HTTP_INTERNAL_SERVER_ERROR |
paulb@76 | 101 | else: |
paulb@76 | 102 | raise |
paulb@76 | 103 | |
paulb@4 | 104 | # vim: tabstop=4 expandtab shiftwidth=4 |