U
    yiP                     @   s  d Z ddlZddlZdddg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ddd d!d"d#d$gZdd%d&d'd(d)d*d+d,d-d.d/d0gZ dee fd1d2Z!G d3d4 d4e"Z#d5Z$e$d6 Z%ed7e$ d8 e% d9 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.8/http/cookies.pyr      s   z!#$%&'*+-.^_`|~:z ()/<=>?@[]{}c                 C   s   i | ]}|d | qS )z\%03or
   ).0nr
   r
   r   
<dictcomp>   s    r      "\"\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kst | r| S d| t d S 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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 d S )N         )chrint)mr
   r
   r   _unquote_replace   s    r'   c                 C   sJ   | d kst | dk r| S | d dks0| d dkr4| S | dd } tt| S )Nr#   r   r   r!   )len_unquote_subr'   r   r
   r
   r   _unquote   s    r+   ZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc              	   C   sR   ddl m}m } | }|||  \	}}}}	}
}}}}d|| ||| ||	|
|f S )Nr   )gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)r-   r,   )ZfutureZweekdaynameZ	monthnamer,   r-   ZnowZyearZmonthZdayZhhZmmZssZwdyzr
   r
   r   _getdate   s    r0   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d4d.d/Zd5d0d1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CommentZDomainzMax-AgeZSecureZHttpOnlyZVersionZSameSite)	r2   pathcommentZdomainmax-agesecurehttponlyversionZsamesiter8   r9   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   s   | j S r   )r;   rA   r
   r
   r   rB     s    z
Morsel.keyc                 C   s   | j S r   )r<   rD   r
   r
   r   value#  s    zMorsel.valuec                 C   s   | j S r   )r=   rD   r
   r
   r   coded_value'  s    zMorsel.coded_valuec                 C   sP   |  }|| jkr td|f t||r>td|d|t| || d S )NInvalid attribute %rz.Control characters are not allowed in cookies r   )lowerr>   r   r   r?   r@   )rA   KVr
   r
   r   r@   +  s    

zMorsel.__setitem__Nc                 C   sH   |  }|| jkr td|f t||r:td||f t| ||S )NrG   z3Control characters are not allowed in cookies %r %r)rH   r>   r   r   r?   
setdefault)rA   rB   r   r
   r
   r   rK   3  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   )
isinstancer1   NotImplementedr?   __eq__r<   r;   r=   rA   Zmorselr
   r
   r   rN   ;  s    



zMorsel.__eq__c                 C   s$   t  }t||  |j| j |S r   )r1   r?   update__dict__rO   r
   r
   r   copyE  s    zMorsel.copyc                 C   sR   i }t | D ]0\}}| }|| jkr8td|f |||< qt | | d S )NrG   )r?   itemsrH   r>   r   rP   )rA   valuesdatarB   r   r
   r
   r   rP   K  s    

zMorsel.updatec                 C   s   |  | jkS r   )rH   r>   )rA   rI   r
   r
   r   isReservedKeyT  s    zMorsel.isReservedKeyc                 C   sf   |  | jkrtd|f t|s2td|f t|||rPtd|||f || _|| _|| _d S )Nz Attempt to set a reserved key %rzIllegal key %rz6Control characters are not allowed in cookies %r %r %r)rH   r>   r   r   r   r;   r<   r=   )rA   rB   r   Z	coded_valr
   r
   r   setW  s    z
Morsel.setc                 C   s   | j | j| jdS )N)rB   rE   rF   r;   r<   r=   rD   r
   r
   r   __getstate__e  s    zMorsel.__getstate__c                 C   s"   |d | _ |d | _|d | _d S )NrB   rE   rF   rX   )rA   stater
   r
   r   __setstate__l  s    

zMorsel.__setstate__Set-Cookie:c                 C   s   d||  |f S )Nz%s %s)OutputString)rA   attrsheaderr
   r
   r   outputq  s    zMorsel.outputc                 C   s   d| j j|  f S )N<%s: %s>)	__class__r   r]   rD   r
   r
   r   __repr__v  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)rA   r^   r
   r
   r   	js_outputy  s    zMorsel.js_outputc                 C   s$  g }|j }|d| j| jf  |d kr,| j}t|  }|D ]\}}|dkrNq<||krXq<|dkrt|tr|d| j| t|f  q<|dkrt|tr|d| j| |f  q<|dkrt|t	r|d| j| t
|f  q<|| jkr|r|t	| j|  q<|d| j| |f  q<t|S )N%s=%sr   r2   r7   z%s=%dr6   )appendrB   rF   r>   sortedrS   rL   r%   r0   r   r    _flags_semispacejoin)rA   r^   resultrg   rS   rB   rE   r
   r
   r   r]     s,    zMorsel.OutputString)N)Nr\   )N)N)r   r   r	   __doc__r>   ri   rC   propertyrB   rE   rF   r@   rK   rN   object__ne__rR   rP   rV   rW   rY   r[   r`   __str__rc   re   r]   r
   r
   r
   r   r1      sD   



	


r1   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
   rA   r   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   rA   r   Zstrvalr
   r
   r   value_encode  s    zBaseCookie.value_encodeNc                 C   s   |r|  | d S r   )load)rA   inputr
   r
   r   rC     s    zBaseCookie.__init__c                 C   s.   |  |t }|||| t| || dS )z+Private method for setting a cookie's valueN)getr1   rW   r?   r@   )rA   rB   Z
real_valuerF   Mr
   r
   r   Z__set  s    zBaseCookie.__setc                 C   s:   t |trt| || n| |\}}| ||| dS )zDictionary style assignment.N)rL   r1   r?   r@   rt   _BaseCookie__set)rA   rB   rE   rvalcvalr
   r
   r   r@     s    
zBaseCookie.__setitem__r\   
c           	      C   sN   g }t |  }|D ].\}}|||}t|r8td|| q||S )z"Return a string suitable for HTTP.z-Control characters are not allowed in cookies)rh   rS   r`   r   r   rg   join)	rA   r^   r_   seprk   rS   rB   rE   Z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 )Nrf   ra   )rh   rS   rg   reprrE   rb   r   
_spacejoin)rA   lrS   rB   rE   r
   r
   r   rc     s
    zBaseCookie.__repr__c                 C   s6   g }t |  }|D ]\}}||| qt|S )z(Return a string suitable for JavaScript.)rh   rS   rg   re   	_nulljoin)rA   r^   rk   rS   rB   rE   r
   r
   r   re     s
    zBaseCookie.js_outputc                 C   s4   t |tr| | n| 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)rL   r   _BaseCookie__parse_stringrS   )rA   ZrawdatarB   rE   r
   r
   r   ru     s
    

zBaseCookie.loadc                 C   s  d}t |}g }d}d}d}d|  kr2|k rn n|||}	|	sJq|	d|	d }
}|	d}|
d dkr|s|q|||
dd  |f q|
 tjkr|sd S |d kr|
 tjkr|||
df qd S n|||
t	|f q|d k	r|||
| 
|f d}qd S qd }|D ]Z\}}
}||krP|d k	sFt|||
< n,||ks^t|\}}| |
|| | |
 }q$d S )	Nr   Fr!   r#   rB   r   $T)r)   matchgroupendrg   rH   r1   r>   ri   r+   rr   AssertionErrorry   )rA   r   Zpattir   Zparsed_itemsZmorsel_seenZTYPE_ATTRIBUTEZTYPE_KEYVALUEr   rB   rE   rx   tprz   r{   r
   r
   r   Z__parse_string  sJ    



zBaseCookie.__parse_string)N)Nr\   r|   )N)r   r   r	   rl   rr   rt   rC   ry   r@   r`   rp   rc   re   ru   _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 r   )r+   rq   r
   r
   r   rr   ]  s    zSimpleCookie.value_decodec                 C   s   t |}|t|fS r   )r   r    rs   r
   r
   r   rt   `  s    zSimpleCookie.value_encodeN)r   r   r	   rl   rr   rt   r
   r
   r
   r   r   V  s   )+rl   restring__all__r}   r   rj   r   	Exceptionr   Zascii_lettersZdigitsZ_LegalCharsZ_UnescapedCharsrW   rangemapordr   rP   compileescape	fullmatchr   r   r   r    subr*   r'   r+   Z_weekdaynameZ
_monthnamer0   r?   r1   Z_LegalKeyCharsZ_LegalValueCharsASCIIVERBOSEr   r   r   r
   r
   r
   r   <module>'   sv   ]
  
           ;
 