Lines Matching refs:cookie

1 r"""HTTP cookie handling for web clients.
7 attributes of the HTTP cookie system as cookie-attributes, to distinguish
467 """Ad-hoc parser for Netscape protocol cookie-attributes.
469 The old Netscape cookie format for Set-Cookie can for instance contain
516 # This is an RFC 2109 cookie.
749 possible to construct Cookie instances that don't comply with the cookie
751 objects -- it deals with cookie parsing, supplying defaults, and
788 # cookie-attribute had an initial dot, in order to follow RFC 2965
850 def set_ok(self, cookie, request):
851 """Return true if (and only if) cookie should be accepted from server.
859 def return_ok(self, cookie, request):
860 """Return true if (and only if) cookie should be returned to server."""
864 """Return false if cookies should not be returned, given cookie domain.
869 """Return false if cookies should not be returned, given cookie path.
949 def set_ok(self, cookie, request):
956 _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
958 assert cookie.name is not None
963 if not fn(cookie, request):
968 def set_ok_version(self, cookie, request):
969 if cookie.version is None:
971 # cookie, so this must be an invalid RFC 2965 cookie.
973 cookie.name, cookie.value)
975 if cookie.version > 0 and not self.rfc2965:
978 elif cookie.version == 0 and not self.netscape:
983 def set_ok_verifiability(self, cookie, request):
985 if cookie.version > 0 and self.strict_rfc2965_unverifiable:
986 _debug(" third-party RFC 2965 cookie during "
989 elif cookie.version == 0 and self.strict_ns_unverifiable:
990 _debug(" third-party Netscape cookie during "
995 def set_ok_name(self, cookie, request):
998 if (cookie.version == 0 and self.strict_ns_set_initial_dollar and
999 cookie.name.startswith("$")):
1000 _debug(" illegal name (starts with '$'): '%s'", cookie.name)
1004 def set_ok_path(self, cookie, request):
1005 if cookie.path_specified:
1007 if ((cookie.version > 0 or
1008 (cookie.version == 0 and self.strict_ns_set_path)) and
1009 not self.path_return_ok(cookie.path, request)):
1011 "path %s", cookie.path, req_path)
1015 def set_ok_domain(self, cookie, request):
1016 if self.is_blocked(cookie.domain):
1017 _debug(" domain %s is in user block-list", cookie.domain)
1019 if self.is_not_allowed(cookie.domain):
1020 _debug(" domain %s is not in user allow-list", cookie.domain)
1022 if cookie.domain_specified:
1024 domain = cookie.domain
1050 if cookie.version == 0:
1059 if (cookie.version > 0 or
1065 if (cookie.version > 0 or
1075 def set_ok_port(self, cookie, request):
1076 if cookie.port_specified:
1082 for p in cookie.port.split(","):
1092 req_port, cookie.port)
1096 def return_ok(self, cookie, request):
1105 _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
1110 if not fn(cookie, request):
1114 def return_ok_version(self, cookie, request):
1115 if cookie.version > 0 and not self.rfc2965:
1118 elif cookie.version == 0 and not self.netscape:
1123 def return_ok_verifiability(self, cookie, request):
1125 if cookie.version > 0 and self.strict_rfc2965_unverifiable:
1126 _debug(" third-party RFC 2965 cookie during unverifiable "
1129 elif cookie.version == 0 and self.strict_ns_unverifiable:
1130 _debug(" third-party Netscape cookie during unverifiable "
1135 def return_ok_secure(self, cookie, request):
1136 if cookie.secure and request.type not in self.secure_protocols:
1137 _debug(" secure cookie with non-secure request")
1141 def return_ok_expires(self, cookie, request):
1142 if cookie.is_expired(self._now):
1143 _debug(" cookie expired")
1147 def return_ok_port(self, cookie, request):
1148 if cookie.port:
1152 for p in cookie.port.split(","):
1156 _debug(" request port %s does not match cookie port %s",
1157 req_port, cookie.port)
1161 def return_ok_domain(self, cookie, request):
1163 domain = cookie.domain
1171 if (cookie.version == 0 and
1173 not cookie.domain_specified and domain != erhn):
1174 _debug(" cookie with unspecified domain does not string-compare "
1178 if cookie.version > 0 and not domain_match(erhn, domain):
1180 "RFC 2965 cookie domain %s", erhn, domain)
1182 if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
1183 _debug(" request-host %s does not match Netscape cookie domain "
1190 # having to load lots of MSIE cookie files unless necessary.
1201 #_debug(" request domain %s does not match cookie domain %s",
1215 _debug("- checking cookie path=%s", path)
1282 for cookie in cookies_by_name.values():
1283 if not self._policy.return_ok(cookie, request):
1284 _debug(" not returning cookie")
1287 cookies.append(cookie)
1298 """Return a list of cookie-attributes to be returned to server.
1312 for cookie in cookies:
1319 version = cookie.version
1325 # quote cookie value if necessary
1328 if ((cookie.value is not None) and
1329 self.non_word_re.search(cookie.value) and version > 0):
1330 value = self.quote_re.sub(r"\\\1", cookie.value)
1332 value = cookie.value
1334 # add cookie-attributes to be returned in Cookie header
1335 if cookie.value is None:
1336 attrs.append(cookie.name)
1338 attrs.append("%s=%s" % (cookie.name, value))
1340 if cookie.path_specified:
1341 attrs.append('$Path="%s"' % cookie.path)
1342 if cookie.domain.startswith("."):
1343 domain = cookie.domain
1344 if (not cookie.domain_initial_dot and
1348 if cookie.port is not None:
1350 if cookie.port_specified:
1351 p = p + ('="%s"' % cookie.port)
1379 for cookie in cookies:
1380 if cookie.version != 1:
1390 """Return list of tuples containing normalised cookie information.
1396 cookie name and value, standard is a dictionary containing the standard
1397 cookie-attributes (discard, secure, version, expires or max-age,
1399 the cookie-attributes.
1413 # Build dictionary of standard cookie-attributes (standard) and
1414 # dictionary of other cookie-attributes (rest).
1417 # cookies should have the Expires cookie-attribute, and V1 cookies
1433 # boolean cookie-attribute is present, but has no value
1452 "attribute: treating as session cookie")
1466 # is a request to discard (old and new) cookie, though.
1487 # standard is dict of standard cookie-attributes, rest is dict of the
1502 return None # invalid version, ignore cookie
1556 # Expiry date in past is request to delete cookie. This can't be
1562 _debug("Expiring cookie, domain='%s', path='%s', name='%s'",
1583 cookie = self._cookie_from_cookie_tuple(tup, request)
1584 if cookie: cookies.append(cookie)
1591 for cookie in cookies:
1592 if cookie.version == 1:
1593 cookie.rfc2109 = True
1597 cookie.version = 0
1601 # get cookie-attributes for RFC 2965 and Netscape protocols
1614 return [] # no relevant cookie headers: quick exit
1635 # For each match, keep the RFC 2965 cookie and ignore the Netscape
1636 # cookie (RFC 2965 section 9.1). Actually, RFC 2109 cookies are
1641 for cookie in cookies:
1642 lookup[(cookie.domain, cookie.path, cookie.name)] = None
1654 def set_cookie_if_ok(self, cookie, request):
1655 """Set a cookie if policy says it's OK to do so."""
1660 if self._policy.set_ok(cookie, request):
1661 self.set_cookie(cookie)
1667 def set_cookie(self, cookie):
1668 """Set a cookie, without checking whether or not it should be set."""
1672 if cookie.domain not in c: c[cookie.domain] = {}
1673 c2 = c[cookie.domain]
1674 if cookie.path not in c2: c2[cookie.path] = {}
1675 c3 = c2[cookie.path]
1676 c3[cookie.name] = cookie
1685 for cookie in self.make_cookies(response, request):
1686 if self._policy.set_ok(cookie, request):
1687 _debug(" setting cookie: %s", cookie)
1688 self.set_cookie(cookie)
1699 the cookie with the specified name, path and domain is removed.
1701 Raises KeyError if no matching cookie exists.
1707 "domain and path must be given to remove a cookie by name")
1728 for cookie in self:
1729 if cookie.discard:
1730 self.clear(cookie.domain, cookie.path, cookie.name)
1747 for cookie in self:
1748 if cookie.is_expired(now):
1749 self.clear(cookie.domain, cookie.path, cookie.name)
1759 for cookie in self: i = i + 1
1764 for cookie in self: r.append(repr(cookie))
1769 for cookie in self: r.append(str(cookie))
1831 def lwp_cookie_str(cookie):
1832 """Return string representation of Cookie in the LWP cookie file format.
1837 h = [(cookie.name, cookie.value),
1838 ("path", cookie.path),
1839 ("domain", cookie.domain)]
1840 if cookie.port is not None: h.append(("port", cookie.port))
1841 if cookie.path_specified: h.append(("path_spec", None))
1842 if cookie.port_specified: h.append(("port_spec", None))
1843 if cookie.domain_initial_dot: h.append(("domain_dot", None))
1844 if cookie.secure: h.append(("secure", None))
1845 if cookie.expires: h.append(("expires",
1846 time2isoz(float(cookie.expires))))
1847 if cookie.discard: h.append(("discard", None))
1848 if cookie.comment: h.append(("comment", cookie.comment))
1849 if cookie.comment_url: h.append(("commenturl", cookie.comment_url))
1851 keys = sorted(cookie._rest.keys())
1853 h.append((k, str(cookie._rest[k])))
1855 h.append(("version", str(cookie.version)))
1880 for cookie in self:
1881 if not ignore_discard and cookie.discard:
1883 if not ignore_expires and cookie.is_expired(now):
1885 r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
2001 In particular, the cookie version and port number information is lost,
2029 # httponly is a cookie flag as defined in rfc6265
2030 # when encoded in a netscape cookie file,
2049 # cookies.txt regards 'Set-Cookie: foo' as a cookie
2051 # cookie with no value.
2098 for cookie in self:
2099 domain = cookie.domain
2100 if not ignore_discard and cookie.discard:
2102 if not ignore_expires and cookie.is_expired(now):
2104 if cookie.secure: secure = "TRUE"
2108 if cookie.expires is not None:
2109 expires = str(cookie.expires)
2112 if cookie.value is None:
2113 # cookies.txt regards 'Set-Cookie: foo' as a cookie
2115 # cookie with no value.
2117 value = cookie.name
2119 name = cookie.name
2120 value = cookie.value
2121 if cookie.has_nonstandard_attr(HTTPONLY_ATTR):
2124 "\t".join([domain, initial_dot, cookie.path,