

_V`(                 @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m	 Z
 d  d l m Z d  d l m Z d  d l m Z d  d l m Z m Z m Z d  d l m Z d  d	 l m Z m Z m Z m Z m Z m Z e j d
  Z  d j!   Z" d Z# d Z$ d Z% d Z& d Z' d Z( e j d e# e% e& e( f  Z) e j d e# e% e' e( f  Z* e j d e% e$ e( e& f  Z+ e, d  Z- e, d  Z. i d d 6d d 6Z/ d d d  Z0 e e0 e j1  Z0 d d d   Z2 e e2 e j1  Z2 d! d"   Z3 e e3 e j1  Z3 d# d$   Z4 e e4 e j1  Z4 d  d% d&  Z d d' d(  Z5 d d) d*  Z6 d+ d,   Z7 d- d.   Z8 d/ d0   Z9 d1 d2   Z: d3 d4   Z; d5 d6   Z< d7 d8   Z= d9 d:   Z> d; d<   Z? d= d>   Z@ d d? d@  ZA d S)A    )unicode_literalsN)Error)
formatdate)six)MultiValueDict)force_bytes	force_str
force_text)
allow_lazy)quote
quote_plusunquoteunquote_plus	urlencodeurlparsez(?:W/)?"((?:\\.|[^"])*)"z/jan feb mar apr may jun jul aug sep oct nov decz(?P<day>\d{2})z(?P<day>[ \d]\d)z(?P<mon>\w{3})z(?P<year>\d{4})z(?P<year>\d{2})z-(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})z^\w{3}, %s %s %s %s GMT$z^\w{6,9}, %s-%s-%s %s GMT$z^\w{3} %s %s %s %s$z:/?#[]@z!$&'()*+,;=P   httpi  https/c             C   s   t  t t |   t |    S)a  
    A version of Python's urllib.quote() function that can operate on unicode
    strings. The url is first UTF-8 encoded before quoting. The returned string
    can safely be used as part of an argument to a subsequent iri_to_uri() call
    without double-quoting occurring.
    )r	   r   r   )urlsafe r   3/tmp/pip-build-ghmbqnp_/Django/django/utils/http.pyurlquote+   s    r    c             C   s   t  t t |   t |    S)a  
    A version of Python's urllib.quote_plus() function that can operate on
    unicode strings. The url is first UTF-8 encoded before quoting. The
    returned string can safely be used as part of an argument to a subsequent
    iri_to_uri() call without double-quoting occurring.
    )r	   r   r   )r   r   r   r   r   urlquote_plus6   s    r   c             C   s   t  t t |     S)z~
    A wrapper for Python's urllib.unquote() function that can operate on
    the result of django.utils.http.urlquote().
    )r	   r   r   )
quoted_urlr   r   r   
urlunquoteA   s    r   c             C   s   t  t t |     S)z
    A wrapper for Python's urllib.unquote_plus() function that can operate on
    the result of django.utils.http.urlquote_plus().
    )r	   r   r   )r   r   r   r   urlunquote_plusJ   s    r   c             C   sV   t  |  t  r |  j   }  n t |  d  r< |  j   }  n  t d d   |  D |  S)z
    A version of Python's urllib.urlencode() function that can operate on
    unicode strings. The parameters are first cast to UTF-8 encoded strings and
    then encoded as per normal.
    itemsc             S   sV   g  |  ]L \ } } t  |  t | t t f  rC d  d   | D n	 t  |  f  q S)c             S   s   g  |  ] } t  |   q Sr   )r   ).0ir   r   r   
<listcomp>_   s   	 z(urlencode.<locals>.<listcomp>.<listcomp>)r   
isinstancelisttuple)r    kvr   r   r   r"   ^   s   	zurlencode.<locals>.<listcomp>)r#   r   listshasattrr   original_urlencode)querydoseqr   r   r   r   S   s    	r   c             C   s;   t  |   } d | d d  | d d  | d d  f S)aI  
    Formats the time to ensure compatibility with Netscape's cookie standard.

    Accepts a floating point number expressed in seconds since the epoch, in
    UTC - such as that outputted by time.time(). If set to None, defaults to
    the current time.

    Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'.
    z%s-%s-%s GMTN               )r   )epoch_secondsZrfcdater   r   r   cookie_dated   s    
r3   c             C   s   t  |  d d S)aa  
    Formats the time to match the RFC1123 date format as specified by HTTP
    RFC2616 section 3.3.1.

    Accepts a floating point number expressed in seconds since the epoch, in
    UTC - such as that outputted by time.time(). If set to None, defaults to
    the current time.

    Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
    usegmtT)r   )r2   r   r   r   	http_dater   s    r5   c       
      C   s{  xF t  t t f D]% } | j |   } | d k	 r Pq q Wt d |    y t | j d   } | d k  r | d k  r | d 7} q | d 7} n  t j | j d  j	    d	 } t | j d
   } t | j d   } t | j d   } t | j d   } t
 j
 | | | | | |  }	 t j |	 j    SWn9 t k
 rvt j t t d |   t j   d  Yn Xd S)a  
    Parses a date format as specified by HTTP RFC2616 section 3.3.1.

    The three formats allowed by the RFC are accepted, even if only the first
    one is still in widespread use.

    Returns an integer expressed in seconds since the epoch, in UTC.
    Nz%%r is not in a valid HTTP date formatyeard   F   i  il  mon   dayhourminsecz%r is not a valid date   )RFC1123_DATERFC850_DATEASCTIME_DATEmatch
ValueErrorintgroupMONTHSindexlowerdatetimecalendartimegmutctimetuple	Exceptionr   reraisesysexc_info)
dateregexmr6   monthr;   r<   r=   r>   resultr   r   r   parse_http_date   s(    "rW   c             C   s'   y t  |   SWn t k
 r" Yn Xd S)zL
    Same as parse_http_date, but returns None if the input is invalid.
    N)rW   rN   )rR   r   r   r   parse_http_date_safe   s    rX   c             C   s[   t  |   d k r! t d   n  t |  d  } t j rW | t j k rW t d   n  | S)zo
    Converts a base 36 string to an ``int``. Raises ``ValueError` if the
    input won't fit into an int.
       zBase36 input too large$   )lenrD   rE   r   PY2rP   maxint)svaluer   r   r   base36_to_int   s    r`   c             C   s   d } |  d k  r! t  d   n  t j rl t |  t j  sK t d   n  |  t j k rl t  d   ql n  |  d k  r | |  Sd } x3 |  d k r t |  d  \ }  } | | | } q W| S)z0
    Converts an integer to a base36 string
    Z$0123456789abcdefghijklmnopqrstuvwxyzr   z!Negative base36 conversion input.z$Non-integer base36 conversion input.z"Base36 conversion input too large.rZ   r   )	rD   r   r\   r#   integer_types	TypeErrorrP   r]   divmod)r!   Zchar_setZb36nr   r   r   int_to_base36   s    	re   c             C   s   t  j |   j d  S)za
    Encodes a bytestring in base64 for use in URLs, stripping any trailing
    equal signs.
    s   
=)base64urlsafe_b64encoderstrip)r^   r   r   r   urlsafe_base64_encode   s    ri   c             C   s{   t  |   }  y1 t j |  j t |   t |   d d   SWn7 t t f k
 rv } z t |   WYd d } ~ Xn Xd S)zr
    Decodes a base64 encoded string, adding back any trailing equal signs that
    might have been stripped.
          =N)r   rf   urlsafe_b64decodeljustr[   LookupErrorBinasciiErrorrD   )r^   er   r   r   urlsafe_base64_decode   s
    1rq   c             C   s3   t  j |   } | s |  g Sd d   | D } | S)z
    Parses a string with one or several etags passed in If-None-Match and
    If-Match headers by the rules in RFC 2616. Returns a list of etags
    without surrounding double quotes (") and unescaped from \<CHAR>.
    c             S   s(   g  |  ] } | j  d   j d   q S)asciiZunicode_escape)encodedecode)r    rp   r   r   r   r"      s   	 zparse_etags.<locals>.<listcomp>)
ETAG_MATCHfindall)Zetag_strZetagsr   r   r   parse_etags   s
    rw   c             C   s    d |  j  d d  j  d d  S)zI
    Wraps a string in double quotes escaping contents as necessary.
    z"%s"\z\\"z\")replace)etagr   r   r   
quote_etag   s    r|   c             C   s/   |  r+ |  j  d  j d d  j d d  S|  S)z;
    Unquote an ETag string; i.e. revert quote_etag().
    ry   z\"z\\rx   )striprz   )r{   r   r   r   unquote_etag   s    r~   c             C   sU   | s
 d S| j    } | d d k rK |  j |  pT |  | d d  k pT | |  k S)a4  
    Return ``True`` if the host is either an exact match or a match
    to the wildcard pattern.

    Any pattern beginning with a period matches a domain and all of its
    subdomains. (e.g. ``.example.com`` matches ``example.com`` and
    ``foo.example.com``). Anything else is an exact string match.
    Fr   .r:   N)rI   endswith)hostpatternr   r   r   is_same_domain  s
    	5r   c             C   s   |  d k	 r |  j    }  n  |  s% d S|  j d d  }  |  j d  rJ d St |   } | j rm | j rm d St j |  d  d d k r d S| j s | j | k o | j p | j d
 k S)z
    Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
    a different host and uses a safe scheme).

    Always returns ``False`` on an empty url.
    NFrx   r   z///r   Cr   r   )zhttpzhttps)r}   rz   
startswithr   netlocschemeunicodedatacategory)r   r   Zurl_infor   r   r   is_safe_url  s    r   )B
__future__r   rf   rK   rJ   rerP   r   binasciir   ro   email.utilsr   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   r   r	   Zdjango.utils.functionalr
   Z#django.utils.six.moves.urllib.parser   r   r   r   r   r*   r   compileru   splitrG   Z__DZ__D2Z__MZ__YZ__Y2Z__Tr@   rA   rB   strZRFC3986_GENDELIMSZRFC3986_SUBDELIMSZPROTOCOL_TO_PORTr   	text_typer   r   r   r3   r5   rW   rX   r`   re   ri   rq   rw   r|   r~   r   r   r   r   r   r   <module>   sf   .
$