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