2017-01-07 19:11:46 +08:00
|
|
|
from http import cookies
|
2012-10-22 02:12:59 +08:00
|
|
|
|
2017-02-18 08:45:34 +08:00
|
|
|
# For backwards compatibility in Django 2.1.
|
|
|
|
SimpleCookie = cookies.SimpleCookie
|
2014-11-01 02:26:27 +08:00
|
|
|
|
2018-04-14 08:58:31 +08:00
|
|
|
# Add support for the SameSite attribute (obsolete when PY37 is unsupported).
|
|
|
|
cookies.Morsel._reserved.setdefault('samesite', 'SameSite')
|
|
|
|
|
2012-10-22 02:12:59 +08:00
|
|
|
|
|
|
|
def parse_cookie(cookie):
|
2016-03-12 10:36:08 +08:00
|
|
|
"""
|
|
|
|
Return a dictionary parsed from a `Cookie:` header string.
|
|
|
|
"""
|
2012-10-22 02:12:59 +08:00
|
|
|
cookiedict = {}
|
2017-01-20 17:20:53 +08:00
|
|
|
for chunk in cookie.split(';'):
|
|
|
|
if '=' in chunk:
|
|
|
|
key, val = chunk.split('=', 1)
|
2016-03-12 10:36:08 +08:00
|
|
|
else:
|
|
|
|
# Assume an empty name per
|
|
|
|
# https://bugzilla.mozilla.org/show_bug.cgi?id=169091
|
2017-01-20 17:20:53 +08:00
|
|
|
key, val = '', chunk
|
2016-03-12 10:36:08 +08:00
|
|
|
key, val = key.strip(), val.strip()
|
|
|
|
if key or val:
|
|
|
|
# unquote using Python's algorithm.
|
2017-01-07 19:11:46 +08:00
|
|
|
cookiedict[key] = cookies._unquote(val)
|
2012-10-22 02:12:59 +08:00
|
|
|
return cookiedict
|