cookies.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. Here's a sample session to show how to use this module.
  40. At the moment, this is the only documentation.
  41. The Basics
  42. ----------
  43. Importing is easy...
  44. >>> from http import cookies
  45. Most of the time you start by creating a cookie.
  46. >>> C = cookies.SimpleCookie()
  47. Once you've created your Cookie, you can add values just as if it were
  48. a dictionary.
  49. >>> C = cookies.SimpleCookie()
  50. >>> C["fig"] = "newton"
  51. >>> C["sugar"] = "wafer"
  52. >>> C.output()
  53. 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
  54. Notice that the printable representation of a Cookie is the
  55. appropriate format for a Set-Cookie: header. This is the
  56. default behavior. You can change the header and printed
  57. attributes by using the .output() function
  58. >>> C = cookies.SimpleCookie()
  59. >>> C["rocky"] = "road"
  60. >>> C["rocky"]["path"] = "/cookie"
  61. >>> print(C.output(header="Cookie:"))
  62. Cookie: rocky=road; Path=/cookie
  63. >>> print(C.output(attrs=[], header="Cookie:"))
  64. Cookie: rocky=road
  65. The load() method of a Cookie extracts cookies from a string. In a
  66. CGI script, you would use this method to extract the cookies from the
  67. HTTP_COOKIE environment variable.
  68. >>> C = cookies.SimpleCookie()
  69. >>> C.load("chips=ahoy; vienna=finger")
  70. >>> C.output()
  71. 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
  72. The load() method is darn-tootin smart about identifying cookies
  73. within a string. Escaped quotation marks, nested semicolons, and other
  74. such trickeries do not confuse it.
  75. >>> C = cookies.SimpleCookie()
  76. >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
  77. >>> print(C)
  78. Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
  79. Each element of the Cookie also supports all of the RFC 2109
  80. Cookie attributes. Here's an example which sets the Path
  81. attribute.
  82. >>> C = cookies.SimpleCookie()
  83. >>> C["oreo"] = "doublestuff"
  84. >>> C["oreo"]["path"] = "/"
  85. >>> print(C)
  86. Set-Cookie: oreo=doublestuff; Path=/
  87. Each dictionary element has a 'value' attribute, which gives you
  88. back the value associated with the key.
  89. >>> C = cookies.SimpleCookie()
  90. >>> C["twix"] = "none for you"
  91. >>> C["twix"].value
  92. 'none for you'
  93. The SimpleCookie expects that all values should be standard strings.
  94. Just to be sure, SimpleCookie invokes the str() builtin to convert
  95. the value to a string, when the values are set dictionary-style.
  96. >>> C = cookies.SimpleCookie()
  97. >>> C["number"] = 7
  98. >>> C["string"] = "seven"
  99. >>> C["number"].value
  100. '7'
  101. >>> C["string"].value
  102. 'seven'
  103. >>> C.output()
  104. 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
  105. Finis.
  106. """
  107. #
  108. # Import our required modules
  109. #
  110. import re
  111. import string
  112. __all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
  113. _nulljoin = ''.join
  114. _semispacejoin = '; '.join
  115. _spacejoin = ' '.join
  116. #
  117. # Define an exception visible to External modules
  118. #
  119. class CookieError(Exception):
  120. pass
  121. # These quoting routines conform to the RFC2109 specification, which in
  122. # turn references the character definitions from RFC2068. They provide
  123. # a two-way quoting algorithm. Any non-text character is translated
  124. # into a 4 character sequence: a forward-slash followed by the
  125. # three-digit octal equivalent of the character. Any '\' or '"' is
  126. # quoted with a preceding '\' slash.
  127. # Because of the way browsers really handle cookies (as opposed to what
  128. # the RFC says) we also encode "," and ";".
  129. #
  130. # These are taken from RFC2068 and RFC2109.
  131. # _LegalChars is the list of chars which don't require "'s
  132. # _Translator hash-table for fast quoting
  133. #
  134. _LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
  135. _UnescapedChars = _LegalChars + ' ()/<=>?@[]{}'
  136. _Translator = {n: '\\%03o' % n
  137. for n in set(range(256)) - set(map(ord, _UnescapedChars))}
  138. _Translator.update({
  139. ord('"'): '\\"',
  140. ord('\\'): '\\\\',
  141. })
  142. _is_legal_key = re.compile('[%s]+' % re.escape(_LegalChars)).fullmatch
  143. def _quote(str):
  144. r"""Quote a string for use in a cookie header.
  145. If the string does not need to be double-quoted, then just return the
  146. string. Otherwise, surround the string in doublequotes and quote
  147. (with a \) special characters.
  148. """
  149. if str is None or _is_legal_key(str):
  150. return str
  151. else:
  152. return '"' + str.translate(_Translator) + '"'
  153. _OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
  154. _QuotePatt = re.compile(r"[\\].")
  155. def _unquote(str):
  156. # If there aren't any doublequotes,
  157. # then there can't be any special characters. See RFC 2109.
  158. if str is None or len(str) < 2:
  159. return str
  160. if str[0] != '"' or str[-1] != '"':
  161. return str
  162. # We have to assume that we must decode this string.
  163. # Down to work.
  164. # Remove the "s
  165. str = str[1:-1]
  166. # Check for special sequences. Examples:
  167. # \012 --> \n
  168. # \" --> "
  169. #
  170. i = 0
  171. n = len(str)
  172. res = []
  173. while 0 <= i < n:
  174. o_match = _OctalPatt.search(str, i)
  175. q_match = _QuotePatt.search(str, i)
  176. if not o_match and not q_match: # Neither matched
  177. res.append(str[i:])
  178. break
  179. # else:
  180. j = k = -1
  181. if o_match:
  182. j = o_match.start(0)
  183. if q_match:
  184. k = q_match.start(0)
  185. if q_match and (not o_match or k < j): # QuotePatt matched
  186. res.append(str[i:k])
  187. res.append(str[k+1])
  188. i = k + 2
  189. else: # OctalPatt matched
  190. res.append(str[i:j])
  191. res.append(chr(int(str[j+1:j+4], 8)))
  192. i = j + 4
  193. return _nulljoin(res)
  194. # The _getdate() routine is used to set the expiration time in the cookie's HTTP
  195. # header. By default, _getdate() returns the current time in the appropriate
  196. # "expires" format for a Set-Cookie header. The one optional argument is an
  197. # offset from now, in seconds. For example, an offset of -3600 means "one hour
  198. # ago". The offset may be a floating point number.
  199. #
  200. _weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  201. _monthname = [None,
  202. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  203. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  204. def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
  205. from time import gmtime, time
  206. now = time()
  207. year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
  208. return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
  209. (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
  210. class Morsel(dict):
  211. """A class to hold ONE (key, value) pair.
  212. In a cookie, each such pair may have several attributes, so this class is
  213. used to keep the attributes associated with the appropriate key,value pair.
  214. This class also includes a coded_value attribute, which is used to hold
  215. the network representation of the value. This is most useful when Python
  216. objects are pickled for network transit.
  217. """
  218. # RFC 2109 lists these attributes as reserved:
  219. # path comment domain
  220. # max-age secure version
  221. #
  222. # For historical reasons, these attributes are also reserved:
  223. # expires
  224. #
  225. # This is an extension from Microsoft:
  226. # httponly
  227. #
  228. # This dictionary provides a mapping from the lowercase
  229. # variant on the left to the appropriate traditional
  230. # formatting on the right.
  231. _reserved = {
  232. "expires" : "expires",
  233. "path" : "Path",
  234. "comment" : "Comment",
  235. "domain" : "Domain",
  236. "max-age" : "Max-Age",
  237. "secure" : "Secure",
  238. "httponly" : "HttpOnly",
  239. "version" : "Version",
  240. }
  241. _flags = {'secure', 'httponly'}
  242. def __init__(self):
  243. # Set defaults
  244. self._key = self._value = self._coded_value = None
  245. # Set default attributes
  246. for key in self._reserved:
  247. dict.__setitem__(self, key, "")
  248. @property
  249. def key(self):
  250. return self._key
  251. @property
  252. def value(self):
  253. return self._value
  254. @property
  255. def coded_value(self):
  256. return self._coded_value
  257. def __setitem__(self, K, V):
  258. K = K.lower()
  259. if not K in self._reserved:
  260. raise CookieError("Invalid attribute %r" % (K,))
  261. dict.__setitem__(self, K, V)
  262. def setdefault(self, key, val=None):
  263. key = key.lower()
  264. if key not in self._reserved:
  265. raise CookieError("Invalid attribute %r" % (key,))
  266. return dict.setdefault(self, key, val)
  267. def __eq__(self, morsel):
  268. if not isinstance(morsel, Morsel):
  269. return NotImplemented
  270. return (dict.__eq__(self, morsel) and
  271. self._value == morsel._value and
  272. self._key == morsel._key and
  273. self._coded_value == morsel._coded_value)
  274. __ne__ = object.__ne__
  275. def copy(self):
  276. morsel = Morsel()
  277. dict.update(morsel, self)
  278. morsel.__dict__.update(self.__dict__)
  279. return morsel
  280. def update(self, values):
  281. data = {}
  282. for key, val in dict(values).items():
  283. key = key.lower()
  284. if key not in self._reserved:
  285. raise CookieError("Invalid attribute %r" % (key,))
  286. data[key] = val
  287. dict.update(self, data)
  288. def isReservedKey(self, K):
  289. return K.lower() in self._reserved
  290. def set(self, key, val, coded_val):
  291. if key.lower() in self._reserved:
  292. raise CookieError('Attempt to set a reserved key %r' % (key,))
  293. if not _is_legal_key(key):
  294. raise CookieError('Illegal key %r' % (key,))
  295. # It's a good key, so save it.
  296. self._key = key
  297. self._value = val
  298. self._coded_value = coded_val
  299. def __getstate__(self):
  300. return {
  301. 'key': self._key,
  302. 'value': self._value,
  303. 'coded_value': self._coded_value,
  304. }
  305. def __setstate__(self, state):
  306. self._key = state['key']
  307. self._value = state['value']
  308. self._coded_value = state['coded_value']
  309. def output(self, attrs=None, header="Set-Cookie:"):
  310. return "%s %s" % (header, self.OutputString(attrs))
  311. __str__ = output
  312. def __repr__(self):
  313. return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
  314. def js_output(self, attrs=None):
  315. # Print javascript
  316. return """
  317. <script type="text/javascript">
  318. <!-- begin hiding
  319. document.cookie = \"%s\";
  320. // end hiding -->
  321. </script>
  322. """ % (self.OutputString(attrs).replace('"', r'\"'))
  323. def OutputString(self, attrs=None):
  324. # Build up our result
  325. #
  326. result = []
  327. append = result.append
  328. # First, the key=value pair
  329. append("%s=%s" % (self.key, self.coded_value))
  330. # Now add any defined attributes
  331. if attrs is None:
  332. attrs = self._reserved
  333. items = sorted(self.items())
  334. for key, value in items:
  335. if value == "":
  336. continue
  337. if key not in attrs:
  338. continue
  339. if key == "expires" and isinstance(value, int):
  340. append("%s=%s" % (self._reserved[key], _getdate(value)))
  341. elif key == "max-age" and isinstance(value, int):
  342. append("%s=%d" % (self._reserved[key], value))
  343. elif key == "comment" and isinstance(value, str):
  344. append("%s=%s" % (self._reserved[key], _quote(value)))
  345. elif key in self._flags:
  346. if value:
  347. append(str(self._reserved[key]))
  348. else:
  349. append("%s=%s" % (self._reserved[key], value))
  350. # Return the result
  351. return _semispacejoin(result)
  352. #
  353. # Pattern for finding cookie
  354. #
  355. # This used to be strict parsing based on the RFC2109 and RFC2068
  356. # specifications. I have since discovered that MSIE 3.0x doesn't
  357. # follow the character rules outlined in those specs. As a
  358. # result, the parsing rules here are less strict.
  359. #
  360. _LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
  361. _LegalValueChars = _LegalKeyChars + r'\[\]'
  362. _CookiePattern = re.compile(r"""
  363. \s* # Optional whitespace at start of cookie
  364. (?P<key> # Start of group 'key'
  365. [""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
  366. ) # End of group 'key'
  367. ( # Optional group: there may not be a value.
  368. \s*=\s* # Equal Sign
  369. (?P<val> # Start of group 'val'
  370. "(?:[^\\"]|\\.)*" # Any doublequoted string
  371. | # or
  372. \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
  373. | # or
  374. [""" + _LegalValueChars + r"""]* # Any word or empty string
  375. ) # End of group 'val'
  376. )? # End of optional value group
  377. \s* # Any number of spaces.
  378. (\s+|;|$) # Ending either at space, semicolon, or EOS.
  379. """, re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.
  380. # At long last, here is the cookie class. Using this class is almost just like
  381. # using a dictionary. See this module's docstring for example usage.
  382. #
  383. class BaseCookie(dict):
  384. """A container class for a set of Morsels."""
  385. def value_decode(self, val):
  386. """real_value, coded_value = value_decode(STRING)
  387. Called prior to setting a cookie's value from the network
  388. representation. The VALUE is the value read from HTTP
  389. header.
  390. Override this function to modify the behavior of cookies.
  391. """
  392. return val, val
  393. def value_encode(self, val):
  394. """real_value, coded_value = value_encode(VALUE)
  395. Called prior to setting a cookie's value from the dictionary
  396. representation. The VALUE is the value being assigned.
  397. Override this function to modify the behavior of cookies.
  398. """
  399. strval = str(val)
  400. return strval, strval
  401. def __init__(self, input=None):
  402. if input:
  403. self.load(input)
  404. def __set(self, key, real_value, coded_value):
  405. """Private method for setting a cookie's value"""
  406. M = self.get(key, Morsel())
  407. M.set(key, real_value, coded_value)
  408. dict.__setitem__(self, key, M)
  409. def __setitem__(self, key, value):
  410. """Dictionary style assignment."""
  411. if isinstance(value, Morsel):
  412. # allow assignment of constructed Morsels (e.g. for pickling)
  413. dict.__setitem__(self, key, value)
  414. else:
  415. rval, cval = self.value_encode(value)
  416. self.__set(key, rval, cval)
  417. def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
  418. """Return a string suitable for HTTP."""
  419. result = []
  420. items = sorted(self.items())
  421. for key, value in items:
  422. result.append(value.output(attrs, header))
  423. return sep.join(result)
  424. __str__ = output
  425. def __repr__(self):
  426. l = []
  427. items = sorted(self.items())
  428. for key, value in items:
  429. l.append('%s=%s' % (key, repr(value.value)))
  430. return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
  431. def js_output(self, attrs=None):
  432. """Return a string suitable for JavaScript."""
  433. result = []
  434. items = sorted(self.items())
  435. for key, value in items:
  436. result.append(value.js_output(attrs))
  437. return _nulljoin(result)
  438. def load(self, rawdata):
  439. """Load cookies from a string (presumably HTTP_COOKIE) or
  440. from a dictionary. Loading cookies from a dictionary 'd'
  441. is equivalent to calling:
  442. map(Cookie.__setitem__, d.keys(), d.values())
  443. """
  444. if isinstance(rawdata, str):
  445. self.__parse_string(rawdata)
  446. else:
  447. # self.update() wouldn't call our custom __setitem__
  448. for key, value in rawdata.items():
  449. self[key] = value
  450. return
  451. def __parse_string(self, str, patt=_CookiePattern):
  452. i = 0 # Our starting point
  453. n = len(str) # Length of string
  454. parsed_items = [] # Parsed (type, key, value) triples
  455. morsel_seen = False # A key=value pair was previously encountered
  456. TYPE_ATTRIBUTE = 1
  457. TYPE_KEYVALUE = 2
  458. # We first parse the whole cookie string and reject it if it's
  459. # syntactically invalid (this helps avoid some classes of injection
  460. # attacks).
  461. while 0 <= i < n:
  462. # Start looking for a cookie
  463. match = patt.match(str, i)
  464. if not match:
  465. # No more cookies
  466. break
  467. key, value = match.group("key"), match.group("val")
  468. i = match.end(0)
  469. if key[0] == "$":
  470. if not morsel_seen:
  471. # We ignore attributes which pertain to the cookie
  472. # mechanism as a whole, such as "$Version".
  473. # See RFC 2965. (Does anyone care?)
  474. continue
  475. parsed_items.append((TYPE_ATTRIBUTE, key[1:], value))
  476. elif key.lower() in Morsel._reserved:
  477. if not morsel_seen:
  478. # Invalid cookie string
  479. return
  480. if value is None:
  481. if key.lower() in Morsel._flags:
  482. parsed_items.append((TYPE_ATTRIBUTE, key, True))
  483. else:
  484. # Invalid cookie string
  485. return
  486. else:
  487. parsed_items.append((TYPE_ATTRIBUTE, key, _unquote(value)))
  488. elif value is not None:
  489. parsed_items.append((TYPE_KEYVALUE, key, self.value_decode(value)))
  490. morsel_seen = True
  491. else:
  492. # Invalid cookie string
  493. return
  494. # The cookie string is valid, apply it.
  495. M = None # current morsel
  496. for tp, key, value in parsed_items:
  497. if tp == TYPE_ATTRIBUTE:
  498. assert M is not None
  499. M[key] = value
  500. else:
  501. assert tp == TYPE_KEYVALUE
  502. rval, cval = value
  503. self.__set(key, rval, cval)
  504. M = self[key]
  505. class SimpleCookie(BaseCookie):
  506. """
  507. SimpleCookie supports strings as cookie values. When setting
  508. the value using the dictionary assignment notation, SimpleCookie
  509. calls the builtin str() to convert the value to a string. Values
  510. received from HTTP are kept as strings.
  511. """
  512. def value_decode(self, val):
  513. return _unquote(val), val
  514. def value_encode(self, val):
  515. strval = str(val)
  516. return strval, _quote(strval)