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