ó
xFWgc           @   sF  d  Z  d g Z d d l Z d d l Z d d l m Z m Z m Z m Z d d l	 Z	 e j
 d ƒ Z e j
 d ƒ Z d e f d „  ƒ  YZ d	 „  Z d
 „  Z d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d d  d „  ƒ  YZ d d! d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  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).
t	   validatoriÿÿÿÿN(   t   DictTypet
   StringTypet	   TupleTypet   ListTypes   ^[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__(    (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   z   s   c         G   s   |  s t  | Œ  ‚ n  d  S(   N(   t   AssertionError(   t   condt   args(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   assert_   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 raise 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 raise an exception
    at that point).
    c             sÂ   t  t |  ƒ d k d ƒ t  | d ƒ |  \ } ‰  t | ƒ g  ‰ ‡  ‡ f d †  } t | d ƒ | d <t | d ƒ | d <ˆ | | ƒ } t  | d  k	 o¤ | t k d ƒ t | ƒ t | ˆ ƒ S(   Ni   s   Two arguments requireds   No keyword arguments allowedc             sÁ   t  t |  ƒ d k p$ t |  ƒ d k d |  f ƒ t  | d ƒ |  d } |  d } t |  ƒ d k rs |  d } n d  } t | ƒ t | ƒ t | | ƒ t | ƒ ˆ j d  ƒ t ˆ  |  Œ  ƒ S(   Ni   i   s   Invalid number of arguments: %ss   No keyword arguments allowedi    i   (	   R   t   lent   Nonet   check_statust   check_headerst   check_content_typet   check_exc_infot   appendt   WriteWrapper(   R   t   kwt   statust   headerst   exc_info(   t   start_responset   start_response_started(    s&   /usr/lib/python2.7/wsgiref/validate.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   s&   /usr/lib/python2.7/wsgiref/validate.pyt   lint_app   s    

(    (   R$   R%   (    (   R$   s&   /usr/lib/python2.7/wsgiref/validate.pyR    ƒ   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(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   __init__¼   s    c         G   sH   t  t | ƒ d k ƒ |  j j | Œ  } t  t | ƒ t d ƒ k ƒ | S(   Ni   t    (   R   R   R&   t   readt   type(   R'   R   t   v(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR+   ¿   s    c         C   s/   |  j  j ƒ  } t t | ƒ t d ƒ k ƒ | S(   NR*   (   R&   t   readlineR   R,   (   R'   R-   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR.   Å   s    c         G   su   t  t | ƒ d k ƒ |  j j | Œ  } t  t | ƒ t g  ƒ k ƒ x* | D]" } t  t | ƒ t d ƒ k ƒ qK W| S(   Ni   R*   (   R   R   R&   t	   readlinesR,   (   R'   R   t   linest   line(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR/   Ê   s     c         c   s&   x |  j  ƒ  } | s d  S| Vq Wd  S(   N(   R.   (   R'   R1   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   __iter__Ò   s
    c         C   s   t  d d ƒ d  S(   Ni    s    input.close() must not be called(   R   (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   closeÙ   s    (   R   R   R)   R+   R.   R/   R2   R3   (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.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(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR)   Þ   s    c         C   s0   t  t | ƒ t d ƒ k ƒ |  j j | ƒ d  S(   NR*   (   R   R,   R4   t   write(   R'   t   s(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR6   á   s    c         C   s   |  j  j ƒ  d  S(   N(   R4   t   flush(   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR8   å   s    c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R6   (   R'   t   seqR1   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt
   writelinesè   s    c         C   s   t  d d ƒ d  S(   Ni    s!   errors.close() must not be called(   R   (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR3   ì   s    (   R   R   R)   R6   R8   R:   R3   (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.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(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR)   ñ   s    c         C   s-   t  t | ƒ t d ƒ k ƒ |  j | ƒ d  S(   NR*   (   R   R,   R;   (   R'   R7   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   __call__ô   s    (   R   R   R)   R=   (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   ï   s   	t   PartialIteratorWrapperc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   R#   (   R'   t   wsgi_iterator(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR)   ú   s    c         C   s   t  |  j d  ƒ S(   N(   R!   R#   R   (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR2   ý   s    (   R   R   R)   R2   (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR>   ø   s   	R!   c           B   s5   e  Z d  „  Z d „  Z d „  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?   RC   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR)     s    		c         C   s   |  S(   N(    (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR2   	  s    c         C   sO   t  |  j d ƒ |  j j ƒ  } |  j d  k	 rK t  |  j 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   RB   R#   t   nextRC   R   (   R'   R-   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyRD     s    
	c         C   s/   t  |  _ t |  j d ƒ r+ |  j j ƒ  n  d  S(   NR3   (   t   TrueRB   t   hasattrR@   R3   (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR3     s    	c         C   s0   |  j  s t j j d ƒ n  t |  j  d ƒ d  S(   Ns/   Iterator garbage collected without being closed(   RB   t   syst   stderrR6   R   (   R'   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   __del__  s
    		
	(   R   R   R)   R2   RD   R3   RI   (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR!     s
   			
	c      	   C   s•  t  t |  ƒ t k d t |  ƒ |  f ƒ xC d d d d d d d d	 d
 g	 D]  } t  | |  k d | f ƒ qK Wx5 d d g D]' } t  | |  k d | | d f ƒ q| Wd |  k rÆ t j d t ƒ n  x^ |  j ƒ  D]P } d | k rë qÓ n  t  t |  | ƒ t k d | t |  | ƒ |  | f ƒ qÓ Wt  t |  d ƒ t k d |  d f ƒ t  |  d d, k d |  d ƒ t	 |  d ƒ t
 |  d ƒ |  d d- k rµt j d! |  d t ƒ n  t  |  j d" ƒ pØ|  d" j d# ƒ d$ |  d" ƒ t  |  j d% ƒ p
|  d% j d# ƒ d& |  d% ƒ |  j d' ƒ rPt  t |  d' ƒ d( k d) |  d' ƒ n  |  j d" ƒ sut  d% |  k d* ƒ n  t  |  j d" ƒ d# k 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   PATCHt   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 '/'(   RQ   RR   (   RS   RT   RU   RV   RW   RX   RY   RZ   (   R   R,   R   t   warningst   warnR   t   keysR   R   t   check_inputt   check_errorst   gett
   startswitht   int(   R"   t   key(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   "  s`    	
#	

c         C   sA   x: d d d d g D]& } t  t |  | ƒ d |  | f ƒ q Wd  S(   NR+   R.   R/   R2   s-   wsgi.input (%r) doesn't have the attribute %s(   R   RF   (   R(   t   attr(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyRb   c  s    c         C   s>   x7 d d d g D]& } t  t |  | ƒ d |  | f ƒ q Wd  S(   NR8   R6   R:   s.   wsgi.errors (%r) doesn't have the attribute %s(   R   RF   (   R5   Rh   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyRc   i  s    c         C   s°   t  t |  ƒ t k d |  ƒ |  j d  d ƒ d } t  t | ƒ d k d | ƒ t | ƒ } t  | d k 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   R,   R   t   splitR   R   Rf   R_   R`   R   (   R   t   status_codet
   status_int(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   o  s    "c         C   s_  t  t |  ƒ t k d |  t |  ƒ f ƒ i  } x)|  D]!} t  t | ƒ t k d | t | ƒ f ƒ t  t | ƒ d k ƒ | \ } } t  | j ƒ  d k d | ƒ d  | | j ƒ  <t  d | k oÌ d | k d | ƒ t  t j | ƒ d	 | ƒ t  | j	 d
 ƒ o| j	 d ƒ d | ƒ t
 j | ƒ r6 t  d d | t
 j | ƒ j d ƒ f ƒ q6 q6 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 '_': %ri    s#   Bad header value: %r (bad char: %r)(   R   R,   R   R   R   t   lowerR   t	   header_ret   searcht   endswitht   bad_header_value_ret   group(   R   t   header_namest   itemt   namet   value(    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   ~  s,     	c         C   s“   t  |  j d  d ƒ d ƒ } d } xJ | D]B \ } } | j ƒ  d k r) | | k rW d  St d d | ƒ q) q) W| | k r t d 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  (   Rf   Rj   R   Rp   R   (   R   R   t   codet   NO_MESSAGE_BODYRx   Ry   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   —  s    	c         C   s?   t  |  d  k p$ t |  ƒ t d ƒ k d |  t |  ƒ f ƒ d  S(   Ns    exc_info (%r) is not a tuple: %r(    (   R   R   R,   (   R   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR   ¥  s    $c         C   s   t  t |  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   t
   isinstancet   str(   R#   (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyR    ª  s    (    (    (    (    (    (   R   t   __all__t   reRG   t   typesR   R   R   R   R_   t   compileRq   Rt   t   WarningR   R   R    R   R   R   R>   R!   R   Rb   Rc   R   R   R   R   R    (    (    (    s&   /usr/lib/python2.7/wsgiref/validate.pyt   <module>n   s.   	"		7"		!	A						