MCPcopy Index your code
hub / github.com/python/cpython / CookieJar

Class CookieJar

Lib/http/cookiejar.py:1247–1771  ·  view source on GitHub ↗

Collection of HTTP cookies. You may not need to know about this class: try urllib.request.build_opener(HTTPCookieProcessor).open(url).

Source from the content-addressed store, hash-verified

1245class Absent: pass
1246
1247class CookieJar:
1248 """Collection of HTTP cookies.
1249
1250 You may not need to know about this class: try
1251 urllib.request.build_opener(HTTPCookieProcessor).open(url).
1252 """
1253
1254 non_word_re = re.compile(r"\W")
1255 quote_re = re.compile(r"([\"\\])")
1256 strict_domain_re = re.compile(r"\.?[^.]*")
1257 domain_re = re.compile(r"[^.]*")
1258 dots_re = re.compile(r"^\.+")
1259
1260 magic_re = re.compile(r"^\#LWP-Cookies-(\d+\.\d+)", re.ASCII)
1261
1262 def __init__(self, policy=None):
1263 if policy is None:
1264 policy = DefaultCookiePolicy()
1265 self._policy = policy
1266
1267 self._cookies_lock = _threading.RLock()
1268 self._cookies = {}
1269
1270 def set_policy(self, policy):
1271 self._policy = policy
1272
1273 def _cookies_for_domain(self, domain, request):
1274 cookies = []
1275 if not self._policy.domain_return_ok(domain, request):
1276 return []
1277 _debug("Checking %s for cookies to return", domain)
1278 cookies_by_path = self._cookies[domain]
1279 for path in cookies_by_path.keys():
1280 if not self._policy.path_return_ok(path, request):
1281 continue
1282 cookies_by_name = cookies_by_path[path]
1283 for cookie in cookies_by_name.values():
1284 if not self._policy.return_ok(cookie, request):
1285 _debug(" not returning cookie")
1286 continue
1287 _debug(" it's a match")
1288 cookies.append(cookie)
1289 return cookies
1290
1291 def _cookies_for_request(self, request):
1292 """Return a list of cookies to be returned to server."""
1293 cookies = []
1294 for domain in self._cookies.keys():
1295 cookies.extend(self._cookies_for_domain(domain, request))
1296 return cookies
1297
1298 def _cookie_attrs(self, cookies):
1299 """Return a list of cookie-attributes to be returned to server.
1300
1301 like ['foo="bar"; $Path="/"', ...]
1302
1303 The $Version attribute is also added when appropriate (currently only
1304 once per request).

Callers 15

test_rfc2109_handlingMethod · 0.90
test_ns_parserMethod · 0.90
test_expiresMethod · 0.90
test_default_pathMethod · 0.90
test_wrong_domainMethod · 0.90
test_strict_domainMethod · 0.90
test_localhost_domainMethod · 0.90

Calls 1

compileMethod · 0.45

Tested by 15

test_rfc2109_handlingMethod · 0.72
test_ns_parserMethod · 0.72
test_expiresMethod · 0.72
test_default_pathMethod · 0.72
test_wrong_domainMethod · 0.72
test_strict_domainMethod · 0.72
test_localhost_domainMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…