Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangxli committed Dec 4, 2023
1 parent 43f92fa commit e91ec9d
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/7583.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement filter_cookies() with domain-matching and path-matching on the keys, instead of testing every single cookie.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ William Grzybowski
William S.
Wilson Ong
wouter bolsterlee
Xiang Li
Yang Zhou
Yannick Koechlin
Yannick Péroux
Expand Down
37 changes: 36 additions & 1 deletion aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,43 @@ def filter_cookies(self, request_url: URL = URL()) -> "BaseCookie[str]":
request_origin = request_url.origin()
is_not_secure = request_origin not in self._treat_as_secure_origin

# d: domain, d could be subdomain
# p: path
d = hostname

pairs = set()
while d:
p = request_url.path
while p:
pairs.add((d, p))
path_leftovers = p.rsplit("/", maxsplit=1)
# handle last element for rsplit
if len(path_leftovers) > 1:
p = path_leftovers[0]
else:
p = None

# handle last element for split
leftovers = d.split(".", maxsplit=1)
if len(leftovers) > 1:
d = leftovers[-1]
else:
d = None

# shared cookie, it should have max of 1 entry
pairs.add(("", "/"))
print(f'pairs: {pairs}')

import itertools

cookies = itertools.chain.from_iterable(
self._cookies[p].values() for p in pairs
)

# try push

# Point 2: https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4
for cookie in sorted(self, key=lambda c: len(c["path"])):
for cookie in list(cookies):
name = cookie.key
domain = cookie["domain"]

Expand Down
115 changes: 115 additions & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,121 @@ async def test_filter_cookies_str_deprecated(loop: Any) -> None:
jar.filter_cookies("http://éé.com")


async def test_filter_cookies_with_domain_path_lookup(loop: Any) -> None:
jar = CookieJar()
cookies = SimpleCookie(
"shared-cookie=first; "
"domain-cookie=second; Domain=example.com; "
"subdomain1-cookie=third; Domain=test1.example.com; "
"subdomain2-cookie=fourth; Domain=test2.example.com; "
"dotted-domain-cookie=fifth; Domain=.example.com; "
"different-domain-cookie=sixth; Domain=different.org; "
"secure-cookie=seventh; Domain=secure.com; Secure; "
"no-path-cookie=eighth; Domain=pathtest.com; "
"path1-cookie=ninth; Domain=pathtest.com; Path=/; "
"path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
"path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
"path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
"expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
" Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
"max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
" Max-Age=60; "
"invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
" Max-Age=string; "
"invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
" Expires=string;"
)
jar.update_cookies(cookies)
cookies = jar.filter_cookies(URL("http://pathtest.com/"))

expected_cookies = [
"shared-cookie",
"no-path-cookie",
"path1-cookie",
]
assert len(cookies) == 3
for c in cookies:
assert c in expected_cookies


async def test_filter_cookies_with_domain_path_lookup_subdomain(loop: Any) -> None:
jar = CookieJar()
cookies = SimpleCookie(
"shared-cookie=first; "
"domain-cookie=second; Domain=example.com; "
"subdomain1-cookie=third; Domain=test1.example.com; "
"subdomain2-cookie=fourth; Domain=test2.example.com; "
"dotted-domain-cookie=fifth; Domain=.example.com; "
"different-domain-cookie=sixth; Domain=different.org; "
"secure-cookie=seventh; Domain=secure.com; Secure; "
"no-path-cookie=eighth; Domain=pathtest.com; "
"path1-cookie=ninth; Domain=pathtest.com; Path=/; "
"path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
"path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
"path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
"expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
" Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
"max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
" Max-Age=60; "
"invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
" Max-Age=string; "
"invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
" Expires=string;"
)
jar.update_cookies(cookies)
cookies = jar.filter_cookies(URL("http://test1.example.com/"))

expected_cookies = [
"shared-cookie",
"domain-cookie",
"subdomain1-cookie",
"dotted-domain-cookie",
]
# assert len(cookies) == 4
for c in cookies:
print(f"c: {c}")
assert c in expected_cookies


async def test_filter_cookies_with_domain_path_lookup_multilevelpath(loop: Any) -> None:
jar = CookieJar()
cookies = SimpleCookie(
"shared-cookie=first; "
"domain-cookie=second; Domain=example.com; "
"subdomain1-cookie=third; Domain=test1.example.com; "
"subdomain2-cookie=fourth; Domain=test2.example.com; "
"dotted-domain-cookie=fifth; Domain=.example.com; "
"different-domain-cookie=sixth; Domain=different.org; "
"secure-cookie=seventh; Domain=secure.com; Secure; "
"no-path-cookie=eighth; Domain=pathtest.com; "
"path1-cookie=ninth; Domain=pathtest.com; Path=/; "
"path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
"path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
"path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
"expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
" Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
"max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
" Max-Age=60; "
"invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
" Max-Age=string; "
"invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
" Expires=string;"
)
jar.update_cookies(cookies)
cookies = jar.filter_cookies(URL("http://pathtest.com/one/two/"))

expected_cookies = [
"path2-cookie",
"shared-cookie",
"path3-cookie",
"path4-cookie",
]
assert len(cookies) == 4
for c in cookies:
# print(f"c: {c}")
assert c in expected_cookies


async def test_domain_filter_ip_cookie_send(loop: Any) -> None:
jar = CookieJar()
cookies = SimpleCookie(
Expand Down

0 comments on commit e91ec9d

Please sign in to comment.