Ñò
$8Zc           @   sF  d  Z  d g Z d d k Z d d k Z d d k l Z l Z l Z l Z d d k	 Z	 e i
 d ƒ Z e i
 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.6/wsgiref/validate.pyR   z   s   c         G   s   |  p t  | Œ  ‚ n d  S(   N(   t   AssertionError(   t   condt   args(    (    s&   /usr/lib/python2.6/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 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  t |  ƒ d j d ƒ t  | d ƒ |  \ } ‰  t | ƒ g  ‰ ‡  ‡ f d †  } t | d ƒ | d <t | d ƒ | d <ˆ | | ƒ } t  | d  j	 o
 | t j d ƒ t | ƒ t | ˆ ƒ S(   Ni   s   Two arguments requireds   No keyword arguments allowedc             sÄ   t  t |  ƒ d j p t |  ƒ d j d |  f ƒ t  | d ƒ |  d } |  d } t |  ƒ d j o |  d } n d  } t | ƒ t | ƒ t | | ƒ t | ƒ ˆ i 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.6/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.6/wsgiref/validate.pyt   lint_app   s    

(    (   R$   R%   (    (   R$   s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyt   __init__¼   s    c         G   sH   t  t | ƒ d j ƒ |  i i | Œ  } t  t | ƒ t d ƒ j ƒ | S(   Ni   t    (   R   R   R&   t   readt   type(   R'   R   t   v(    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR+   ¿   s    c         C   s/   |  i  i ƒ  } t t | ƒ t d ƒ j ƒ | S(   NR*   (   R&   t   readlineR   R,   (   R'   R-   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR.   Å   s    c         G   su   t  t | ƒ d j ƒ |  i i | Œ  } t  t | ƒ t g  ƒ j ƒ x* | D]" } t  t | ƒ t d ƒ j ƒ qK W| S(   Ni   R*   (   R   R   R&   t	   readlinesR,   (   R'   R   t   linest   line(    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR/   Ê   s      c         c   s'   x  |  i  ƒ  } | p d  S| Vq d  S(   N(   R.   (   R'   R1   (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyt   closeÙ   s    (   R   R   R)   R+   R.   R/   R2   R3   (    (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyR)   Þ   s    c         C   s0   t  t | ƒ t d ƒ j ƒ |  i i | ƒ d  S(   NR*   (   R   R,   R4   t   write(   R'   t   s(    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR6   á   s    c         C   s   |  i  i ƒ  d  S(   N(   R4   t   flush(   R'   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR8   å   s    c         C   s"   x | D] } |  i  | ƒ q Wd  S(   N(   R6   (   R'   t   seqR1   (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyR3   ì   s    (   R   R   R)   R6   R8   R:   R3   (    (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyR)   ñ   s    c         C   s-   t  t | ƒ t d ƒ j ƒ |  i | ƒ d  S(   NR*   (   R   R,   R;   (   R'   R7   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyt   __call__ô   s    (   R   R   R)   R=   (    (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyR)   ú   s    c         C   s   t  |  i d  ƒ S(   N(   R!   R#   R   (   R'   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR2   ý   s    (   R   R   R)   R2   (    (    (    s&   /usr/lib/python2.6/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.6/wsgiref/validate.pyR)     s    		c         C   s   |  S(   N(    (   R'   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR2   	  s    c         C   sQ   t  |  i d ƒ |  i i ƒ  } |  i d  j	 o t  |  i 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.6/wsgiref/validate.pyRD     s    
	c         C   s1   t  |  _ t |  i d ƒ o |  i i ƒ  n d  S(   NR3   (   t   TrueRB   t   hasattrR@   R3   (   R'   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR3     s    	c         C   s2   |  i  p t i i d ƒ n t |  i  d ƒ d  S(   Ns/   Iterator garbage collected without being closed(   RB   t   syst   stderrR6   R   (   R'   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyt   __del__  s
    
		(   R   R   R)   R2   RD   R3   RI   (    (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR!     s
   			
	c         C   s¤  t  t |  ƒ t j d t |  ƒ |  f ƒ xC d d d d d d d d	 d
 g	 D]  } t  | |  j d | f ƒ qK Wx5 d d g D]' } t  | |  j d | | d f ƒ q| Wd |  j o t i d t ƒ n x` |  i ƒ  D]R } d | j o qÕ n t  t |  | ƒ t j d | t |  | ƒ |  | f ƒ qÕ Wt  t |  d ƒ t j d |  d f ƒ t  |  d d+ j d |  d ƒ t	 |  d ƒ t
 |  d ƒ |  d d, j o t i d  |  d t ƒ n t  |  i d! ƒ p |  d! i d" ƒ d# |  d! ƒ t  |  i d$ ƒ p |  d$ i d" ƒ d% |  d$ ƒ |  i d& ƒ o) t  t |  d& ƒ d' j d( |  d& ƒ n |  i d! ƒ p t  |  i d$ ƒ d) ƒ n t  |  i d! ƒ d" j 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 '/'(   RQ   RR   (   RS   RT   RU   RV   RW   RX   RY   (   R   R,   R   t   warningst   warnR   t   keysR   R   t   check_inputt   check_errorst   gett
   startswitht   intt   has_key(   R"   t   key(    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR   "  sf    	   #
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.6/wsgiref/validate.pyRa   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.6/wsgiref/validate.pyRb   i  s
     c         C   s³   t  t |  ƒ t j d |  ƒ |  i d  d ƒ d } t  t | ƒ d j d | ƒ t | ƒ } t  | d j d | ƒ t |  ƒ d j  p |  d d	 j o t i 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   Re   R^   R_   R   (   R   t   status_codet
   status_int(    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR   o  s    $c         C   sc  t  t |  ƒ t j d |  t |  ƒ f ƒ h  } x-|  D]%} t  t | ƒ t j d | t | ƒ f ƒ t  t | ƒ d j ƒ | \ } } t  | i ƒ  d j d | ƒ d  | | i ƒ  <t  d | j o
 d | j d | ƒ t  t i | ƒ d	 | ƒ t  | i	 d
 ƒ o | i	 d ƒ d | ƒ t
 i | ƒ o- t  d d | t
 i | ƒ i 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.6/wsgiref/validate.pyR   ~  s.     !	c         C   s™   t  |  i d  d ƒ d ƒ } d } xN | D]F \ } } | i ƒ  d j o' | | j o d  St d d | ƒ q) q) W| | j o 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  (   Re   Rj   R   Rp   R   (   R   R   t   codet   NO_MESSAGE_BODYRx   Ry   (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyR   —  s     	c         C   s@   t  |  d  j p t |  ƒ t d ƒ j d |  t |  ƒ f ƒ d  S(   Ns    exc_info (%r) is not a tuple: %r(    (   R   R   R,   (   R   (    (    s&   /usr/lib/python2.6/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.6/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   Ra   Rb   R   R   R   R   R    (    (    (    s&   /usr/lib/python2.6/wsgiref/validate.pyt   <module>n   s.   	"		7"		!	A						