o
    éT•j›T  ã                   @   sl  d Z ddlZddlZddlZg d¢ZdjZdjZdj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 edƒdedƒdi¡ e de e¡ ¡jZe d¡Zdd„ Zdd„ Ze d¡jZdd„ Zdd„ Zg d¢Z g d¢Z!de e!fdd „Z"G d!d"„ d"e#ƒZ$d#Z%e%d$ Z&e d%e% d& e& d' ej'ej(B ¡Z)G d(d)„ d)e#ƒZ*G d*d+„ 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=;";')
   >>> print(C)
   Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=;"

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                   @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r
   r
   ú#/usr/lib/python3.10/http/cookies.pyr   ‘   s    r   z!#$%&'*+-.^_`|~:z ()/<=>?@[]{}c                 C   s   i | ]}|d | “qS )z\%03or
   )Ú.0Únr
   r
   r   Ú
<dictcomp>¥   s    ÿr   é   ú"z\"ú\z\\z[%s]+z[\x00-\x1F\x7F]c                  G   s   t dd„ | D ƒƒS )zhDetects control characters within a value.
    Supports any type, as header values can be any type.
    c                 s   s   � | ]
}t  t|ƒ¡V  qd S ©N)Ú_control_character_reÚsearchÚstr)r   Úvr
   r
   r   Ú	<genexpr>´   s   € z)_has_control_character.<locals>.<genexpr>)Úany)Úvalr
   r
   r   Ú_has_control_character°   s   r   c                 C   s&   | du st | ƒr
| S d|  t¡ 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©r   r
   r
   r   Ú_quote·   s   r   z\\(?:([0-3][0-7][0-7])|(.))c                 C   s"   | d rt t| d dƒƒS | d S )Né   é   é   )ÚchrÚint)Úmr
   r
   r   Ú_unquote_replaceÆ   s   r&   c                 C   sJ   | d u s
t | ƒdk r| S | d dks| d dkr| S | dd… } tt| ƒS )Nr"   r   r   éÿÿÿÿr    )ÚlenÚ_unquote_subr&   r   r
   r
   r   Ú_unquoteÌ   s   
r*   )ÚMonÚTueÚWedÚThuÚFriÚSatÚSun)NÚ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)r?   r>   )ÚfutureÚweekdaynameÚ	monthnamer>   r?   ÚnowÚyearÚmonthÚdayÚhhÚmmÚssÚwdÚyÚzr
   r
   r   Ú_getdateí   s   ÿrM   c                
   @   sè   e Zd ZdZdddddddd	d
dœ	ZddhZdd„ Zedd„ ƒZedd„ ƒZ	edd„ ƒZ
dd„ Zd4dd„Zdd„ ZejZdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd5d,d-„ZeZd.d/„ Zd4d0d1„Zd4d2d3„ZeejƒZdS )6ÚMorselaC  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.
    ÚexpiresÚPathÚCommentÚDomainzMax-AgeÚSecureÚHttpOnlyÚVersionÚSameSite)	rO   ÚpathÚcommentÚdomainúmax-ageÚsecureÚhttponlyÚversionÚsamesiter[   r\   c                 C   s0   d  | _  | _| _| jD ]	}t | |d¡ qd S )Nr   )Ú_keyÚ_valueÚ_coded_valueÚ	_reservedÚdictÚ__setitem__)ÚselfÚkeyr
   r
   r   Ú__init__  s   
ÿzMorsel.__init__c                 C   ó   | j S r   )r_   ©re   r
   r
   r   rf      ó   z
Morsel.keyc                 C   rh   r   )r`   ri   r
   r
   r   Úvalue$  rj   zMorsel.valuec                 C   rh   r   )ra   ri   r
   r
   r   Úcoded_value(  rj   zMorsel.coded_valuec                 C   sP   |  ¡ }|| jvrtd|f ƒ‚t||ƒrtd|›d|›�ƒ‚t | ||¡ d S ©NúInvalid attribute %rú.Control characters are not allowed in cookies r   )Úlowerrb   r   r   rc   rd   )re   ÚKÚVr
   r
   r   rd   ,  s   

zMorsel.__setitem__Nc                 C   sH   |  ¡ }|| jvrtd|f ƒ‚t||ƒrtd||f ƒ‚t | ||¡S )Nrn   z3Control characters are not allowed in cookies %r %r)rp   rb   r   r   rc   Ú
setdefault)re   rf   r   r
   r
   r   rs   4  s   

zMorsel.setdefaultc                 C   s>   t |tƒstS t | |¡o| j|jko| j|jko| j|jkS r   )Ú
isinstancerN   ÚNotImplementedrc   Ú__eq__r`   r_   ra   ©re   Úmorselr
   r
   r   rv   <  s   

ÿ
þ
ýzMorsel.__eq__c                 C   s$   t ƒ }t || ¡ |j | j¡ |S r   )rN   rc   ÚupdateÚ__dict__rw   r
   r
   r   ÚcopyF  s   zMorsel.copyc                 C   sp   i }t |ƒ ¡ D ]'\}}| ¡ }|| jvrtd|f ƒ‚t||ƒr+td|›d|›�ƒ‚|||< qt  | |¡ d S rm   )rc   Úitemsrp   rb   r   r   ry   )re   ÚvaluesÚdatarf   r   r
   r
   r   ry   L  s   

ÿÿ
zMorsel.updatec                 C   s   |   |¡ | S r   )ry   )re   r}   r
   r
   r   Ú__ior__X  s   
zMorsel.__ior__c                 C   s   |  ¡ | jv S r   )rp   rb   )re   rq   r
   r
   r   ÚisReservedKey\  s   zMorsel.isReservedKeyc                 C   sf   |  ¡ | jv rtd|f ƒ‚t|ƒstd|f ƒ‚t|||ƒr(td|||f ƒ‚|| _|| _|| _d S )Nz Attempt to set a reserved key %rzIllegal key %rz6Control characters are not allowed in cookies %r %r %r)rp   rb   r   r   r   r_   r`   ra   )re   rf   r   Ú	coded_valr
   r
   r   Úset_  s   ÿ
z
Morsel.setc                 C   s   | j | j| jdœS )N)rf   rk   rl   )r_   r`   ra   ri   r
   r
   r   Ú__getstate__m  s   ýzMorsel.__getstate__c                 C   sT   |d }|d }|d }t |||ƒrtd|›d|›d|›�ƒ‚|| _|| _|| _d S )Nrf   rk   rl   ro   r   )r   r   r_   r`   ra   )re   Ústaterf   rk   rl   r
   r
   r   Ú__setstate__t  s   ÿÿÿ
zMorsel.__setstate__úSet-Cookie:c                 C   s   d||   |¡f S )Nz%s %s)ÚOutputString)re   ÚattrsÚheaderr
   r
   r   Úoutput  s   zMorsel.outputc                 C   s   d| j j|  ¡ f S )Nú<%s: %s>)Ú	__class__r   r‡   ri   r
   r
   r   Ú__repr__„  s   zMorsel.__repr__c                 C   s>   dd l }|  |¡}t|ƒrtdƒ‚|jj|ddd�}d|f S )Nr   ú-Control characters are not allowed in cookiesr   zutf-8)ÚsafeÚencodingz«
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = decodeURIComponent("%s");
        // end hiding -->
        </script>
        )Úurllib.parser‡   r   r   ÚparseÚquote)re   rˆ   ÚurllibÚoutput_stringÚoutput_encodedr
   r
   r   Ú	js_output‡  s   
úzMorsel.js_outputc                 C   s   g }|j }|d| j| jf ƒ |d u r| j}t|  ¡ ƒ}|D ]m\}}|dkr'q||vr,q|dkrCt|tƒrC|d| j| t|ƒf ƒ q|dkrXt|tƒrX|d| j| |f ƒ q|dkrot|t	ƒro|d| j| t
|ƒf ƒ q|| jv r€|r|t	| j| ƒƒ q|d| j| |f ƒ qt|ƒS )Nú%s=%sr   rO   rZ   z%s=%drX   )Úappendrf   rl   rb   Úsortedr|   rt   r$   rM   r   r   Ú_flagsÚ_semispacejoin)re   rˆ   Úresultr™   r|   rf   rk   r
   r
   r   r‡   ˜  s.   
€zMorsel.OutputStringr   )Nr†   ) r   r   r	   Ú__doc__rb   r›   rg   Úpropertyrf   rk   rl   rd   rs   rv   ÚobjectÚ__ne__r{   ry   r   r€   r‚   rƒ   r…   rŠ   Ú__str__r�   r—   r‡   ÚclassmethodÚtypesÚGenericAliasÚ__class_getitem__r
   r
   r
   r   rN   õ   sJ    ÷






!rN   z,\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=z\[\]zŒ
    \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
   ©re   r   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   ©re   r   Ústrvalr
   r
   r   Úvalue_encodeê  s   zBaseCookie.value_encodeNc                 C   s   |r	|   |¡ d S d S r   )Úload)re   Úinputr
   r
   r   rg   ó  s   ÿzBaseCookie.__init__c                 C   s.   |   |tƒ ¡}| |||¡ t | ||¡ dS )z+Private method for setting a cookie's valueN)ÚgetrN   r‚   rc   rd   )re   rf   Ú
real_valuerl   ÚMr
   r
   r   Ú__set÷  s   zBaseCookie.__setc                 C   s<   t |tƒrt | ||¡ dS |  |¡\}}|  |||¡ dS )zDictionary style assignment.N)rt   rN   rc   rd   r«   Ú_BaseCookie__set)re   rf   rk   ÚrvalÚcvalr
   r
   r   rd   ý  s   
zBaseCookie.__setitem__r†   ú
c           	      C   sN   g }t |  ¡ ƒ}|D ]\}}| ||¡}t|ƒrtdƒ‚| |¡ q
| |¡S )z"Return a string suitable for HTTP.rŽ   )rš   r|   rŠ   r   r   r™   Újoin)	re   rˆ   r‰   Úsepr�   r|   rf   rk   Úvalue_outputr
   r
   r   rŠ     s   
zBaseCookie.outputc                 C   sJ   g }t |  ¡ ƒ}|D ]\}}| d|t|jƒf ¡ q
d| jjt|ƒf S )Nr˜   r‹   )rš   r|   r™   Úreprrk   rŒ   r   Ú
_spacejoin)re   Úlr|   rf   rk   r
   r
   r   r�     s
   zBaseCookie.__repr__c                 C   s6   g }t |  ¡ ƒ}|D ]\}}| | |¡¡ q
t|ƒS )z(Return a string suitable for JavaScript.)rš   r|   r™   r—   Ú	_nulljoin)re   rˆ   r�   r|   rf   rk   r
   r
   r   r—     s
   zBaseCookie.js_outputc                 C   s6   t |tƒr|  |¡ dS | ¡ D ]\}}|| |< q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)rt   r   Ú_BaseCookie__parse_stringr|   )re   Úrawdatarf   rk   r
   r
   r   r¬   "  s   

þ
zBaseCookie.loadc                 C   sˆ  d}t |ƒ}g }d}d}d}d|  kr|k r”n nz| ||¡}	|	s#nq|	 d¡|	 d¡}
}|	 d¡}|
d dkrI|s<q| ||
dd … |f¡ n@|
 ¡ tjv ru|sTd S |d u rj|
 ¡ tjv rh| ||
df¡ n!d S | ||
t	|ƒf¡ n|d ur‡| ||
|  
|¡f¡ d}nd S d|  kr“|k sn d }|D ])\}}
}||kr¬|d us§J ‚|||
< q˜||ks²J ‚|\}}|  |
||¡ | |
 }q˜d S )	Nr   Fr    r"   rf   r   ú$T)r(   ÚmatchÚgroupÚendr™   rp   rN   rb   r›   r*   r¨   r²   )re   r   ÚpattÚir   Úparsed_itemsÚmorsel_seenÚTYPE_ATTRIBUTEÚTYPE_KEYVALUErÀ   rf   rk   r°   Útpr³   r´   r
   r
   r   Ú__parse_string0  sN   
Þ%

øzBaseCookie.__parse_stringr   )Nr†   rµ   )r   r   r	   rž   r¨   r«   rg   r²   rd   rŠ   r¢   r�   r—   r¬   Ú_CookiePatternr½   r
   r
   r
   r   r   Þ  s    	
	
	
r   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 r   )r*   r§   r
   r
   r   r¨   t  s   zSimpleCookie.value_decodec                 C   s   t |ƒ}|t|ƒfS r   )r   r   r©   r
   r
   r   r«   w  s   zSimpleCookie.value_encodeN)r   r   r	   rž   r¨   r«   r
   r
   r
   r   r   m  s    r   ),rž   ÚreÚstringr¤   Ú__all__r¶   r¼   rœ   rº   Ú	Exceptionr   Úascii_lettersÚdigitsÚ_LegalCharsÚ_UnescapedCharsr‚   ÚrangeÚmapÚordr   ry   ÚcompileÚescapeÚ	fullmatchr   r   r   r   Úsubr)   r&   r*   Ú_weekdaynameÚ
_monthnamerM   rc   rN   Ú_LegalKeyCharsÚ_LegalValueCharsÚASCIIÚVERBOSErË   r   r   r
   r
   r
   r   Ú<module>   s\   &]ÿþ
 Qýýôô
ï 