File: //opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyo
�
a�Nc           @   s)  d  Z  d d l m Z d d l Z d g Z d d g Z d e f d �  �  YZ d e f d	 �  �  YZ	 d e f d
 �  �  YZ
 d e f d �  �  YZ d e f d
 �  �  YZ
 d e
 f d �  �  YZ d e
 f d �  �  YZ d e f d �  �  YZ d e
 f d �  �  YZ d e f d �  �  YZ d �  Z e  e _  d S(   s  
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 a ``body`` attribute, a
      ``status`` attribute, and a ``headers`` 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).
i����(   t   StringIONt   RecursiveMiddlewaret   ForwardRequestExceptiont
   RecursionLoopc           B   s   e  Z d  Z RS(   s*   Raised when a recursion enters into a loop(   t   __name__t
   __module__t   __doc__(    (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR       s   t   CheckForRecursionMiddlewarec           B   s   e  Z d  �  Z d �  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   appt   env(   t   selfR   R	   (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyt   __init__%   s    	c         C   s�   | j  d d � } | |  j j  d g  � k rM t d | |  j d f � � n  |  j j d g  � } | j |  j j  d d � � |  j | | � S(   Nt	   PATH_INFOt    s   paste.recursive.old_path_infosG   Forwarding loop detected; %r visited twice (internal redirect path: %s)(   t   getR	   R   t
   setdefaultt   appendR   (   R
   t   environt   start_responset	   path_infot
   old_path_info(    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyt   __call__)   s    (   R   R   R   R   (    (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR   $   s   	c           B   s#   e  Z d  Z d d � Z d �  Z RS(   s}  
    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`` and
    ``paste.recursive.include`` environmental keys.
    c         C   s
   | |  _  d  S(   N(   t   application(   R
   R   t   global_conf(    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR   A   s    c         C   s�   t  |  j | | � | d <t |  j | | � | d <t |  j | | � | d <| j d d � } | | d <y |  j | | � SWn5 t k
 r� } t | j |  � | � } | | | � SXd  S(   Ns   paste.recursive.forwards   paste.recursive.includes    paste.recursive.include_app_itert   SCRIPT_NAMER
   s   paste.recursive.script_name(   t	   ForwarderR   t   Includert   IncluderAppIterR   R   R   t   factory(   R
   R   R   t   my_script_namet   et
   middleware(    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR   D   s(    
N(   R   R   R   t   NoneR   R   (    (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR   5   s   
c           B   s#   e  Z d  Z d i  d d d � Z RS(   s
  
    Used to signal that a request should be forwarded to a different location.
    ``url``
        The URL to forward to starting with a ``/`` and relative to
        ``RecursiveMiddleware``. 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 of ``url`` or ``environ``.
        ``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) :
    .. code-block:: python
        from paste.recursive import ForwardRequestException
        def app(environ, start_response):
            if environ['PATH_INFO'] == '/hello':
                start_response("200 OK", [('Content-type', 'text/plain')])
                return ['Hello World!']
            elif environ['PATH_INFO'] == '/error':
                start_response("404 Not Found", [('Content-type', 'text/plain')])
                return ['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 a
    ``Hello World!`` message. If you ran the application and visited
    ``/not_found`` a ``ForwardRequestException`` would be raised and the caught
    by the ``RecursiveMiddleware``. The ``RecursiveMiddleware`` would then
    return the headers and response from the ``/error`` URL but would display
    a ``404 Not found`` status message.
    You could also specify an ``environ`` dictionary instead of a url. Using
    the same example as before:
    .. code-block:: python
        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:
    .. code-block:: python
        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 ['Hello World!']
            elif environ['PATH_INFO'] == '/error':
                start_response("404 Not Found", [('Content-type', 'text/plain')])
                return ['Page not found']
            else:
                def factory(app):
                    return StatusKeeper(app, status='404 Not Found', url='/error')
                raise ForwardRequestException(factory=factory)
        app = RecursiveMiddleware(app)
    c            sP  | r � r t  d � � n | r6 � r6 t  d � � n  � rQ � rQ t  d � � n  | r� � ss t j d t d � n t  d � � | |  _ n  � r� d t � � k r� � |  _ n  d t f d	 �  �  Y�  t |  d
 � r� |  j � �  � f d �  } | |  _ nQ � r�  � f d �  } | |  _ n- � rC�  � f d
 �  } | |  _ n	 | |  _ d  S(   Ns?   You cannot specify factory and a url in ForwardRequestExceptionsA   You cannot specify factory and environ in ForwardRequestExceptions=   You cannot specify environ and url in ForwardRequestExceptionsg   ForwardRequestException(path_info=...) has been deprecated; please use ForwardRequestException(url=...)i   s;   You cannot use url and path_info in ForwardRequestExceptiont   ?t!   ForwardRequestExceptionMiddlewarec           B   s   e  Z d  �  Z RS(   c         S   s
   | |  _  d  S(   N(   R   (   R
   R   (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR   �   s    (   R   R   R   (    (    (    s@   /opt/alt/python27/lib/python2.7/site-packages/paste/recursive.pyR"