$fPdZddlZddlZddlZgdZdj Zdj Zdj ZGdde Z ejejzd zZ e d zZeed eeeez Dcic]}|d |z c}Zej)ed deddiej*dej,e zj.ZdZej*dZej*dZdZgdZgdZdeefdZGdde Z!dZ"e"dzZ#ej*de"zdze#zdzejHejJzZ&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]z[\\].c |t|dkr|S|ddk7s|ddk7r|S|dd}d}t|}g}d|cxkr|kr n t|Stj||}tj||}|s"|s |j ||d t|Sdx}}|r|j d}|r|j d}|r8|r||kr1|j ||||j ||dz|dz}nF|j ||||j t t||dz|dzd|dz}d|cxkr|krt|St|S)Nrr) len _OctalPattsearch _QuotePattappendstartchrint _nulljoin)rinreso_matchq_matchjks r_unquoter0s {c#hl  1v}B3 a)C A CA C q*1*( S>'##C+##C+w JJs12w   S> A  a A  a A Gq1u JJs1Qx JJs1Q3x AA JJs1Qx JJs3s1Q3qs|Q/0 1AA' q*1*( S>) ( S>r )MonTueWedThuFriSatSun) NJanFebMarAprMayJunJulAugSepOctNovDecc nddlm}m}|}|||z\ }}}} } } } } }d|| ||||| | | fzS)Nr)gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)rFrE)future weekdayname monthnamerErFnowyearmonthdayhhmmsswdyzs r_getdaterTsW! &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) rWpathcommentdomainmax-agesecurehttponlyversionsamesitercrdc~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)rhrns rroz Morsel.key)s yyr c|jSrr)rirss rvaluez Morsel.value-s {{r c|jSrr)rjrss r coded_valuezMorsel.coded_value1s   r c|j}||jvrtd|tj |||yNzInvalid attribute )lowerrkrrlrm)rnKVs rrmzMorsel.__setitem__5s9 GGIDNN";< < q!$r Nc|j}||jvrtd|tj |||Sry)rzrkrrl setdefault)rnrovals rr~zMorsel.setdefault;s:iik dnn $=> >tS#..r ct|tstStj ||xrO|j |j k(xr4|j |j k(xr|j|jk(Srr) isinstancerVNotImplementedrl__eq__rirhrjrnmorsels rrz Morsel.__eq__Asj&&)! ! D&)9 v}},9 V[[(9!!V%8%88 :r ct}tj|||jj|j|Srr)rVrlupdate__dict__rs rcopyz Morsel.copyKs2 FD!t}}- r ci}t|jD]6\}}|j}||jvrt d||||<8tj ||yry)rlitemsrzrkrr)rnvaluesdatarors rrz Morsel.updateQseV **, HC))+C$..(!C"ABBDI   D$r c:|j|jvSrr)rzrk)rnr{s r isReservedKeyzMorsel.isReservedKeyZswwyDNN**r c|j|jvrtd|t|std|||_||_||_y)NzAttempt to set a reserved key z Illegal key )rzrkrrrhrirj)rnror coded_vals rsetz Morsel.set]sP 99;$.. (CIJ JS!#78 8  %r cJ|j|j|jdS)N)rorurwrhrirjrss r __getstate__zMorsel.__getstate__hs#99[[,,  r c@|d|_|d|_|d|_y)Nrorurwr)rnstates r __setstate__zMorsel.__setstate__os%%L Gn !-0r c.|d|j|S)Nr) OutputString)rnattrsheaders routputz Morsel.outputts $"3"3E":;;r cXd|jjd|jdS)N<: >) __class__r rrss r__repr__zMorsel.__repr__ys !^^44d6G6G6IJJr cJd|j|jddzS)Nz rr)rreplace)rnrs r js_outputzMorsel.js_output|s.   '//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=rrWrbz%s=%dr`) r$rorwrksortedrrr'rTrr_flags_semispacejoin)rnrresultr$rrorus rrzMorsel.OutputStrings= $((D$4$456 =NNEtzz|$ ?JC{%iJuc$:$.."5xGH !j&<w$.."5u!==> !j&<$.."5ve}EF #3t~~c234$.."5u=> ?$f%%r rr)N Set-Cookie:)r r r __doc__rkrrppropertyrorurwrmr~robject__ne__rrrrrrr__str__rrr classmethodtypes GenericAlias__class_getitem__r r rrVrVs* I #F,!!% / :]]F  + & 1 <GK=&B$E$6$67r rVz,\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 rnrs 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. rrnrstrvals r value_encodezBaseCookie.value_encodes Sv~r Nc,|r|j|yyrr)load)rninputs rrpzBaseCookie.__init__s  IIe  r c|j|t}|j|||tj |||y)z+Private method for setting a cookie's valueN)getrVrrlrm)rnro real_valuerwMs r__setzBaseCookie.__sets6 HHS&( # c:{+ sA&r ct|trtj|||y|j |\}}|j |||y)zDictionary style assignment.N)rrVrlrmr_BaseCookie__set)rnrorurvalcvals rrmzBaseCookie.__setitem__sB eV $   T3 .**51JD$ JJsD$ 'r cg}t|j}|D]&\}}|j|j||(|j |S)z"Return a string suitable for HTTP.)rrr$rjoin)rnrrseprrrorus rrzBaseCookie.outputsPtzz|$ 7JC MM%,,uf5 6 7xxr cg}t|j}|D].\}}|j|dt|j0d|j j dt|dS)Nrrrr)rrr$reprrurr _spacejoin)rnlrrorus 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.)rrr$rr()rnrrrrorus 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)rr_BaseCookie__parse_stringr)rnrawdatarorus rrzBaseCookie.load sH 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) NrFrrror$T) r matchgroupendr$rzrVrkrr0rr)rnrpattr)r* parsed_items morsel_seenTYPE_ATTRIBUTE TYPE_KEYVALUErrorurtprrs r__parse_stringzBaseCookie.__parse_strings  H   1jqjJJsA&EU+U[[-?C ! A1v}"##^SWe$DE 0 00"=yy{fmm3$++^S$,GH ''huo(NO"##]C9J9J59Q$RS" E1jqjJ * NBU^#}$}#]***" d 3d+I r rr)Nrz )r r r rrrrprrmrrrrr_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||fSrr)r0rs rrzSimpleCookie.value_decode_s}c!!r c2t|}|t|fSrr)rrrs rrzSimpleCookie.value_encodebsSvf~%%r N)r r r rrrr r rrrXs "&r r))rrestringr__all__rr(rr Exceptionr ascii_lettersdigits _LegalChars_UnescapedCharsrrangemapordrrcompileescape fullmatchrrr!r#r0 _weekdayname _monthnamerTrlrV_LegalKeyChars_LegalValueCharsASCIIVERBOSErrr)r*s0rrsNXz 7 GG  XX  ) """V]]25GG /E#J#c#.G*HHJ(Q,J HeIv  7YRYY{%;;<FF  6RZZ, - RZZ ! )dA 8 <:Fi8Ti8jB!G+        " BJJ # .IIX &: &g Js E;