diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-04-16 20:31:40 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-04-16 20:31:40 +0000 |
| commit | d1209e077a1607440677a363651bda4393d72d82 (patch) | |
| tree | 90a202a00438645d1624445cf7c34f8a73a4a46a /repoze/bfg/wsgi.py | |
| parent | 168c10641aecba898616c01a25091b745362e366 (diff) | |
| download | pyramid-d1209e077a1607440677a363651bda4393d72d82.tar.gz pyramid-d1209e077a1607440677a363651bda4393d72d82.tar.bz2 pyramid-d1209e077a1607440677a363651bda4393d72d82.zip | |
- The interface for ``repoze.bfg.interfaces.ITraverser`` and the
built-in implementations that implement the interface
(``repoze.bfg.traversal.ModelGraphTraverser``, and
``repoze.bfg.urldispatch.RoutesModelTraverser``) now expect the
``__call__`` method of an ITraverser to return 3 additional
arguments: ``traversed``, ``virtual_root``, and
``virtual_root_path`` (the old contract was that the ``__call__``
method of an ITraverser returned; three arguments, the contract new
is that it returns six). ``traversed`` will be a sequence of
Unicode names that were traversed (including the virtual root path,
if any) or ``None`` if no traversal was performed, ``virtual_root``
will be a model object representing the virtual root (or the
physical root if traversal was not performed), and
``virtual_root_path`` will be a sequence representing the virtual
root path (a sequence of Unicode names) or ``None`` if traversal was
not performed.
Six arguments are now returned from BFG ITraversers. They are
returned in this order: ``context``, ``view_name``, ``subpath``,
``traversed``, ``virtual_root``, and ``virtual_root_path``.
Places in the BFG code which called an ITraverser continue to accept
a 3-argument return value, although BFG will generate and log a
warning when one is encountered.
- The request object now has the following attributes: ``traversed``
(the sequence of names traversed or ``None`` if traversal was not
performed), ``virtual_root`` (the model object representing the
virtual root, including the virtual root path if any), and
``virtual_root_path`` (the seuquence of names representing the
virtual root path or ``None`` if traversal was not performed).
- A new decorator named ``wsgiapp2`` was added to the
``repoze.bfg.wsgi`` module. This decorator performs the same
function as ``repoze.bfg.wsgi.wsgiapp`` except it fixes up the
``SCRIPT_NAME``, and ``PATH_INFO`` environment values before
invoking the WSGI subapplication.
- The ``repoze.bfg.testing.DummyRequest`` object now has default
attributes for ``traversed``, ``virtual_root``, and
``virtual_root_path``.
- The RoutesModelTraverser now behaves more like the Routes
"RoutesMiddleware" object when an element in the match dict is named
``path_info`` (usually when there's a pattern like
``http://foo/*path_info``). When this is the case, the
``PATH_INFO`` environment variable is set to the value in the match
dict, and the ``SCRIPT_NAME`` is appended to with the prefix of the
original ``PATH_INFO`` not including the value of the new variable.
- The notfound debug now shows the traversed path, the virtual root,
and the virtual root path too.
Diffstat (limited to 'repoze/bfg/wsgi.py')
| -rw-r--r-- | repoze/bfg/wsgi.py | 72 |
1 files changed, 70 insertions, 2 deletions
diff --git a/repoze/bfg/wsgi.py b/repoze/bfg/wsgi.py index 667de4412..9e524ce6f 100644 --- a/repoze/bfg/wsgi.py +++ b/repoze/bfg/wsgi.py @@ -6,8 +6,47 @@ except ImportError: # < 2.5 from repoze.bfg.functional import wraps +from repoze.bfg.traversal import quote_path_segment + def wsgiapp(wrapped): - """ Decorator to turn a WSGI application into a repoze.bfg view callable. + """ Decorator to turn a WSGI application into a repoze.bfg view + callable. This decorator differs from the `wsgiapp2`` decorator + inasmuch as fixups of ``PATH_INFO`` and ``SCRIPT_NAME`` within the + WSGI environment *are not* performed before the application is + invoked. + + E.g.:: + + @wsgiapp + def hello_world(environ, start_response): + body = 'Hello world' + start_response('200 OK', [ ('Content-Type', 'text/plain'), + ('Content-Length', len(body)) ] ) + return [body] + + Allows the following view declaration to be made:: + + <view + view=".views.hello_world" + name="hello_world.txt" + context="*" + /> + + The wsgiapp decorator will convert the result of the WSGI + application to a Response and return it to repoze.bfg as if the + WSGI app were a repoze.bfg view. + + """ + def decorator(context, request): + return request.get_response(wrapped) + return wraps(wrapped)(decorator) # pickleability + +def wsgiapp2(wrapped): + """ Decorator to turn a WSGI application into a repoze.bfg view + callable. This decorator differs from the `wsgiapp`` decorator + inasmuch as fixups of ``PATH_INFO`` and ``SCRIPT_NAME`` within the + WSGI environment *are* performed before the application is + invoked. E.g.:: @@ -28,9 +67,38 @@ def wsgiapp(wrapped): The wsgiapp decorator will convert the result of the WSGI application to a Response and return it to repoze.bfg as if the - WSGI app were a repoze.bfg view. + WSGI app were a repoze.bfg view. The ``SCRIPT_NAME`` and + ``PATH_INFO`` values present in the WSGI environment are fixed up + before the application is invoked. """ def decorator(context, request): + traversed = request.traversed + if traversed is not None: + # We need to fix up PATH_INFO and SCRIPT_NAME to give the + # subapplication the right information, sans the info it + # took to traverse here. If ``traversed`` is None here, + # it means that no traversal was done. For example, it + # will be None in the case that the context is one + # obtained via a Routes match (Routes 'traversal' doesn't + # actually traverse). If this view is invoked on a Routes + # context, this fixup is not invoked. Instead, the route + # used to reach it should use *path_info in the actual + # route pattern to get a similar fix-up done. + vroot_path = request.virtual_root_path or [] + view_name = request.view_name + subpath = request.subpath or [] + script_list = traversed[len(vroot_path):] + script_list = [ quote_path_segment(name) for name in script_list ] + if view_name: + script_list.append(quote_path_segment(view_name)) + script_name = '/' + '/'.join(script_list) + path_list = [ quote_path_segment(name) for name in subpath ] + path_info = '/' + '/'.join(path_list) + request.environ['PATH_INFO'] = path_info + script_name = request.environ['SCRIPT_NAME'] + script_name + if script_name.endswith('/'): + script_name = script_name[:-1] + request.environ['SCRIPT_NAME'] = script_name return request.get_response(wrapped) return wraps(wrapped)(decorator) # pickleability |
