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 abody
attribute, astatus
attribute, and aheaders
attribute.- paste.recursive.script_name:
The
SCRIPT_NAME
at the point that recursive lives. Only paths underneath this path can be redirected to.- paste.recursive.old_path_info:
A list of previous
PATH_INFO
values 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.forward
andpaste.recursive.include
environmental 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.
url
The URL to forward to starting with a
/
and relative toRecursiveMiddleware
. URL fragments can also contain query strings so/error?code=404
would be a valid URL fragment.environ
An altertative WSGI environment dictionary to use for the forwarded request. If specified is used instead of the
url_fragment
factory
If specifed
factory
is used instead ofurl
orenviron
.factory
is 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
RecursiveMiddleware
present) :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
/hello
you would get aHello World!
message. If you ran the application and visited/not_found
aForwardRequestException
would be raised and the caught by theRecursiveMiddleware
. TheRecursiveMiddleware
would then return the headers and response from the/error
URL but would display a404 Not found
status message.You could also specify an
environ
dictionary 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)