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