Home | History | Annotate | Download | only in python2.7

Lines Matching defs:Cookie

1 r"""HTTP cookie handling for web clients.
7 attributes of the HTTP cookie system as cookie-attributes, to distinguish
28 __all__ = ['Cookie', 'CookieJar', 'CookiePolicy', 'DefaultCookiePolicy',
445 """Ad-hoc parser for Netscape protocol cookie-attributes.
447 The old Netscape cookie format for Set-Cookie can for instance contain
452 that Netscape Cookie headers contain. Ronald Tschalar's HTTPClient
480 # This is an RFC 2109 cookie.
707 class Cookie:
708 """HTTP Cookie.
713 possible to construct Cookie instances that don't comply with the cookie
714 standards. CookieJar.make_cookies is the factory function for Cookie
715 objects -- it deals with cookie parsing, supplying defaults, and
752 # cookie-attribute had an initial dot, in order to follow RFC 2965
788 return "<Cookie %s for %s>" % (namevalue, limit)
802 return "Cookie(%s)" % ", ".join(args)
814 def set_ok(self, cookie, request):
815 """Return true if (and only if) cookie should be accepted from server.
823 def return_ok(self, cookie, request):
824 """Return true if (and only if) cookie should be returned to server."""
828 """Return false if cookies should not be returned, given cookie domain.
833 """Return false if cookies should not be returned, given cookie path.
911 def set_ok(self, cookie, request):
918 _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
920 assert cookie.name is not None
925 if not fn(cookie, request):
930 def set_ok_version(self, cookie, request):
931 if cookie.version is None:
933 # cookie, so this must be an invalid RFC 2965 cookie.
935 cookie.name, cookie.value)
937 if cookie.version > 0 and not self.rfc2965:
940 elif cookie.version == 0 and not self.netscape:
945 def set_ok_verifiability(self, cookie, request):
947 if cookie.version > 0 and self.strict_rfc2965_unverifiable:
948 _debug(" third-party RFC 2965 cookie during "
951 elif cookie.version == 0 and self.strict_ns_unverifiable:
952 _debug(" third-party Netscape cookie during "
957 def set_ok_name(self, cookie, request):
960 if (cookie.version == 0 and self.strict_ns_set_initial_dollar and
961 cookie.name.startswith("$")):
962 cookie.name)
966 def set_ok_path(self, cookie, request):
967 if cookie.path_specified:
969 if ((cookie.version > 0 or
970 (cookie.version == 0 and self.strict_ns_set_path)) and
971 not req_path.startswith(cookie.path)):
973 "path %s", cookie.path, req_path)
977 def set_ok_domain(self, cookie, request):
978 if self.is_blocked(cookie.domain):
979 _debug(" domain %s is in user block-list", cookie.domain)
981 if self.is_not_allowed(cookie.domain):
982 _debug(" domain %s is not in user allow-list", cookie.domain)
984 if cookie.domain_specified:
986 domain = cookie.domain
1012 if cookie.version == 0:
1020 if (cookie.version > 0 or
1026 if (cookie.version > 0 or
1036 def set_ok_port(self, cookie, request):
1037 if cookie.port_specified:
1043 for p in cookie.port.split(","):
1053 req_port, cookie.port)
1057 def return_ok(self, cookie, request):
1066 _debug(" - checking cookie %s=%s", cookie.name, cookie.value)
1071 if not fn(cookie, request):
1075 def return_ok_version(self, cookie, request):
1076 if cookie.version > 0 and not self.rfc2965:
1079 elif cookie.version == 0 and not self.netscape:
1084 def return_ok_verifiability(self, cookie, request):
1086 if cookie.version > 0 and self.strict_rfc2965_unverifiable:
1087 _debug(" third-party RFC 2965 cookie during unverifiable "
1090 elif cookie.version == 0 and self.strict_ns_unverifiable:
1091 _debug(" third-party Netscape cookie during unverifiable "
1096 def return_ok_secure(self, cookie, request):
1097 if cookie.secure and request.get_type() != "https":
1098 _debug(" secure cookie with non-secure request")
1102 def return_ok_expires(self, cookie, request):
1103 if cookie.is_expired(self._now):
1104 _debug(" cookie expired")
1108 def return_ok_port(self, cookie, request):
1109 if cookie.port:
1113 for p in cookie.port.split(","):
1117 _debug(" request port %s does not match cookie port %s",
1118 req_port, cookie.port)
1122 def return_ok_domain(self, cookie, request):
1124 domain = cookie.domain
1127 if (cookie.version == 0 and
1129 not cookie.domain_specified and domain != erhn):
1130 _debug(" cookie with unspecified domain does not string-compare "
1134 if cookie.version > 0 and not domain_match(erhn, domain):
1136 "RFC 2965 cookie domain %s", erhn, domain)
1138 if cookie.version == 0 and not ("."+erhn).endswith(domain):
1139 _debug(" request-host %s does not match Netscape cookie domain "
1146 # having to load lots of MSIE cookie files unless necessary.
1153 #_debug(" request domain %s does not match cookie domain %s",
1167 _debug("- checking cookie path=%s", path)
1238 for cookie in cookies_by_name.values():
1239 if not self._policy.return_ok(cookie, request):
1240 _debug(" not returning cookie")
1243 cookies.append(cookie)
1254 """Return a list of cookie-attributes to be returned to server.
1268 for cookie in cookies:
1269 # set version of Cookie header
1271 # What should it be if multiple matching Set-Cookie headers have
1275 version = cookie.version
1281 # quote cookie value if necessary
1283 # intact, due to the poorly-specified Netscape Cookie: syntax)
1284 if ((cookie.value is not None) and
1285 self.non_word_re.search(cookie.value) and version > 0):
1286 value = self.quote_re.sub(r"\\\1", cookie.value)
1288 value = cookie.value
1290 # add cookie-attributes to be returned in Cookie header
1291 if cookie.value is None:
1292 attrs.append(cookie.name)
1294 attrs.append("%s=%s" % (cookie.name, value))
1296 if cookie.path_specified:
1297 attrs.append('$Path="%s"' % cookie.path)
1298 if cookie.domain.startswith("."):
1299 domain = cookie.domain
1300 if (not cookie.domain_initial_dot and
1304 if cookie.port is not None:
1306 if cookie.port_specified:
1307 p = p + ('="%s"' % cookie.port)
1313 """Add correct Cookie: header to request (urllib2.Request object).
1328 if not request.has_header("Cookie"):
1330 "Cookie", "; ".join(attrs))
1335 for cookie in cookies:
1336 if cookie.version != 1:
1346 """Return list of tuples containing normalised cookie information.
1349 the Set-Cookie or Set-Cookie2 headers.
1352 cookie name and value, standard is a dictionary containing the standard
1353 cookie-attributes (discard, secure, version, expires or max-age,
1355 the cookie-attributes.
1369 # Build dictionary of standard cookie-attributes (standard) and
1370 # dictionary of other cookie-attributes (rest).
1373 # cookies should have the Expires cookie-attribute, and V1 cookies
1389 # boolean cookie-attribute is present, but has no value
1408 "attribute: treating as session cookie")
1422 # is a request to discard (old and new) cookie, though.
1443 # standard is dict of standard cookie-attributes, rest is dict of the
1458 return None # invalid version, ignore cookie
1498 # Cookie should then only be sent back on that port.
1504 # No port attr present. Cookie can be sent back on any port.
1512 # Expiry date in past is request to delete cookie. This can't be
1518 _debug("Expiring cookie, domain='%s', path='%s', name='%s'",
1522 return Cookie(version,
1539 cookie = self._cookie_from_cookie_tuple(tup, request)
1540 if cookie: cookies.append(cookie)
1547 for cookie in cookies:
1548 if cookie.version == 1:
1549 cookie.rfc2109 = True
1553 cookie.version = 0
1556 """Return sequence of Cookie objects extracted from response object."""
1557 # get cookie-attributes for RFC 2965 and Netscape protocols
1560 ns_hdrs = headers.getheaders("Set-Cookie")
1569 return [] # no relevant cookie headers: quick exit
1588 # Look for Netscape cookies (from Set-Cookie headers) that match
1590 # For each match, keep the RFC 2965 cookie and ignore the Netscape
1591 # cookie (RFC 2965 section 9.1). Actually, RFC 2109 cookies are
1596 for cookie in cookies:
1597 lookup[(cookie.domain, cookie.path, cookie.name)] = None
1609 def set_cookie_if_ok(self, cookie, request):
1610 """Set a cookie if policy says it's OK to do so."""
1615 if self._policy.set_ok(cookie, request):
1616 self.set_cookie(cookie)
1622 def set_cookie(self, cookie):
1623 """Set a cookie, without checking whether or not it should be set."""
1627 if cookie.domain not in c: c[cookie.domain] = {}
1628 c2 = c[cookie.domain]
1629 if cookie.path not in c2: c2[cookie.path] = {}
1630 c3 = c2[cookie.path]
1631 c3[cookie.name] = cookie
1642 for cookie in self.make_cookies(response, request):
1643 if self._policy.set_ok(cookie, request):
1644 _debug(" setting cookie: %s", cookie)
1645 self.set_cookie(cookie)
1656 the cookie with the specified name, path and domain is removed.
1658 Raises KeyError if no matching cookie exists.
1664 "domain and path must be given to remove a cookie by name")
1685 for cookie in self:
1686 if cookie.discard:
1687 self.clear(cookie.domain, cookie.path, cookie.name)
1704 for cookie in self:
1705 if cookie.is_expired(now):
1706 self.clear(cookie.domain, cookie.path, cookie.name)
1716 for cookie in self: i = i + 1
1721 for cookie in self: r.append(repr(cookie))
1726 for cookie in self: r.append(str(cookie))