paste.recursive – internal requests¶
Middleware to make internal requests and forward requests internally.
When applied, several keys are added to the environment that will allow you to trigger recursive redirects and forwards.
- paste.recursive.include:
When you call
environ['paste.recursive.include'](new_path_info)a response will be returned. The response has abodyattribute, astatusattribute, and aheadersattribute.- paste.recursive.script_name:
The
SCRIPT_NAMEat the point that recursive lives. Only paths underneath this path can be redirected to.- paste.recursive.old_path_info:
A list of previous
PATH_INFOvalues from previous redirects.
Raise ForwardRequestException(new_path_info) to do a forward
(aborting the current request).
Module Contents¶
- class paste.recursive.RecursiveMiddleware(application, global_conf=None)¶
A WSGI middleware that allows for recursive and forwarded calls. All these calls go to the same ‘application’, but presumably that application acts differently with different URLs. The forwarded URLs must be relative to this container.
Interface is entirely through the
paste.recursive.forwardandpaste.recursive.includeenvironmental keys.
- paste.recursive.ForwardRequestException(url=None, environ={}, factory=None, path_info=None)¶
Used to signal that a request should be forwarded to a different location.
urlThe URL to forward to starting with a
/and relative toRecursiveMiddleware. URL fragments can also contain query strings so/error?code=404would be a valid URL fragment.environAn altertative WSGI environment dictionary to use for the forwarded request. If specified is used instead of the
url_fragmentfactoryIf specifed
factoryis used instead ofurlorenviron.factoryis a callable that takes a WSGI application object as the first argument and returns an initialised WSGI middleware which can alter the forwarded response.
Basic usage (must have
RecursiveMiddlewarepresent) :from paste.recursive import ForwardRequestException def app(environ, start_response): if environ['PATH_INFO'] == '/hello': start_response("200 OK", [('Content-type', 'text/plain')]) return [b'Hello World!'] elif environ['PATH_INFO'] == '/error': start_response("404 Not Found", [('Content-type', 'text/plain')]) return [b'Page not found'] else: raise ForwardRequestException('/error') from paste.recursive import RecursiveMiddleware app = RecursiveMiddleware(app)
If you ran this application and visited
/helloyou would get aHello World!message. If you ran the application and visited/not_foundaForwardRequestExceptionwould be raised and the caught by theRecursiveMiddleware. TheRecursiveMiddlewarewould then return the headers and response from the/errorURL but would display a404 Not foundstatus message.You could also specify an
environdictionary instead of a url. Using the same example as before:def app(environ, start_response): ... same as previous example ... else: new_environ = environ.copy() new_environ['PATH_INFO'] = '/error' raise ForwardRequestException(environ=new_environ)
Finally, if you want complete control over every aspect of the forward you can specify a middleware factory. For example to keep the old status code but use the headers and resposne body from the forwarded response you might do this:
from paste.recursive import ForwardRequestException from paste.recursive import RecursiveMiddleware from paste.errordocument import StatusKeeper def app(environ, start_response): if environ['PATH_INFO'] == '/hello': start_response("200 OK", [('Content-type', 'text/plain')]) return [b'Hello World!'] elif environ['PATH_INFO'] == '/error': start_response("404 Not Found", [('Content-type', 'text/plain')]) return [b'Page not found'] else: def factory(app): return StatusKeeper(app, status='404 Not Found', url='/error') raise ForwardRequestException(factory=factory) app = RecursiveMiddleware(app)