From: Chase Sterling Date: Sat, 16 Feb 2013 05:56:59 +0000 (-0500) Subject: Make sure session cookies do not overwrite explicit request cookies X-Git-Tag: v1.2.0~40^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3f86e22a07fb1bd91c70d3c487efbadb55f6b36a;p=services%2Fpython-requests.git Make sure session cookies do not overwrite explicit request cookies Implement RequestsCookieJar.copy Use RequestsCookieJar.update when merging cookiejars --- 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