Example #1
0
def parse_cookie(cookie):
    if cookie == '':
        return {}
    if not isinstance(cookie, http_cookies.BaseCookie):
        try:
            c = SimpleCookie()
            c.load(cookie)
        except http_cookies.CookieError:
            # Invalid cookie
            return {}
    else:
        c = cookie
    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    try:
        if six.PY2:
            cookie = force_str(cookie)
        for chunk in cookie.split(';'):
            if '=' in chunk:
                key, val = chunk.split('=', 1)
            else:
                key, val = '', chunk
            key, val = key.strip(), val.strip()
            if key or val:
                if not cookiedict.get(key):
                    cookiedict[key] = http_cookies._unquote(val)
    except:
        pass
    return cookiedict
Example #2
0
 def safe_parse_cookie(cookie):
     """
     Return a dictionary parsed from a `Cookie:` header string.
     """
     cookiedict = {}
     if six.PY2:
         cookie = force_str(cookie)
     for chunk in cookie.split(';'):
         if '=' in chunk:
             key, val = chunk.split('=', 1)
         else:
             # Assume an empty name per
             # https://bugzilla.mozilla.org/show_bug.cgi?id=169091
             key, val = '', chunk
         key, val = key.strip(), val.strip()
         if key or val:
             # unquote using Python's algorithm.
             cookiedict[key] = http_cookies._unquote(val)
     return cookiedict
Example #3
0
File: cookie.py Project: 01-/django
def parse_cookie(cookie):
    """
    Return a dictionary parsed from a `Cookie:` header string.
    """
    cookiedict = {}
    if six.PY2:
        cookie = force_str(cookie)
    for chunk in cookie.split(str(';')):
        if str('=') in chunk:
            key, val = chunk.split(str('='), 1)
        else:
            # Assume an empty name per
            # https://bugzilla.mozilla.org/show_bug.cgi?id=169091
            key, val = str(''), chunk
        key, val = key.strip(), val.strip()
        if key or val:
            # unquote using Python's algorithm.
            cookiedict[key] = http_cookies._unquote(val)
    return cookiedict