ó
`¾Tc           @   s  d  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 e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d d „ 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 S(   sÇ  
    werkzeug.contrib.lint
    ~~~~~~~~~~~~~~~~~~~~~

    .. versionadded:: 0.5

    This module provides a middleware that performs sanity checks of the WSGI
    application.  It checks that :pep:`333` is properly implemented and warns
    on some common HTTP errors such as non-empty responses for 304 status
    codes.

    This module provides a middleware, the :class:`LintMiddleware`.  Wrap your
    application with it and it will warn about common problems with WSGI and
    HTTP while your application is running.

    It's strongly recommended to use it during development.

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿ(   t   urlparse(   t   warn(   t   Headers(   t   is_entity_header(   t   FileWrapper(   t   string_typest   WSGIWarningc           B   s   e  Z d  Z RS(   s    Warning class for WSGI warnings.(   t   __name__t
   __module__t   __doc__(    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR      s   t   HTTPWarningc           B   s   e  Z d  Z RS(   s    Warning class for HTTP warnings.(   R   R   R	   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR
   #   s   i   c         C   s9   t  | ƒ t k	 r5 t t d |  | j j f ƒ ƒ n  d  S(   Ns   %s requires bytestrings, got %s(   t   typet   strR   R   t	   __class__R   (   t   contextt   objt
   stacklevel(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   check_string'   s    	t   InputStreamc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   _stream(   t   selft   stream(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   __init__/   s    c         G   sf   t  | ƒ d k r+ t t d ƒ d d ƒn+ t  | ƒ d k rV t t d ƒ d d ƒn  |  j j | Œ  S(   Ni    sž   wsgi does not guarantee an EOF marker on the input stream, thus making calls to wsgi.input.read() unsafe.  Conforming servers may never return from this call.R   i   i   s/   too many parameters passed to wsgi.input.read()(   t   lenR   R   R   t   read(   R   t   args(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   2   s    

c         G   sr   t  | ƒ d k r+ t t d ƒ d d ƒn7 t  | ƒ d k rV t t d ƒ d d ƒn t d ƒ ‚ |  j j | Œ  S(   Ni    s\   Calls to wsgi.input.readline() without arguments are unsafe.  Use wsgi.input.read() instead.R   i   i   s|   wsgi.input.readline() was called with a size hint. WSGI does not support this, although it's available on all major servers.s2   too many arguments passed to wsgi.input.readline()(   R   R   R   t	   TypeErrorR   t   readline(   R   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   >   s    

c         C   sF   y t  |  j ƒ SWn. t k
 rA t t d ƒ d d ƒt  d ƒ SXd  S(   Ns   wsgi.input is not iterable.R   i   (    (   t   iterR   R   R   R   (   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   __iter__L   s
    c         C   s'   t  t d ƒ d d ƒ|  j j ƒ  d  S(   Ns$   application closed the input stream!R   i   (   R   R   R   t   close(   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   S   s    (   R   R   R   R   R   R   R   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   -   s
   				t   ErrorStreamc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   [   s    c         C   s!   t  d | ƒ |  j j | ƒ d  S(   Ns   wsgi.error.write()(   R   R   t   write(   R   t   s(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR    ^   s    c         C   s   |  j  j ƒ  d  S(   N(   R   t   flush(   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR"   b   s    c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R    (   R   t   seqt   line(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt
   writelinese   s    c         C   s'   t  t d ƒ d d ƒ|  j j ƒ  d  S(   Ns$   application closed the error stream!R   i   (   R   R   R   R   (   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   i   s    (   R   R   R   R    R"   R%   R   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   Y   s
   				t   GuardedWritec           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   _writet   _chunks(   R   R    t   chunks(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   q   s    	c         C   s7   t  d | ƒ |  j j | ƒ |  j j t | ƒ ƒ d  S(   Ns   write()(   R   R'   R    R(   t   appendR   (   R   R!   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   __call__u   s    (   R   R   R   R+   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR&   o   s   	t   GuardedIteratorc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s:   | |  _  t | ƒ j |  _ t |  _ | |  _ | |  _ d  S(   N(   t	   _iteratorR   t   nextt   _nextt   Falset   closedt   headers_setR)   (   R   t   iteratorR2   R)   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   }   s
    			c         C   s   |  S(   N(    (   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   „   s    c         C   sw   |  j  r" t t d ƒ d d ƒn  |  j ƒ  } |  j sP t t d ƒ d d ƒn  t d | ƒ |  j j t | ƒ ƒ | S(   Ns   iterated over closed app_iterR   i   s3   Application returned before it started the responses   application iterator items(	   R1   R   R   R/   R2   R   R)   R*   R   (   R   t   rv(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR.   ‡   s    	
	
c         C   s~  t  |  _ t |  j d ƒ r+ |  j j ƒ  n  |  j rz|  j \ } } t |  j ƒ } | j d d t	 ƒ} | d k rá xO | D]G \ } } | j
 ƒ  } | d k rz t | ƒ rz t t d | ƒ ƒ qz qz W| rwt t d ƒ ƒ qwqzd	 | k oø d
 k  n s	| d k rL| d k r,t t d ƒ | ƒ n  | rwt t d | ƒ ƒ qwqz| d  k	 rz| | k rzt t d ƒ ƒ qzn  d  S(   NR   s   content-lengthR   i0  t   expiress   content-locations&   entity header %r found in 304 responses"   304 responses must not have a bodyid   iÈ   iÌ   i    s.   %r responses must have an empty content lengths!   %r responses must not have a bodysG   Content-Length and the number of bytes sent to the client do not match.(   R5   s   content-location(   t   TrueR1   t   hasattrR-   R   R2   t   sumR)   t   gett   intt   lowerR   R   R
   t   NoneR   (   R   t   status_codet   headerst
   bytes_sentt   content_lengtht   keyt   value(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   “   s2    			(	c         C   s8   |  j  s4 y t t d ƒ ƒ Wq4 t k
 r0 q4 Xn  d  S(   Ns4   Iterator was garbage collected before it was closed.(   R1   R   R   t	   Exception(   R   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   __del__±   s
    	(   R   R   R   R   R.   R   RD   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR,   {   s
   				t   LintMiddlewarec           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s¿  This middleware wraps an application and warns on common errors.
    Among other thing it currently checks for the following problems:

    -   invalid status codes
    -   non-bytestrings sent to the WSGI server
    -   strings returned from the WSGI application
    -   non-empty conditional responses
    -   unquoted etags
    -   relative URLs in the Location header
    -   unsafe calls to wsgi.input
    -   unclosed iterators

    Detected errors are emitted using the standard Python :mod:`warnings`
    system and usually end up on :data:`stderr`.

    ::

        from werkzeug.contrib.lint import LintMiddleware
        app = LintMiddleware(app)

    :param app: the application to wrap
    c         C   s   | |  _  d  S(   N(   t   app(   R   RF   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR   Ò   s    c      	   C   s  t  | ƒ t k	 r+ t t d ƒ d d ƒn  x7 d D]/ } | | k r2 t t d | ƒ d d ƒq2 q2 W| d d k rŽ t t d ƒ d d ƒn  | j d d ƒ } | rÓ | d  d k rÓ t t d | ƒ d d ƒn  | j d d ƒ } | d  d k rt t d | ƒ d d ƒn  d  S(   Ns/   WSGI environment is not a standard python dict.R   i   t   REQUEST_METHODt   SERVER_NAMEt   SERVER_PORTs   wsgi.versions
   wsgi.inputs   wsgi.errorss   wsgi.multithreads   wsgi.multiprocesss   wsgi.run_onces%   required environment key %r not foundi   i   i    s!   environ is not a WSGI 1.0 environt   SCRIPT_NAMEt    t   /s+   SCRIPT_NAME does not start with a slash: %rt	   PATH_INFOs)   PATH_INFO does not start with a slash: %r(	   RG   RH   RI   s   wsgi.versions
   wsgi.inputs   wsgi.errorss   wsgi.multithreads   wsgi.multiprocesss   wsgi.run_once(   i   i    (   R   t   dictR   R   R9   (   R   t   environRA   t   script_namet	   path_info(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   check_environÕ   s*    
  
	
		c      	   C   s  t  d | ƒ | j d  d ƒ d } t | ƒ d k sB | j ƒ  r[ t t d ƒ d d ƒn  t | ƒ d k  s} | d d k r– t t d	 ƒ d d ƒn  t | ƒ } | d
 k  rÇ t t d ƒ d d ƒn  t | ƒ t	 k	 rò t t d ƒ d d ƒn  x¿ | D]· } t | ƒ t
 k	 s#t | ƒ d k r<t t d ƒ d d ƒn  | \ } } t | ƒ t k	 slt | ƒ t k	 r…t t d ƒ d d ƒn  | j ƒ  d k rù t t d ƒ d d ƒqù qù W| d  k	 rét | t
 ƒ rét t d ƒ d d ƒn  t | ƒ } |  j | ƒ | | f S(   Nt   statusi   i    i   s    Status code must be three digitsR   i   t    se   Invalid value for status %r.  Valid status strings are three digits, a space and a status explanationid   s   status code < 100 detecteds   header list is not a listi   s    Headers must tuple 2-item tupless   header items must be stringssF   The status header is not supported due to conflicts with the CGI spec.s   invalid value for exc_info(   R   t   splitR<   R   t   isdigitR   R   R:   R   t   listt   tupleR   R;   t
   isinstanceR   t   check_headers(   R   RS   R>   t   exc_infoR=   t   itemt   nameRB   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   check_start_responseî   s8    "
$
$
c         C   sÁ   | j  d ƒ } | d  k	 rw | j d ƒ r7 | d } n  | d  | d k oV d k n sw t t d ƒ d d	 ƒqw n  | j  d
 ƒ } | d  k	 r½ t | ƒ j s½ t t d ƒ d d	 ƒq½ n  d  S(   Nt   etags   w/i   i   iÿÿÿÿt   "s   unquoted etag emitted.R   i   t   locations*   absolute URLs required for location header(   R9   R<   t
   startswithR   R
   R    t   netloc(   R   R>   R_   Ra   (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyRZ     s    $c         C   s,   t  | t ƒ r( t t d ƒ d d ƒn  d  S(   Ns›   application returned string.  Response will send character for character to the client which will kill the performance.  Return a list or iterable instead.R   i   (   RY   R   R   R   (   R   t   app_iter(    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   check_iterator   s    c            sè   t  | ƒ d k r+ t t d ƒ d d ƒn  | rJ t t d ƒ d d ƒn  | \ } ‰ ˆ j | ƒ t | d ƒ | d <t | d ƒ | d <t | d <g  ‰  g  ‰ ‡  ‡ ‡ ‡ f d †  } ˆ j | | ƒ } ˆ j | ƒ t	 | ˆ  ˆ ƒ S(	   Ni   s"   Two arguments to WSGI app requiredR   s(   No keyword arguments to WSGI app alloweds
   wsgi.inputs   wsgi.errorss   wsgi.file_wrapperc             s²   t  |  ƒ d k r5 t t d t  |  ƒ d d ƒƒ n  | rN t t d ƒ ƒ n  |  d  \ } } t  |  ƒ d k r} |  d } n d  } ˆ j | | | ƒ ˆ  (t ˆ | | | ƒ ˆ ƒ S(   Ni   i   s0   Invalid number of arguments: %s, expected 2 or 3R   s   no keyword arguments allowed.(   i   i   (   R   R   R   R<   R^   R&   (   R   t   kwargsRS   R>   R[   (   R2   t   start_responseR   R)   (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   checking_start_response:  s    	
(
   R   R   R   RR   R   R   R   RF   Re   R,   (   R   R   Rf   RO   Rh   Rd   (    (   R2   Rg   R   R)   s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyR+   '  s     

(	   R   R   R	   R   RR   R^   RZ   Re   R+   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyRE   º   s   			$		N(   R	   R    t   warningsR   t   werkzeug.datastructuresR   t   werkzeug.httpR   t   werkzeug.wsgiR   t   werkzeug._compatR   t   WarningR   R
   R   t   objectR   R   R&   R,   RE   (    (    (    s[   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/lint.pyt   <module>   s   ,?