1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Adapters/WSGI.py Wed Dec 01 00:56:03 2004 +0000
1.3 @@ -0,0 +1,54 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +WSGI adapter.
1.8 +"""
1.9 +
1.10 +import WebStack.WSGI
1.11 +from WebStack.Generic import EndOfResponse
1.12 +
1.13 +class WSGIAdapter:
1.14 +
1.15 + "A WSGI adapter class."
1.16 +
1.17 + def __init__(self, resource, authenticator=None):
1.18 +
1.19 + """
1.20 + Initialise the adapter with the given WebStack 'resource' and the
1.21 + optional 'authenticator'.
1.22 + """
1.23 +
1.24 + self.resource = resource
1.25 + self.authenticator = authenticator
1.26 +
1.27 + def __call__(self, environ, start_response):
1.28 +
1.29 + """
1.30 + Dispatch to the root application-specific 'resource'. Return a list of
1.31 + strings comprising the response body text.
1.32 + """
1.33 +
1.34 + trans = WebStack.WSGI.Transaction(environ)
1.35 +
1.36 + try:
1.37 + if self.authenticator is None or self.authenticator.authenticate(trans):
1.38 + try:
1.39 + self.resource.respond(trans)
1.40 + except EndOfResponse:
1.41 + pass
1.42 + else:
1.43 + trans.set_response_code(401) # Unauthorized
1.44 + trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
1.45 + self.authenticator.get_auth_type(), self.authenticator.get_realm()))
1.46 + finally:
1.47 + trans.commit()
1.48 +
1.49 + # NOTE: Provide sensible messages.
1.50 +
1.51 + start_response(
1.52 + "%s WebStack status" % trans.get_response_code(),
1.53 + trans.get_wsgi_headers()
1.54 + )
1.55 + return [trans.get_wsgi_content()]
1.56 +
1.57 +# vim: tabstop=4 expandtab shiftwidth=4