def error_page(app, html): 'WSGI middleware for serving static "Internal Server Error" page.' app = lighten(app) error_response = ('500 Internal Server Error', [('Content-Type', 'text/html; charset=utf-8')], [html]) @lite def error_page_middleware(environ): try: return app(environ) except Exception: log.error('Uncaught exception', exc_info=True) return error_response return error_page_middleware
def latinator(app): from wsgi_lite import lite, lighten def middleware(environ): status, headers, body = app(environ) for name, value in headers: if name.lower() == 'content-type' and value == 'text/plain': break else: # Not text/plain, pass it through unchanged return status, headers, body # Strip content-length if present, else it'll be wrong headers = [ (name, value) for name, value in headers if name.lower() != 'content-length' ] def pliter(body): # Python 2.3 doesn't do generator expressions for data in body: yield piglatin(data) return status, headers, pliter(body) # Make sure that `app` can be invoked via the Lite protocol app = lighten(app) return lite(middleware)