fMdZddlZddlZddlZgdZdj Zdj Zdj ZGdde Z ejejzd zZ e d zZeed eeeez Dcic]}|d |z c}Zej)ed deddiej*dej,e zj.ZdZej*dj4ZdZdZgdZgdZdeefdZ Gdde!Z"dZ#e#dzZ$ej*de#zdze$zdzejJejLzZ'Gd d!e!Z(Gd"d#e(Z)ycc}w)$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 SimpleCookiez;  c eZdZy)rN)__name__ __module__ __qualname__#/usr/lib/python3.12/http/cookies.pyrrsr rz!#$%&'*+-.^_`|~:z ()/<=>?@[]{}z\%03o"\"\z\\z[%s]+cV| t|r|Sd|jtzdzS)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. r) _is_legal_key translate _Translatorstrs r_quoters. {mC( S]];//#55r z\\(?:([0-3][0-7][0-7])|(.))cF|drtt|ddS|dS)N)chrint)ms r_unquote_replacer!s't3qtQ<  t r ct|t|dkr|S|ddk7s|ddk7r|S|dd}tt|S)Nrrrr)len _unquote_subr!rs r_unquoter&sO {c#hl  1v}B3 a)C (# ..r )MonTueWedThuFriSatSun) NJanFebMarAprMayJunJulAugSepOctNovDecc nddlm}m}|}|||z\ }}}} } } } } }d|| ||||| | | fzS)Nr)gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)r<r;)future weekdayname monthnamer;r<nowyearmonthdayhhmmsswdyzs r_getdaterJsW! &C-3C&L-A*D%b"b"a 0 OS)E"2D"b" E FFr c eZdZdZdddddddd d d Zd d hZdZedZedZ edZ dZ d dZ dZ ejZdZdZdZdZdZdZd!dZeZdZd dZd dZeej:Zy)"MorselaCA 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. expiresPathCommentDomainzMax-AgeSecureHttpOnlyVersionSameSite) rMpathcommentdomainmax-agesecurehttponlyversionsamesiterYrZc~dx|_x|_|_|jD]}tj ||dy)Nr)_key_value _coded_value _reserveddict __setitem__)selfkeys r__init__zMorsel.__init__s?6:: :DK$"3>> ,C   T3 + ,r c|jSN)r^rds rrez Morsel.keys yyr c|jSrh)r_ris rvaluez Morsel.values {{r c|jSrh)r`ris r coded_valuezMorsel.coded_values   r c|j}||jvrtd|tj |||yNzInvalid attribute )lowerrarrbrc)rdKVs rrczMorsel.__setitem__#s9 GGIDNN";< < q!$r Nc|j}||jvrtd|tj |||Sro)rprarrb setdefault)rdrevals rrtzMorsel.setdefault)s:iik dnn $=> >tS#..r ct|tstStj ||xrO|j |j k(xr4|j |j k(xr|j|jk(Srh) isinstancerLNotImplementedrb__eq__r_r^r`rdmorsels rryz Morsel.__eq__/sj&&)! ! D&)9 v}},9 V[[(9!!V%8%88 :r ct}tj|||jj|j|Srh)rLrbupdate__dict__rzs rcopyz Morsel.copy9s2 FD!t}}- r ci}t|jD]6\}}|j}||jvrt d||||<8tj ||yro)rbitemsrprarr})rdvaluesdatarerus rr}z Morsel.update?seV **, HC))+C$..(!C"ABBDI   D$r c:|j|jvSrh)rpra)rdrqs r isReservedKeyzMorsel.isReservedKeyHswwyDNN**r c|j|jvrtd|t|std|||_||_||_y)NzAttempt to set a reserved key z Illegal key )rprarrr^r_r`)rdreru coded_vals rsetz Morsel.setKsP 99;$.. (CIJ JS!#78 8  %r cJ|j|j|jdS)N)rerkrmr^r_r`ris r __getstate__zMorsel.__getstate__Vs#99[[,,  r c@|d|_|d|_|d|_y)Nrerkrmr)rdstates r __setstate__zMorsel.__setstate__]s%%L Gn !-0r c.|d|j|S)Nr) OutputString)rdattrsheaders routputz Morsel.outputbs $"3"3E":;;r cXd|jjd|jdS)N<: >) __class__r rris r__repr__zMorsel.__repr__gs !^^44d6G6G6IJJr cJd|j|jddzS)Nz rr)rreplace)rdrs r js_outputzMorsel.js_outputjs.   '//U; = =r cg}|j}||jd|j| |j}t |j }|D]\}}|dk(r ||vr|dk(r4t |tr$||j|dt|J|dk(r+t |tr|d|j||fzz|dk(r4t |tr$||j|dt|||jvr"|s|t|j|||j|d|t|S)N=rrMrXz%s=%drV) appendrermrasortedrrwrrJrr_flags_semispacejoin)rdrresultrrrerks rrzMorsel.OutputStringts= $((D$4$456 =NNEtzz|$ ?JC{%iJuc$:$.."5xGH !j&<w$.."5u!==> !j&<$.."5ve}EF #3t~~c234$.."5u=> ?$f%%r rh)N Set-Cookie:)r r r __doc__rarrfpropertyrerkrmrcrtryobject__ne__rr}rrrrr__str__rrr classmethodtypes GenericAlias__class_getitem__r r rrLrLs* I #F,!!% / :]]F  + & 1 <GK=&B$E$6$67r rLz,\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=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. cZeZdZdZdZdZd dZdZdZddZ e Z d Z d d Z d Z efd Zy)rz'A container class for a set of Morsels.c ||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 rdrus r value_decodezBaseCookie.value_decodes Cxr c t|}||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. rrdrustrvals r value_encodezBaseCookie.value_encodes Sv~r Nc,|r|j|yyrh)load)rdinputs rrfzBaseCookie.__init__s  IIe  r c|j|t}|j|||tj |||y)z+Private method for setting a cookie's valueN)getrLrrbrc)rdre real_valuermMs r__setzBaseCookie.__sets6 HHS&( # c:{+ sA&r ct|trtj|||y|j |\}}|j |||y)zDictionary style assignment.N)rwrLrbrcr_BaseCookie__set)rdrerkrvalcvals rrczBaseCookie.__setitem__sB eV $   T3 .**51JD$ JJsD$ 'r cg}t|j}|D]&\}}|j|j||(|j |S)z"Return a string suitable for HTTP.)rrrrjoin)rdrrseprrrerks rrzBaseCookie.outputsPtzz|$ 7JC MM%,,uf5 6 7xxr cg}t|j}|D].\}}|j|dt|j0d|j j dt|dS)Nrrrr)rrrreprrkrr _spacejoin)rdlrrerks rrzBaseCookie.__repr__s] tzz|$ 9JC HHT%++%67 8 9!^^44jmDDr cg}t|j}|D]%\}}|j|j|'t |S)z(Return a string suitable for JavaScript.)rrrr _nulljoin)rdrrrrerks rrzBaseCookie.js_outputsJtzz|$ 2JC MM%//%0 1 2  r ct|tr|j|y|jD] \}}|||< y)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)rwr_BaseCookie__parse_stringr)rdrawdatarerks rrzBaseCookie.loadsH gs #    ( &mmo " U!S  "r cd}t|}g}d}d}d}d|cxkr|kr!nn|j||} | sn| jd| jd} } | jd}| ddk(r|sd|j || dd| fn| j t jvrY|sy| 6| j t jvr|j || dfnHy|j || t| fn)| &|j || |j| fd}nyd|cxkr|krnd} |D]9\} } } | |k(r | J| | | <| |k(sJ| \}}|j| |||| } ;y) NrFrrreru$T) r$matchgroupendrrprLrarr&rr)rdrpattin parsed_items morsel_seenTYPE_ATTRIBUTE TYPE_KEYVALUErrerkrtprrs r__parse_stringzBaseCookie.__parse_string s  H   1jqjJJsA&EU+U[[-?C ! A1v}"##^SWe$DE 0 00"=yy{fmm3$++^S$,GH ''huo(NO"##]C9J9J59Q$RS" E1jqjJ * NBU^#}$}#]***" d 3d+I r rh)Nrz )r r r rrrrfrrcrrrrr_CookiePatternrr r rrrsD1' ( GE! (6:r rceZdZdZdZdZy)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. ct||fSrh)r&rs rrzSimpleCookie.value_decodeMs}c!!r c2t|}|t|fSrh)rrrs rrzSimpleCookie.value_encodePsSvf~%%r N)r r r rrrr r rrrFs "&r r)*rrestringr__all__rrrr Exceptionr ascii_lettersdigits _LegalChars_UnescapedCharsrrangemapordrr}compileescape fullmatchrrsubr%r!r& _weekdayname _monthnamerJrbrL_LegalKeyChars_LegalValueCharsASCIIVERBOSErrr)rs0rrsNXz 7 GG  XX  ) """V]]25GG /E#J#c#.G*HHJ(Q,J HeIv  7YRYY{%;;<FF  6rzz89==  /6A 8 <:Fi8Ti8jB!G+        " BJJ # .IIX &: &C Js E6