| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import re |
| 4 |
import time |
| 5 |
from .polyglot import ( |
| 6 |
Cookie as _Cookie, CookieJar as CJ, MozillaCookieJar as MCJ, request_host |
| 7 |
as request_host_lc, DEFAULT_HTTP_PORT, CookiePolicy, DefaultCookiePolicy, |
| 8 |
FileCookieJar, LoadError, LWPCookieJar, _debug, domain_match, |
| 9 |
eff_request_host, escape_path, is_HDN, lwp_cookie_str, reach, request_path, |
| 10 |
request_port, user_domain_match, iteritems) |
| 11 |
|
| 12 |
__all__ = [ |
| 13 |
'DEFAULT_HTTP_PORT', 'CookiePolicy', 'DefaultCookiePolicy', |
| 14 |
'request_host_lc', 'MozillaCookieJar', 'escape_path', 'is_HDN', |
| 15 |
'request_port', 'LWPCookieJar', 'LoadError', 'reach', 'FileCookieJar', |
| 16 |
'lwp_cookie_str', 'domain_match', 'request_path', 'user_domain_match' |
| 17 |
] |
| 18 |
|
| 19 |
|
| 20 |
def effective_request_host(request): |
| 21 |
"""Return the effective request-host, as defined by RFC 2965.""" |
| 22 |
return eff_request_host(request)[1] |
| 23 |
|
| 24 |
|
| 25 |
def request_is_unverifiable(request): |
| 26 |
try: |
| 27 |
return request.is_unverifiable() |
| 28 |
except AttributeError: |
| 29 |
if hasattr(request, "unverifiable"): |
| 30 |
return request.unverifiable |
| 31 |
else: |
| 32 |
raise |
| 33 |
|
| 34 |
|
| 35 |
def cookies_equal(a, b): |
| 36 |
return all(getattr(a, x) == getattr(b, x) for x in Cookie._attrs) |
| 37 |
|
| 38 |
|
| 39 |
class Cookie(_Cookie): |
| 40 |
_attrs = ("version", "name", "value", "port", "port_specified", "domain", |
| 41 |
"domain_specified", "domain_initial_dot", "path", |
| 42 |
"path_specified", "secure", "expires", "discard", "comment", |
| 43 |
"comment_url", "rfc2109", "_rest") |
| 44 |
|
| 45 |
def __eq__(self, other): |
| 46 |
return all(getattr(self, a) == getattr(other, a) for a in self._attrs) |
| 47 |
|
| 48 |
def __ne__(self, other): |
| 49 |
return not (self == other) |
| 50 |
|
| 51 |
|
| 52 |
class CookieJar(CJ): |
| 53 |
|
| 54 |
def __getstate__(self): |
| 55 |
ans = self.__dict__.copy() |
| 56 |
del ans['_cookies_lock'] |
| 57 |
return ans |
| 58 |
|
| 59 |
def __setstate__(self, val): |
| 60 |
for k, v in iteritems(val): |
| 61 |
setattr(self, k, v) |
| 62 |
|
| 63 |
def cookies_for_request(self, request): |
| 64 |
"""Return a list of cookies to be returned to server. |
| 65 |
|
| 66 |
The returned list of cookie instances is sorted in the order they |
| 67 |
should appear in the Cookie: header for return to the server. |
| 68 |
|
| 69 |
See add_cookie_header.__doc__ for the interface required of the |
| 70 |
request argument. |
| 71 |
""" |
| 72 |
with self._cookies_lock: |
| 73 |
self._policy._now = self._now = int(time.time()) |
| 74 |
cookies = self._cookies_for_request(request) |
| 75 |
|
| 76 |
|
| 77 |
def key(x): |
| 78 |
return len(x.path) |
| 79 |
|
| 80 |
cookies.sort(key=key, reverse=True) |
| 81 |
return cookies |
| 82 |
|
| 83 |
def get_policy(self): |
| 84 |
return self._policy |
| 85 |
|
| 86 |
def _normalized_cookie_tuples(self, attrs_set): |
| 87 |
"""Return list of tuples containing normalised cookie information. |
| 88 |
|
| 89 |
attrs_set is the list of lists of key,value pairs extracted from |
| 90 |
the Set-Cookie or Set-Cookie2 headers. |
| 91 |
|
| 92 |
Tuples are name, value, standard, rest, where name and value are the |
| 93 |
cookie name and value, standard is a dictionary containing the standard |
| 94 |
cookie-attributes (discard, secure, version, expires or max-age, |
| 95 |
domain, path and port) and rest is a dictionary containing the rest of |
| 96 |
the cookie-attributes. |
| 97 |
|
| 98 |
""" |
| 99 |
cookie_tuples = [] |
| 100 |
|
| 101 |
boolean_attrs = "discard", "secure" |
| 102 |
value_attrs = ("version", "expires", "max-age", "domain", "path", |
| 103 |
"port", "comment", "commenturl") |
| 104 |
|
| 105 |
for cookie_attrs in attrs_set: |
| 106 |
name, value = cookie_attrs[0] |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
max_age_set = False |
| 117 |
|
| 118 |
bad_cookie = False |
| 119 |
|
| 120 |
standard = {} |
| 121 |
rest = {} |
| 122 |
for k, v in cookie_attrs[1:]: |
| 123 |
lc = k.lower() |
| 124 |
|
| 125 |
if lc in value_attrs or lc in boolean_attrs: |
| 126 |
k = lc |
| 127 |
if k in boolean_attrs and v is None: |
| 128 |
|
| 129 |
|
| 130 |
v = True |
| 131 |
if k in standard: |
| 132 |
|
| 133 |
continue |
| 134 |
if k == "domain": |
| 135 |
if v is None: |
| 136 |
_debug(" missing value for domain attribute") |
| 137 |
bad_cookie = True |
| 138 |
break |
| 139 |
|
| 140 |
v = v.lower() |
| 141 |
if k == "expires": |
| 142 |
if max_age_set: |
| 143 |
|
| 144 |
continue |
| 145 |
if v is None: |
| 146 |
_debug(" missing or invalid value for expires " |
| 147 |
"attribute: treating as session cookie") |
| 148 |
continue |
| 149 |
if k == "max-age": |
| 150 |
max_age_set = True |
| 151 |
try: |
| 152 |
v = int(v) |
| 153 |
except ValueError: |
| 154 |
_debug(" missing or invalid (non-numeric) value for " |
| 155 |
"max-age attribute") |
| 156 |
bad_cookie = True |
| 157 |
break |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
k = "expires" |
| 163 |
v = self._now + v |
| 164 |
if not v and k == 'path': |
| 165 |
|
| 166 |
v = '/' |
| 167 |
if (k in value_attrs) or (k in boolean_attrs): |
| 168 |
if (v is None and |
| 169 |
k not in ("port", "comment", "commenturl")): |
| 170 |
_debug(" missing value for %s attribute" % k) |
| 171 |
bad_cookie = True |
| 172 |
break |
| 173 |
standard[k] = v |
| 174 |
else: |
| 175 |
rest[k] = v |
| 176 |
|
| 177 |
if bad_cookie: |
| 178 |
continue |
| 179 |
|
| 180 |
cookie_tuples.append((name, value, standard, rest)) |
| 181 |
|
| 182 |
return cookie_tuples |
| 183 |
|
| 184 |
def __getitem__(self, i): |
| 185 |
for q, ans in enumerate(self): |
| 186 |
if q == i: |
| 187 |
return ans |
| 188 |
raise IndexError() |
| 189 |
|
| 190 |
|
| 191 |
try: |
| 192 |
from http.cookiejar import NETSCAPE_MAGIC_RGX, NETSCAPE_HEADER_TEXT |
| 193 |
except ImportError: |
| 194 |
NETSCAPE_MAGIC_RGX = MCJ.magic_re |
| 195 |
NETSCAPE_HEADER_TEXT = MCJ.header |
| 196 |
else: |
| 197 |
MCJ.header = NETSCAPE_HEADER_TEXT |
| 198 |
|
| 199 |
|
| 200 |
class MozillaCookieJar(MCJ): |
| 201 |
|
| 202 |
def _really_load(self, f, filename, ignore_discard, ignore_expires): |
| 203 |
now = time.time() |
| 204 |
|
| 205 |
magic = f.readline() |
| 206 |
if not re.search(NETSCAPE_MAGIC_RGX, magic): |
| 207 |
f.close() |
| 208 |
raise LoadError( |
| 209 |
"%r does not look like a Netscape format cookies file" % |
| 210 |
filename) |
| 211 |
|
| 212 |
try: |
| 213 |
while 1: |
| 214 |
line = f.readline() |
| 215 |
if line == "": |
| 216 |
break |
| 217 |
|
| 218 |
|
| 219 |
if line.endswith("\n"): |
| 220 |
line = line[:-1] |
| 221 |
|
| 222 |
|
| 223 |
if (line.strip().startswith(("#", "$")) or line.strip() == ""): |
| 224 |
continue |
| 225 |
|
| 226 |
(domain, domain_specified, path, secure, expires, name, |
| 227 |
value) = line.split("\t", 6) |
| 228 |
secure = (secure == "TRUE") |
| 229 |
domain_specified = (domain_specified == "TRUE") |
| 230 |
if name == "": |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
name = value |
| 235 |
value = None |
| 236 |
|
| 237 |
initial_dot = domain.startswith(".") |
| 238 |
if domain_specified != initial_dot: |
| 239 |
raise ValueError() |
| 240 |
assert domain_specified == initial_dot |
| 241 |
|
| 242 |
discard = False |
| 243 |
if expires == "": |
| 244 |
expires = None |
| 245 |
discard = True |
| 246 |
|
| 247 |
|
| 248 |
c = Cookie(0, name, value, None, False, domain, |
| 249 |
domain_specified, initial_dot, path, False, secure, |
| 250 |
expires, discard, None, None, {}) |
| 251 |
if not ignore_discard and c.discard: |
| 252 |
continue |
| 253 |
if not ignore_expires and c.is_expired(now): |
| 254 |
continue |
| 255 |
self.set_cookie(c) |
| 256 |
|
| 257 |
except IOError: |
| 258 |
raise |
| 259 |
except Exception: |
| 260 |
raise LoadError("invalid Netscape format cookies file %r: %r" % |
| 261 |
(filename, line)) |