;
Ðâ"Ic               @   s  d  Z  d g Z d d l Z d d l Z d d l Z e j d ƒ Z e j d ƒ Z Gd „  d e ƒ Z	 d „  Z
 d	 „  Z Gd
 „  d ƒ Z Gd „  d ƒ Z Gd „  d ƒ Z Gd „  d ƒ Z Gd „  d ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   u&  
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).
u	   validatori    Nu   ^[a-zA-Z][a-zA-Z0-9\-_]*$u   [\000-\037]c             B   s   |  Ee  Z d  Z d S(   u:   
    Raised in response to WSGI-spec-related warnings
    N(   u   __name__u
   __module__u   __doc__(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   WSGIWarningy   s   
u   WSGIWarningc             G   s   |  p t  | Œ  ‚ n d  S(   N(   u   AssertionError(   u   condu   args(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   assert_~   s    c                s   ‡  f d †  } | S(   u®  
    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 k d ƒ t  | d ƒ |  \ } ‰  t | ƒ g  ‰ ‡  ‡ f d †  } t | d ƒ | d <t | d ƒ | d <ˆ | | ƒ } t  | d  k	 o
 | d k d ƒ t | ƒ t | ˆ ƒ S(	   Ni   u   Two arguments requiredu   No keyword arguments allowedc                 sÄ   t  t |  ƒ d k p t |  ƒ d k d |  f ƒ t  | d ƒ |  d } |  d } t |  ƒ d k o |  d } n d  } t | ƒ t | ƒ t | | ƒ t | ƒ ˆ j d  ƒ t ˆ  |  Œ  ƒ S(   Ni   i   u   Invalid number of arguments: %su   No keyword arguments allowedi    i   (	   u   assert_u   lenu   Noneu   check_statusu   check_headersu   check_content_typeu   check_exc_infou   appendu   WriteWrapper(   u   argsu   kwu   statusu   headersu   exc_info(   u   start_responseu   start_response_started(    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   start_response_wrapper™   s    %




u
   wsgi.inputu   wsgi.errorsu>   The application must return an iterator, if only an empty listF(	   u   assert_u   lenu   check_environu   InputWrapperu   ErrorWrapperu   Noneu   Falseu   check_iteratoru   IteratorWrapper(   u   argsu   kwu   environu   start_response_wrapperu   iterator(   u   application(   u   start_responseu   start_response_startedu-   /mit/python/lib/python3.0/wsgiref/validate.pyu   lint_appŽ   s    

(    (   u   applicationu   lint_app(    (   u   applicationu-   /mit/python/lib/python3.0/wsgiref/validate.pyu	   validator‚   s    )c             B   sD   |  Ee  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   c             C   s   | |  _  d  S(   N(   u   input(   u   selfu
   wsgi_input(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __init__»   s    c             G   s?   t  t | ƒ d k ƒ |  j j | Œ  } t  t | t ƒ ƒ | S(   Ni   (   u   assert_u   lenu   inputu   readu
   isinstanceu   str(   u   selfu   argsu   v(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   read¾   s    c             C   s&   |  j  j ƒ  } t t | t ƒ ƒ | S(   N(   u   inputu   readlineu   assert_u
   isinstanceu   str(   u   selfu   v(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   readlineÄ   s    c             G   sc   t  t | ƒ d k ƒ |  j j | Œ  } t  t | t ƒ ƒ x! | D] } t  t | t ƒ ƒ qB W| S(   Ni   (   u   assert_u   lenu   inputu	   readlinesu
   isinstanceu   listu   str(   u   selfu   argsu   linesu   line(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu	   readlinesÉ   s     c             c   s'   x  |  j  ƒ  } | p d  S| Vq d  S(   N(   u   readline(   u   selfu   line(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __iter__Ñ   s
    c             C   s   t  d d ƒ d  S(   Ni    u    input.close() must not be called(   u   assert_(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   closeØ   s    N(   u   __name__u
   __module__u   __init__u   readu   readlineu	   readlinesu   __iter__u   close(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   InputWrapper¹   s   
					u   InputWrapperc             B   s;   |  Ee  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d S(   c             C   s   | |  _  d  S(   N(   u   errors(   u   selfu   wsgi_errors(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __init__Ý   s    c             C   s'   t  t | t ƒ ƒ |  j j | ƒ d  S(   N(   u   assert_u
   isinstanceu   stru   errorsu   write(   u   selfu   s(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   writeà   s    c             C   s   |  j  j ƒ  d  S(   N(   u   errorsu   flush(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   flushä   s    c             C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   u   write(   u   selfu   sequ   line(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu
   writelinesç   s     c             C   s   t  d d ƒ d  S(   Ni    u!   errors.close() must not be called(   u   assert_(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   closeë   s    N(   u   __name__u
   __module__u   __init__u   writeu   flushu
   writelinesu   close(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   ErrorWrapperÛ   s
   
				u   ErrorWrapperc             B   s    |  Ee  Z d  „  Z d „  Z d S(   c             C   s   | |  _  d  S(   N(   u   writer(   u   selfu   wsgi_writer(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __init__ð   s    c             C   s$   t  t | t ƒ ƒ |  j | ƒ d  S(   N(   u   assert_u
   isinstanceu   stru   writer(   u   selfu   s(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __call__ó   s    N(   u   __name__u
   __module__u   __init__u   __call__(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   WriteWrapperî   s   
	u   WriteWrapperc             B   s    |  Ee  Z d  „  Z d „  Z d S(   c             C   s   | |  _  d  S(   N(   u   iterator(   u   selfu   wsgi_iterator(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __init__ù   s    c             C   s   t  |  j d  ƒ S(   N(   u   IteratorWrapperu   iteratoru   None(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __iter__ü   s    N(   u   __name__u
   __module__u   __init__u   __iter__(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   PartialIteratorWrapper÷   s   
	u   PartialIteratorWrapperc             B   s;   |  Ee  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d S(   c             C   s.   | |  _  t | ƒ |  _ d |  _ | |  _ d  S(   NF(   u   original_iteratoru   iteru   iteratoru   Falseu   closedu   check_start_response(   u   selfu   wsgi_iteratoru   check_start_response(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __init__  s    		c             C   s   |  S(   N(    (   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __iter__  s    c             C   sQ   t  |  j d ƒ t |  j ƒ } |  j d  k	 o t  |  j d ƒ d  |  _ n | S(   Nu   Iterator read after closeduj   The application returns and we started iterating over its body, but start_response has not yet been called(   u   assert_u   closedu   nextu   iteratoru   check_start_responseu   None(   u   selfu   v(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __next__  s    
	c             C   s1   d |  _ t |  j d ƒ o |  j j ƒ  n d  S(   Nu   closeT(   u   Trueu   closedu   hasattru   original_iteratoru   close(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   close  s    	c             C   s2   |  j  p t j j d ƒ n t |  j  d ƒ d  S(   Nu/   Iterator garbage collected without being closed(   u   closedu   sysu   stderru   writeu   assert_(   u   self(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   __del__  s
    
		N(   u   __name__u
   __module__u   __init__u   __iter__u   __next__u   closeu   __del__(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   IteratorWrapper   s
   
			
	u   IteratorWrapperc             C   sž  t  t |  t ƒ d t |  ƒ |  f ƒ xC d d d d d d d d	 d
 g	 D]  } t  | |  k d | f ƒ qH Wx; d d g D]- } t  | |  k d | | d d  … f ƒ qy Wd |  k o t j d t ƒ n x] |  j ƒ  D]O } d | k o qØ n t  t |  | t ƒ d | t |  | ƒ |  | f ƒ qØ Wt  t |  d t	 ƒ d |  d f ƒ t  |  d d+ k d |  d ƒ t
 |  d ƒ t |  d ƒ |  d d, k o 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& ƒ o) t  t |  d& ƒ d' k d( |  d& ƒ n |  j d! ƒ p t  d$ |  k d) ƒ n t  |  j d! ƒ d" k d* ƒ d  S(-   Nu:   Environment is not of the right type: %r (environment: %r)u   REQUEST_METHODu   SERVER_NAMEu   SERVER_PORTu   wsgi.versionu
   wsgi.inputu   wsgi.errorsu   wsgi.multithreadu   wsgi.multiprocessu   wsgi.run_onceu$   Environment missing required key: %ru   HTTP_CONTENT_TYPEu   HTTP_CONTENT_LENGTHu8   Environment should not have the key: %s (use %s instead)i   u   QUERY_STRINGu’   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 likelyu   .u9   Environmental variable %s is not a string: %r (value: %r)u#   wsgi.version should be a tuple (%r)u   wsgi.url_schemeu   httpu   httpsu   wsgi.url_scheme unknown: %ru   GETu   HEADu   POSTu   OPTIONSu   PUTu   DELETEu   TRACEu   Unknown REQUEST_METHOD: %ru   SCRIPT_NAMEu   /u$   SCRIPT_NAME doesn't start with /: %ru	   PATH_INFOu"   PATH_INFO doesn't start with /: %ru   CONTENT_LENGTHi    u   Invalid CONTENT_LENGTH: %rug   One of SCRIPT_NAME or PATH_INFO are required (PATH_INFO should at least be '/' if SCRIPT_NAME is empty)uO   SCRIPT_NAME cannot be '/'; it should instead be '', and PATH_INFO should be '/'(   u   httpu   https(   u   GETu   HEADu   POSTu   OPTIONSu   PUTu   DELETEu   TRACE(   u   assert_u
   isinstanceu   dictu   typeu   warningsu   warnu   WSGIWarningu   keysu   stru   tupleu   check_inputu   check_errorsu   getu
   startswithu   int(   u   environu   key(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_environ!  sf    	   #
c             C   sA   x: d d d d g D]& } t  t |  | ƒ d |  | f ƒ q Wd  S(   Nu   readu   readlineu	   readlinesu   __iter__u-   wsgi.input (%r) doesn't have the attribute %s(   u   assert_u   hasattr(   u
   wsgi_inputu   attr(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_inputb  s
     c             C   s>   x7 d d d g D]& } t  t |  | ƒ d |  | f ƒ q Wd  S(   Nu   flushu   writeu
   writelinesu.   wsgi.errors (%r) doesn't have the attribute %s(   u   assert_u   hasattr(   u   wsgi_errorsu   attr(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_errorsh  s
     c             C   s°   t  t |  t ƒ d |  ƒ |  j d  d ƒ d } t  t | ƒ d k d | ƒ t | ƒ } t  | d k d | ƒ t |  ƒ d k  p |  d d	 k o t j d
 |  t	 ƒ n d  S(   Nu    Status must be a string (not %r)i   i    i   u)   Status codes must be three characters: %rid   u   Status code is invalid: %ri   u    uj   The status string (%r) should be a three-digit integer followed by a single space and a status explanation(
   u   assert_u
   isinstanceu   stru   splitu   Noneu   lenu   intu   warningsu   warnu   WSGIWarning(   u   statusu   status_codeu
   status_int(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_statusn  s    $c             C   s]  t  t |  t ƒ d |  t |  ƒ f ƒ i  } x*|  D]"} t  t | t ƒ 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	 | ƒ o- t  d d | t j	 | ƒ j d ƒ f ƒ q3 q3 Wd  S(   Nu%   Headers (%r) must be of type list: %ru1   Individual headers (%r) must be of type tuple: %ri   u   statusuy   The Status header cannot be used; it conflicts with CGI script, and HTTP status is not given through headers (value: %r).u   
u   :u,   Header names may not contain ':' or '\n': %ru   Bad header name: %ru   -u   _u#   Names may not end in '-' or '_': %ri    u#   Bad header value: %r (bad char: %r)(   u   assert_u
   isinstanceu   listu   typeu   tupleu   lenu   loweru   Noneu	   header_reu   searchu   endswithu   bad_header_value_reu   group(   u   headersu   header_namesu   itemu   nameu   value(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_headers}  s.     !	c             C   s™   t  |  j d  d ƒ d ƒ } d } xN | D]F \ } } | j ƒ  d k o' | | k o d  St d d | ƒ q) q) W| | k o t d d | ƒ n d  S(	   Ni   i    iÌ   i0  u   content-typeuJ   Content-Type header found in a %s response, which must not return content.u,   No Content-Type header found in headers (%s)(   iÌ   i0  (   u   intu   splitu   Noneu   loweru   assert_(   u   statusu   headersu   codeu   NO_MESSAGE_BODYu   nameu   value(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_content_type–  s     	c             C   s7   t  |  d  k p t |  t ƒ d |  t |  ƒ f ƒ d  S(   Nu    exc_info (%r) is not a tuple: %r(   u   assert_u   Noneu
   isinstanceu   tupleu   type(   u   exc_info(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_exc_info¤  s    c             C   s   t  t |  t ƒ d ƒ d  S(   Nuv   You should not return a string as your application iterator, instead return a single-item list containing that string.(   u   assert_u
   isinstanceu   str(   u   iterator(    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   check_iterator©  s    (   u   __doc__u   __all__u   reu   sysu   warningsu   compileu	   header_reu   bad_header_value_reu   Warningu   WSGIWarningu   assert_u	   validatoru   InputWrapperu   ErrorWrapperu   WriteWrapperu   PartialIteratorWrapperu   IteratorWrapperu   check_environu   check_inputu   check_errorsu   check_statusu   check_headersu   check_content_typeu   check_exc_infou   check_iterator(    (    (    u-   /mit/python/lib/python3.0/wsgiref/validate.pyu   <module>n   s,   			7"		!	A						