Modify format_set_cookie_header to take cookie list

and return a comma separated string of cookies
This commit is contained in:
Shadab Zafar 2016-09-27 21:04:52 +05:30
parent a104d65851
commit 6e6ed35e3e
2 changed files with 24 additions and 15 deletions

View File

@ -188,12 +188,12 @@ def _has_special(s):
return False
def _format_pairs(lst, specials=(), sep="; "):
def _format_pairs(pairs, specials=(), sep="; "):
"""
specials: A lower-cased list of keys that will not be quoted.
"""
vals = []
for k, v in lst:
for k, v in pairs:
if v is None:
vals.append(k)
else:
@ -256,24 +256,33 @@ def parse_set_cookie_header(line):
def parse_set_cookie_headers(headers):
ret = []
rv = []
for header in headers:
cookies = parse_set_cookie_header(header)
if cookies:
for name, value, attrs in cookies:
ret.append((name, SetCookie(value, attrs)))
return ret
rv.append((name, SetCookie(value, attrs)))
return rv
def format_set_cookie_header(name, value, attrs):
def format_set_cookie_header(set_cookies):
"""
Formats a Set-Cookie header value.
"""
rv = []
for set_cookie in set_cookies:
name, value, attrs = set_cookie
pairs = [(name, value)]
pairs.extend(
attrs.fields if hasattr(attrs, "fields") else attrs
)
return _format_set_cookie_pairs(pairs)
rv.append(_format_set_cookie_pairs(pairs))
return ", ".join(rv)
def refresh_set_cookie_header(c, delta):
@ -303,10 +312,10 @@ def refresh_set_cookie_header(c, delta):
# For now, we just ignore this.
attrs = attrs.with_delitem("expires")
ret = format_set_cookie_header(name, value, attrs)
if not ret:
rv = format_set_cookie_header([(name, value, attrs)])
if not rv:
raise ValueError("Invalid Cookie")
return ret
return rv
def get_expiration_ts(cookie_attrs):

View File

@ -155,7 +155,7 @@ class Response(message.Message):
def _set_cookies(self, value):
cookie_headers = []
for k, v in value:
header = cookies.format_set_cookie_header(k, v[0], v[1])
header = cookies.format_set_cookie_header([(k, v[0], v[1])])
cookie_headers.append(header)
self.headers.set_all("set-cookie", cookie_headers)