ó
‰]c           @   sB  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l	 m
 Z d d
 l m Z y d d l m Z Wn! e k
 r¹ d d l m Z n Xd 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 S(   s  
Fixers
======

.. warning::
    .. deprecated:: 0.15
        ``ProxyFix`` has moved to :mod:`werkzeug.middleware.proxy_fix`.
        All other code in this module is deprecated and will be removed
        in version 1.0.

.. versionadded:: 0.5

This module includes various helpers that fix web server behavior.

.. autoclass:: ProxyFix
   :members:

.. autoclass:: CGIRootFix

.. autoclass:: PathInfoFromRequestUriFix

.. autoclass:: HeaderRewriterFix

.. autoclass:: InternetExplorerFix

:copyright: 2007 Pallets
:license: BSD-3-Clause
iÿÿÿÿNi   (   t   Headers(   t   ResponseCacheControl(   t   parse_cache_control_header(   t   parse_options_header(   t   parse_set_header(   t   ProxyFix(   t	   UserAgent(   t   unquotet
   CGIRootFixc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s5  Wrap the application in this middleware if you are using FastCGI
    or CGI and you have problems with your app root being set to the CGI
    script's path instead of the path users are going to visit.

    :param app: the WSGI application
    :param app_root: Defaulting to ``'/'``, you can set this to
        something else if your app is mounted somewhere else.

    .. deprecated:: 0.15
        This middleware will be removed in version 1.0.

    .. versionchanged:: 0.9
        Added `app_root` parameter and renamed from
        ``LighttpdCGIRootFix``.
    t   /c         C   s5   t  j d t d d ƒ| |  _ | j d ƒ |  _ d  S(   NsQ   'CGIRootFix' is deprecated as of version 0.15 and will be removed in version 1.0.t
   stackleveli   R	   (   t   warningst   warnt   DeprecationWarningt   appt   stript   app_root(   t   selfR   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   __init__>   s    	c         C   s   |  j  | d <|  j | | ƒ S(   Nt   SCRIPT_NAME(   R   R   (   R   t   environt   start_response(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   __call__H   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   -   s   
t   LighttpdCGIRootFixc           B   s   e  Z d  „  Z RS(   c         O   s3   t  j d t d d ƒt t |  ƒ j | | Ž  d  S(   NsR   'LighttpdCGIRootFix' is renamed 'CGIRootFix'. Both will be removed in version 1.0.R
   i   (   R   R   R   t   superR   R   (   R   t   argst   kwargs(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   N   s
    (   R   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   M   s   t   PathInfoFromRequestUriFixc           B   s    e  Z d  Z d „  Z d „  Z RS(   sq  On windows environment variables are limited to the system charset
    which makes it impossible to store the `PATH_INFO` variable in the
    environment without loss of information on some systems.

    This is for example a problem for CGI scripts on a Windows Apache.

    This fixer works by recreating the `PATH_INFO` from `REQUEST_URI`,
    `REQUEST_URL`, or `UNENCODED_URL` (whatever is available).  Thus the
    fix can only be applied if the webserver supports either of these
    variables.

    :param app: the WSGI application

    .. deprecated:: 0.15
        This middleware will be removed in version 1.0.
    c         C   s#   t  j d t d d ƒ| |  _ d  S(   Ns`   'PathInfoFromRequestUriFix' is deprecated as of version 0.15 and will be removed in version 1.0.R
   i   (   R   R   R   R   (   R   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   j   s
    c         C   s’   x d
 D]w } | | k r q n  t  | | ƒ } t  | j d d ƒ ƒ } | j | ƒ r | t | ƒ j d d ƒ d | d	 <Pq q W|  j | | ƒ S(   Nt   REQUEST_URLt   REQUEST_URIt   UNENCODED_URLR   t    t   ?i   i    t	   PATH_INFO(   R   R    R!   (   R   t   gett
   startswitht   lent   splitR   (   R   R   R   t   keyt   request_urit   script_name(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   s   s    $(   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   X   s   		R   c           B   s   e  Z d  Z d „  Z RS(   s²   
    .. deprecated:: 0.15
        ``werkzeug.contrib.fixers.ProxyFix`` has moved to
        :mod:`werkzeug.middleware.proxy_fix`. This import will be
        removed in 1.0.
    c         O   s3   t  j d t d d ƒt t |  ƒ j | | Ž  d  S(   Ns¢   'werkzeug.contrib.fixers.ProxyFix' has moved to 'werkzeug.middleware.proxy_fix.ProxyFix'. This import is deprecated as of version 0.15 and will be removed in 1.0.R
   i   (   R   R   R   R   R   R   (   R   R   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ‡   s
    (   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR      s   t   HeaderRewriterFixc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   sÓ  This middleware can remove response headers and add others.  This
    is for example useful to remove the `Date` header from responses if you
    are using a server that adds that header, no matter if it's present or
    not or to add `X-Powered-By` headers::

        app = HeaderRewriterFix(app, remove_headers=['Date'],
                                add_headers=[('X-Powered-By', 'WSGI')])

    :param app: the WSGI application
    :param remove_headers: a sequence of header keys that should be
                           removed.
    :param add_headers: a sequence of ``(key, value)`` tuples that should
                        be added.

    .. deprecated:: 0.15
        This middleware will be removed in 1.0.
    c         C   sW   t  j d t d d ƒ| |  _ t d „  | p1 d Dƒ ƒ |  _ t | pJ d ƒ |  _ d  S(   NsX   'HeaderRewriterFix' is deprecated as of version 0.15 and will be removed in version 1.0.R
   i   c         s   s   |  ] } | j  ƒ  Vq d  S(   N(   t   lower(   t   .0t   x(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pys	   <genexpr>­   s    (    (    (   R   R   R   R   t   sett   remove_headerst   listt   add_headers(   R   R   R1   R3   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ¥   s    	c            s%   d  ‡  ‡ f d † } ˆ  j | | ƒ S(   Nc            se   g  } x? | D]7 \ } } | j  ƒ  ˆ  j k r | j | | f ƒ q q W| ˆ  j 7} ˆ |  | | ƒ S(   N(   R-   R1   t   appendR3   (   t   statust   headerst   exc_infot   new_headersR)   t   value(   R   R   (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   rewriting_start_response±   s    (   t   NoneR   (   R   R   R   R:   (    (   R   R   s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   °   s    N(   R   R   R   R;   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR,   ’   s   t   InternetExplorerFixc           B   s;   e  Z d  Z e e d „ Z d d „ Z d „  Z d „  Z RS(   sí  This middleware fixes a couple of bugs with Microsoft Internet
    Explorer.  Currently the following fixes are applied:

    -   removing of `Vary` headers for unsupported mimetypes which
        causes troubles with caching.  Can be disabled by passing
        ``fix_vary=False`` to the constructor.
        see: https://support.microsoft.com/en-us/help/824847

    -   removes offending headers to work around caching bugs in
        Internet Explorer if `Content-Disposition` is set.  Can be
        disabled by passing ``fix_attach=False`` to the constructor.

    If it does not detect affected Internet Explorer versions it won't touch
    the request / response.

    .. deprecated:: 0.15
        This middleware will be removed in 1.0.
    c         C   s5   t  j d t d d ƒ| |  _ | |  _ | |  _ d  S(   NsZ   'InternetExplorerFix' is deprecated as of version 0.15 and will be removed in version 1.0.R
   i   (   R   R   R   R   t   fix_varyt
   fix_attach(   R   R   R=   R>   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   Õ   s    		c   	      C   s-  |  j  rO | j d d ƒ } t | ƒ \ } } | d k rO | j d d  ƒ qO n  |  j r)d | k r)t | j d d ƒ ƒ } | j d	 ƒ | j ƒ  } | s® | j d d ƒ n
 | | d
 <| j d d ƒ } | r)t	 | d t
 ƒ} d  | _ t | _ | j ƒ  } | s| j d d ƒ q&| | d <q)n  d  S(   Ns   content-typeR"   s	   text/htmls
   text/plains	   text/sgmlt   varys   content-dispositiont   pragmas   no-cachet   Pragmas   cache-controlt   clss   Cache-Control(   s	   text/htmls
   text/plains	   text/sgml(   R=   R%   R   t   popR;   R>   R   t   discardt	   to_headerR   R   t   no_cachet   Falset   no_store(	   R   R   R6   R5   t   headert   mimetypet   optionsR@   t   cc(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   fix_headersà   s*    	
		c            s(   d  ‡  ‡ ‡ f d † } ˆ j ˆ  | ƒ S(   Nc            s5   t  | ƒ } ˆ j ˆ  | |  ƒ ˆ |  | j ƒ  | ƒ S(   N(   R    RM   t   to_wsgi_list(   R5   R6   R7   (   R   R   R   (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   fixing_start_responseû   s    (   R;   R   (   R   R   R   RO   (    (   R   R   R   s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt	   run_fixedú   s    c         C   s;   t  | ƒ } | j d k r+ |  j | | ƒ S|  j | | ƒ S(   Nt   msie(   R   t   browserR   RP   (   R   R   R   t   ua(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR     s    N(	   R   R   R   t   TrueR   R;   RM   RP   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR<   ¼   s
   	(   R   R   t   datastructuresR    R   t   httpR   R   R   t   middleware.proxy_fixR   t	   _ProxyFixt
   useragentsR   t   urllib.parseR   t   ImportErrort   urllibt   objectR   R   R   R,   R<   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   <module>   s$    '*