From 6e6ed35e3ec509ca443033c953d95a11ce1338c2 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 27 Sep 2016 21:04:52 +0530 Subject: [PATCH] Modify format_set_cookie_header to take cookie list and return a comma separated string of cookies --- netlib/http/cookies.py | 37 +++++++++++++++++++++++-------------- netlib/http/response.py | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index 02f820650..2c0fcafd4 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -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. """ - pairs = [(name, value)] - pairs.extend( - attrs.fields if hasattr(attrs, "fields") else attrs - ) - return _format_set_cookie_pairs(pairs) + + 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 + ) + + 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): diff --git a/netlib/http/response.py b/netlib/http/response.py index 385e233ae..ec19640da 100644 --- a/netlib/http/response.py +++ b/netlib/http/response.py @@ -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)