cookies.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. ####
  2. # Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>
  3. #
  4. # All Rights Reserved
  5. #
  6. # Permission to use, copy, modify, and distribute this software
  7. # and its documentation for any purpose and without fee is hereby
  8. # granted, provided that the above copyright notice appear in all
  9. # copies and that both that copyright notice and this permission
  10. # notice appear in supporting documentation, and that the name of
  11. # Timothy O'Malley not be used in advertising or publicity
  12. # pertaining to distribution of the software without specific, written
  13. # prior permission.
  14. #
  15. # Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  17. # AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR
  18. # ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  21. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. # PERFORMANCE OF THIS SOFTWARE.
  23. #
  24. ####
  25. #
  26. # Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
  27. # by Timothy O'Malley <timo@alum.mit.edu>
  28. #
  29. # Cookie.py is a Python module for the handling of HTTP
  30. # cookies as a Python dictionary. See RFC 2109 for more
  31. # information on cookies.
  32. #
  33. # The original idea to treat Cookies as a dictionary came from
  34. # Dave Mitchell (davem@magnet.com) in 1995, when he released the
  35. # first version of nscookie.py.
  36. #
  37. ####
  38. r"""
  39. http.cookies module ported to python-future from Py3.3
  40. Here's a sample session to show how to use this module.
  41. At the moment, this is the only documentation.
  42. The Basics
  43. ----------
  44. Importing is easy...
  45. >>> from http import cookies
  46. Most of the time you start by creating a cookie.
  47. >>> C = cookies.SimpleCookie()
  48. Once you've created your Cookie, you can add values just as if it were
  49. a dictionary.
  50. >>> C = cookies.SimpleCookie()
  51. >>> C["fig"] = "newton"
  52. >>> C["sugar"] = "wafer"
  53. >>> C.output()
  54. 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
  55. Notice that the printable representation of a Cookie is the
  56. appropriate format for a Set-Cookie: header. This is the
  57. default behavior. You can change the header and printed
  58. attributes by using the .output() function
  59. >>> C = cookies.SimpleCookie()
  60. >>> C["rocky"] = "road"
  61. >>> C["rocky"]["path"] = "/cookie"
  62. >>> print(C.output(header="Cookie:"))
  63. Cookie: rocky=road; Path=/cookie
  64. >>> print(C.output(attrs=[], header="Cookie:"))
  65. Cookie: rocky=road
  66. The load() method of a Cookie extracts cookies from a string. In a
  67. CGI script, you would use this method to extract the cookies from the
  68. HTTP_COOKIE environment variable.
  69. >>> C = cookies.SimpleCookie()
  70. >>> C.load("chips=ahoy; vienna=finger")
  71. >>> C.output()
  72. 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
  73. The load() method is darn-tootin smart about identifying cookies
  74. within a string. Escaped quotation marks, nested semicolons, and other
  75. such trickeries do not confuse it.
  76. >>> C = cookies.SimpleCookie()
  77. >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
  78. >>> print(C)
  79. Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
  80. Each element of the Cookie also supports all of the RFC 2109
  81. Cookie attributes. Here's an example which sets the Path
  82. attribute.
  83. >>> C = cookies.SimpleCookie()
  84. >>> C["oreo"] = "doublestuff"
  85. >>> C["oreo"]["path"] = "/"
  86. >>> print(C)
  87. Set-Cookie: oreo=doublestuff; Path=/
  88. Each dictionary element has a 'value' attribute, which gives you
  89. back the value associated with the key.
  90. >>> C = cookies.SimpleCookie()
  91. >>> C["twix"] = "none for you"
  92. >>> C["twix"].value
  93. 'none for you'
  94. The SimpleCookie expects that all values should be standard strings.
  95. Just to be sure, SimpleCookie invokes the str() builtin to convert
  96. the value to a string, when the values are set dictionary-style.
  97. >>> C = cookies.SimpleCookie()
  98. >>> C["number"] = 7
  99. >>> C["string"] = "seven"
  100. >>> C["number"].value
  101. '7'
  102. >>> C["string"].value
  103. 'seven'
  104. >>> C.output()
  105. 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
  106. Finis.
  107. """
  108. from __future__ import unicode_literals
  109. from __future__ import print_function
  110. from __future__ import division
  111. from __future__ import absolute_import
  112. from future.builtins import chr, dict, int, str
  113. from future.utils import PY2, as_native_str
  114. #
  115. # Import our required modules
  116. #
  117. import re
  118. if PY2:
  119. re.ASCII = 0 # for py2 compatibility
  120. import string
  121. __all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
  122. _nulljoin = ''.join
  123. _semispacejoin = '; '.join
  124. _spacejoin = ' '.join
  125. #
  126. # Define an exception visible to External modules
  127. #
  128. class CookieError(Exception):
  129. pass
  130. # These quoting routines conform to the RFC2109 specification, which in
  131. # turn references the character definitions from RFC2068. They provide
  132. # a two-way quoting algorithm. Any non-text character is translated
  133. # into a 4 character sequence: a forward-slash followed by the
  134. # three-digit octal equivalent of the character. Any '\' or '"' is
  135. # quoted with a preceeding '\' slash.
  136. #
  137. # These are taken from RFC2068 and RFC2109.
  138. # _LegalChars is the list of chars which don't require "'s
  139. # _Translator hash-table for fast quoting
  140. #
  141. _LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
  142. _Translator = {
  143. '\000' : '\\000', '\001' : '\\001', '\002' : '\\002',
  144. '\003' : '\\003', '\004' : '\\004', '\005' : '\\005',
  145. '\006' : '\\006', '\007' : '\\007', '\010' : '\\010',
  146. '\011' : '\\011', '\012' : '\\012', '\013' : '\\013',
  147. '\014' : '\\014', '\015' : '\\015', '\016' : '\\016',
  148. '\017' : '\\017', '\020' : '\\020', '\021' : '\\021',
  149. '\022' : '\\022', '\023' : '\\023', '\024' : '\\024',
  150. '\025' : '\\025', '\026' : '\\026', '\027' : '\\027',
  151. '\030' : '\\030', '\031' : '\\031', '\032' : '\\032',
  152. '\033' : '\\033', '\034' : '\\034', '\035' : '\\035',
  153. '\036' : '\\036', '\037' : '\\037',
  154. # Because of the way browsers really handle cookies (as opposed
  155. # to what the RFC says) we also encode , and ;
  156. ',' : '\\054', ';' : '\\073',
  157. '"' : '\\"', '\\' : '\\\\',
  158. '\177' : '\\177', '\200' : '\\200', '\201' : '\\201',
  159. '\202' : '\\202', '\203' : '\\203', '\204' : '\\204',
  160. '\205' : '\\205', '\206' : '\\206', '\207' : '\\207',
  161. '\210' : '\\210', '\211' : '\\211', '\212' : '\\212',
  162. '\213' : '\\213', '\214' : '\\214', '\215' : '\\215',
  163. '\216' : '\\216', '\217' : '\\217', '\220' : '\\220',
  164. '\221' : '\\221', '\222' : '\\222', '\223' : '\\223',
  165. '\224' : '\\224', '\225' : '\\225', '\226' : '\\226',
  166. '\227' : '\\227', '\230' : '\\230', '\231' : '\\231',
  167. '\232' : '\\232', '\233' : '\\233', '\234' : '\\234',
  168. '\235' : '\\235', '\236' : '\\236', '\237' : '\\237',
  169. '\240' : '\\240', '\241' : '\\241', '\242' : '\\242',
  170. '\243' : '\\243', '\244' : '\\244', '\245' : '\\245',
  171. '\246' : '\\246', '\247' : '\\247', '\250' : '\\250',
  172. '\251' : '\\251', '\252' : '\\252', '\253' : '\\253',
  173. '\254' : '\\254', '\255' : '\\255', '\256' : '\\256',
  174. '\257' : '\\257', '\260' : '\\260', '\261' : '\\261',
  175. '\262' : '\\262', '\263' : '\\263', '\264' : '\\264',
  176. '\265' : '\\265', '\266' : '\\266', '\267' : '\\267',
  177. '\270' : '\\270', '\271' : '\\271', '\272' : '\\272',
  178. '\273' : '\\273', '\274' : '\\274', '\275' : '\\275',
  179. '\276' : '\\276', '\277' : '\\277', '\300' : '\\300',
  180. '\301' : '\\301', '\302' : '\\302', '\303' : '\\303',
  181. '\304' : '\\304', '\305' : '\\305', '\306' : '\\306',
  182. '\307' : '\\307', '\310' : '\\310', '\311' : '\\311',
  183. '\312' : '\\312', '\313' : '\\313', '\314' : '\\314',
  184. '\315' : '\\315', '\316' : '\\316', '\317' : '\\317',
  185. '\320' : '\\320', '\321' : '\\321', '\322' : '\\322',
  186. '\323' : '\\323', '\324' : '\\324', '\325' : '\\325',
  187. '\326' : '\\326', '\327' : '\\327', '\330' : '\\330',
  188. '\331' : '\\331', '\332' : '\\332', '\333' : '\\333',
  189. '\334' : '\\334', '\335' : '\\335', '\336' : '\\336',
  190. '\337' : '\\337', '\340' : '\\340', '\341' : '\\341',
  191. '\342' : '\\342', '\343' : '\\343', '\344' : '\\344',
  192. '\345' : '\\345', '\346' : '\\346', '\347' : '\\347',
  193. '\350' : '\\350', '\351' : '\\351', '\352' : '\\352',
  194. '\353' : '\\353', '\354' : '\\354', '\355' : '\\355',
  195. '\356' : '\\356', '\357' : '\\357', '\360' : '\\360',
  196. '\361' : '\\361', '\362' : '\\362', '\363' : '\\363',
  197. '\364' : '\\364', '\365' : '\\365', '\366' : '\\366',
  198. '\367' : '\\367', '\370' : '\\370', '\371' : '\\371',
  199. '\372' : '\\372', '\373' : '\\373', '\374' : '\\374',
  200. '\375' : '\\375', '\376' : '\\376', '\377' : '\\377'
  201. }
  202. def _quote(str, LegalChars=_LegalChars):
  203. r"""Quote a string for use in a cookie header.
  204. If the string does not need to be double-quoted, then just return the
  205. string. Otherwise, surround the string in doublequotes and quote
  206. (with a \) special characters.
  207. """
  208. if all(c in LegalChars for c in str):
  209. return str
  210. else:
  211. return '"' + _nulljoin(_Translator.get(s, s) for s in str) + '"'
  212. _OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
  213. _QuotePatt = re.compile(r"[\\].")
  214. def _unquote(mystr):
  215. # If there aren't any doublequotes,
  216. # then there can't be any special characters. See RFC 2109.
  217. if len(mystr) < 2:
  218. return mystr
  219. if mystr[0] != '"' or mystr[-1] != '"':
  220. return mystr
  221. # We have to assume that we must decode this string.
  222. # Down to work.
  223. # Remove the "s
  224. mystr = mystr[1:-1]
  225. # Check for special sequences. Examples:
  226. # \012 --> \n
  227. # \" --> "
  228. #
  229. i = 0
  230. n = len(mystr)
  231. res = []
  232. while 0 <= i < n:
  233. o_match = _OctalPatt.search(mystr, i)
  234. q_match = _QuotePatt.search(mystr, i)
  235. if not o_match and not q_match: # Neither matched
  236. res.append(mystr[i:])
  237. break
  238. # else:
  239. j = k = -1
  240. if o_match:
  241. j = o_match.start(0)
  242. if q_match:
  243. k = q_match.start(0)
  244. if q_match and (not o_match or k < j): # QuotePatt matched
  245. res.append(mystr[i:k])
  246. res.append(mystr[k+1])
  247. i = k + 2
  248. else: # OctalPatt matched
  249. res.append(mystr[i:j])
  250. res.append(chr(int(mystr[j+1:j+4], 8)))
  251. i = j + 4
  252. return _nulljoin(res)
  253. # The _getdate() routine is used to set the expiration time in the cookie's HTTP
  254. # header. By default, _getdate() returns the current time in the appropriate
  255. # "expires" format for a Set-Cookie header. The one optional argument is an
  256. # offset from now, in seconds. For example, an offset of -3600 means "one hour
  257. # ago". The offset may be a floating point number.
  258. #
  259. _weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  260. _monthname = [None,
  261. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  262. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  263. def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
  264. from time import gmtime, time
  265. now = time()
  266. year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
  267. return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
  268. (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
  269. class Morsel(dict):
  270. """A class to hold ONE (key, value) pair.
  271. In a cookie, each such pair may have several attributes, so this class is
  272. used to keep the attributes associated with the appropriate key,value pair.
  273. This class also includes a coded_value attribute, which is used to hold
  274. the network representation of the value. This is most useful when Python
  275. objects are pickled for network transit.
  276. """
  277. # RFC 2109 lists these attributes as reserved:
  278. # path comment domain
  279. # max-age secure version
  280. #
  281. # For historical reasons, these attributes are also reserved:
  282. # expires
  283. #
  284. # This is an extension from Microsoft:
  285. # httponly
  286. #
  287. # This dictionary provides a mapping from the lowercase
  288. # variant on the left to the appropriate traditional
  289. # formatting on the right.
  290. _reserved = {
  291. "expires" : "expires",
  292. "path" : "Path",
  293. "comment" : "Comment",
  294. "domain" : "Domain",
  295. "max-age" : "Max-Age",
  296. "secure" : "secure",
  297. "httponly" : "httponly",
  298. "version" : "Version",
  299. }
  300. _flags = set(['secure', 'httponly'])
  301. def __init__(self):
  302. # Set defaults
  303. self.key = self.value = self.coded_value = None
  304. # Set default attributes
  305. for key in self._reserved:
  306. dict.__setitem__(self, key, "")
  307. def __setitem__(self, K, V):
  308. K = K.lower()
  309. if not K in self._reserved:
  310. raise CookieError("Invalid Attribute %s" % K)
  311. dict.__setitem__(self, K, V)
  312. def isReservedKey(self, K):
  313. return K.lower() in self._reserved
  314. def set(self, key, val, coded_val, LegalChars=_LegalChars):
  315. # First we verify that the key isn't a reserved word
  316. # Second we make sure it only contains legal characters
  317. if key.lower() in self._reserved:
  318. raise CookieError("Attempt to set a reserved key: %s" % key)
  319. if any(c not in LegalChars for c in key):
  320. raise CookieError("Illegal key value: %s" % key)
  321. # It's a good key, so save it.
  322. self.key = key
  323. self.value = val
  324. self.coded_value = coded_val
  325. def output(self, attrs=None, header="Set-Cookie:"):
  326. return "%s %s" % (header, self.OutputString(attrs))
  327. __str__ = output
  328. @as_native_str()
  329. def __repr__(self):
  330. if PY2 and isinstance(self.value, unicode):
  331. val = str(self.value) # make it a newstr to remove the u prefix
  332. else:
  333. val = self.value
  334. return '<%s: %s=%s>' % (self.__class__.__name__,
  335. str(self.key), repr(val))
  336. def js_output(self, attrs=None):
  337. # Print javascript
  338. return """
  339. <script type="text/javascript">
  340. <!-- begin hiding
  341. document.cookie = \"%s\";
  342. // end hiding -->
  343. </script>
  344. """ % (self.OutputString(attrs).replace('"', r'\"'))
  345. def OutputString(self, attrs=None):
  346. # Build up our result
  347. #
  348. result = []
  349. append = result.append
  350. # First, the key=value pair
  351. append("%s=%s" % (self.key, self.coded_value))
  352. # Now add any defined attributes
  353. if attrs is None:
  354. attrs = self._reserved
  355. items = sorted(self.items())
  356. for key, value in items:
  357. if value == "":
  358. continue
  359. if key not in attrs:
  360. continue
  361. if key == "expires" and isinstance(value, int):
  362. append("%s=%s" % (self._reserved[key], _getdate(value)))
  363. elif key == "max-age" and isinstance(value, int):
  364. append("%s=%d" % (self._reserved[key], value))
  365. elif key == "secure":
  366. append(str(self._reserved[key]))
  367. elif key == "httponly":
  368. append(str(self._reserved[key]))
  369. else:
  370. append("%s=%s" % (self._reserved[key], value))
  371. # Return the result
  372. return _semispacejoin(result)
  373. #
  374. # Pattern for finding cookie
  375. #
  376. # This used to be strict parsing based on the RFC2109 and RFC2068
  377. # specifications. I have since discovered that MSIE 3.0x doesn't
  378. # follow the character rules outlined in those specs. As a
  379. # result, the parsing rules here are less strict.
  380. #
  381. _LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
  382. _CookiePattern = re.compile(r"""
  383. (?x) # This is a verbose pattern
  384. (?P<key> # Start of group 'key'
  385. """ + _LegalCharsPatt + r"""+? # Any word of at least one letter
  386. ) # End of group 'key'
  387. ( # Optional group: there may not be a value.
  388. \s*=\s* # Equal Sign
  389. (?P<val> # Start of group 'val'
  390. "(?:[^\\"]|\\.)*" # Any doublequoted string
  391. | # or
  392. \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
  393. | # or
  394. """ + _LegalCharsPatt + r"""* # Any word or empty string
  395. ) # End of group 'val'
  396. )? # End of optional value group
  397. \s* # Any number of spaces.
  398. (\s+|;|$) # Ending either at space, semicolon, or EOS.
  399. """, re.ASCII) # May be removed if safe.
  400. # At long last, here is the cookie class. Using this class is almost just like
  401. # using a dictionary. See this module's docstring for example usage.
  402. #
  403. class BaseCookie(dict):
  404. """A container class for a set of Morsels."""
  405. def value_decode(self, val):
  406. """real_value, coded_value = value_decode(STRING)
  407. Called prior to setting a cookie's value from the network
  408. representation. The VALUE is the value read from HTTP
  409. header.
  410. Override this function to modify the behavior of cookies.
  411. """
  412. return val, val
  413. def value_encode(self, val):
  414. """real_value, coded_value = value_encode(VALUE)
  415. Called prior to setting a cookie's value from the dictionary
  416. representation. The VALUE is the value being assigned.
  417. Override this function to modify the behavior of cookies.
  418. """
  419. strval = str(val)
  420. return strval, strval
  421. def __init__(self, input=None):
  422. if input:
  423. self.load(input)
  424. def __set(self, key, real_value, coded_value):
  425. """Private method for setting a cookie's value"""
  426. M = self.get(key, Morsel())
  427. M.set(key, real_value, coded_value)
  428. dict.__setitem__(self, key, M)
  429. def __setitem__(self, key, value):
  430. """Dictionary style assignment."""
  431. rval, cval = self.value_encode(value)
  432. self.__set(key, rval, cval)
  433. def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
  434. """Return a string suitable for HTTP."""
  435. result = []
  436. items = sorted(self.items())
  437. for key, value in items:
  438. result.append(value.output(attrs, header))
  439. return sep.join(result)
  440. __str__ = output
  441. @as_native_str()
  442. def __repr__(self):
  443. l = []
  444. items = sorted(self.items())
  445. for key, value in items:
  446. if PY2 and isinstance(value.value, unicode):
  447. val = str(value.value) # make it a newstr to remove the u prefix
  448. else:
  449. val = value.value
  450. l.append('%s=%s' % (str(key), repr(val)))
  451. return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
  452. def js_output(self, attrs=None):
  453. """Return a string suitable for JavaScript."""
  454. result = []
  455. items = sorted(self.items())
  456. for key, value in items:
  457. result.append(value.js_output(attrs))
  458. return _nulljoin(result)
  459. def load(self, rawdata):
  460. """Load cookies from a string (presumably HTTP_COOKIE) or
  461. from a dictionary. Loading cookies from a dictionary 'd'
  462. is equivalent to calling:
  463. map(Cookie.__setitem__, d.keys(), d.values())
  464. """
  465. if isinstance(rawdata, str):
  466. self.__parse_string(rawdata)
  467. else:
  468. # self.update() wouldn't call our custom __setitem__
  469. for key, value in rawdata.items():
  470. self[key] = value
  471. return
  472. def __parse_string(self, mystr, patt=_CookiePattern):
  473. i = 0 # Our starting point
  474. n = len(mystr) # Length of string
  475. M = None # current morsel
  476. while 0 <= i < n:
  477. # Start looking for a cookie
  478. match = patt.search(mystr, i)
  479. if not match:
  480. # No more cookies
  481. break
  482. key, value = match.group("key"), match.group("val")
  483. i = match.end(0)
  484. # Parse the key, value in case it's metainfo
  485. if key[0] == "$":
  486. # We ignore attributes which pertain to the cookie
  487. # mechanism as a whole. See RFC 2109.
  488. # (Does anyone care?)
  489. if M:
  490. M[key[1:]] = value
  491. elif key.lower() in Morsel._reserved:
  492. if M:
  493. if value is None:
  494. if key.lower() in Morsel._flags:
  495. M[key] = True
  496. else:
  497. M[key] = _unquote(value)
  498. elif value is not None:
  499. rval, cval = self.value_decode(value)
  500. self.__set(key, rval, cval)
  501. M = self[key]
  502. class SimpleCookie(BaseCookie):
  503. """
  504. SimpleCookie supports strings as cookie values. When setting
  505. the value using the dictionary assignment notation, SimpleCookie
  506. calls the builtin str() to convert the value to a string. Values
  507. received from HTTP are kept as strings.
  508. """
  509. def value_decode(self, val):
  510. return _unquote(val), val
  511. def value_encode(self, val):
  512. strval = str(val)
  513. return strval, _quote(strval)