Skip to content

Commit 44cb89a

Browse files
bpo-12144: Handle cookies with expires attribute in CookieJar.make_cookies (GH-13921)
Handle time comparison for cookies with `expires` attribute when `CookieJar.make_cookies` is called. Co-authored-by: Demian Brecht <[email protected]> https://bugs.python.org/issue12144 Automerge-Triggered-By: @asvetlov (cherry picked from commit bb41147) Co-authored-by: Xtreak <[email protected]>
1 parent b9bfe14 commit 44cb89a

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/http/cookiejar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,7 @@ def make_cookies(self, response, request):
15931593
headers = response.info()
15941594
rfc2965_hdrs = headers.get_all("Set-Cookie2", [])
15951595
ns_hdrs = headers.get_all("Set-Cookie", [])
1596+
self._policy._now = self._now = int(time.time())
15961597

15971598
rfc2965 = self._policy.rfc2965
15981599
netscape = self._policy.netscape
@@ -1672,8 +1673,6 @@ def extract_cookies(self, response, request):
16721673
_debug("extract_cookies: %s", response.info())
16731674
self._cookies_lock.acquire()
16741675
try:
1675-
self._policy._now = self._now = int(time.time())
1676-
16771676
for cookie in self.make_cookies(response, request):
16781677
if self._policy.set_ok(cookie, request):
16791678
_debug(" setting cookie: %s", cookie)

Lib/test/test_http_cookiejar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,15 @@ def test_expires(self):
585585
# if expires is in future, keep cookie...
586586
c = CookieJar()
587587
future = time2netscape(time.time()+3600)
588+
589+
with test.support.check_no_warnings(self):
590+
headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
591+
req = urllib.request.Request("http://www.coyote.com/")
592+
res = FakeResponse(headers, "http://www.coyote.com/")
593+
cookies = c.make_cookies(res, req)
594+
self.assertEqual(len(cookies), 1)
595+
self.assertEqual(time2netscape(cookies[0].expires), future)
596+
588597
interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
589598
future)
590599
self.assertEqual(len(c), 1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure cookies with ``expires`` attribute are handled in
2+
:meth:`CookieJar.make_cookies`.

0 commit comments

Comments
 (0)