3 sj]W @sdZddlZddlZdddgZdjZdjZdjZd d ZGd dde Z ej ej d Z e d ZddeedeeeeDZejeddeddiejdeje jZejdZddZddZejdjZddZddZd d!d"d#d$d%d&gZ dd'd(d)d*d+d,d-d.d/d0d1d2g Z!de e!fd3d4Z"Gd5d6d6e#Z$d7Z%e%d8Z&ejd9e%d:e&d;ej'ej(BZ)Gda% 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;  cCs$ddl}d|}|j|tdddS)NrzvThe .%s setter is deprecated. The attribute will be read-only in future releases. Please use the set() method instead.) stacklevel)warningswarnDeprecationWarning)setterr msgr/usr/lib64/python3.6/cookies.py_warn_deprecated_settersrc@s eZdZdS)rN)__name__ __module__ __qualname__rrrrrsz!#$%&'*+-.^_`|~:z ()/<=>?@[]{}cCsi|]}d||qS)z\%03or).0nrrr sr"z\"\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|]}tjt|VqdS)N)_control_character_researchstr)rvrrr sz)_has_control_character..)any)valrrr_has_control_charactersr!cCs*|dkst|r|Sd|jtdSdS)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)rrrr_quotesr%z\\(?:([0-3][0-7][0-7])|(.))cCs&|drtt|ddS|dSdS)N)chrint)mrrr_unquote_replacesr,cCsJ|dkst|dkr|S|ddks0|ddkr4|S|dd}tt|S)Nr(rrr&r-)len _unquote_subr,)rrrr_unquotes  r0ZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc CsRddlm}m}|}|||\ }}}} } } } } }d|| ||||| | | fS)Nr)gmtimetimez#%s, %02d %3s %4d %02d:%02d:%02d GMT)r2r1)ZfutureZ weekdaynameZ monthnamer1r2ZnowZyearZmonthZdayZhhZmmZssZwdyzrrr_getdates r5c @s eZdZdZdddddddd d Zd d hZd dZeddZej ddZeddZ e j ddZ eddZ e j ddZ ddZ d6ddZ ddZejZdd Zd!d"Zd#d$Zd%d&Zefd'd(Zd)d*Zd+d,Zd7d.d/ZeZd0d1Zd8d2d3Zd9d4d5ZdS):MorselaA 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. This is most useful when Python objects are pickled for network transit. expiresZPathCommentZDomainzMax-AgeZSecureZHttpOnlyZVersion)r7pathcommentZdomainzmax-agesecurehttponlyversionr;r<cCs4d|_|_|_x|jD]}tj||dqWdS)Nr)_key_value _coded_value _reserveddict __setitem__)selfkeyrrr__init__s zMorsel.__init__cCs|jS)N)r>)rDrrrrE%sz Morsel.keycCstd||_dS)NrE)rr>)rDrErrrrE)scCs|jS)N)r?)rDrrrvalue.sz Morsel.valuecCstd||_dS)NrG)rr?)rDrGrrrrG2scCs|jS)N)r@)rDrrr coded_value7szMorsel.coded_valuecCstd||_dS)NrH)rr@)rDrHrrrrH;scCsP|j}||jkr td|ft||r>td|d|tj|||dS)NzInvalid attribute %rz.Control characters are not allowed in cookies r)lowerrArr!rBrC)rDKVrrrrC@s   zMorsel.__setitem__NcCsH|j}||jkr td|ft||r:td||ftj|||S)NzInvalid attribute %rz3Control characters are not allowed in cookies %r %r)rIrArr!rB setdefault)rDrEr rrrrLHs   zMorsel.setdefaultcCs>t|tstStj||o<|j|jko<|j|jko<|j|jkS)N) isinstancer6NotImplementedrB__eq__r?r>r@)rDmorselrrrrOPs     z Morsel.__eq__cCs$t}tj|||jj|j|S)N)r6rBupdate__dict__)rDrPrrrcopyZs z Morsel.copycCsti}x^t|jD]N\}}|j}||jkr:td|ft||rXtd|d||||<qWtj||dS)NzInvalid attribute %rz.Control characters are not allowed in cookies r)rBitemsrIrArr!rQ)rDvaluesdatarEr rrrrQ`s   z Morsel.updatecCs|j||S)N)rQ)rDrUrrr__ior__ls zMorsel.__ior__cCs|j|jkS)N)rIrA)rDrJrrr isReservedKeypszMorsel.isReservedKeycCs|tkr ddl}|jdtdd|j|jkrr?r@)rDrEr Z coded_valZ LegalCharsr rrrsetss  z Morsel.setcCs|j|j|jdS)N)rErGrH)r>r?r@)rDrrr __getstate__szMorsel.__getstate__cCsT|d}|d}|d}t|||r>td|d|d|||_||_||_dS)NrErGrHz.Control characters are not allowed in cookies r)r!rr>r?r@)rDstaterErGrHrrr __setstate__s zMorsel.__setstate__ Set-Cookie:cCsd||j|fS)Nz%s %s) OutputString)rDattrsheaderrrroutputsz Morsel.outputcCsd|jj|jfS)Nz<%s: %s>) __class__rr_)rDrrr__repr__szMorsel.__repr__cCs*|j|}t|rtdd|jddS)Nz-Control characters are not allowed in cookiesz rz\")r_r!rreplace)rDr`Z output_stringrrr js_outputs zMorsel.js_outputcCs(g}|j}|d|j|jf|dkr,|j}t|j}x|D]\}}|dkrPq>||krZq>|dkrt|tr|d|j|t|fq>|dkrt|tr|d|j||fq>|dkrt|t r|d|j|t |fq>||j kr|r|t |j|q>|d|j||fq>Wt |S)Nz%s=%srr7zmax-agez%s=%dr:) appendrErHrAsortedrTrMr*r5rr%_flags_semispacejoin)rDr`resultrgrTrErGrrrr_s,  zMorsel.OutputString)N)Nr^)N)N)rrr__doc__rArirFpropertyrEr rGrHrCrLrOobject__ne__rSrQrWrXrYrZr[r]rb__str__rdrfr_rrrrr6sB        r6z,\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@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)rDr rrr 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)rDr strvalrrr value_encodeszBaseCookie.value_encodeNcCs|r|j|dS)N)load)rDinputrrrrFszBaseCookie.__init__cCs.|j|t}|j|||tj|||dS)z+Private method for setting a cookie's valueN)getr6rZrBrC)rDrEZ real_valuerHMrrrZ__set szBaseCookie.__setcCs:t|trtj|||n|j|\}}|j|||dS)zDictionary style assignment.N)rMr6rBrCrs_BaseCookie__set)rDrErGrvalcvalrrrrCs zBaseCookie.__setitem__ Set-Cookie: c CsRg}t|j}x6|D].\}}|j||}t|r:td|j|qW|j|S)z"Return a string suitable for HTTP.z-Control characters are not allowed in cookies)rhrTrbr!rrgjoin) rDr`raseprkrTrErGZ value_outputrrrrbs  zBaseCookie.outputcCsNg}t|j}x(|D] \}}|jd|t|jfqWd|jjt|fS)Nz%s=%sz<%s: %s>)rhrTrgreprrGrcr _spacejoin)rDlrTrErGrrrrd(s  zBaseCookie.__repr__cCs:g}t|j}x |D]\}}|j|j|qWt|S)z(Return a string suitable for JavaScript.)rhrTrgrf _nulljoin)rDr`rkrTrErGrrrrf/s  zBaseCookie.js_outputcCs8t|tr|j|nx|jD]\}}|||<q WdS)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)rMr_BaseCookie__parse_stringrT)rDZrawdatarErGrrrrt7s    zBaseCookie.loadcCspd}t|}g}d}d}d}xd|ko2|knr|j||} | sLP| jd| jd} } | jd}| ddkr|s~q |j|| dd| fq | jtjkr|sdS| dkr| jtjkr|j|| dfqdSn|j|| t | fq | dk r|j|| |j | fd}q dSq Wd} xF|D]>\} } } | |krH| | | <n| \}}|j | |||| } q*WdS) NrFr&r(rEr $T) r.matchgroupendrgrIr6rArir0rqrx)rDrZpattirZ parsed_itemsZ morsel_seenZTYPE_ATTRIBUTEZ TYPE_KEYVALUErrErGrwtpryrzrrrZ__parse_stringEsF      zBaseCookie.__parse_string)N)Nr{r|)N)rrrrlrqrsrFrxrCrbrprdrfrt_CookiePatternrrrrrrs    c@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||fS)N)r0)rDr rrrrqszSimpleCookie.value_decodecCst|}|t|fS)N)rr%)rDr rrrrrrsszSimpleCookie.value_encodeN)rrrrlrqrsrrrrrs),rlrestring__all__r}rrjrr ExceptionrZ ascii_lettersZdigitsrYZ_UnescapedCharsrZrangemapordr$rQcompileescape fullmatchr"rr!r%subr/r,r0Z _weekdaynameZ _monthnamer5rBr6Z_LegalKeyCharsZ_LegalValueCharsASCIIVERBOSErrrrrrrsJ     a