a :jS@sldZddlZddlZddlZgdZdjZdjZdjZGddde Z ej ej d Z e d Zd d eed eeeeDZeeddeddiedee jZedZddZddZedjZddZddZgdZ gdZ!de e!fdd Z"Gd!d"d"e#Z$d#Z%e%d$Z&ed%e%d&e&d'ej'ej(BZ)Gd(d)d)e#Z*Gd*d+d+e*Z+dS),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 SimpleCookiez;  c@s eZdZdS)rN)__name__ __module__ __qualname__r r "/usr/lib/python3.9/http/cookies.pyrsrz!#$%&'*+-.^_`|~:z ()/<=>?@[]{}cCsi|]}|d|qS)z\%03or ).0nr r r sr"\"\z\\z[%s]+z[\x00-\x1F\x7F]cGstdd|DS)zhDetects control characters within a value. Supports any type, as header values can be any type. css|]}tt|VqdSN)_control_character_researchstr)r vr r r z)_has_control_character..)any)valr r r _has_control_charactersrcCs*|dust|r|Sd|tdSdS)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 _Translatorrr r r _quotesr!z\\(?:([0-3][0-7][0-7])|(.))cCs&|drtt|ddS|dSdS)N)chrint)mr r r _unquote_replacesr(cCsJ|dust|dkr|S|ddks0|ddkr4|S|dd}tt|S)Nr$rrr")len _unquote_subr(r r r r _unquotes  r,)ZMonZTueZWedZThuZFriZSatZSun) NZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc CsRddlm}m}|}|||\ }}}} } } } } }d|| ||||| | | fS)Nr)gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)r.r-)ZfutureZ weekdaynameZ monthnamer-r.ZnowZyearZmonthZdayZhhZmmZssZwdyzr r r _getdates r1c @seZdZdZdddddddd d d Zd d hZddZeddZeddZ eddZ ddZ d4ddZ ddZ ejZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd5d,d-ZeZd.d/Zd6d0d1Zd7d2d3ZeejZdS)8MorselaCA 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. expiresPathCommentZDomainzMax-AgeZSecureZHttpOnlyZVersionZSameSite) r3pathcommentZdomainmax-agesecurehttponlyversionZsamesiter9r:cCs0d|_|_|_|jD]}t||dqdS)Nr)_key_value _coded_value _reserveddict __setitem__)selfkeyr r r __init__s zMorsel.__init__cCs|jSr)r<rBr r r rC sz Morsel.keycCs|jSr)r=rEr r r value$sz Morsel.valuecCs|jSr)r>rEr r r coded_value(szMorsel.coded_valuecCsP|}||jvr td|ft||r>td|d|t|||dSNInvalid attribute %r.Control characters are not allowed in cookies r)lowerr?rrr@rA)rBKVr r r rA,s   zMorsel.__setitem__NcCsH|}||jvr td|ft||r:td||ft|||S)NrIz3Control characters are not allowed in cookies %r %r)rKr?rrr@ setdefault)rBrCrr r r rN4s   zMorsel.setdefaultcCs>t|tstSt||o<|j|jko<|j|jko<|j|jkSr) isinstancer2NotImplementedr@__eq__r=r<r>rBZmorselr r r rQ<s     z Morsel.__eq__cCs$t}t|||j|j|Sr)r2r@update__dict__rRr r r copyFs z Morsel.copycCspi}t|D]N\}}|}||jvr8td|ft||rVtd|d||||<qt||dSrH)r@itemsrKr?rrrS)rBvaluesdatarCrr r r rSLs   z Morsel.updatecCs|||Sr)rS)rBrWr r r __ior__Xs zMorsel.__ior__cCs||jvSr)rKr?)rBrLr r r isReservedKey\szMorsel.isReservedKeycCsf||jvrtd|ft|s2td|ft|||rPtd|||f||_||_||_dS)Nz Attempt to set a reserved key %rzIllegal key %rz6Control characters are not allowed in cookies %r %r %r)rKr?rrrr<r=r>)rBrCrZ coded_valr r r set_s  z Morsel.setcCs|j|j|jdS)N)rCrFrG)r<r=r>rEr r r __getstate__mszMorsel.__getstate__cCsT|d}|d}|d}t|||r>td|d|d|||_||_||_dS)NrCrFrGrJr)rrr<r=r>)rBstaterCrFrGr r r __setstate__ts zMorsel.__setstate__ Set-Cookie:cCsd|||fS)Nz%s %s) OutputString)rBattrsheaderr r r outputsz Morsel.outputcCsd|jj|fS)N<%s: %s>) __class__rr`rEr r r __repr__szMorsel.__repr__cCs*||}t|rtdd|ddS)N-Control characters are not allowed in cookiesz rr)r`rrreplace)rBraZ output_stringr r r js_outputs   zMorsel.js_outputcCs$g}|j}|d|j|jf|dur,|j}t|}|D]\}}|dkrNq<||vrXq<|dkrt|tr|d|j|t|fq<|dkrt|tr|d|j||fq<|dkrt|t r|d|j|t |fq<||j vr|r|t |j|q<|d|j||fq<@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=z\[\]z \s* # Optional whitespace at start of cookie (?P # 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 # 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@sneZdZdZddZddZdddZd d Zd d ZdddZ e Z ddZ dddZ ddZ efddZdS)rz'A container class for a set of Morsels.cCs||fS)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 rBrr r r value_decodeszBaseCookie.value_decodecCst|}||fS)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 rBrZstrvalr r r value_encodeszBaseCookie.value_encodeNcCs|r||dSr)load)rBinputr r r rDszBaseCookie.__init__cCs.||t}||||t|||dS)z+Private method for setting a cookie's valueN)getr2r[r@rA)rBrCZ real_valuerGMr r r Z__setszBaseCookie.__setcCs:t|trt|||n||\}}||||dS)zDictionary style assignment.N)rOr2r@rAr|_BaseCookie__set)rBrCrFrvalcvalr r r rAs zBaseCookie.__setitem__r_ c CsNg}t|}|D].\}}|||}t|r8td||q||S)z"Return a string suitable for HTTP.rg)rlrVrcrrrkjoin) rBrarbseprorVrCrFZ value_outputr r r rcs    zBaseCookie.outputcCsJg}t|}|D] \}}|d|t|jfqd|jjt|fS)Nrjrd)rlrVrkreprrFrer _spacejoin)rBlrVrCrFr r r rfs   zBaseCookie.__repr__cCs6g}t|}|D]\}}|||qt|S)z(Return a string suitable for JavaScript.)rlrVrkri _nulljoin)rBrarorVrCrFr r r ris   zBaseCookie.js_outputcCs4t|tr||n|D]\}}|||<qdS)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)rOr_BaseCookie__parse_stringrV)rBZrawdatarCrFr r r r}s    zBaseCookie.loadcCsd}t|}g}d}d}d}d|kr2|krnn|||} | sJq| d| d} } | d}| ddkr|s|q||| dd| fq| tjvr|sdS| dur| tjvr||| dfqdSn||| t | fq| dur||| | | fd}qdSqd} |D]Z\} } } | |krP| dusFJ| | | <n,| |ks^J| \}}| | |||| } q$dS) NrFr"r$rCr$T) r*matchgroupendrkrKr2r?rmr,rzr)rBrZpattir Z parsed_itemsZ morsel_seenZTYPE_ATTRIBUTEZ TYPE_KEYVALUErrCrFrtprrr r r Z__parse_string,sJ      zBaseCookie.__parse_string)N)Nr_r)N)rrr rprzr|rDrrArcrtrfrir}_CookiePatternrr r r r rs    rc@s eZdZdZddZddZdS)rz 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. cCs t||fSr)r,ryr r r rzpszSimpleCookie.value_decodecCst|}|t|fSr)rr!r{r r r r|sszSimpleCookie.value_encodeN)rrr rprzr|r r r r risr),rprestringrv__all__rrrnr ExceptionrZ ascii_lettersZdigitsZ _LegalCharsZ_UnescapedCharsr[rangemapordrrScompileescape fullmatchrrrr!subr+r(r,Z _weekdaynameZ _monthnamer1r@r2Z_LegalKeyCharsZ_LegalValueCharsASCIIVERBOSErrrr r r r 'sZ]  M