|
8 | 8 | import io |
9 | 9 | import os |
10 | 10 | import re |
11 | | -from urllib.parse import quote, urlencode, urlsplit |
12 | | -from http.cookies import SimpleCookie, Morsel |
| 11 | + |
13 | 12 | from collections import namedtuple |
| 13 | +from http.cookies import SimpleCookie, Morsel |
| 14 | +from math import ceil |
14 | 15 | from pathlib import Path |
| 16 | +from urllib.parse import quote, urlencode, urlsplit |
15 | 17 |
|
16 | 18 | import multidict |
17 | 19 |
|
@@ -589,9 +591,17 @@ def __init__(self, *, loop=None): |
589 | 591 | super().__init__(loop=loop) |
590 | 592 | self._host_only_cookies = set() |
591 | 593 |
|
592 | | - def _expire_cookie(self, name): |
593 | | - if name in self._cookies: |
594 | | - del self._cookies[name] |
| 594 | + def _expire_cookie(self, when, name, DAY=24*3600): |
| 595 | + now = self._loop.time() |
| 596 | + delta = when - now |
| 597 | + if delta <= 0: |
| 598 | + # expired |
| 599 | + self._cookies.pop(name, None) |
| 600 | + if delta > DAY: |
| 601 | + # Huge timeouts (more than 24 days) breaks event loop |
| 602 | + self._loop.call_at(ceil(now+DAY), self._expire_cookie, when, name) |
| 603 | + else: |
| 604 | + self._loop.call_at(ceil(when), self._expire_cookie, when, name) |
595 | 605 |
|
596 | 606 | def update_cookies(self, cookies, response_url=None): |
597 | 607 | """Update cookies.""" |
@@ -636,18 +646,17 @@ def update_cookies(self, cookies, response_url=None): |
636 | 646 | if max_age: |
637 | 647 | try: |
638 | 648 | delta_seconds = int(max_age) |
639 | | - self._loop.call_later( |
640 | | - delta_seconds, self._expire_cookie, name) |
| 649 | + self._expire_cookie(self._loop.time() + delta_seconds, |
| 650 | + name) |
641 | 651 | except ValueError: |
642 | 652 | cookie["max-age"] = "" |
643 | 653 |
|
644 | 654 | expires = cookie["expires"] |
645 | 655 | if not cookie["max-age"] and expires: |
646 | 656 | expire_time = self._parse_date(expires) |
647 | 657 | if expire_time: |
648 | | - self._loop.call_at( |
649 | | - expire_time.timestamp(), |
650 | | - self._expire_cookie, name) |
| 658 | + self._expire_cookie(expire_time.timestamp(), |
| 659 | + name) |
651 | 660 | else: |
652 | 661 | cookie["expires"] = "" |
653 | 662 |
|
|
0 commit comments