ยปCore Development>Code coverage>Lib/_MozillaCookieJar.py

Python code coverage for Lib/_MozillaCookieJar.py

#countcontent
11"""Mozilla / Netscape cookie loading / saving."""
2n/a
31import re, time
4n/a
51from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
6n/a Cookie, MISSING_FILENAME_TEXT)
7n/a
82class MozillaCookieJar(FileCookieJar):
9n/a """
10n/a
11n/a WARNING: you may want to backup your browser's cookies file if you use
12n/a this class to save cookies. I *think* it works, but there have been
13n/a bugs in the past!
14n/a
15n/a This class differs from CookieJar only in the format it uses to save and
16n/a load cookies to and from a file. This class uses the Mozilla/Netscape
17n/a `cookies.txt' format. lynx uses this file format, too.
18n/a
19n/a Don't expect cookies saved while the browser is running to be noticed by
20n/a the browser (in fact, Mozilla on unix will overwrite your saved cookies if
21n/a you change them on disk while it's running; on Windows, you probably can't
22n/a save at all while the browser is running).
23n/a
24n/a Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to
25n/a Netscape cookies on saving.
26n/a
27n/a In particular, the cookie version and port number information is lost,
28n/a together with information about whether or not Path, Port and Discard were
29n/a specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the
30n/a domain as set in the HTTP header started with a dot (yes, I'm aware some
31n/a domains in Netscape files start with a dot and some don't -- trust me, you
32n/a really don't want to know any more about this).
33n/a
34n/a Note that though Mozilla and Netscape use the same format, they use
35n/a slightly different headers. The class saves cookies using the Netscape
36n/a header by default (Mozilla can cope with that).
37n/a
381 """
391 magic_re = "#( Netscape)? HTTP Cookie File"
40n/a header = """\
41n/a # Netscape HTTP Cookie File
42n/a # http://www.netscape.com/newsref/std/cookie_spec.html
43n/a # This is a generated file! Do not edit.
44n/a
451"""
46n/a
471 def _really_load(self, f, filename, ignore_discard, ignore_expires):
484 now = time.time()
49n/a
504 magic = f.readline()
514 if not re.search(self.magic_re, magic):
521 f.close()
531 raise LoadError(
541 "%r does not look like a Netscape format cookies file" %
551 filename)
56n/a
573 try:
583 while 1:
5924 line = f.readline()
6024 if line == "": break
61n/a
62n/a # last field may be absent, so keep any trailing tab
6321 if line.endswith("\n"): line = line[:-1]
64n/a
65n/a # skip comments and blank lines XXX what is $ for?
6621 if (line.strip().startswith(("#", "$")) or
6715 line.strip() == ""):
683 continue
69n/a
70n/a domain, domain_specified, path, secure, expires, name, value = \
7112 line.split("\t")
7212 secure = (secure == "TRUE")
7312 domain_specified = (domain_specified == "TRUE")
7412 if name == "":
75n/a # cookies.txt regards 'Set-Cookie: foo' as a cookie
76n/a # with no name, whereas cookielib regards it as a
77n/a # cookie with no value.
782 name = value
792 value = None
80n/a
8112 initial_dot = domain.startswith(".")
8212 assert domain_specified == initial_dot
83n/a
8412 discard = False
8512 if expires == "":
863 expires = None
873 discard = True
88n/a
89n/a # assume path_specified is false
9012 c = Cookie(0, name, value,
9112 None, False,
9212 domain, domain_specified, initial_dot,
9312 path, False,
9412 secure,
9512 expires,
9612 discard,
9712 None,
9812 None,
9912 {})
10012 if not ignore_discard and c.discard:
1010 continue
10212 if not ignore_expires and c.is_expired(now):
1030 continue
10412 self.set_cookie(c)
105n/a
1060 except IOError:
1070 raise
1080 except Exception:
1090 _warn_unhandled_exception()
1100 raise LoadError("invalid Netscape format cookies file %r: %r" %
1110 (filename, line))
112n/a
1131 def save(self, filename=None, ignore_discard=False, ignore_expires=False):
1143 if filename is None:
1153 if self.filename is not None: filename = self.filename
1160 else: raise ValueError(MISSING_FILENAME_TEXT)
117n/a
1183 f = open(filename, "w")
1193 try:
1203 f.write(self.header)
1213 now = time.time()
12217 for cookie in self:
12314 if not ignore_discard and cookie.discard:
1242 continue
12512 if not ignore_expires and cookie.is_expired(now):
1260 continue
12712 if cookie.secure: secure = "TRUE"
12811 else: secure = "FALSE"
12912 if cookie.domain.startswith("."): initial_dot = "TRUE"
1308 else: initial_dot = "FALSE"
13112 if cookie.expires is not None:
1329 expires = str(cookie.expires)
133n/a else:
1343 expires = ""
13512 if cookie.value is None:
136n/a # cookies.txt regards 'Set-Cookie: foo' as a cookie
137n/a # with no name, whereas cookielib regards it as a
138n/a # cookie with no value.
1392 name = ""
1402 value = cookie.name
141n/a else:
14210 name = cookie.name
14310 value = cookie.value
14412 f.write(
14512 "\t".join([cookie.domain, initial_dot, cookie.path,
14612 secure, expires, name, value])+
14712 "\n")
148n/a finally:
1493 f.close()