o
    w[eP                     @   sZ  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dd ZedZe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=\\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                   @   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[%s]+c                 C   s&   | du st | r
| S d| t d S )zQuote 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   sn  | d u s
t | dk r| S | d dks| d dkr| S | dd } d}t | }g }d|  kr2|k rn t|S t| |}t| |}|sU|sU|| |d   	 t|S d }}|r`|d}|rg|d}|r|ro||k r|| ||  || |d   |d }n|| ||  |tt| |d |d  d |d }d|  kr|k s7t|S  t|S )N   r   r            )	len
_OctalPattsearch
_QuotePattappendstartchrint	_nulljoin)r   ir   reso_matchq_matchjkr
   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)rB   rA   )futureweekdayname	monthnamerA   rB   nowyearmonthdayhhmmsswdyzr
   r
   r   _getdate   s   rP   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d2d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d3d*d+ZeZd,d- Zd2d.d/Zd2d0d1ZeejZdS )4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)	rR   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 N)rb   rh   r
   r
   r   ri   )     z
Morsel.keyc                 C   rk   rl   )rc   rm   r
   r
   r   value-  rn   zMorsel.valuec                 C   rk   rl   )rd   rm   r
   r
   r   coded_value1  rn   zMorsel.coded_valuec                 C   s2   |  }|| jvrtd|f t| || d S NzInvalid attribute %r)lowerre   r   rf   rg   )rh   KVr
   r
   r   rg   5  s   
zMorsel.__setitem__Nc                 C   s.   |  }|| jvrtd|f t| ||S rq   )rr   re   r   rf   
setdefault)rh   ri   valr
   r
   r   ru   ;  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 rl   )
isinstancerQ   NotImplementedrf   __eq__rc   rb   rd   rh   morselr
   r
   r   ry   A  s   



zMorsel.__eq__c                 C   s$   t  }t||  |j| j |S rl   )rQ   rf   update__dict__rz   r
   r
   r   copyK  s   zMorsel.copyc                 C   sR   i }t | D ]\}}| }|| jvrtd|f |||< qt | | d S rq   )rf   itemsrr   re   r   r|   )rh   valuesdatari   rv   r
   r
   r   r|   Q  s   

zMorsel.updatec                 C   s   |  | jv S rl   )rr   re   )rh   rs   r
   r
   r   isReservedKeyZ  s   zMorsel.isReservedKeyc                 C   sH   |  | jv rtd|f t|std|f || _|| _|| _d S )Nz Attempt to set a reserved key %rzIllegal key %r)rr   re   r   r   rb   rc   rd   )rh   ri   rv   	coded_valr
   r
   r   set]  s   
z
Morsel.setc                 C   s   | j | j| jdS )N)ri   ro   rp   rb   rc   rd   rm   r
   r
   r   __getstate__h  s   zMorsel.__getstate__c                 C   s"   |d | _ |d | _|d | _d S )Nri   ro   rp   r   )rh   stater
   r
   r   __setstate__o  s   

zMorsel.__setstate__Set-Cookie:c                 C   s   d||  |f S )Nz%s %s)OutputString)rh   attrsheaderr
   r
   r   outputt  s   zMorsel.outputc                 C   s   d| j j|  f S )N<%s: %s>)	__class__r   r   rm   r
   r
   r   __repr__y  s   zMorsel.__repr__c                 C   s   d|  |dd S )Nz
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "%s";
        // end hiding -->
        </script>
        r   r   )r   replace)rh   r   r
   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   rR   r]   z%s=%dr[   )r"   ri   rp   re   sortedr   rw   r%   rP   r   r   _flags_semispacejoin)rh   r   resultr"   r   ri   ro   r
   r
   r   r     s.   
zMorsel.OutputStringrl   )Nr   )r   r   r	   __doc__re   r   rj   propertyri   ro   rp   rg   ru   ry   object__ne__r~   r|   r   r   r   r   r   __str__r   r   r   classmethodtypesGenericAlias__class_getitem__r
   r
   r
   r   rQ      sH    



	



!rQ   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
   rh   rv   r
   r
   r   value_decode  s   zBaseCookie.value_decodec                 C   s   t |}||fS )zreal_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   rh   rv   strvalr
   r
   r   value_encode  s   zBaseCookie.value_encodeNc                 C   s   |r	|  | d S d S rl   )load)rh   inputr
   r
   r   rj     s   zBaseCookie.__init__c                 C   s.   |  |t }|||| t| || dS )z+Private method for setting a cookie's valueN)getrQ   r   rf   rg   )rh   ri   
real_valuerp   Mr
   r
   r   __set  s   zBaseCookie.__setc                 C   s<   t |trt| || dS | |\}}| ||| dS )zDictionary style assignment.N)rw   rQ   rf   rg   r   _BaseCookie__set)rh   ri   ro   rvalcvalr
   r
   r   rg     s   
zBaseCookie.__setitem__r   
c                 C   s:   g }t |  }|D ]\}}|||| q
||S )z"Return a string suitable for HTTP.)r   r   r"   r   join)rh   r   r   sepr   r   ri   ro   r
   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"   reprro   r   r   
_spacejoin)rh   lr   ri   ro   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   r&   )rh   r   r   r   ri   ro   r
   r
   r   r     s
   zBaseCookie.js_outputc                 C   s6   t |tr| | dS | D ]\}}|| |< qdS )zLoad 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)rw   r   _BaseCookie__parse_stringr   )rh   rawdatari   ro   r
   r
   r   r     s   


zBaseCookie.loadc                 C   s  d}t |}g }d}d}d}d|  kr|k rn 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sJ |||
< q||ksJ |\}}| |
|| | |
 }qd S )	Nr   Fr   r   ri   rv   $T)r   matchgroupendr"   rr   rQ   re   r   r-   r   r   )rh   r   pattr'   r   parsed_itemsmorsel_seenTYPE_ATTRIBUTETYPE_KEYVALUEr   ri   ro   r   tpr   r   r
   r
   r   __parse_string  sN   
%

zBaseCookie.__parse_stringrl   )Nr   r   )r   r   r	   r   r   r   rj   r   rg   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 rl   )r-   r   r
   r
   r   r   _  s   zSimpleCookie.value_decodec                 C   s   t |}|t|fS rl   )r   r   r   r
   r
   r   r   b  s   zSimpleCookie.value_encodeN)r   r   r	   r   r   r   r
   r
   r
   r   r   X  s    r   ))r   restringr   __all__r   r&   r   r   	Exceptionr   ascii_lettersdigits_LegalChars_UnescapedCharsr   rangemapordr   r|   compileescape	fullmatchr   r   r   r!   r-   _weekdayname
_monthnamerP   rf   rQ   _LegalKeyChars_LegalValueCharsASCIIVERBOSEr   r   r   r
   r
   r
   r   <module>   sX   &]

2 6
 