| 1 | n/a | """Load / save to libwww-perl (LWP) format files. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Actually, the format is slightly extended from that used by LWP's |
|---|
| 4 | n/a | (libwww-perl's) HTTP::Cookies, to avoid losing some RFC 2965 information |
|---|
| 5 | n/a | not recorded by LWP. |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | It uses the version string "2.0", though really there isn't an LWP Cookies |
|---|
| 8 | n/a | 2.0 format. This indicates that there is extra information in here |
|---|
| 9 | n/a | (domain_dot and # port_spec) while still being compatible with |
|---|
| 10 | n/a | libwww-perl, I hope. |
|---|
| 11 | n/a | |
|---|
| 12 | 1 | """ |
|---|
| 13 | n/a | |
|---|
| 14 | 1 | import time, re |
|---|
| 15 | 1 | from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError, |
|---|
| 16 | n/a | Cookie, MISSING_FILENAME_TEXT, |
|---|
| 17 | n/a | join_header_words, split_header_words, |
|---|
| 18 | n/a | iso2time, time2isoz) |
|---|
| 19 | n/a | |
|---|
| 20 | 1 | def lwp_cookie_str(cookie): |
|---|
| 21 | n/a | """Return string representation of Cookie in an the LWP cookie file format. |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | Actually, the format is extended a bit -- see module docstring. |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | """ |
|---|
| 26 | 8 | h = [(cookie.name, cookie.value), |
|---|
| 27 | 8 | ("path", cookie.path), |
|---|
| 28 | 8 | ("domain", cookie.domain)] |
|---|
| 29 | 8 | if cookie.port is not None: h.append(("port", cookie.port)) |
|---|
| 30 | 8 | if cookie.path_specified: h.append(("path_spec", None)) |
|---|
| 31 | 8 | if cookie.port_specified: h.append(("port_spec", None)) |
|---|
| 32 | 8 | if cookie.domain_initial_dot: h.append(("domain_dot", None)) |
|---|
| 33 | 8 | if cookie.secure: h.append(("secure", None)) |
|---|
| 34 | 8 | if cookie.expires: h.append(("expires", |
|---|
| 35 | 2 | time2isoz(float(cookie.expires)))) |
|---|
| 36 | 8 | if cookie.discard: h.append(("discard", None)) |
|---|
| 37 | 8 | if cookie.comment: h.append(("comment", cookie.comment)) |
|---|
| 38 | 8 | if cookie.comment_url: h.append(("commenturl", cookie.comment_url)) |
|---|
| 39 | n/a | |
|---|
| 40 | 8 | keys = cookie._rest.keys() |
|---|
| 41 | 8 | keys.sort() |
|---|
| 42 | 8 | for k in keys: |
|---|
| 43 | 0 | h.append((k, str(cookie._rest[k]))) |
|---|
| 44 | n/a | |
|---|
| 45 | 8 | h.append(("version", str(cookie.version))) |
|---|
| 46 | n/a | |
|---|
| 47 | 8 | return join_header_words([h]) |
|---|
| 48 | n/a | |
|---|
| 49 | 2 | class LWPCookieJar(FileCookieJar): |
|---|
| 50 | n/a | """ |
|---|
| 51 | n/a | The LWPCookieJar saves a sequence of"Set-Cookie3" lines. |
|---|
| 52 | n/a | "Set-Cookie3" is the format used by the libwww-perl libary, not known |
|---|
| 53 | n/a | to be compatible with any browser, but which is easy to read and |
|---|
| 54 | n/a | doesn't lose information about RFC 2965 cookies. |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | Additional methods |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | as_lwp_str(ignore_discard=True, ignore_expired=True) |
|---|
| 59 | n/a | |
|---|
| 60 | 1 | """ |
|---|
| 61 | n/a | |
|---|
| 62 | 1 | def as_lwp_str(self, ignore_discard=True, ignore_expires=True): |
|---|
| 63 | n/a | """Return cookies as a string of "\n"-separated "Set-Cookie3" headers. |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | ignore_discard and ignore_expires: see docstring for FileCookieJar.save |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | """ |
|---|
| 68 | 2 | now = time.time() |
|---|
| 69 | 2 | r = [] |
|---|
| 70 | 9 | for cookie in self: |
|---|
| 71 | 7 | if not ignore_discard and cookie.discard: |
|---|
| 72 | 0 | continue |
|---|
| 73 | 7 | if not ignore_expires and cookie.is_expired(now): |
|---|
| 74 | 0 | continue |
|---|
| 75 | 7 | r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie)) |
|---|
| 76 | 2 | return "\n".join(r+[""]) |
|---|
| 77 | n/a | |
|---|
| 78 | 1 | def save(self, filename=None, ignore_discard=False, ignore_expires=False): |
|---|
| 79 | 2 | if filename is None: |
|---|
| 80 | 0 | if self.filename is not None: filename = self.filename |
|---|
| 81 | 0 | else: raise ValueError(MISSING_FILENAME_TEXT) |
|---|
| 82 | n/a | |
|---|
| 83 | 2 | f = open(filename, "w") |
|---|
| 84 | 2 | try: |
|---|
| 85 | n/a | # There really isn't an LWP Cookies 2.0 format, but this indicates |
|---|
| 86 | n/a | # that there is extra information in here (domain_dot and |
|---|
| 87 | n/a | # port_spec) while still being compatible with libwww-perl, I hope. |
|---|
| 88 | 2 | f.write("#LWP-Cookies-2.0\n") |
|---|
| 89 | 2 | f.write(self.as_lwp_str(ignore_discard, ignore_expires)) |
|---|
| 90 | n/a | finally: |
|---|
| 91 | 2 | f.close() |
|---|
| 92 | n/a | |
|---|
| 93 | 1 | def _really_load(self, f, filename, ignore_discard, ignore_expires): |
|---|
| 94 | 3 | magic = f.readline() |
|---|
| 95 | 3 | if not re.search(self.magic_re, magic): |
|---|
| 96 | 1 | msg = ("%r does not look like a Set-Cookie3 (LWP) format " |
|---|
| 97 | 1 | "file" % filename) |
|---|
| 98 | 1 | raise LoadError(msg) |
|---|
| 99 | n/a | |
|---|
| 100 | 2 | now = time.time() |
|---|
| 101 | n/a | |
|---|
| 102 | 2 | header = "Set-Cookie3:" |
|---|
| 103 | 0 | boolean_attrs = ("port_spec", "path_spec", "domain_dot", |
|---|
| 104 | 2 | "secure", "discard") |
|---|
| 105 | 0 | value_attrs = ("version", |
|---|
| 106 | 0 | "port", "path", "domain", |
|---|
| 107 | 0 | "expires", |
|---|
| 108 | 2 | "comment", "commenturl") |
|---|
| 109 | n/a | |
|---|
| 110 | 2 | try: |
|---|
| 111 | 2 | while 1: |
|---|
| 112 | 9 | line = f.readline() |
|---|
| 113 | 9 | if line == "": break |
|---|
| 114 | 7 | if not line.startswith(header): |
|---|
| 115 | 0 | continue |
|---|
| 116 | 7 | line = line[len(header):].strip() |
|---|
| 117 | n/a | |
|---|
| 118 | 14 | for data in split_header_words([line]): |
|---|
| 119 | 7 | name, value = data[0] |
|---|
| 120 | 7 | standard = {} |
|---|
| 121 | 7 | rest = {} |
|---|
| 122 | 42 | for k in boolean_attrs: |
|---|
| 123 | 35 | standard[k] = False |
|---|
| 124 | 45 | for k, v in data[1:]: |
|---|
| 125 | 38 | if k is not None: |
|---|
| 126 | 38 | lc = k.lower() |
|---|
| 127 | n/a | else: |
|---|
| 128 | 0 | lc = None |
|---|
| 129 | n/a | # don't lose case distinction for unknown fields |
|---|
| 130 | 38 | if (lc in value_attrs) or (lc in boolean_attrs): |
|---|
| 131 | 38 | k = lc |
|---|
| 132 | 38 | if k in boolean_attrs: |
|---|
| 133 | 12 | if v is None: v = True |
|---|
| 134 | 12 | standard[k] = v |
|---|
| 135 | 26 | elif k in value_attrs: |
|---|
| 136 | 26 | standard[k] = v |
|---|
| 137 | n/a | else: |
|---|
| 138 | 0 | rest[k] = v |
|---|
| 139 | n/a | |
|---|
| 140 | 7 | h = standard.get |
|---|
| 141 | 7 | expires = h("expires") |
|---|
| 142 | 7 | discard = h("discard") |
|---|
| 143 | 7 | if expires is not None: |
|---|
| 144 | 2 | expires = iso2time(expires) |
|---|
| 145 | 7 | if expires is None: |
|---|
| 146 | 5 | discard = True |
|---|
| 147 | 7 | domain = h("domain") |
|---|
| 148 | 7 | domain_specified = domain.startswith(".") |
|---|
| 149 | 7 | c = Cookie(h("version"), name, value, |
|---|
| 150 | 7 | h("port"), h("port_spec"), |
|---|
| 151 | 7 | domain, domain_specified, h("domain_dot"), |
|---|
| 152 | 7 | h("path"), h("path_spec"), |
|---|
| 153 | 7 | h("secure"), |
|---|
| 154 | 7 | expires, |
|---|
| 155 | 7 | discard, |
|---|
| 156 | 7 | h("comment"), |
|---|
| 157 | 7 | h("commenturl"), |
|---|
| 158 | 7 | rest) |
|---|
| 159 | 7 | if not ignore_discard and c.discard: |
|---|
| 160 | 0 | continue |
|---|
| 161 | 7 | if not ignore_expires and c.is_expired(now): |
|---|
| 162 | 0 | continue |
|---|
| 163 | 7 | self.set_cookie(c) |
|---|
| 164 | n/a | |
|---|
| 165 | 0 | except IOError: |
|---|
| 166 | 0 | raise |
|---|
| 167 | 0 | except Exception: |
|---|
| 168 | 0 | _warn_unhandled_exception() |
|---|
| 169 | 0 | raise LoadError("invalid Set-Cookie3 format file %r: %r" % |
|---|
| 170 | 0 | (filename, line)) |
|---|