view wsgintegrate/dispatcher.py @ 33:b582d2e32d92 default tip

bump version for release
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Jun 2024 16:17:49 -0700
parents 425663344481
children
line wrap: on
line source

"""
multi-application dispatcher for WSGI apps
"""

from webob import Request
from webob import exc

class Dispatcher(object):

    def __init__(self, *apps):
        self.apps = apps
        self.codes = set([404])

    def __call__(self, environ, start_response):
        request = Request(environ)
        for app in self.apps:
            try:
                response = request.get_response(app)
                if response.status_int in self.codes:
                    continue
                break
            except exc.HTTPNotFound:
                continue
            except:
                print("Dispatcher failure with app: {}".format(app))
                raise
        else:
            response = exc.HTTPNotFound()
        try:
            return response(environ, start_response)
        except:
            response.headerlist = [(i,str(j)) for i, j in response.headerlist]
            return response(environ, start_response)