| 1 | 1 | """Mozilla / Netscape cookie loading / saving.""" |
|---|
| 2 | n/a | |
|---|
| 3 | 1 | import re, time |
|---|
| 4 | n/a | |
|---|
| 5 | 1 | from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError, |
|---|
| 6 | n/a | Cookie, MISSING_FILENAME_TEXT) |
|---|
| 7 | n/a | |
|---|
| 8 | 2 | class MozillaCookieJar(FileCookieJar): |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | WARNING: you may want to backup your browser's cookies file if you use |
|---|
| 12 | n/a | this class to save cookies. I *think* it works, but there have been |
|---|
| 13 | n/a | bugs in the past! |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | This class differs from CookieJar only in the format it uses to save and |
|---|
| 16 | n/a | load cookies to and from a file. This class uses the Mozilla/Netscape |
|---|
| 17 | n/a | `cookies.txt' format. lynx uses this file format, too. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | Don't expect cookies saved while the browser is running to be noticed by |
|---|
| 20 | n/a | the browser (in fact, Mozilla on unix will overwrite your saved cookies if |
|---|
| 21 | n/a | you change them on disk while it's running; on Windows, you probably can't |
|---|
| 22 | n/a | save at all while the browser is running). |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to |
|---|
| 25 | n/a | Netscape cookies on saving. |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | In particular, the cookie version and port number information is lost, |
|---|
| 28 | n/a | together with information about whether or not Path, Port and Discard were |
|---|
| 29 | n/a | specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the |
|---|
| 30 | n/a | domain as set in the HTTP header started with a dot (yes, I'm aware some |
|---|
| 31 | n/a | domains in Netscape files start with a dot and some don't -- trust me, you |
|---|
| 32 | n/a | really don't want to know any more about this). |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | Note that though Mozilla and Netscape use the same format, they use |
|---|
| 35 | n/a | slightly different headers. The class saves cookies using the Netscape |
|---|
| 36 | n/a | header by default (Mozilla can cope with that). |
|---|
| 37 | n/a | |
|---|
| 38 | 1 | """ |
|---|
| 39 | 1 | magic_re = "#( Netscape)? HTTP Cookie File" |
|---|
| 40 | n/a | header = """\ |
|---|
| 41 | n/a | # Netscape HTTP Cookie File |
|---|
| 42 | n/a | # http://www.netscape.com/newsref/std/cookie_spec.html |
|---|
| 43 | n/a | # This is a generated file! Do not edit. |
|---|
| 44 | n/a | |
|---|
| 45 | 1 | """ |
|---|
| 46 | n/a | |
|---|
| 47 | 1 | def _really_load(self, f, filename, ignore_discard, ignore_expires): |
|---|
| 48 | 4 | now = time.time() |
|---|
| 49 | n/a | |
|---|
| 50 | 4 | magic = f.readline() |
|---|
| 51 | 4 | if not re.search(self.magic_re, magic): |
|---|
| 52 | 1 | f.close() |
|---|
| 53 | 1 | raise LoadError( |
|---|
| 54 | 1 | "%r does not look like a Netscape format cookies file" % |
|---|
| 55 | 1 | filename) |
|---|
| 56 | n/a | |
|---|
| 57 | 3 | try: |
|---|
| 58 | 3 | while 1: |
|---|
| 59 | 24 | line = f.readline() |
|---|
| 60 | 24 | if line == "": break |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | # last field may be absent, so keep any trailing tab |
|---|
| 63 | 21 | if line.endswith("\n"): line = line[:-1] |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # skip comments and blank lines XXX what is $ for? |
|---|
| 66 | 21 | if (line.strip().startswith(("#", "$")) or |
|---|
| 67 | 15 | line.strip() == ""): |
|---|
| 68 | 3 | continue |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | domain, domain_specified, path, secure, expires, name, value = \ |
|---|
| 71 | 12 | line.split("\t") |
|---|
| 72 | 12 | secure = (secure == "TRUE") |
|---|
| 73 | 12 | domain_specified = (domain_specified == "TRUE") |
|---|
| 74 | 12 | if name == "": |
|---|
| 75 | n/a | # cookies.txt regards 'Set-Cookie: foo' as a cookie |
|---|
| 76 | n/a | # with no name, whereas cookielib regards it as a |
|---|
| 77 | n/a | # cookie with no value. |
|---|
| 78 | 2 | name = value |
|---|
| 79 | 2 | value = None |
|---|
| 80 | n/a | |
|---|
| 81 | 12 | initial_dot = domain.startswith(".") |
|---|
| 82 | 12 | assert domain_specified == initial_dot |
|---|
| 83 | n/a | |
|---|
| 84 | 12 | discard = False |
|---|
| 85 | 12 | if expires == "": |
|---|
| 86 | 3 | expires = None |
|---|
| 87 | 3 | discard = True |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # assume path_specified is false |
|---|
| 90 | 12 | c = Cookie(0, name, value, |
|---|
| 91 | 12 | None, False, |
|---|
| 92 | 12 | domain, domain_specified, initial_dot, |
|---|
| 93 | 12 | path, False, |
|---|
| 94 | 12 | secure, |
|---|
| 95 | 12 | expires, |
|---|
| 96 | 12 | discard, |
|---|
| 97 | 12 | None, |
|---|
| 98 | 12 | None, |
|---|
| 99 | 12 | {}) |
|---|
| 100 | 12 | if not ignore_discard and c.discard: |
|---|
| 101 | 0 | continue |
|---|
| 102 | 12 | if not ignore_expires and c.is_expired(now): |
|---|
| 103 | 0 | continue |
|---|
| 104 | 12 | self.set_cookie(c) |
|---|
| 105 | n/a | |
|---|
| 106 | 0 | except IOError: |
|---|
| 107 | 0 | raise |
|---|
| 108 | 0 | except Exception: |
|---|
| 109 | 0 | _warn_unhandled_exception() |
|---|
| 110 | 0 | raise LoadError("invalid Netscape format cookies file %r: %r" % |
|---|
| 111 | 0 | (filename, line)) |
|---|
| 112 | n/a | |
|---|
| 113 | 1 | def save(self, filename=None, ignore_discard=False, ignore_expires=False): |
|---|
| 114 | 3 | if filename is None: |
|---|
| 115 | 3 | if self.filename is not None: filename = self.filename |
|---|
| 116 | 0 | else: raise ValueError(MISSING_FILENAME_TEXT) |
|---|
| 117 | n/a | |
|---|
| 118 | 3 | f = open(filename, "w") |
|---|
| 119 | 3 | try: |
|---|
| 120 | 3 | f.write(self.header) |
|---|
| 121 | 3 | now = time.time() |
|---|
| 122 | 17 | for cookie in self: |
|---|
| 123 | 14 | if not ignore_discard and cookie.discard: |
|---|
| 124 | 2 | continue |
|---|
| 125 | 12 | if not ignore_expires and cookie.is_expired(now): |
|---|
| 126 | 0 | continue |
|---|
| 127 | 12 | if cookie.secure: secure = "TRUE" |
|---|
| 128 | 11 | else: secure = "FALSE" |
|---|
| 129 | 12 | if cookie.domain.startswith("."): initial_dot = "TRUE" |
|---|
| 130 | 8 | else: initial_dot = "FALSE" |
|---|
| 131 | 12 | if cookie.expires is not None: |
|---|
| 132 | 9 | expires = str(cookie.expires) |
|---|
| 133 | n/a | else: |
|---|
| 134 | 3 | expires = "" |
|---|
| 135 | 12 | if cookie.value is None: |
|---|
| 136 | n/a | # cookies.txt regards 'Set-Cookie: foo' as a cookie |
|---|
| 137 | n/a | # with no name, whereas cookielib regards it as a |
|---|
| 138 | n/a | # cookie with no value. |
|---|
| 139 | 2 | name = "" |
|---|
| 140 | 2 | value = cookie.name |
|---|
| 141 | n/a | else: |
|---|
| 142 | 10 | name = cookie.name |
|---|
| 143 | 10 | value = cookie.value |
|---|
| 144 | 12 | f.write( |
|---|
| 145 | 12 | "\t".join([cookie.domain, initial_dot, cookie.path, |
|---|
| 146 | 12 | secure, expires, name, value])+ |
|---|
| 147 | 12 | "\n") |
|---|
| 148 | n/a | finally: |
|---|
| 149 | 3 | f.close() |
|---|