1 #!/usr/bin/env python 2 3 """ 4 WSGI 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 21 """ 22 23 import WebStack.WSGI 24 from WebStack.Generic import EndOfResponse 25 from WebStack.Adapters.Helpers.wsgi_cgi import run_with_cgi 26 27 class WSGIAdapter: 28 29 "A WSGI adapter class." 30 31 def __init__(self, resource, authenticator=None, handle_errors=1): 32 33 """ 34 Initialise the adapter with the given WebStack 'resource' and the 35 optional 'authenticator'. The optional 'handle_errors' parameter (if 36 true) causes handlers to deal with uncaught exceptions cleanly. 37 """ 38 39 self.resource = resource 40 self.authenticator = authenticator 41 self.handle_errors = handle_errors 42 43 def __call__(self, environ, start_response): 44 45 """ 46 Dispatch to the root application-specific 'resource'. Return a list of 47 strings comprising the response body text. 48 """ 49 50 # NOTE: It would be best to give start_response to the transaction so 51 # NOTE: that the underlying response's write method can be used by the 52 # NOTE: transaction directly. Unfortunately, WebStack doesn't provide 53 # NOTE: any means of declaring when the headers have been set and when 54 # NOTE: response body output is the only thing to be subsequently 55 # NOTE: produced. 56 57 trans = WebStack.WSGI.Transaction(environ) 58 59 try: 60 if self.authenticator is None or self.authenticator.authenticate(trans): 61 try: 62 self.resource.respond(trans) 63 except EndOfResponse: 64 pass 65 except: 66 if self.handle_errors: 67 trans.set_response_code(500) # Internal error 68 else: 69 raise 70 else: 71 trans.set_response_code(401) # Unauthorized 72 trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( 73 self.authenticator.get_auth_type(), self.authenticator.get_realm())) 74 finally: 75 trans.commit() 76 77 # NOTE: Provide sensible messages. 78 # NOTE: Ignoring the write method returned by start_response. 79 80 start_response( 81 "%s WebStack status" % trans.get_response_code(), 82 trans.get_wsgi_headers() 83 ) 84 return [trans.get_wsgi_content()] 85 86 def deploy(resource, authenticator=None, address=None, handle_errors=1): 87 88 """ 89 Deploy the given 'resource', with the given optional 'authenticator', at the 90 given optional 'address', where 'address' is a 2-tuple of the form 91 (host_string, port_integer). 92 93 NOTE: The 'address' is ignored with the current WSGI implementation. 94 95 The optional 'handle_errors' flag (true by default) specifies whether error 96 conditions are handled gracefully. 97 """ 98 99 handler = WSGIAdapter(resource, authenticator, handle_errors) 100 run_with_cgi(handler) 101 102 # vim: tabstop=4 expandtab shiftwidth=4