]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.7] bpo-12144: Handle cookies with expires attribute in CookieJar.make_cookies...
authorXtreak <tir.karthi@gmail.com>
Fri, 13 Sep 2019 12:22:12 +0000 (13:22 +0100)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Fri, 13 Sep 2019 12:22:12 +0000 (15:22 +0300)
* [3.7] 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 <demianbrecht@gmail.com>
https://bugs.python.org/issue12144

Automerge-Triggered-By: @asvetlov
(cherry picked from commit bb41147)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
* Use warnings module instead of test.support.check_no_warnings

* [3.7] 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 <demianbrecht@gmail.com>
https://bugs.python.org/issue12144

Automerge-Triggered-By: @asvetlov.
(cherry picked from commit bb41147eab15a2958f4ad38261e5bf608f6ace1b)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
Lib/http/cookiejar.py
Lib/test/test_http_cookiejar.py
Misc/NEWS.d/next/Library/2019-06-08-23-26-58.bpo-12144.Z7mz-q.rst [new file with mode: 0644]

index d63544a5f52e2325a77d1f5434e4ff9bea10f6f0..1bed893b1e2666ed9a51f4b96b11d4799c2f290c 100644 (file)
@@ -1590,6 +1590,7 @@ class CookieJar:
         headers = response.info()
         rfc2965_hdrs = headers.get_all("Set-Cookie2", [])
         ns_hdrs = headers.get_all("Set-Cookie", [])
+        self._policy._now = self._now = int(time.time())
 
         rfc2965 = self._policy.rfc2965
         netscape = self._policy.netscape
@@ -1669,8 +1670,6 @@ class CookieJar:
         _debug("extract_cookies: %s", response.info())
         self._cookies_lock.acquire()
         try:
-            self._policy._now = self._now = int(time.time())
-
             for cookie in self.make_cookies(response, request):
                 if self._policy.set_ok(cookie, request):
                     _debug(" setting cookie: %s", cookie)
index 16edf34a99259f64a8dec005c58505e831d3833c..6eabeeae999d8b00a29ab29d5ce5b1b8a3704545 100644 (file)
@@ -6,6 +6,7 @@ import test.support
 import time
 import unittest
 import urllib.request
+import warnings
 
 from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
      parse_ns_headers, join_header_words, split_header_words, Cookie,
@@ -560,6 +561,16 @@ class CookieTests(unittest.TestCase):
         # if expires is in future, keep cookie...
         c = CookieJar()
         future = time2netscape(time.time()+3600)
+
+        with warnings.catch_warnings(record=True) as warns:
+            headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
+            req = urllib.request.Request("http://www.coyote.com/")
+            res = FakeResponse(headers, "http://www.coyote.com/")
+            cookies = c.make_cookies(res, req)
+            self.assertEqual(len(cookies), 1)
+            self.assertEqual(time2netscape(cookies[0].expires), future)
+            self.assertEqual(len(warns), 0)
+
         interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
                           future)
         self.assertEqual(len(c), 1)
diff --git a/Misc/NEWS.d/next/Library/2019-06-08-23-26-58.bpo-12144.Z7mz-q.rst b/Misc/NEWS.d/next/Library/2019-06-08-23-26-58.bpo-12144.Z7mz-q.rst
new file mode 100644 (file)
index 0000000..ee802f8
--- /dev/null
@@ -0,0 +1,2 @@
+Ensure cookies with ``expires`` attribute are handled in
+:meth:`CookieJar.make_cookies`.