2014-11-01 02:26:27 +08:00
|
|
|
import sys
|
2017-01-07 19:11:46 +08:00
|
|
|
from http import cookies
|
2012-10-22 02:12:59 +08:00
|
|
|
|
2017-01-20 09:06:03 +08:00
|
|
|
# Cookie pickling bug is fixed in Python 3.4.3+
|
2014-11-01 02:26:27 +08:00
|
|
|
# http://bugs.python.org/issue22775
|
2017-01-20 09:06:03 +08:00
|
|
|
if sys.version_info >= (3, 4, 3):
|
2017-01-07 19:11:46 +08:00
|
|
|
SimpleCookie = cookies.SimpleCookie
|
2012-10-22 02:12:59 +08:00
|
|
|
else:
|
2017-01-07 19:11:46 +08:00
|
|
|
Morsel = cookies.Morsel
|
2012-10-22 02:12:59 +08:00
|
|
|
|
2017-01-07 19:11:46 +08:00
|
|
|
class SimpleCookie(cookies.SimpleCookie):
|
2017-01-20 09:06:03 +08:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if isinstance(value, Morsel):
|
|
|
|
# allow assignment of constructed Morsels (e.g. for pickling)
|
|
|
|
dict.__setitem__(self, key, value)
|
|
|
|
else:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__setitem__(key, value)
|
2014-11-01 02:26:27 +08:00
|
|
|
|
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
|