1 #!/usr/bin/env python 2 3 """ 4 mod_python adapter. 5 6 Copyright (C) 2004, 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 """ 22 23 import WebStack.ModPython 24 from WebStack.Generic import EndOfResponse 25 from mod_python import apache 26 27 def respond(request, resource, handle_errors=1): 28 29 """ 30 Dispatch the given 'request' to the root application-specific 'resource'. 31 The optional 'handle_errors' flag, if set to false, causes tracebacks to be 32 displayed in the browser. 33 """ 34 35 trans = WebStack.ModPython.Transaction(request) 36 37 # NOTE: Resource pooling may be appropriate. 38 39 try: 40 try: 41 resource.respond(trans) 42 except EndOfResponse: 43 pass 44 trans.commit() 45 return trans.get_response_code() 46 except: 47 48 # NOTE: Error conditions should be investigated further, along with 49 # NOTE: other response states. 50 51 if handle_errors: 52 return apache.HTTP_INTERNAL_SERVER_ERROR 53 else: 54 raise 55 56 def authenticate(request, authenticator, handle_errors=1): 57 58 """ 59 Dispatch the given 'request' to the application-specific 'authenticator'. 60 The optional 'handle_errors' flag, if set to false, causes tracebacks to be 61 displayed in the browser. 62 """ 63 64 trans = WebStack.ModPython.Transaction(request) 65 66 # NOTE: Resource pooling may be appropriate. 67 # NOTE: Forbidden access is not yet considered here. 68 69 try: 70 if authenticator.authenticate(trans): 71 return apache.OK 72 else: 73 return apache.HTTP_UNAUTHORIZED 74 except: 75 76 # NOTE: Error conditions should be investigated further, along with 77 # NOTE: other response states. 78 79 if handle_errors: 80 return apache.HTTP_INTERNAL_SERVER_ERROR 81 else: 82 raise 83 84 # vim: tabstop=4 expandtab shiftwidth=4