ó
ú2ec           @   sN  d  Z  d d l Z d d l Z d d l Z d d l Z e j d ƒ Z e j d ƒ Z 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 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e  e _  d d g Z d S(   s"  
Middleware to check for obedience to the WSGI specification.

Some of the things this checks:

* Signature of the application and start_response (including that
  keyword arguments are not used).

* Environment checks:

  - Environment is a dictionary (and not a subclass).

  - That all the required keys are in the environment: REQUEST_METHOD,
    SERVER_NAME, SERVER_PORT, wsgi.version, wsgi.input, wsgi.errors,
    wsgi.multithread, wsgi.multiprocess, wsgi.run_once

  - That HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH are not in the
    environment (these headers should appear as CONTENT_LENGTH and
    CONTENT_TYPE).

  - Warns if QUERY_STRING is missing, as the cgi module acts
    unpredictably in that case.

  - That CGI-style variables (that don't contain a .) have
    (non-unicode) string values

  - That wsgi.version is a tuple

  - That wsgi.url_scheme is 'http' or 'https' (@@: is this too
    restrictive?)

  - Warns if the REQUEST_METHOD is not known (@@: probably too
    restrictive).

  - That SCRIPT_NAME and PATH_INFO are empty or start with /

  - That at least one of SCRIPT_NAME or PATH_INFO are set.

  - That CONTENT_LENGTH is a positive integer.

  - That SCRIPT_NAME is not '/' (it should be '', and PATH_INFO should
    be '/').

  - That wsgi.input has the methods read, readline, readlines, and
    __iter__

  - That wsgi.errors has the methods flush, write, writelines

* The status is a string, contains a space, starts with an integer,
  and that integer is in range (> 100).

* That the headers is a list (not a subclass, not another kind of
  sequence).

* That the items of the headers are tuples of strings.

* That there is no 'status' header (that is used in CGI, but not in
  WSGI).

* That the headers don't contain newlines or colons, end in _ or -, or
  contain characters codes below 037.

* That Content-Type is given if there is content (CGI often has a
  default content type, but WSGI does not).

* That no Content-Type is given when there is no content (@@: is this
  too restrictive?)

* That the exc_info argument to start_response is a tuple or None.

* That all calls to the writer are with strings, and no other methods
  on the writer are accessed.

* That wsgi.input is used properly:

  - .read() is called with zero or one argument

  - That it returns a string

  - That readline, readlines, and __iter__ return strings

  - That .close() is not called

  - No other methods are provided

* That wsgi.errors is used properly:

  - .write() and .writelines() is called with a string

  - That .close() is not called, and no other methods are provided.

* The response iterator:

  - That it is not a string (it should be a list of a single string; a
    string will work, but perform horribly).

  - That .next() returns a string

  - That the iterator is not iterated over until start_response has
    been called (that can signal either a server or application
    error).

  - That .close() is called (doesn't raise exception, only prints to
    sys.stderr, because we only know it isn't called when the object
    is garbage collected).
iÿÿÿÿNs   ^[a-zA-Z][a-zA-Z0-9\-_]*$s   [\000-\037]t   WSGIWarningc           B   s   e  Z d  Z RS(   s:   
    Raised in response to WSGI-spec-related warnings
    (   t   __name__t
   __module__t   __doc__(    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR    x   s   c            s   ‡  f d †  } | S(   s®  
    When applied between a WSGI server and a WSGI application, this
    middleware will check for WSGI compliancy on a number of levels.
    This middleware does not modify the request or response in any
    way, but will throw an AssertionError if anything seems off
    (except for a failure to close the application iterator, which
    will be printed to stderr -- there's no way to throw an exception
    at that point).
    c             sÑ   t  |  ƒ d k s t d ƒ ‚ | s1 t d ƒ ‚ |  \ } ‰  t | ƒ g  ‰ ‡  ‡ f d †  } t | d ƒ | d <t | d ƒ | d <ˆ | | ƒ } | d  k	 r® | t k sº t d ƒ ‚ t | ƒ t | ˆ ƒ S(   Ni   s   Two arguments requireds   No keyword arguments allowedc             sÈ   t  |  ƒ d k s4 t  |  ƒ d k s4 t d |  ƒ ‚ | sG t d ƒ ‚ |  d } |  d } t  |  ƒ d k rz |  d } n d  } t | ƒ t | ƒ t | | ƒ t | ƒ ˆ j d  ƒ t ˆ  |  Œ  ƒ S(   Ni   i   s   Invalid number of arguments: %ss   No keyword arguments allowedi    i   (	   t   lent   AssertionErrort   Nonet   check_statust   check_headerst   check_content_typet   check_exc_infot   appendt   WriteWrapper(   t   argst   kwt   statust   headerst   exc_info(   t   start_responset   start_response_started(    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   start_response_wrapper”   s    '




s
   wsgi.inputs   wsgi.errorss>   The application must return an iterator, if only an empty list(	   R   R   t   check_environt   InputWrappert   ErrorWrapperR   t   Falset   check_iteratort   IteratorWrapper(   R   R   t   environR   t   iterator(   t   application(   R   R   s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   lint_app‰   s    
	
(    (   R   t   global_confR   (    (   R   s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt
   middleware}   s    )R   c           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   input(   t   selft
   wsgi_input(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   __init__¶   s    c         G   sF   t  | ƒ d k s t ‚ |  j j | Œ  } t | t j ƒ sB t ‚ | S(   Ni   (   R   R   R!   t   readt
   isinstancet   sixt   binary_type(   R"   R   t   v(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR%   ¹   s    c         G   s.   |  j  j | Œ  } t | t j ƒ s* t ‚ | S(   N(   R!   t   readlineR&   R'   R(   R   (   R"   R   R)   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR*   ¿   s    c         G   sl   t  | ƒ d k s t ‚ |  j j | Œ  } t | t ƒ s? t ‚ x& | D] } t | t j ƒ sF t ‚ qF W| S(   Ni   (   R   R   R!   t	   readlinesR&   t   listR'   R(   (   R"   R   t   linest   line(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR+   Ä   s    c         c   s&   x |  j  ƒ  } | s d  S| Vq Wd  S(   N(   R*   (   R"   R.   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   __iter__Ì   s
    c         C   s   d s t  d ƒ ‚ d  S(   Ni    s    input.close() must not be called(   R   (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   closeÓ   s    (   R   R   R$   R%   R*   R+   R/   R0   (    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   ´   s   					R   c           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   errors(   R"   t   wsgi_errors(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR$   Ø   s    c         C   s,   t  | t j ƒ s t ‚ |  j j | ƒ d  S(   N(   R&   R'   t   string_typesR   R1   t   write(   R"   t   s(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR4   Û   s    c         C   s   |  j  j ƒ  d  S(   N(   R1   t   flush(   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR6   ß   s    c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R4   (   R"   t   seqR.   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt
   writelinesâ   s    c         C   s   d s t  d ƒ ‚ d  S(   Ni    s!   errors.close() must not be called(   R   (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR0   æ   s    (   R   R   R$   R4   R6   R8   R0   (    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   Ö   s
   				R   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   writer(   R"   t   wsgi_writer(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR$   ë   s    c         C   s)   t  | t j ƒ s t ‚ |  j | ƒ d  S(   N(   R&   R'   R(   R   R9   (   R"   R5   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   __call__î   s    (   R   R   R$   R;   (    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   é   s   	t   PartialIteratorWrapperc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   R   (   R"   t   wsgi_iterator(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR$   ô   s    c         C   s   t  |  j ƒ S(   N(   R   R   (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR/   ÷   s    (   R   R   R$   R/   (    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR<   ò   s   	R   c           B   s;   e  Z d  „  Z d „  Z d „  Z e Z d „  Z d „  Z RS(   c         C   s.   | |  _  t | ƒ |  _ t |  _ | |  _ d  S(   N(   t   original_iteratort   iterR   R   t   closedt   check_start_response(   R"   R=   RA   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR$   ý   s    		c         C   s   |  S(   N(    (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR/     s    c         C   s\   |  j  s t d ƒ ‚ t j |  j ƒ } |  j d  k	 rX |  j sL t d ƒ ‚ d  |  _ n  | S(   Ns   Iterator read after closedsj   The application returns and we started iterating over its body, but start_response has not yet been called(   R@   R   R'   t   nextR   RA   R   (   R"   R)   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyRB     s    		c         C   s/   t  |  _ t |  j d ƒ r+ |  j j ƒ  n  d  S(   NR0   (   t   TrueR@   t   hasattrR>   R0   (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR0     s    	c         C   s5   |  j  s t j j d ƒ n  |  j  s1 t d ƒ ‚ d  S(   Ns/   Iterator garbage collected without being closed(   R@   t   syst   stderrR4   R   (   R"   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   __del__  s
    		
(   R   R   R$   R/   RB   t   __next__R0   RG   (    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   û   s   			
	c      	   C   s½  t  |  t ƒ s+ t d t |  ƒ |  f ƒ ‚ xE d d d d d d d d	 d
 g	 D]" } | |  k sM t d | ƒ ‚ qM Wx: d d g D], } | |  k s€ t d | | d f ƒ ‚ q€ Wd |  k rÏ t j d t ƒ n  x` |  j ƒ  D]R } d | k rô qÜ n  t  |  | t ƒ sÜ t d | t |  | ƒ |  | f ƒ ‚ qÜ Wt  |  d t	 ƒ sYt d |  d ƒ ‚ |  d d+ k s}t d |  d ƒ ‚ t
 |  d ƒ t |  d ƒ |  d d, k rÄt j d  |  d t ƒ n  |  j d! ƒ sû|  d! j d" ƒ sût d# |  d! ƒ ‚ |  j d$ ƒ s2|  d$ j d" ƒ s2t d% |  d$ ƒ ‚ |  j d& ƒ rnt |  d& ƒ d' k snt d( |  d& ƒ ‚ n  |  j d! ƒ s˜d$ |  k s˜t d) ƒ ‚ n  |  j d! ƒ d" k s¹t d* ƒ ‚ d  S(-   Ns:   Environment is not of the right type: %r (environment: %r)t   REQUEST_METHODt   SERVER_NAMEt   SERVER_PORTs   wsgi.versions
   wsgi.inputs   wsgi.errorss   wsgi.multithreads   wsgi.multiprocesss   wsgi.run_onces$   Environment missing required key: %rt   HTTP_CONTENT_TYPEt   HTTP_CONTENT_LENGTHs8   Environment should not have the key: %s (use %s instead)i   t   QUERY_STRINGs’   QUERY_STRING is not in the WSGI environment; the cgi module will use sys.argv when this variable is missing, so application errors are more likelyt   .s9   Environmental variable %s is not a string: %r (value: %r)s#   wsgi.version should be a tuple (%r)s   wsgi.url_schemet   httpt   httpss   wsgi.url_scheme unknown: %rt   GETt   HEADt   POSTt   OPTIONSt   PUTt   DELETEt   TRACEs   Unknown REQUEST_METHOD: %rt   SCRIPT_NAMEt   /s$   SCRIPT_NAME doesn't start with /: %rt	   PATH_INFOs"   PATH_INFO doesn't start with /: %rt   CONTENT_LENGTHi    s   Invalid CONTENT_LENGTH: %rsg   One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO should at least be '/' if SCRIPT_NAME is empty)sO   SCRIPT_NAME cannot be '/'; it should instead be '', and PATH_INFO should be '/'(   RP   RQ   (   RR   RS   RT   RU   RV   RW   RX   (   R&   t   dictR   t   typet   warningst   warnR    t   keyst   strt   tuplet   check_inputt   check_errorst   gett
   startswitht   int(   R   t   key(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR     s`    	
%	
c         C   sF   x? d d d d g D]+ } t  |  | ƒ s t d |  | f ƒ ‚ q Wd  S(   NR%   R*   R+   R/   s-   wsgi.input (%r) doesn't have the attribute %s(   RD   R   (   R#   t   attr(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyRd   _  s    c         C   sC   x< d d d g D]+ } t  |  | ƒ s t d |  | f ƒ ‚ q Wd  S(   NR6   R4   R8   s.   wsgi.errors (%r) doesn't have the attribute %s(   RD   R   (   R2   Rj   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyRe   e  s    c         C   s¼   t  |  t ƒ s t d |  ƒ ‚ |  j d  d ƒ d } t | ƒ d k sW t d | ƒ ‚ t | ƒ } | d k s t d | ƒ ‚ t |  ƒ d k  s¡ |  d d	 k r¸ t j d
 |  t	 ƒ n  d  S(   Ns    Status must be a string (not %r)i   i    i   s)   Status codes must be three characters: %rid   s   Status code is invalid: %ri   t    sj   The status string (%r) should be a three-digit integer followed by a single space and a status explanation(
   R&   Rb   R   t   splitR   R   Rh   R_   R`   R    (   R   t   status_codet
   status_int(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   k  s    "c         C   sv  t  |  t ƒ s+ t d |  t |  ƒ f ƒ ‚ i  } x>|  D]6} t  | t ƒ si t d | t | ƒ f ƒ ‚ t | ƒ d k s t ‚ | \ } } | j ƒ  d k s¯ t d | ƒ ‚ d  | | j ƒ  <d | k r× d | k sç t d | ƒ ‚ t j	 | ƒ st d	 | ƒ ‚ | j
 d
 ƒ r&| j
 d ƒ s6t d | ƒ ‚ t j	 | ƒ s8 t d | t j	 | ƒ j d ƒ f ƒ ‚ q8 Wd  S(   Ns%   Headers (%r) must be of type list: %rs1   Individual headers (%r) must be of type tuple: %ri   R   sy   The Status header cannot be used; it conflicts with CGI script, and HTTP status is not given through headers (value: %r).s   
t   :s,   Header names may not contain ':' or '\n': %rs   Bad header name: %rt   -t   _s#   Names may not end in '-' or '_': %rs#   Bad header value: %r (bad char: %r)i    (   R&   R,   R   R^   Rc   R   t   lowerR   t	   header_ret   searcht   endswitht   bad_header_value_ret   group(   R   t   header_namest   itemt   namet   value(    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   z  s,    
#c         C   s£   t  |  j d  d ƒ d ƒ } d } d	 } xO | D]G \ } } | j ƒ  d k r/ | | k r] d  Sd sv t d | ƒ ‚ q/ q/ W| | k rŸ d sŸ t d | ƒ ‚ n  d  S(
   Ni   i    iÌ   i0  s   content-typesJ   Content-Type header found in a %s response, which must not return content.s,   No Content-Type header found in headers (%s)(   iÌ   i0  (   iÌ   i0  (   Rh   Rl   R   Rr   R   (   R   R   t   codet   NO_MESSAGE_BODYt   NO_MESSAGE_TYPERz   R{   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR	   “  s    c         C   sD   |  d  k s@ t |  ƒ t d ƒ k s@ t d |  t |  ƒ f ƒ ‚ d  S(   Ns    exc_info (%r) is not a tuple: %r(    (   R   R^   R   (   R   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR
   ¢  s    'c         C   s    t  |  t ƒ s t d ƒ ‚ d  S(   Nsv   You should not return a string as your application iterator, instead return a single-item list containing that string.(   R&   Rb   R   (   R   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyR   §  s    c         C   s
   t  |  ƒ S(   N(   R    (   R   R   (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   make_middleware¯  s    R    R   (   R   t   reR'   RE   R_   t   compileRs   Rv   t   WarningR    R   R    t   objectR   R   R   R<   R   R   Rd   Re   R   R   R	   R
   R   R   t   __all__(    (    (    s4   /usr/local/lib/python2.7/dist-packages/paste/lint.pyt   <module>n   s0   7"		#	A									