\dPdZddlZddlZddlZgdZdjZdjZdjZGdde Z ej ej zd zZ e d zZd eed eeeez DZeed deddiejdeje zjZdZejdZejdZdZgdZgdZdeefdZGdde Z!dZ"e"dzZ#ejde"zdze#zdzej$ej%zZ&Gd d!e Z'Gd"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=\\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;  ceZdZdS)rN)__name__ __module__ __qualname__..\python\lib\http\cookies.pyrrsDr rz!#$%&'*+-.^_`|~:z ()/<=>?@[]{}ci|]}|d|z S)z\%03or ).0ns r rs4JJJ(Q,JJJr "\"\z\\z[%s]+ch|t|r|Sd|tzdzS)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 _Translatorstrs r_quoters6 {mC(({ S]];///#55r z\\[0-3][0-7][0-7]z[\\].c j|t|dkr|S|ddks |ddkr|S|dd}d}t|}g}d|cxkr|krInnEt||}t||}|s |s|||dndx}}|r|d}|r|d}|rI|r||krA|||||||dz|dz}n`|||||t t||dz|dzd|dz}d|cxkr|kCnt|S)Nrr) len _OctalPattsearch _QuotePattappendstartchrint _nulljoin)rirreso_matchq_matchjks r_unquoter3s {c#hhll  1v}}B3 ad)C A CA C q****1*****##C++##C++ w  JJs122w     A  ! a  A  ! a  A  G q1uu JJs1Q3x JJs1Q3x AAA JJs1Q3x JJs3s1Q3qs7|Q//00 1 1 1AA' q****1****( S>>r )MonTueWedThuFriSatSun) NJanFebMarAprMayJunJulAugSepOctNovDecc ddlm}m}|}|||z\ }}}} } } } } }d|| ||||| | | fzS)Nr)gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)rIrH)future weekdayname monthnamerHrInowyearmonthdayhhmmsswdyzs r_getdaterWsu!!!!!!!! $&&C-3VC&L-A-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!d ZeejZdS)#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) rZpathcommentdomainmax-agesecurehttponlyversionsamesiterfrgc|dx|_x|_|_|jD]}t||ddS)Nr)_key_value _coded_value _reserveddict __setitem__)selfkeys r__init__zMorsel.__init__!sP6:: :DK$"3> , ,C   T3 + + + + , ,r c|jSN)rkrqs rrrz Morsel.key)s yr c|jSru)rlrvs rvaluez Morsel.value-s {r c|jSru)rmrvs r coded_valuezMorsel.coded_value1s   r c|}||jvrtd|t|||dSNzInvalid attribute )lowerrnrrorp)rqKVs rrpzMorsel.__setitem__5sO GGIIDN""+;<< < q!$$$$$r Nc|}||jvrtd|t|||Sr|)r}rnrro setdefault)rqrrvals rrzMorsel.setdefault;sGiikk dn $ $+=>> >tS#...r ct|tstSt||o/|j|jko|j|jko|j|jkSru) isinstancerYNotImplementedro__eq__rlrkrmrqmorsels rrz Morsel.__eq__Asg&&)) "! ! D&))9 v},9 V[(9!V%88 :r ct}t|||j|j|Sru)rYroupdate__dict__rs rcopyz Morsel.copyKs< FD!!!t}--- r ci}t|D]9\}}|}||jvrt d||||<:t||dSr|)roitemsr}rnrr)rqvaluesdatarrrs rrz Morsel.updateQsV **,,  HC))++C$.((!kCC"ABBBDII D$r c8||jvSru)r}rn)rqr~s r isReservedKeyzMorsel.isReservedKeyZswwyyDN**r c||jvrtd|t|std|||_||_||_dS)NzAttempt to set a reserved key z Illegal key )r}rnrrrkrlrm)rqrrr coded_vals rsetz Morsel.set]so 99;;$. ( (+CCIJJ JS!! 9+##788 8  %r c,|j|j|jdS)N)rrrxrzrkrlrmrvs r __getstate__zMorsel.__getstate__hs!9[,   r cT|d|_|d|_|d|_dS)Nrrrxrzr)rqstates r __setstate__zMorsel.__setstate__os+%L Gn !-0r Set-Cookie:c6|d||S)Nr) OutputString)rqattrsheaders routputz Morsel.outputts" &&$"3"3E":":":;;r cLd|jjd|dS)N<: >) __class__r rrvs r__repr__zMorsel.__repr__ys,!^444d6G6G6I6I6I6IJJr cZd||ddzS)Nz rr)rreplace)rqrs r js_outputzMorsel.js_output|s4   ''//U;; = =r cg}|j}||jd|j||j}t |}|D]\}}|dkr ||vr|dkr>t |tr)||j|dt|V|dkr1t |tr|d|j||fz|dkr>t |tr)||j|dt|||j vr'|r#|t|j|||j|d|t|S)N=rrZrez%s=%drc) r(rrrzrnsortedrrr+rWrr_flags_semispacejoin)rqrresultr(rrrrxs rrzMorsel.OutputStrings $(((D$4$45666 =NEtzz||$$ ? ?JC{{%iJuc$:$:$."5"5"5xGHHHH !!j&<&<!w$."5u!==>>>> !!j&<&<!$."5"5"5ve}}}EFFFF ##5F3t~c233444$."5"5"5uu=>>>>f%%%r ru)Nr)r r r __doc__rnrrspropertyrrrxrzrprrobject__ne__rrrrrrr__str__rrr classmethodtypes GenericAlias__class_getitem__r r rrYrYs*  I #F,,,XX!!X!%%% //// :::]F    +++ & & &   111 <<<<GKKK====&&&&B$ E$677r rYz,\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. c\eZdZdZdZdZddZdZdZdd Z e Z d Z dd Z d Z efdZdS)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 rqrs 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. rrqrstrvals r value_encodezBaseCookie.value_encodes Sv~r Nc8|r||dSdSru)load)rqinputs rrszBaseCookie.__init__s,   IIe       r c||t}||||t|||dS)z+Private method for setting a cookie's valueN)getrYrrorp)rqrr real_valuerzMs r__setzBaseCookie.__setsM HHS&(( # # c:{+++ sA&&&&&r ct|trt|||dS||\}}||||dS)zDictionary style assignment.N)rrYrorpr_BaseCookie__set)rqrrrxrvalcvals rrpzBaseCookie.__setitem__se eV $ $ (   T3 . . . . .**511JD$ JJsD$ ' ' ' ' 'r r cg}t|}|D].\}}||||/||S)z"Return a string suitable for HTTP.)rrr(rjoin)rqrrseprrrrrxs rrzBaseCookie.outputsdtzz||$$ 7 7JC MM%,,uf55 6 6 6 6xxr cg}t|}|D]1\}}||dt|j2d|jjdt|dS)Nrrrr)rrr(reprrxrr _spacejoin)rqlrrrrxs rrzBaseCookie.__repr__s tzz||$$ 9 9JC HHT%+%6%6%67 8 8 8 8 9!^444jmmmmDDr cg}t|}|D]-\}}|||.t |S)z(Return a string suitable for JavaScript.)rrr(rr,)rqrrrrrrxs rrzBaseCookie.js_outputs^tzz||$$ 2 2JC MM%//%00 1 1 1 1   r ct|tr||n|D] \}}|||< dS)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)rqrawdatarrrxs rrzBaseCookie.load sZ gs # # "    ( ( ( (&mmoo " " U!S r cd}t|}g}d}d}d}d|cxkr|krVnnR|||} | sn8| d| d} } | d}| ddkr$|sz||| dd| fn| t jvrg|sdS| ;| t jvr||| dfnZdS||| t| fn2| .||| | | fd}ndSd|cxkr|kPnd} |D]B\} } } | |kr | J| | | <| |ksJ| \}}| | |||| } CdS) NrFr!rrrr$T) r$matchgroupendr(r}rYrnrr3rr)rqrpattr-r parsed_items morsel_seenTYPE_ATTRIBUTE TYPE_KEYVALUErrrrxrtprrs r__parse_stringzBaseCookie.__parse_strings-  HH   1jjjjqjjjjjJJsA&&E U++U[[-?-?C ! A1v}}"##^SWe$DEEEE 000"F=yy{{fm33$++^S$,GHHHH ''huoo(NOOOO"##]C9J9J59Q9Q$RSSS" E1jjjjqjjjjJ *  NBU^##}}}#]****" d 3d+++I  r ru)Nrr)r r r rrrrsrrprrrrr_CookiePatternrr r rrrs11''' (((    GEEE!!!!   (6::::::r rceZdZdZdZdZdS)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. c$t||fSru)r3rs rrzSimpleCookie.value_decode_s}}c!!r cBt|}|t|fSru)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'r3 _weekdayname _monthnamerWrorY_LegalKeyChars_LegalValueCharsASCIIVERBOSErrrr r rrsNXXz 7 7 7 G  X      )   ""V]25GG /JJEE#JJ##cc#.G.G*H*HHJJJ CHHeCIIv  7YRY{%;%;;<<F  6 6 6RZ, - - RZ ! ! )))dA@@ 888 <:FFFFi8i8i8i8i8Ti8i8i8jB!G+        " BJ #  .IIIIIIIIX & & & & &: & & & & &r