From 3f86e22a07fb1bd91c70d3c487efbadb55f6b36a Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Sat, 16 Feb 2013 00:56:59 -0500 Subject: [PATCH] Make sure session cookies do not overwrite explicit request cookies Implement RequestsCookieJar.copy Use RequestsCookieJar.update when merging cookiejars --- requests/cookies.py | 6 ++++-- requests/sessions.py | 10 +++++----- requests/utils.py | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/requests/cookies.py b/requests/cookies.py index 856258c..1235711 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -307,8 +307,10 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): self._cookies_lock = threading.RLock() def copy(self): - """This is not implemented. Calling this will throw an exception.""" - raise NotImplementedError + """Return a copy of this RequestsCookieJar.""" + new_cj = RequestsCookieJar() + new_cj.update(self) + return new_cj def create_cookie(name, value, **kwargs): diff --git a/requests/sessions.py b/requests/sessions.py index c53ccfc..615075a 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -251,9 +251,10 @@ class Session(SessionRedirectMixin): if not isinstance(cookies, cookielib.CookieJar): cookies = cookiejar_from_dict(cookies) - # Bubble down session cookies. - for cookie in self.cookies: - cookies.set_cookie(cookie) + # Merge with session cookies + merged_cookies = self.cookies.copy() + merged_cookies.update(cookies) + cookies = merged_cookies # Gather clues from the surrounding environment. if self.trust_env: @@ -312,8 +313,7 @@ class Session(SessionRedirectMixin): resp = self.send(prep, **send_kwargs) # Persist cookies. - for cookie in resp.cookies: - self.cookies.set_cookie(cookie) + self.cookies.update(resp.cookies) return resp diff --git a/requests/utils.py b/requests/utils.py index bb07f83..a2d434e 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -252,8 +252,7 @@ def add_dict_to_cookiejar(cj, cookie_dict): """ cj2 = cookiejar_from_dict(cookie_dict) - for cookie in cj2: - cj.set_cookie(cookie) + cj.update(cj2) return cj -- 2.7.4