paulb@302 | 1 | #!/usr/bin/env python |
paulb@302 | 2 | |
paulb@302 | 3 | """ |
paulb@302 | 4 | WSGI adapter. |
paulb@302 | 5 | """ |
paulb@302 | 6 | |
paulb@302 | 7 | import WebStack.WSGI |
paulb@302 | 8 | from WebStack.Generic import EndOfResponse |
paulb@302 | 9 | |
paulb@302 | 10 | class WSGIAdapter: |
paulb@302 | 11 | |
paulb@302 | 12 | "A WSGI adapter class." |
paulb@302 | 13 | |
paulb@310 | 14 | def __init__(self, resource, authenticator=None, handle_errors=1): |
paulb@302 | 15 | |
paulb@302 | 16 | """ |
paulb@302 | 17 | Initialise the adapter with the given WebStack 'resource' and the |
paulb@310 | 18 | optional 'authenticator'. The optional 'handle_errors' parameter (if |
paulb@310 | 19 | true) causes handlers to deal with uncaught exceptions cleanly. |
paulb@302 | 20 | """ |
paulb@302 | 21 | |
paulb@302 | 22 | self.resource = resource |
paulb@302 | 23 | self.authenticator = authenticator |
paulb@310 | 24 | self.handle_errors = handle_errors |
paulb@302 | 25 | |
paulb@302 | 26 | def __call__(self, environ, start_response): |
paulb@302 | 27 | |
paulb@302 | 28 | """ |
paulb@302 | 29 | Dispatch to the root application-specific 'resource'. Return a list of |
paulb@302 | 30 | strings comprising the response body text. |
paulb@302 | 31 | """ |
paulb@302 | 32 | |
paulb@305 | 33 | # NOTE: It would be best to give start_response to the transaction so |
paulb@305 | 34 | # NOTE: that the underlying response's write method can be used by the |
paulb@305 | 35 | # NOTE: transaction directly. Unfortunately, WebStack doesn't provide |
paulb@305 | 36 | # NOTE: any means of declaring when the headers have been set and when |
paulb@305 | 37 | # NOTE: response body output is the only thing to be subsequently |
paulb@305 | 38 | # NOTE: produced. |
paulb@305 | 39 | |
paulb@302 | 40 | trans = WebStack.WSGI.Transaction(environ) |
paulb@302 | 41 | |
paulb@302 | 42 | try: |
paulb@302 | 43 | if self.authenticator is None or self.authenticator.authenticate(trans): |
paulb@302 | 44 | try: |
paulb@302 | 45 | self.resource.respond(trans) |
paulb@302 | 46 | except EndOfResponse: |
paulb@302 | 47 | pass |
paulb@310 | 48 | except: |
paulb@310 | 49 | if self.handle_errors: |
paulb@310 | 50 | trans.set_response_code(500) # Internal error |
paulb@310 | 51 | else: |
paulb@310 | 52 | raise |
paulb@302 | 53 | else: |
paulb@302 | 54 | trans.set_response_code(401) # Unauthorized |
paulb@302 | 55 | trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( |
paulb@302 | 56 | self.authenticator.get_auth_type(), self.authenticator.get_realm())) |
paulb@302 | 57 | finally: |
paulb@302 | 58 | trans.commit() |
paulb@302 | 59 | |
paulb@302 | 60 | # NOTE: Provide sensible messages. |
paulb@305 | 61 | # NOTE: Ignoring the write method returned by start_response. |
paulb@302 | 62 | |
paulb@302 | 63 | start_response( |
paulb@302 | 64 | "%s WebStack status" % trans.get_response_code(), |
paulb@302 | 65 | trans.get_wsgi_headers() |
paulb@302 | 66 | ) |
paulb@302 | 67 | return [trans.get_wsgi_content()] |
paulb@302 | 68 | |
paulb@302 | 69 | # vim: tabstop=4 expandtab shiftwidth=4 |