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

View File

@ -155,7 +155,7 @@ class Response(message.Message):
def _set_cookies(self, value): def _set_cookies(self, value):
cookie_headers = [] cookie_headers = []
for k, v in value: 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) cookie_headers.append(header)
self.headers.set_all("set-cookie", cookie_headers) self.headers.set_all("set-cookie", cookie_headers)