blob: 35ac2dc6ae280c5ff5adc817af1fbc9620041105 [file] [log] [blame]
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00001####
2# Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>
Tim Peters88869f92001-01-14 23:36:06 +00003#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00004# All Rights Reserved
Tim Peters88869f92001-01-14 23:36:06 +00005#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00006# 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
Tim Peters88869f92001-01-14 23:36:06 +000013# prior permission.
14#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000015# 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
Tim Peters88869f92001-01-14 23:36:06 +000022# PERFORMANCE OF THIS SOFTWARE.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000023#
24####
Tim Peters88869f92001-01-14 23:36:06 +000025#
26# Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000027# 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
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +000034# Dave Mitchell (davem@magnet.com) in 1995, when he released the
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000035# first version of nscookie.py.
36#
37####
38
Guido van Rossum58b6f5b2001-04-06 19:39:11 +000039r"""
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000040Here's a sample session to show how to use this module.
41At the moment, this is the only documentation.
42
43The Basics
44----------
45
Georg Brandl76e155a2010-07-31 21:04:00 +000046Importing is easy...
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000047
Georg Brandl24420152008-05-26 16:32:26 +000048 >>> from http import cookies
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000049
Georg Brandl61013952008-05-28 15:56:30 +000050Most of the time you start by creating a cookie.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000051
Georg Brandl24420152008-05-26 16:32:26 +000052 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000053
54Once you've created your Cookie, you can add values just as if it were
55a dictionary.
56
Georg Brandl61013952008-05-28 15:56:30 +000057 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000058 >>> C["fig"] = "newton"
59 >>> C["sugar"] = "wafer"
Georg Brandl532efab2005-08-24 22:34:21 +000060 >>> C.output()
61 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000062
63Notice that the printable representation of a Cookie is the
64appropriate format for a Set-Cookie: header. This is the
65default behavior. You can change the header and printed
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000066attributes by using the .output() function
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000067
Georg Brandl61013952008-05-28 15:56:30 +000068 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000069 >>> C["rocky"] = "road"
70 >>> C["rocky"]["path"] = "/cookie"
Guido van Rossumfff80df2007-02-09 20:33:44 +000071 >>> print(C.output(header="Cookie:"))
Georg Brandl532efab2005-08-24 22:34:21 +000072 Cookie: rocky=road; Path=/cookie
Guido van Rossumfff80df2007-02-09 20:33:44 +000073 >>> print(C.output(attrs=[], header="Cookie:"))
Georg Brandl532efab2005-08-24 22:34:21 +000074 Cookie: rocky=road
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000075
76The load() method of a Cookie extracts cookies from a string. In a
77CGI script, you would use this method to extract the cookies from the
78HTTP_COOKIE environment variable.
79
Georg Brandl61013952008-05-28 15:56:30 +000080 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000081 >>> C.load("chips=ahoy; vienna=finger")
Georg Brandl532efab2005-08-24 22:34:21 +000082 >>> C.output()
83 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000084
85The load() method is darn-tootin smart about identifying cookies
86within a string. Escaped quotation marks, nested semicolons, and other
87such trickeries do not confuse it.
88
Georg Brandl61013952008-05-28 15:56:30 +000089 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000090 >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
Guido van Rossumfff80df2007-02-09 20:33:44 +000091 >>> print(C)
Georg Brandl532efab2005-08-24 22:34:21 +000092 Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000093
94Each element of the Cookie also supports all of the RFC 2109
95Cookie attributes. Here's an example which sets the Path
96attribute.
97
Georg Brandl61013952008-05-28 15:56:30 +000098 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000099 >>> C["oreo"] = "doublestuff"
100 >>> C["oreo"]["path"] = "/"
Guido van Rossumfff80df2007-02-09 20:33:44 +0000101 >>> print(C)
Georg Brandl532efab2005-08-24 22:34:21 +0000102 Set-Cookie: oreo=doublestuff; Path=/
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000103
104Each dictionary element has a 'value' attribute, which gives you
Tim Peters88869f92001-01-14 23:36:06 +0000105back the value associated with the key.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000106
Georg Brandl61013952008-05-28 15:56:30 +0000107 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000108 >>> C["twix"] = "none for you"
109 >>> C["twix"].value
110 'none for you'
111
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000112The SimpleCookie expects that all values should be standard strings.
113Just to be sure, SimpleCookie invokes the str() builtin to convert
114the value to a string, when the values are set dictionary-style.
115
Georg Brandl24420152008-05-26 16:32:26 +0000116 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000117 >>> C["number"] = 7
118 >>> C["string"] = "seven"
119 >>> C["number"].value
120 '7'
121 >>> C["string"].value
122 'seven'
Georg Brandl532efab2005-08-24 22:34:21 +0000123 >>> C.output()
124 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000125
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000126Finis.
Georg Brandl76e155a2010-07-31 21:04:00 +0000127"""
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000128
129#
130# Import our required modules
Tim Peters88869f92001-01-14 23:36:06 +0000131#
Georg Brandl76e155a2010-07-31 21:04:00 +0000132import re
Martin v. Löwis02d893c2001-08-02 07:15:29 +0000133import string
Batuhan Taşkaya03615562020-04-10 17:46:36 +0300134import types
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000135
Georg Brandl61013952008-05-28 15:56:30 +0000136__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000137
Fred Draked451ec12002-04-26 02:29:55 +0000138_nulljoin = ''.join
Georg Brandl532efab2005-08-24 22:34:21 +0000139_semispacejoin = '; '.join
Georg Brandl8246c432005-08-25 07:32:42 +0000140_spacejoin = ' '.join
Fred Draked451ec12002-04-26 02:29:55 +0000141
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000142#
143# Define an exception visible to External modules
144#
145class CookieError(Exception):
146 pass
147
148
149# These quoting routines conform to the RFC2109 specification, which in
150# turn references the character definitions from RFC2068. They provide
151# a two-way quoting algorithm. Any non-text character is translated
152# into a 4 character sequence: a forward-slash followed by the
153# three-digit octal equivalent of the character. Any '\' or '"' is
Martin Panter46f50722016-05-26 05:35:26 +0000154# quoted with a preceding '\' slash.
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200155# Because of the way browsers really handle cookies (as opposed to what
156# the RFC says) we also encode "," and ";".
Tim Peters88869f92001-01-14 23:36:06 +0000157#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000158# These are taken from RFC2068 and RFC2109.
159# _LegalChars is the list of chars which don't require "'s
160# _Translator hash-table for fast quoting
161#
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200162_LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
163_UnescapedChars = _LegalChars + ' ()/<=>?@[]{}'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000164
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200165_Translator = {n: '\\%03o' % n
166 for n in set(range(256)) - set(map(ord, _UnescapedChars))}
167_Translator.update({
168 ord('"'): '\\"',
169 ord('\\'): '\\\\',
170})
R. David Murraye05ca2a2010-12-28 18:54:13 +0000171
Anish Shah102d8132016-02-07 05:36:00 +0500172_is_legal_key = re.compile('[%s]+' % re.escape(_LegalChars)).fullmatch
R. David Murraye05ca2a2010-12-28 18:54:13 +0000173
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200174def _quote(str):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000175 r"""Quote a string for use in a cookie header.
176
177 If the string does not need to be double-quoted, then just return the
178 string. Otherwise, surround the string in doublequotes and quote
179 (with a \) special characters.
180 """
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200181 if str is None or _is_legal_key(str):
Fred Drakeff5364a2000-08-24 14:40:35 +0000182 return str
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000183 else:
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200184 return '"' + str.translate(_Translator) + '"'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000185
186
187_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
188_QuotePatt = re.compile(r"[\\].")
189
Fred Draked451ec12002-04-26 02:29:55 +0000190def _unquote(str):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000191 # If there aren't any doublequotes,
192 # then there can't be any special characters. See RFC 2109.
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200193 if str is None or len(str) < 2:
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000194 return str
195 if str[0] != '"' or str[-1] != '"':
196 return str
197
198 # We have to assume that we must decode this string.
199 # Down to work.
200
201 # Remove the "s
202 str = str[1:-1]
Fred Drakeff5364a2000-08-24 14:40:35 +0000203
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000204 # Check for special sequences. Examples:
205 # \012 --> \n
206 # \" --> "
207 #
208 i = 0
209 n = len(str)
210 res = []
211 while 0 <= i < n:
Georg Brandl76e155a2010-07-31 21:04:00 +0000212 o_match = _OctalPatt.search(str, i)
213 q_match = _QuotePatt.search(str, i)
214 if not o_match and not q_match: # Neither matched
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000215 res.append(str[i:])
216 break
217 # else:
218 j = k = -1
Georg Brandl76e155a2010-07-31 21:04:00 +0000219 if o_match:
220 j = o_match.start(0)
221 if q_match:
222 k = q_match.start(0)
223 if q_match and (not o_match or k < j): # QuotePatt matched
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000224 res.append(str[i:k])
225 res.append(str[k+1])
Georg Brandl76e155a2010-07-31 21:04:00 +0000226 i = k + 2
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000227 else: # OctalPatt matched
228 res.append(str[i:j])
Georg Brandl76e155a2010-07-31 21:04:00 +0000229 res.append(chr(int(str[j+1:j+4], 8)))
230 i = j + 4
Fred Draked451ec12002-04-26 02:29:55 +0000231 return _nulljoin(res)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000232
Georg Brandl76e155a2010-07-31 21:04:00 +0000233# The _getdate() routine is used to set the expiration time in the cookie's HTTP
234# header. By default, _getdate() returns the current time in the appropriate
235# "expires" format for a Set-Cookie header. The one optional argument is an
236# offset from now, in seconds. For example, an offset of -3600 means "one hour
237# ago". The offset may be a floating point number.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000238#
239
240_weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
241
242_monthname = [None,
243 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
244 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
245
246def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
247 from time import gmtime, time
248 now = time()
249 year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
Senthil Kumaran00c2ec22012-05-20 12:05:16 +0800250 return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000251 (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
252
253
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000254class Morsel(dict):
Georg Brandl76e155a2010-07-31 21:04:00 +0000255 """A class to hold ONE (key, value) pair.
Georg Brandl9cf32a12009-09-04 08:28:01 +0000256
257 In a cookie, each such pair may have several attributes, so this class is
258 used to keep the attributes associated with the appropriate key,value pair.
259 This class also includes a coded_value attribute, which is used to hold
Alex Gaynorafbbac12018-04-15 17:23:47 -0400260 the network representation of the value.
Georg Brandl9cf32a12009-09-04 08:28:01 +0000261 """
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000262 # RFC 2109 lists these attributes as reserved:
263 # path comment domain
264 # max-age secure version
Tim Peters88869f92001-01-14 23:36:06 +0000265 #
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000266 # For historical reasons, these attributes are also reserved:
267 # expires
268 #
Benjamin Peterson35e661c2008-09-06 19:37:35 +0000269 # This is an extension from Microsoft:
270 # httponly
271 #
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000272 # This dictionary provides a mapping from the lowercase
273 # variant on the left to the appropriate traditional
274 # formatting on the right.
Georg Brandl76e155a2010-07-31 21:04:00 +0000275 _reserved = {
276 "expires" : "expires",
277 "path" : "Path",
278 "comment" : "Comment",
279 "domain" : "Domain",
280 "max-age" : "Max-Age",
Benjamin Petersonbd341622015-01-16 20:43:55 -0500281 "secure" : "Secure",
282 "httponly" : "HttpOnly",
Georg Brandl76e155a2010-07-31 21:04:00 +0000283 "version" : "Version",
Alex Gaynorc87eb092018-04-07 16:09:42 -0400284 "samesite" : "SameSite",
Georg Brandl76e155a2010-07-31 21:04:00 +0000285 }
Fred Drakeff5364a2000-08-24 14:40:35 +0000286
R David Murraycd0f74b2013-08-25 11:09:02 -0400287 _flags = {'secure', 'httponly'}
288
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000289 def __init__(self):
290 # Set defaults
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200291 self._key = self._value = self._coded_value = None
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000292
293 # Set default attributes
Georg Brandl76e155a2010-07-31 21:04:00 +0000294 for key in self._reserved:
295 dict.__setitem__(self, key, "")
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000296
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200297 @property
298 def key(self):
299 return self._key
300
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200301 @property
302 def value(self):
303 return self._value
304
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200305 @property
306 def coded_value(self):
307 return self._coded_value
308
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000309 def __setitem__(self, K, V):
Fred Draked451ec12002-04-26 02:29:55 +0000310 K = K.lower()
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000311 if not K in self._reserved:
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200312 raise CookieError("Invalid attribute %r" % (K,))
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000313 dict.__setitem__(self, K, V)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000314
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200315 def setdefault(self, key, val=None):
316 key = key.lower()
317 if key not in self._reserved:
318 raise CookieError("Invalid attribute %r" % (key,))
319 return dict.setdefault(self, key, val)
320
321 def __eq__(self, morsel):
322 if not isinstance(morsel, Morsel):
323 return NotImplemented
324 return (dict.__eq__(self, morsel) and
325 self._value == morsel._value and
326 self._key == morsel._key and
327 self._coded_value == morsel._coded_value)
328
329 __ne__ = object.__ne__
330
331 def copy(self):
332 morsel = Morsel()
333 dict.update(morsel, self)
334 morsel.__dict__.update(self.__dict__)
335 return morsel
336
337 def update(self, values):
338 data = {}
339 for key, val in dict(values).items():
340 key = key.lower()
341 if key not in self._reserved:
342 raise CookieError("Invalid attribute %r" % (key,))
343 data[key] = val
344 dict.update(self, data)
345
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000346 def isReservedKey(self, K):
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000347 return K.lower() in self._reserved
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000348
Serhiy Storchakacc283372017-01-13 09:23:15 +0200349 def set(self, key, val, coded_val):
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000350 if key.lower() in self._reserved:
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200351 raise CookieError('Attempt to set a reserved key %r' % (key,))
352 if not _is_legal_key(key):
353 raise CookieError('Illegal key %r' % (key,))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000354
355 # It's a good key, so save it.
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200356 self._key = key
357 self._value = val
358 self._coded_value = coded_val
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000359
Serhiy Storchaka6c325852015-03-18 18:03:40 +0200360 def __getstate__(self):
361 return {
362 'key': self._key,
363 'value': self._value,
364 'coded_value': self._coded_value,
365 }
366
367 def __setstate__(self, state):
368 self._key = state['key']
369 self._value = state['value']
370 self._coded_value = state['coded_value']
371
Georg Brandl76e155a2010-07-31 21:04:00 +0000372 def output(self, attrs=None, header="Set-Cookie:"):
373 return "%s %s" % (header, self.OutputString(attrs))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000374
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000375 __str__ = output
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000376
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000377 def __repr__(self):
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200378 return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
Fred Drakeff5364a2000-08-24 14:40:35 +0000379
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000380 def js_output(self, attrs=None):
381 # Print javascript
382 return """
Georg Brandl03a33ea2005-06-26 21:02:49 +0000383 <script type="text/javascript">
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000384 <!-- begin hiding
Georg Brandl03a33ea2005-06-26 21:02:49 +0000385 document.cookie = \"%s\";
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000386 // end hiding -->
387 </script>
Georg Brandl76e155a2010-07-31 21:04:00 +0000388 """ % (self.OutputString(attrs).replace('"', r'\"'))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000389
390 def OutputString(self, attrs=None):
391 # Build up our result
392 #
393 result = []
Georg Brandl76e155a2010-07-31 21:04:00 +0000394 append = result.append
Fred Drakeff5364a2000-08-24 14:40:35 +0000395
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000396 # First, the key=value pair
Georg Brandl76e155a2010-07-31 21:04:00 +0000397 append("%s=%s" % (self.key, self.coded_value))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000398
399 # Now add any defined attributes
Fred Drake8152d322000-12-12 23:20:45 +0000400 if attrs is None:
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000401 attrs = self._reserved
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000402 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000403 for key, value in items:
404 if value == "":
405 continue
406 if key not in attrs:
407 continue
408 if key == "expires" and isinstance(value, int):
409 append("%s=%s" % (self._reserved[key], _getdate(value)))
410 elif key == "max-age" and isinstance(value, int):
411 append("%s=%d" % (self._reserved[key], value))
Berker Peksagd5a23772018-04-23 02:48:11 +0300412 elif key == "comment" and isinstance(value, str):
413 append("%s=%s" % (self._reserved[key], _quote(value)))
Serhiy Storchaka9c1a9b22015-03-18 10:59:57 +0200414 elif key in self._flags:
415 if value:
416 append(str(self._reserved[key]))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000417 else:
Georg Brandl76e155a2010-07-31 21:04:00 +0000418 append("%s=%s" % (self._reserved[key], value))
Fred Drakeff5364a2000-08-24 14:40:35 +0000419
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000420 # Return the result
Georg Brandl532efab2005-08-24 22:34:21 +0000421 return _semispacejoin(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000422
Batuhan Taşkaya03615562020-04-10 17:46:36 +0300423 __class_getitem__ = classmethod(types.GenericAlias)
424
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000425
426#
427# Pattern for finding cookie
428#
429# This used to be strict parsing based on the RFC2109 and RFC2068
430# specifications. I have since discovered that MSIE 3.0x doesn't
431# follow the character rules outlined in those specs. As a
432# result, the parsing rules here are less strict.
433#
434
Benjamin Peterson9bd476e2015-05-23 10:36:48 -0500435_LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
R David Murray44b548d2016-09-08 13:59:53 -0400436_LegalValueChars = _LegalKeyChars + r'\[\]'
Georg Brandl76e155a2010-07-31 21:04:00 +0000437_CookiePattern = re.compile(r"""
Antoine Pitrou7d0b8f92014-09-17 00:23:55 +0200438 \s* # Optional whitespace at start of cookie
Georg Brandl76e155a2010-07-31 21:04:00 +0000439 (?P<key> # Start of group 'key'
Benjamin Peterson9bd476e2015-05-23 10:36:48 -0500440 [""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
Georg Brandl76e155a2010-07-31 21:04:00 +0000441 ) # End of group 'key'
R David Murraycd0f74b2013-08-25 11:09:02 -0400442 ( # Optional group: there may not be a value.
443 \s*=\s* # Equal Sign
444 (?P<val> # Start of group 'val'
445 "(?:[^\\"]|\\.)*" # Any doublequoted string
446 | # or
Senthil Kumaranaeeba262012-05-20 16:58:30 +0800447 \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
R David Murraycd0f74b2013-08-25 11:09:02 -0400448 | # or
Benjamin Petersond504f202015-05-23 10:38:48 -0500449 [""" + _LegalValueChars + r"""]* # Any word or empty string
R David Murraycd0f74b2013-08-25 11:09:02 -0400450 ) # End of group 'val'
451 )? # End of optional value group
452 \s* # Any number of spaces.
453 (\s+|;|$) # Ending either at space, semicolon, or EOS.
Serhiy Storchakabd48d272016-09-11 12:50:02 +0300454 """, re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000455
456
Georg Brandl76e155a2010-07-31 21:04:00 +0000457# At long last, here is the cookie class. Using this class is almost just like
458# using a dictionary. See this module's docstring for example usage.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000459#
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000460class BaseCookie(dict):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000461 """A container class for a set of Morsels."""
Fred Drakeff5364a2000-08-24 14:40:35 +0000462
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000463 def value_decode(self, val):
464 """real_value, coded_value = value_decode(STRING)
465 Called prior to setting a cookie's value from the network
466 representation. The VALUE is the value read from HTTP
467 header.
468 Override this function to modify the behavior of cookies.
469 """
470 return val, val
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000471
472 def value_encode(self, val):
473 """real_value, coded_value = value_encode(VALUE)
474 Called prior to setting a cookie's value from the dictionary
475 representation. The VALUE is the value being assigned.
476 Override this function to modify the behavior of cookies.
477 """
478 strval = str(val)
479 return strval, strval
Fred Drakeff5364a2000-08-24 14:40:35 +0000480
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000481 def __init__(self, input=None):
Georg Brandl76e155a2010-07-31 21:04:00 +0000482 if input:
483 self.load(input)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000484
485 def __set(self, key, real_value, coded_value):
486 """Private method for setting a cookie's value"""
487 M = self.get(key, Morsel())
488 M.set(key, real_value, coded_value)
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000489 dict.__setitem__(self, key, M)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000490
491 def __setitem__(self, key, value):
492 """Dictionary style assignment."""
Serhiy Storchaka8cf7c1c2014-11-02 22:18:25 +0200493 if isinstance(value, Morsel):
494 # allow assignment of constructed Morsels (e.g. for pickling)
495 dict.__setitem__(self, key, value)
496 else:
497 rval, cval = self.value_encode(value)
498 self.__set(key, rval, cval)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000499
Georg Brandl532efab2005-08-24 22:34:21 +0000500 def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000501 """Return a string suitable for HTTP."""
502 result = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000503 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000504 for key, value in items:
505 result.append(value.output(attrs, header))
Fred Draked451ec12002-04-26 02:29:55 +0000506 return sep.join(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000507
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000508 __str__ = output
509
510 def __repr__(self):
Georg Brandl76e155a2010-07-31 21:04:00 +0000511 l = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000512 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000513 for key, value in items:
514 l.append('%s=%s' % (key, repr(value.value)))
515 return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
Fred Drakeff5364a2000-08-24 14:40:35 +0000516
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000517 def js_output(self, attrs=None):
518 """Return a string suitable for JavaScript."""
519 result = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000520 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000521 for key, value in items:
522 result.append(value.js_output(attrs))
Fred Draked451ec12002-04-26 02:29:55 +0000523 return _nulljoin(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000524
525 def load(self, rawdata):
526 """Load cookies from a string (presumably HTTP_COOKIE) or
527 from a dictionary. Loading cookies from a dictionary 'd'
528 is equivalent to calling:
529 map(Cookie.__setitem__, d.keys(), d.values())
530 """
Georg Brandl76e155a2010-07-31 21:04:00 +0000531 if isinstance(rawdata, str):
532 self.__parse_string(rawdata)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000533 else:
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000534 # self.update() wouldn't call our custom __setitem__
Georg Brandl76e155a2010-07-31 21:04:00 +0000535 for key, value in rawdata.items():
536 self[key] = value
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000537 return
Fred Drakeff5364a2000-08-24 14:40:35 +0000538
Georg Brandl76e155a2010-07-31 21:04:00 +0000539 def __parse_string(self, str, patt=_CookiePattern):
Antoine Pitroub1e36072014-11-21 01:20:57 +0100540 i = 0 # Our starting point
541 n = len(str) # Length of string
542 parsed_items = [] # Parsed (type, key, value) triples
543 morsel_seen = False # A key=value pair was previously encountered
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000544
Antoine Pitroub1e36072014-11-21 01:20:57 +0100545 TYPE_ATTRIBUTE = 1
546 TYPE_KEYVALUE = 2
547
548 # We first parse the whole cookie string and reject it if it's
549 # syntactically invalid (this helps avoid some classes of injection
550 # attacks).
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000551 while 0 <= i < n:
552 # Start looking for a cookie
Antoine Pitrou7d0b8f92014-09-17 00:23:55 +0200553 match = patt.match(str, i)
Georg Brandl76e155a2010-07-31 21:04:00 +0000554 if not match:
555 # No more cookies
556 break
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000557
Georg Brandl76e155a2010-07-31 21:04:00 +0000558 key, value = match.group("key"), match.group("val")
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000559 i = match.end(0)
560
Georg Brandl76e155a2010-07-31 21:04:00 +0000561 if key[0] == "$":
Antoine Pitroub1e36072014-11-21 01:20:57 +0100562 if not morsel_seen:
563 # We ignore attributes which pertain to the cookie
564 # mechanism as a whole, such as "$Version".
565 # See RFC 2965. (Does anyone care?)
566 continue
567 parsed_items.append((TYPE_ATTRIBUTE, key[1:], value))
Georg Brandl76e155a2010-07-31 21:04:00 +0000568 elif key.lower() in Morsel._reserved:
Antoine Pitroub1e36072014-11-21 01:20:57 +0100569 if not morsel_seen:
570 # Invalid cookie string
571 return
572 if value is None:
573 if key.lower() in Morsel._flags:
574 parsed_items.append((TYPE_ATTRIBUTE, key, True))
R David Murraycd0f74b2013-08-25 11:09:02 -0400575 else:
Antoine Pitroub1e36072014-11-21 01:20:57 +0100576 # Invalid cookie string
577 return
578 else:
579 parsed_items.append((TYPE_ATTRIBUTE, key, _unquote(value)))
R David Murraycd0f74b2013-08-25 11:09:02 -0400580 elif value is not None:
Antoine Pitroub1e36072014-11-21 01:20:57 +0100581 parsed_items.append((TYPE_KEYVALUE, key, self.value_decode(value)))
582 morsel_seen = True
583 else:
584 # Invalid cookie string
585 return
586
587 # The cookie string is valid, apply it.
588 M = None # current morsel
589 for tp, key, value in parsed_items:
590 if tp == TYPE_ATTRIBUTE:
591 assert M is not None
592 M[key] = value
593 else:
594 assert tp == TYPE_KEYVALUE
595 rval, cval = value
Georg Brandl76e155a2010-07-31 21:04:00 +0000596 self.__set(key, rval, cval)
597 M = self[key]
Georg Brandl4eff9f72009-09-04 08:22:00 +0000598
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000599
600class SimpleCookie(BaseCookie):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000601 """
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000602 SimpleCookie supports strings as cookie values. When setting
603 the value using the dictionary assignment notation, SimpleCookie
604 calls the builtin str() to convert the value to a string. Values
605 received from HTTP are kept as strings.
606 """
607 def value_decode(self, val):
Georg Brandl76e155a2010-07-31 21:04:00 +0000608 return _unquote(val), val
609
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000610 def value_encode(self, val):
611 strval = str(val)
Georg Brandl76e155a2010-07-31 21:04:00 +0000612 return strval, _quote(strval)