³ò
,e|Mc           @   sž  d  Z  d d k Z d d k l Z l Z d d k l Z d d k Z d d k Z y d d k l	 Z	 Wn# e
 j
 o d d k l	 Z	 n Xd d k l Z d d d	 d
 d d d d d g	 Z d „  Z d „  Z d „  Z d „  Z e d „ Z e e d d d d „ Z d „  Z d „  Z d „  Z h  d d <d d <d d <Z d „  Z d e	 f d  „  ƒ  YZ d! „  Z e e i _ e  d" j o d d k! Z! e! i" ƒ  n d S(#   sÑ  
This module provides helper routines with work directly on a WSGI
environment to solve common requirements.

   * get_cookies(environ)
   * parse_querystring(environ)
   * parse_formvars(environ, include_get_vars=True)
   * construct_url(environ, with_query_string=True, with_path_info=True,
                   script_name=None, path_info=None, querystring=None)
   * path_info_split(path_info)
   * path_info_pop(environ)
   * resolve_relative_url(url, environ)

iÿÿÿÿN(   t   SimpleCookiet   CookieError(   t   StringIO(   t	   DictMixin(   t	   MultiDictt   get_cookiest   get_cookie_dictt   parse_querystringt   parse_formvarst   construct_urlt   path_info_splitt   path_info_popt   resolve_relative_urlt   EnvironHeadersc         C   s   |  i  d d ƒ } |  i d ƒ o) |  d \ } } | | j o | SqK n t ƒ  } y | i | ƒ Wn t j
 o n X| | f |  d <| S(   sµ   
    Gets a cookie object (which is a dictionary-like object) from the
    request environment; caches this value in case get_cookies is
    called again for the same request.

    t   HTTP_COOKIEt    s   paste.cookies(   t   gett   has_keyR    t   loadR   (   t   environt   headert   cookiest   check_header(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   #   s    	c         C   sÃ   |  i  d ƒ } | p h  Sn |  i d ƒ o) |  d \ } } | | j o | SqW n t ƒ  } y | i | ƒ Wn t j
 o n Xh  } x | D] } | | i | | <q” W| | f |  d <| S(   s#  Return a *plain* dictionary of cookies as found in the request.

    Unlike ``get_cookies`` this returns a dictionary, not a
    ``SimpleCookie`` object.  For incoming cookies a dictionary fully
    represents the information.  Like ``get_cookies`` this caches and
    checks the cache.
    R   s   paste.cookies.dict(   R   R   R    R   R   t   value(   R   R   R   R   t   resultt   name(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   7   s$    	 c         C   s†   |  i  d d ƒ } | p g  Sn d |  j o) |  d \ } } | | j o | SqW n t i | d t d t ƒ} | | f |  d <| S(   s<  
    Parses a query string into a list like ``[(name, value)]``.
    Caches this value in case parse_querystring is called again
    for the same request.

    You can pass the result to ``dict()``, but be aware that keys that
    appear multiple times will be lost (only the last value will be
    preserved).

    t   QUERY_STRINGR   s   paste.parsed_querystringt   keep_blank_valuest   strict_parsing(   R   t   cgit	   parse_qslt   Truet   False(   R   t   sourcet   parsedt   check_source(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   Q   s    	c         C   s•   |  i  d d ƒ } | p t ƒ  Sn d |  j o) |  d \ } } | | j o | SqZ n t i | d t d t ƒ} t | ƒ } | | f |  d <| S(   sÊ  Parses a query string like parse_querystring, but returns a MultiDict

    Caches this value in case parse_dict_querystring is called again
    for the same request.

    Example::

        >>> environ = {'QUERY_STRING': 'day=Monday&user=fred&user=jane'}
        >>> parsed = parse_dict_querystring(environ)

        >>> parsed['day']
        'Monday'
        >>> parsed['user']
        'fred'
        >>> parsed.getall('user')
        ['fred', 'jane']

    R   R   s   paste.parsed_dict_querystringR   R   (   R   R   R   R   R   R    (   R   R!   R"   R#   t   multi(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   parse_dict_querystringh   s    	c         C   sE  |  d } d |  j oG |  d \ } } | | j o& | o | i  t |  ƒ ƒ n | Sq^ n |  i d d ƒ i ƒ  } d | j o | i d d ƒ d } n | d j } |  i d
 ƒ p d |  d
 <n |  i d d ƒ } d |  d <| oB t d ƒ } |  i d ƒ }	 |  i d
 ƒ }
 d |  d
 <d |  d <n |  d } t i d | d |  d d ƒ } | |  d <| o |	 |  d <|
 |  d
 <n t ƒ  } t	 | i
 t ƒ oz xw | i ƒ  D]e } | | } t	 | t ƒ p | g } n x5 | D]- } | i p | i
 } n | i | | ƒ qÚWq¦Wn | | f |  d <| o | i  t |  ƒ ƒ n | S(   s¥  Parses the request, returning a MultiDict of form variables.

    If ``include_get_vars`` is true then GET (query string) variables
    will also be folded into the MultiDict.

    All values should be strings, except for file uploads which are
    left as ``FieldStorage`` instances.

    If the request was not a normal form request (e.g., a POST with an
    XML body) then ``environ['wsgi.input']`` won't be read.
    s
   wsgi.inputs   paste.parsed_formvarst   CONTENT_TYPER   t   ;i   i    s!   application/x-www-form-urlencodeds   multipart/form-datat   CONTENT_LENGTHt   0R   t   fpR   R   (   R   s!   application/x-www-form-urlencodeds   multipart/form-data(   t   updateR   R   t   lowert   splitR   R   t   FieldStorageR   t
   isinstanceR   t   listt   keyst   filenamet   add(   R   t   include_get_varsR!   R"   R#   t   typet   fake_out_cgit   old_query_stringt   inputt   old_content_typet   old_content_lengtht   fst   formvarsR   t   valuesR   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   ˆ   s\    
	


	

	 
 
c   	      C   s7  |  d d } |  i  d ƒ o¸ |  d } d } d | j ot | i d d ƒ \ } } |  d d j o | d j o
 d } q« q¯ |  d d j o | d	 j o
 d } q« q¯ n | | 7} | o | d
 | 7} qGnr | |  d 7} |  d d j o+ |  d d j o | d |  d 7} qGn( |  d d	 j o | d |  d 7} n | d j o# | t i |  i  d d ƒ ƒ 7} n | t i | ƒ 7} | oG | d j o# | t i |  i  d d ƒ ƒ 7} qØ| t i | ƒ 7} n | oT | d j o* |  i  d ƒ o | d |  d 7} q/q3| o | d | 7} q3n | S(   s“   Reconstructs the URL from the WSGI environment.

    You may override SCRIPT_NAME, PATH_INFO, and QUERYSTRING with
    the keyword arguments.

    s   wsgi.url_schemes   ://t	   HTTP_HOSTt   :i   t   httpst   443t   httpt   80s   :%st   SERVER_NAMEt   SERVER_PORTt   SCRIPT_NAMER   t	   PATH_INFOR   t   ?N(   R   t   NoneR-   t   urllibt   quote(	   R   t   with_query_stringt   with_path_infot   script_namet	   path_infot   querystringt   urlt   hostt   port(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR	   È   sF    

##c         C   s"   t  | d t ƒ} t i | |  ƒ S(   s  
    Resolve the given relative URL as being relative to the
    location represented by the environment.  This can be used
    for redirecting to a relative path.  Note: if url is already
    absolute, this function will (intentionally) have no effect
    on it.

    RL   (   R	   R    t   urlparset   urljoin(   RQ   R   t   cur_url(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   ú   s    	c         C   s   |  p d Sn |  i d ƒ p t d |  ‚ |  i d ƒ }  d |  j o* |  i d d ƒ \ } } | d | f Sn |  d f Sd S(   s   
    Splits off the first segment of the path.  Returns (first_part,
    rest_of_path).  first_part can be None (if PATH_INFO is empty), ''
    (if PATH_INFO is '/'), or a name without any /'s.  rest_of_path
    can be '' or a string starting with /.

    R   t   /s!   PATH_INFO should start with /: %ri   N(   NR   (   RI   t
   startswitht   AssertionErrort   lstripR-   (   RO   t   firstt   rest(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR
     s    c         C   sÀ   |  i  d d ƒ } | p d Sn x/ | i d ƒ o |  d c d 7<| d } q$ Wd | j o" |  d c | 7<d |  d <| Sn; | i d d ƒ \ } } d | |  d <|  d c | 7<| Sd S(   s…  
    'Pops' off the next segment of PATH_INFO, pushing it onto
    SCRIPT_NAME, and returning that segment.

    For instance::

        >>> def call_it(script_name, path_info):
        ...     env = {'SCRIPT_NAME': script_name, 'PATH_INFO': path_info}
        ...     result = path_info_pop(env)
        ...     print 'SCRIPT_NAME=%r; PATH_INFO=%r; returns=%r' % (
        ...         env['SCRIPT_NAME'], env['PATH_INFO'], result)
        >>> call_it('/foo', '/bar')
        SCRIPT_NAME='/foo/bar'; PATH_INFO=''; returns='bar'
        >>> call_it('/foo/bar', '')
        SCRIPT_NAME='/foo/bar'; PATH_INFO=''; returns=None
        >>> call_it('/foo/bar', '/')
        SCRIPT_NAME='/foo/bar/'; PATH_INFO=''; returns=''
        >>> call_it('', '/1/2/3')
        SCRIPT_NAME='/1'; PATH_INFO='/2/3'; returns='1'
        >>> call_it('', '//1/2')
        SCRIPT_NAME='//1'; PATH_INFO='/2'; returns='1'

    RG   R   RW   RF   i   N(   R   RI   RX   R-   (   R   t   patht   segment(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR     s     
t   Authorizationt   HTTP_CGI_AUTHORIZATIONs   Content-LengthR(   s   Content-TypeR&   c         c   sv   xo |  i  ƒ  D]a \ } } | t j o t | | f Vq | i d ƒ o% | d i ƒ  i d d ƒ | f Vq q Wd S(   s‚   
    Parse the headers in the environment (like ``HTTP_HOST``) and
    yield a sequence of those (header_name, value) tuples.
    t   HTTP_i   t   _t   -N(   t	   iteritemst   _parse_headers_specialRX   t   titlet   replace(   R   t   cgi_varR   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   parse_headersH  s     c           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   sg  An object that represents the headers as present in a
    WSGI environment.

    This object is a wrapper (with no internal state) for a WSGI
    request object, representing the CGI-style HTTP_* keys as a
    dictionary.  Because a CGI environment can only hold one value for
    each key, this dictionary is single-valued (unlike outgoing
    headers).
    c         C   s   | |  _  d  S(   N(   R   (   t   selfR   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __init___  s    c         C   sN   d | i  d d ƒ i ƒ  } | d j o
 d } n | d j o
 d } n | S(   NRa   Rc   Rb   t   HTTP_CONTENT_LENGTHR(   t   HTTP_CONTENT_TYPER&   (   Rg   t   upper(   Rj   R   t   key(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   _trans_nameb  s    

c         C   s`   | d j o d SnH | d j o d Sn3 | i  d ƒ o | d i d d ƒ i ƒ  Sn d  Sd  S(	   NR&   s   Content-TypeR(   s   Content-LengthRa   i   Rb   Rc   (   RX   Rg   Rf   RI   (   Rj   Ro   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt
   _trans_keyj  s    c         C   s   |  i  |  i | ƒ S(   N(   R   Rp   (   Rj   t   item(    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __getitem__t  s    c         C   s   | |  i  |  i | ƒ <d  S(   N(   R   Rp   (   Rj   Rr   R   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __setitem__w  s    c         C   s   |  i  |  i | ƒ =d  S(   N(   R   Rp   (   Rj   Rr   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __delitem__{  s    c         c   s=   x6 |  i  D]+ } |  i | ƒ } | d  j	 o	 | Vq
 q
 Wd  S(   N(   R   Rq   RI   (   Rj   Ro   R   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __iter__~  s
    
 c         C   s   t  t |  ƒ ƒ S(   N(   R0   t   iter(   Rj   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR1   „  s    c         C   s   |  i  | ƒ |  i j S(   N(   Rp   R   (   Rj   Rr   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   __contains__‡  s    (   t   __name__t
   __module__t   __doc__Rk   Rp   Rq   Rs   Rt   Ru   Rv   R1   Rx   (    (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyR   T  s   				
					c         C   s<   |  i  o d |  i |  i f Sn d |  i |  i |  i f S(   sÛ    monkey patch for FieldStorage.__repr__

    Unbelievely, the default __repr__ on FieldStorage reads
    the entire file content instead of being sane about it.
    This is a simple replacement that doesn't do that
    s   FieldStorage(%r, %r)s   FieldStorage(%r, %r, %r)(   t   fileR   R2   R   (   Rj   (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pyt   _cgi_FieldStorage__repr__patchŠ  s
    
t   __main__(#   R{   R   t   CookieR    R   R   RT   RJ   t   UserDictR   t   ImportErrort   paste.util.UserDict24t   paste.util.multidictR   t   __all__R   R   R   R%   R   R   RI   R	   R   R
   R   Re   Ri   R   R}   R.   t   __repr__Ry   t   doctestt   testmod(    (    (    s\   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/request.pys   <module>   sD   						 @1			(			6	