ó
`¾Tc           @   sø   d  Z  y d d l m Z Wn! e k
 r= d d l m Z n Xd d l m Z m Z m Z d d l	 m
 Z
 d d l m Z m Z d e f d „  ƒ  YZ e Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sl  
    werkzeug.contrib.fixers
    ~~~~~~~~~~~~~~~~~~~~~~~

    .. versionadded:: 0.5

    This module includes various helpers that fix bugs in web servers.  They may
    be necessary for some versions of a buggy web server but not others.  We try
    to stay updated with the status of the bugs as good as possible but you have
    to make sure whether they fix the problem you encounter.

    If you notice bugs in webservers not fixed in this module consider
    contributing a patch.

    :copyright: Copyright 2009 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿ(   t   unquote(   t   parse_options_headert   parse_cache_control_headert   parse_set_header(   t	   UserAgent(   t   Headerst   ResponseCacheControlt
   CGIRootFixc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s×  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

    .. versionchanged:: 0.9
       Added `app_root` parameter and renamed from `LighttpdCGIRootFix`.

    :param app: the WSGI application
    :param app_root: Defaulting to ``'/'``, you can set this to something else
        if your app is mounted somewhere else.
    t   /c         C   s   | |  _  | |  _ d  S(   N(   t   appt   app_root(   t   selfR	   R
   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   __init__*   s    	c         C   sk   d | k s | d d k  rE | j  d d ƒ | j  d d ƒ | d <n  |  j j d ƒ | d <|  j | | ƒ S(   Nt   SERVER_SOFTWAREs   lighttpd/1.4.28t   SCRIPT_NAMEt    t	   PATH_INFOR   (   t   getR
   t   stripR	   (   R   t   environt   start_response(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   __call__.   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR      s   t   PathInfoFromRequestUriFixc           B   s    e  Z d  Z d „  Z d „  Z RS(   s  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
    c         C   s   | |  _  d  S(   N(   R	   (   R   R	   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   N   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   R   t   ?i   i    R   (   R   R   R   (   R    R   t
   startswitht   lent   splitR	   (   R   R   R   t   keyt   request_urit   script_name(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   Q   s    (   R   R   R   R   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ?   s   	t   ProxyFixc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s  This middleware can be applied to add HTTP proxy support to an
    application that was not designed with HTTP proxies in mind.  It
    sets `REMOTE_ADDR`, `HTTP_HOST` from `X-Forwarded` headers.

    If you have more than one proxy server in front of your app, set
    `num_proxies` accordingly.

    Do not use this middleware in non-proxy setups for security reasons.

    The original values of `REMOTE_ADDR` and `HTTP_HOST` are stored in
    the WSGI environment as `werkzeug.proxy_fix.orig_remote_addr` and
    `werkzeug.proxy_fix.orig_http_host`.

    :param app: the WSGI application
    :param num_proxies: the number of proxy servers in front of the app.
    i   c         C   s   | |  _  | |  _ d  S(   N(   R	   t   num_proxies(   R   R	   R%   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   p   s    	c         C   s(   t  | ƒ |  j k r$ | d |  j Sd S(   sù   Selects the new remote addr from the given list of ips in
        X-Forwarded-For.  By default it picks the one that the `num_proxies`
        proxy server provides.  Before 0.9 it would always pick the first.

        .. versionadded:: 0.8
        iÿÿÿÿN(   R   R%   (   R   t   forwarded_for(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   get_remote_addrt   s    c   	      C   s	  | j  } | d d ƒ } | d d ƒ j d ƒ } | d d ƒ } | j i | d ƒ d 6| d ƒ d	 6| d
 ƒ d 6ƒ g  g  | D] } | j ƒ  ^ q} D] } | r“ | ^ q“ } |  j | ƒ } | d  k	 rÓ | | d <n  | ræ | | d
 <n  | rù | | d <n  |  j | | ƒ S(   Nt   HTTP_X_FORWARDED_PROTOR   t   HTTP_X_FORWARDED_FORt   ,t   HTTP_X_FORWARDED_HOSTs   wsgi.url_schemes'   werkzeug.proxy_fix.orig_wsgi_url_schemet   REMOTE_ADDRs#   werkzeug.proxy_fix.orig_remote_addrt	   HTTP_HOSTs!   werkzeug.proxy_fix.orig_http_host(   R   R    t   updateR   R'   t   NoneR	   (	   R   R   R   t   gettert   forwarded_protoR&   t   forwarded_hostt   xt   remote_addr(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ~   s"    		8(   R   R   R   R   R'   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/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.
    c         C   sA   | |  _  t d „  | p d Dƒ ƒ |  _ t | p4 d ƒ |  _ d  S(   Nc         s   s   |  ] } | j  ƒ  Vq d  S(   N(   t   lower(   t   .0R3   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pys	   <genexpr>¥   s    (    (    (   R	   t   sett   remove_headerst   listt   add_headers(   R   R	   R9   R;   (    (    s]   /var/www/send.findwatt.com/datamanager/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(   R6   R9   t   appendR;   (   t   statust   headerst   exc_infot   new_headersR!   t   value(   R   R   (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   rewriting_start_response©   s    (   R/   R	   (   R   R   R   RB   (    (   R   R   s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ¨   s    N(   R   R   R   R/   R   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR5   “   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: http://support.microsoft.com/kb/824847/en-us

    -   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.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   R	   t   fix_varyt
   fix_attach(   R   R	   RD   RE   (    (    s]   /var/www/send.findwatt.com/datamanager/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(   RD   R   R   t   popR/   RE   R   t   discardt	   to_headerR   R   t   no_cachet   Falset   no_store(	   R   R   R>   R=   t   headert   mimetypet   optionsRG   t   cc(    (    s]   /var/www/send.findwatt.com/datamanager/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   RT   t   to_wsgi_list(   R=   R>   R?   (   R   R   R   (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   fixing_start_responseê   s    (   R/   R	   (   R   R   R   RV   (    (   R   R   R   s]   /var/www/send.findwatt.com/datamanager/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	   RW   (   R   R   R   t   ua(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyR   ð   s    N(	   R   R   R   t   TrueR   R/   RT   RW   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyRC   ³   s
   	N(   R   t   urllibR    t   ImportErrort   urllib.parset   werkzeug.httpR   R   R   t   werkzeug.useragentsR   t   werkzeug.datastructuresR   R   t   objectR   t   LighttpdCGIRootFixR   R$   R5   RC   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/fixers.pyt   <module>   s   5 