3
Z_ð]|^  ã               @   sv  d Z ddlZddlZdddgZdjZdjZdjZd	d
„ ZG dd„ de	ƒZ
ejej d Zed Zdd„ eedƒƒeeeeƒƒ D ƒZejedƒdedƒdiƒ ejdejeƒ ƒjZdd„ ZejdƒZejdƒZdd„ Zddddd d!d"gZdd#d$d%d&d'd(d)d*d+d,d-d.gZdeefd/d0„ZG d1d2„ d2e ƒZ!d3Z"e"d4 Z#ejd5e" d6 e# d7 ej$ƒZ%G d8d„ de ƒZ&G d9d„ de&ƒZ'dS ):a.
  
Here's a sample session to show how to use this module.
At the moment, this is the only documentation.

The Basics
----------

Importing is easy...

   >>> from http import cookies

Most of the time you start by creating a cookie.

   >>> C = cookies.SimpleCookie()

Once you've created your Cookie, you can add values just as if it were
a dictionary.

   >>> C = cookies.SimpleCookie()
   >>> C["fig"] = "newton"
   >>> C["sugar"] = "wafer"
   >>> C.output()
   'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'

Notice that the printable representation of a Cookie is the
appropriate format for a Set-Cookie: header.  This is the
default behavior.  You can change the header and printed
attributes by using the .output() function

   >>> C = cookies.SimpleCookie()
   >>> C["rocky"] = "road"
   >>> C["rocky"]["path"] = "/cookie"
   >>> print(C.output(header="Cookie:"))
   Cookie: rocky=road; Path=/cookie
   >>> print(C.output(attrs=[], header="Cookie:"))
   Cookie: rocky=road

The load() method of a Cookie extracts cookies from a string.  In a
CGI script, you would use this method to extract the cookies from the
HTTP_COOKIE environment variable.

   >>> C = cookies.SimpleCookie()
   >>> C.load("chips=ahoy; vienna=finger")
   >>> C.output()
   'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'

The load() method is darn-tootin smart about identifying cookies
within a string.  Escaped quotation marks, nested semicolons, and other
such trickeries do not confuse it.

   >>> C = cookies.SimpleCookie()
   >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
   >>> print(C)
   Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"

Each element of the Cookie also supports all of the RFC 2109
Cookie attributes.  Here's an example which sets the Path
attribute.

   >>> C = cookies.SimpleCookie()
   >>> C["oreo"] = "doublestuff"
   >>> C["oreo"]["path"] = "/"
   >>> print(C)
   Set-Cookie: oreo=doublestuff; Path=/

Each dictionary element has a 'value' attribute, which gives you
back the value associated with the key.

   >>> C = cookies.SimpleCookie()
   >>> C["twix"] = "none for you"
   >>> C["twix"].value
   'none for you'

The SimpleCookie expects that all values should be standard strings.
Just to be sure, SimpleCookie invokes the str() builtin to convert
the value to a string, when the values are set dictionary-style.

   >>> C = cookies.SimpleCookie()
   >>> C["number"] = 7
   >>> C["string"] = "seven"
   >>> C["number"].value
   '7'
   >>> C["string"].value
   'seven'
   >>> C.output()
   'Set-Cookie: number=7\r\nSet-Cookie: string=seven'

Finis.
é    NÚCookieErrorÚ
BaseCookieÚSimpleCookieÚ z; ú c             C   s$   dd l }d|  }|j|tdd� d S )Nr   zvThe .%s setter is deprecated. The attribute will be read-only in future releases. Please use the set() method instead.é   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarning)Úsetterr	   Úmsg© r   ú?/tmp/pip-build-yz_zf6az/eventlet/eventlet/green/http/cookies.pyÚ_warn_deprecated_setterÃ   s    r   c               @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   Ì   s   z!#$%&'*+-.^_`|~:z ()/<=>?@[]{}c             C   s   i | ]}d | |“qS )z\%03or   )Ú.0Únr   r   r   ú
<dictcomp>à   s   r   é   ú"z\"ú\z\\z[%s]+\Zc             C   s*   | dkst | ƒr| S d| jtƒ d S dS )zãQuote a string for use in a cookie header.

    If the string does not need to be double-quoted, then just return the
    string.  Otherwise, surround the string in doublequotes and quote
    (with a \) special characters.
    Nr   )Ú_is_legal_keyÚ	translateÚ_Translator)Ústrr   r   r   Ú_quoteê   s    r   z\\[0-3][0-7][0-7]z[\\].c             C   sT  | d kst | ƒdk r| S | d dks0| d dkr4| S | dd… } d}t | ƒ}g }xúd|  kod|k n  �rJtj| |ƒ}tj| |ƒ}| r¤| r¤|j| |d … ƒ P d	 }}|rº|jdƒ}|rÈ|jdƒ}|oØ| sÜ||k �r
|j| ||… ƒ |j| |d  ƒ |d }qR|j| ||… ƒ |jtt| |d |d … dƒƒƒ |d }qRW t|ƒS )
Né   r   r   é   é   é   éÿÿÿÿr#   r#   )	ÚlenÚ
_OctalPattÚsearchÚ
_QuotePattÚappendÚstartÚchrÚintÚ	_nulljoin)r   Úir   ÚresÚo_matchÚq_matchÚjÚkr   r   r   Ú_unquoteú   s6    


$r3   ÚMonÚTueÚWedÚThuÚFriÚSatÚSunÚJanÚFebÚMarÚAprÚMayÚJunÚJulÚAugÚSepÚOctÚNovÚDecc          	   C   sR   ddl m}m} |ƒ }|||  ƒ\	}}}}	}
}}}}d|| ||| ||	|
|f S )Nr   )ÚgmtimeÚtimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)Zeventlet.green.timerG   rH   )ÚfutureÚweekdaynameÚ	monthnamerG   rH   ÚnowÚyearÚmonthÚdayÚhhÚmmÚssÚwdÚyÚzr   r   r   Ú_getdate2  s
    rV   c            	   @   s  e Zd ZdZdddddddd	d
œZddhZdd„ Zedd„ ƒZej	dd„ ƒZedd„ ƒZ
e
j	dd„ ƒZ
edd„ ƒZej	dd„ ƒZdd„ Zd4dd„Zdd„ ZejZdd „ Zd!d"„ Zd#d$„ Zefd%d&„Zd'd(„ Zd)d*„ Zd5d,d-„ZeZd.d/„ Zd6d0d1„Zd7d2d3„ZdS )8ÚMorsela‘  A class to hold ONE (key, value) pair.

    In a cookie, each such pair may have several attributes, so this class is
    used to keep the attributes associated with the appropriate key,value pair.
    This class also includes a coded_value attribute, which is used to hold
    the network representation of the value.  This is most useful when Python
    objects are pickled for network transit.
    ÚexpiresÚPathÚCommentÚDomainzMax-AgeÚSecureÚHttpOnlyÚVersion)rX   ÚpathÚcommentÚdomainzmax-ageÚsecureÚhttponlyÚversionrb   rc   c             C   s4   d  | _  | _| _x| jD ]}tj| |dƒ qW d S )Nr   )Ú_keyÚ_valueÚ_coded_valueÚ	_reservedÚdictÚ__setitem__)ÚselfÚkeyr   r   r   Ú__init__]  s    zMorsel.__init__c             C   s   | j S )N)re   )rk   r   r   r   rl   e  s    z
Morsel.keyc             C   s   t dƒ || _d S )Nrl   )r   re   )rk   rl   r   r   r   rl   i  s    c             C   s   | j S )N)rf   )rk   r   r   r   Úvaluen  s    zMorsel.valuec             C   s   t dƒ || _d S )Nrn   )r   rf   )rk   rn   r   r   r   rn   r  s    c             C   s   | j S )N)rg   )rk   r   r   r   Úcoded_valuew  s    zMorsel.coded_valuec             C   s   t dƒ || _d S )Nro   )r   rg   )rk   ro   r   r   r   ro   {  s    c             C   s2   |j ƒ }|| jkr td|f ƒ‚tj| ||ƒ d S )NzInvalid attribute %r)Úlowerrh   r   ri   rj   )rk   ÚKÚVr   r   r   rj   €  s    
zMorsel.__setitem__Nc             C   s.   |j ƒ }|| jkr td|f ƒ‚tj| ||ƒS )NzInvalid attribute %r)rp   rh   r   ri   Ú
setdefault)rk   rl   Úvalr   r   r   rs   †  s    
zMorsel.setdefaultc             C   s>   t |tƒstS tj| |ƒo<| j|jko<| j|jko<| j|jkS )N)Ú
isinstancerW   ÚNotImplementedri   Ú__eq__rf   re   rg   )rk   Úmorselr   r   r   rw   Œ  s    
zMorsel.__eq__c             C   s$   t ƒ }tj|| ƒ |jj| jƒ |S )N)rW   ri   ÚupdateÚ__dict__)rk   rx   r   r   r   Úcopy–  s    zMorsel.copyc             C   sV   i }x@t |ƒjƒ D ]0\}}|jƒ }|| jkr:td|f ƒ‚|||< qW t j| |ƒ d S )NzInvalid attribute %r)ri   Úitemsrp   rh   r   ry   )rk   ÚvaluesÚdatarl   rt   r   r   r   ry   œ  s    
zMorsel.updatec             C   s   |j ƒ | jkS )N)rp   rh   )rk   rq   r   r   r   ÚisReservedKey¥  s    zMorsel.isReservedKeyc             C   sh   |t kr dd l}|jdtdd� |jƒ | jkr<td|f ƒ‚t|ƒsRtd|f ƒ‚|| _|| _	|| _
d S )Nr   zSLegalChars parameter is deprecated, ignored and will be removed in future versions.r   )r   z Attempt to set a reserved key %rzIllegal key %r)Ú_LegalCharsr	   r
   r   rp   rh   r   r   re   rf   rg   )rk   rl   rt   Ú	coded_valÚ
LegalCharsr	   r   r   r   Úset¨  s    z
Morsel.setc             C   s   | j | j| jdœS )N)rl   rn   ro   )re   rf   rg   )rk   r   r   r   Ú__getstate__º  s    zMorsel.__getstate__c             C   s"   |d | _ |d | _|d | _d S )Nrl   rn   ro   )re   rf   rg   )rk   Ústater   r   r   Ú__setstate__Á  s    

zMorsel.__setstate__úSet-Cookie:c             C   s   d|| j |ƒf S )Nz%s %s)ÚOutputString)rk   ÚattrsÚheaderr   r   r   ÚoutputÆ  s    zMorsel.outputc             C   s   d| j j| jƒ f S )Nz<%s: %s>)Ú	__class__r   rˆ   )rk   r   r   r   Ú__repr__Ë  s    zMorsel.__repr__c             C   s   d| j |ƒjddƒ S )Nz—
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "%s";
        // end hiding -->
        </script>
        r   z\")rˆ   Úreplace)rk   r‰   r   r   r   Ú	js_outputÎ  s    zMorsel.js_outputc             C   sö   g }|j }|d| j| jf ƒ |d kr,| j}t| jƒ ƒ}x´|D ]¬\}}|dkrPq>||krZq>|dkrˆt|tƒrˆ|d| j| t|ƒf ƒ q>|dkr²t|tƒr²|d| j| |f ƒ q>|| j	krÔ|rê|t
| j| ƒƒ q>|d| j| |f ƒ q>W t|ƒS )Nz%s=%sr   rX   zmax-agez%s=%d)r(   rl   ro   rh   Úsortedr|   ru   r+   rV   Ú_flagsr   Ú_semispacejoin)rk   r‰   Úresultr(   r|   rl   rn   r   r   r   rˆ   Ø  s(    
zMorsel.OutputString)N)Nr‡   )N)N)r   r   r   Ú__doc__rh   r‘   rm   Úpropertyrl   r   rn   ro   rj   rs   rw   ÚobjectÚ__ne__r{   ry   r   r€   rƒ   r„   r†   r‹   Ú__str__r�   r�   rˆ   r   r   r   r   rW   :  s@   
	


rW   z,\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=z\[\]zË
    (?x)                           # This is a verbose pattern
    \s*                            # Optional whitespace at start of cookie
    (?P<key>                       # Start of group 'key'
    [a	  ]+?   # Any word of at least one letter
    )                              # End of group 'key'
    (                              # Optional group: there may not be a value.
    \s*=\s*                          # Equal Sign
    (?P<val>                         # Start of group 'val'
    "(?:[^\\"]|\\.)*"                  # Any doublequoted string
    |                                  # or
    \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT  # Special case for "expires" attr
    |                                  # or
    [a-  ]*      # Any word or empty string
    )                                # End of group 'val'
    )?                             # End of optional value group
    \s*                            # Any number of spaces.
    (\s+|;|$)                      # Ending either at space, semicolon, or EOS.
    c               @   sn   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ Zdd„ Zddd„Z	e	Z
dd„ Zddd„Zdd„ Zefdd„ZdS )r   z'A container class for a set of Morsels.c             C   s   ||fS )a
  real_value, coded_value = value_decode(STRING)
        Called prior to setting a cookie's value from the network
        representation.  The VALUE is the value read from HTTP
        header.
        Override this function to modify the behavior of cookies.
        r   )rk   rt   r   r   r   Úvalue_decode  s    zBaseCookie.value_decodec             C   s   t |ƒ}||fS )zýreal_value, coded_value = value_encode(VALUE)
        Called prior to setting a cookie's value from the dictionary
        representation.  The VALUE is the value being assigned.
        Override this function to modify the behavior of cookies.
        )r   )rk   rt   Ústrvalr   r   r   Úvalue_encode'  s    zBaseCookie.value_encodeNc             C   s   |r| j |ƒ d S )N)Úload)rk   Úinputr   r   r   rm   0  s    zBaseCookie.__init__c             C   s.   | j |tƒ ƒ}|j|||ƒ tj| ||ƒ dS )z+Private method for setting a cookie's valueN)ÚgetrW   rƒ   ri   rj   )rk   rl   Ú
real_valuero   ÚMr   r   r   Ú__set4  s    zBaseCookie.__setc             C   s:   t |tƒrtj| ||ƒ n| j|ƒ\}}| j|||ƒ dS )zDictionary style assignment.N)ru   rW   ri   rj   r›   Ú_BaseCookie__set)rk   rl   rn   ÚrvalÚcvalr   r   r   rj   :  s    
zBaseCookie.__setitem__úSet-Cookie:ú
c             C   s>   g }t | jƒ ƒ}x"|D ]\}}|j|j||ƒƒ qW |j|ƒS )z"Return a string suitable for HTTP.)r�   r|   r(   r‹   Újoin)rk   r‰   rŠ   Úsepr“   r|   rl   rn   r   r   r   r‹   C  s
    zBaseCookie.outputc             C   sN   g }t | jƒ ƒ}x(|D ] \}}|jd|t|jƒf ƒ qW d| jjt|ƒf S )Nz%s=%sz<%s: %s>)r�   r|   r(   Úreprrn   rŒ   r   Ú
_spacejoin)rk   Úlr|   rl   rn   r   r   r   r�   M  s
    zBaseCookie.__repr__c             C   s:   g }t | jƒ ƒ}x |D ]\}}|j|j|ƒƒ qW t|ƒS )z(Return a string suitable for JavaScript.)r�   r|   r(   r�   r,   )rk   r‰   r“   r|   rl   rn   r   r   r   r�   T  s
    zBaseCookie.js_outputc             C   s8   t |tƒr| j|ƒ nx|jƒ D ]\}}|| |< q W dS )zÝLoad cookies from a string (presumably HTTP_COOKIE) or
        from a dictionary.  Loading cookies from a dictionary 'd'
        is equivalent to calling:
            map(Cookie.__setitem__, d.keys(), d.values())
        N)ru   r   Ú_BaseCookie__parse_stringr|   )rk   Úrawdatarl   rn   r   r   r   rœ   \  s
    
zBaseCookie.loadc             C   sŒ  d}t |ƒ}g }d}d}d}�x d|  ko2|k n  �r|j||ƒ}	|	sLP |	jdƒ|	jdƒ }
}|	jdƒ}|
d dkr˜|s~q |j||
dd … |fƒ q |
jƒ tjkrò|s®d S |d krÜ|
jƒ tjkrÖ|j||
dfƒ qðd S n|j||
t	|ƒfƒ q |d k	�r|j||
| j
|ƒfƒ d}q d S q W d }xb|D ]Z\}}
}||k�rV|d k	�sLt‚|||
< n,||k�sdt‚|\}}| j|
||ƒ | |
 }�q*W d S )	Nr   Fr    r   rl   rt   ú$T)r$   ÚmatchÚgroupÚendr(   rp   rW   rh   r‘   r3   r™   ÚAssertionErrorr¢   )rk   r   Úpattr-   r   Úparsed_itemsÚmorsel_seenÚTYPE_ATTRIBUTEÚTYPE_KEYVALUEr¯   rl   rn   r    Útpr£   r¤   r   r   r   Ú__parse_stringj  sJ    



zBaseCookie.__parse_string)N)Nr¥   r¦   )N)r   r   r   r”   r™   r›   rm   r¢   rj   r‹   r˜   r�   r�   rœ   Ú_CookiePatternr¬   r   r   r   r   r     s   		
	

c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   zþ
    SimpleCookie supports strings as cookie values.  When setting
    the value using the dictionary assignment notation, SimpleCookie
    calls the builtin str() to convert the value to a string.  Values
    received from HTTP are kept as strings.
    c             C   s   t |ƒ|fS )N)r3   )rk   rt   r   r   r   r™   ®  s    zSimpleCookie.value_decodec             C   s   t |ƒ}|t|ƒfS )N)r   r   )rk   rt   rš   r   r   r   r›   ±  s    zSimpleCookie.value_encodeN)r   r   r   r”   r™   r›   r   r   r   r   r   §  s   )(r”   ÚreÚstringÚ__all__r§   r,   r’   rª   r   Ú	Exceptionr   Úascii_lettersÚdigitsr€   Ú_UnescapedCharsrƒ   ÚrangeÚmapÚordr   ry   ÚcompileÚescaper¯   r   r   r%   r'   r3   Ú_weekdaynameÚ
_monthnamerV   ri   rW   Ú_LegalKeyCharsÚ_LegalValueCharsÚASCIIrº   r   r   r   r   r   r   Ú<module>µ   sF   
	

2 H 