MCPcopy
hub / github.com/scrapy/scrapy / add_cookie_header

Method add_cookie_header

scrapy/http/cookies.py:44–73  ·  view source on GitHub ↗
(self, request: Request)

Source from the content-addressed store, hash-verified

42 self.jar.extract_cookies(wrsp, wreq) # type: ignore[arg-type]
43
44 def add_cookie_header(self, request: Request) -> None:
45 wreq = WrappedRequest(request)
46 self.policy._now = self.jar._now = int(time.time()) # type: ignore[attr-defined]
47
48 # the cookiejar implementation iterates through all domains
49 # instead we restrict to potential matches on the domain
50 req_host = urlparse_cached(request).hostname
51 if not req_host:
52 return
53
54 if not IPV4_RE.search(req_host):
55 hosts = potential_domain_matches(req_host)
56 if "." not in req_host:
57 hosts.append(req_host + ".local")
58 else:
59 hosts = [req_host]
60
61 cookies = []
62 for host in hosts:
63 if host in self.jar._cookies: # type: ignore[attr-defined]
64 cookies.extend(self.jar._cookies_for_domain(host, wreq)) # type: ignore[attr-defined]
65
66 attrs = self.jar._cookie_attrs(cookies) # type: ignore[attr-defined]
67 if attrs and not wreq.has_header("Cookie"):
68 wreq.add_unredirected_header("Cookie", "; ".join(attrs))
69
70 self.processed += 1
71 if self.processed % self.check_expired_frequency == 0:
72 # This is still quite inefficient for large number of cookies
73 self.jar.clear_expired_cookies()
74
75 @property
76 def _cookies(self) -> dict[str, dict[str, dict[str, Cookie]]]:

Callers 1

process_requestMethod · 0.80

Calls 7

has_headerMethod · 0.95
urlparse_cachedFunction · 0.90
WrappedRequestClass · 0.85
potential_domain_matchesFunction · 0.85
extendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected