Skip to content

Commit b961f6b

Browse files
author
Rémi Lapeyre
committed
Make MozillaCookieJar use curl's format for session cookies
1 parent f289084 commit b961f6b

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,8 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
20332033
assert domain_specified == initial_dot
20342034

20352035
discard = False
2036-
if expires == "":
2036+
# curl and Wget set expires to 0 for session cookies.
2037+
if expires == "0" or expires == "":
20372038
expires = None
20382039
discard = True
20392040

@@ -2081,7 +2082,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20812082
if cookie.expires is not None:
20822083
expires = str(cookie.expires)
20832084
else:
2084-
expires = ""
2085+
expires = "0"
20852086
if cookie.value is None:
20862087
# cookies.txt regards 'Set-Cookie: foo' as a cookie
20872088
# with no name, whereas http.cookiejar regards it as a

Lib/test/test_http_cookiejar.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,31 @@ def save_and_restore(cj, ignore_discard):
16891689
self.assertEqual(len(new_c), 4) # 2 of them discarded on save
16901690
self.assertIn("name='foo1', value='bar'", repr(new_c))
16911691

1692+
# Check compatibility with curl / wget cookiejar format.
1693+
# See issue 17164
1694+
try:
1695+
expires = int(time.time() + 3600)
1696+
with open(filename, "w") as f:
1697+
f.write(MozillaCookieJar.header)
1698+
f.write("www.foo.com\tFALSE\t/\tFALSE\t%u\tfoo1\tbar\n" %
1699+
expires)
1700+
f.write("www.foo.com\tFALSE\t/\tFALSE\t0\tfoo2\tbar\n")
1701+
f.write("www.foo.com\tFALSE\t/\tFALSE\t\tfoo3\tbar\n")
1702+
c = MozillaCookieJar()
1703+
c.revert(filename)
1704+
self.assertEqual(len(c), 1)
1705+
c.revert(filename, ignore_discard = True)
1706+
self.assertEqual(len(c), 3)
1707+
c.save(filename, ignore_discard = True)
1708+
with open(filename, "r") as f:
1709+
for line in f:
1710+
if line == '\n' or line.startswith('#'):
1711+
continue
1712+
self.assertRegex(line.split('\t')[4], '^\d+$')
1713+
finally:
1714+
try: os.unlink(filename)
1715+
except OSError: pass
1716+
16921717
def test_netscape_misc(self):
16931718
# Some additional Netscape cookies tests.
16941719
c = CookieJar()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`MozillaCookieJar` is now compatible with curl and wget cookiejar file
2+
format. Contributed by Jérémie Detrey.

0 commit comments

Comments
 (0)