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