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