Skip to content

Commit

Permalink
Fix #967: large cookie expiration/max-age doesn't break an event loop…
Browse files Browse the repository at this point in the history
… from now
  • Loading branch information
asvetlov committed Jul 16, 2016
1 parent e65d3f8 commit 2cb70cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGES
=======

0.22.1 (08-16-2016)
-------------------

- Large cookie expiration/max-age doesn't break an event loop from now
(fixes #967)


0.22.0 (08-15-2016)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This relies on each of the submodules having an __all__ variable.

__version__ = '0.22.0'
__version__ = '0.22.1'

import multidict # noqa

Expand Down
29 changes: 19 additions & 10 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import io
import os
import re
from urllib.parse import quote, urlencode, urlsplit
from http.cookies import SimpleCookie, Morsel

from collections import namedtuple
from http.cookies import SimpleCookie, Morsel
from math import ceil
from pathlib import Path
from urllib.parse import quote, urlencode, urlsplit

import multidict

Expand Down Expand Up @@ -589,9 +591,17 @@ def __init__(self, *, loop=None):
super().__init__(loop=loop)
self._host_only_cookies = set()

def _expire_cookie(self, name):
if name in self._cookies:
del self._cookies[name]
def _expire_cookie(self, when, name, DAY=24*3600):
now = self._loop.time()
delta = when - now
if delta <= 0:
# expired
self._cookies.pop(name, None)
if delta > DAY:
# Huge timeouts (more than 24 days) breaks event loop
self._loop.call_at(ceil(now+DAY), self._expire_cookie, when, name)
else:
self._loop.call_at(ceil(when), self._expire_cookie, when, name)

def update_cookies(self, cookies, response_url=None):
"""Update cookies."""
Expand Down Expand Up @@ -636,18 +646,17 @@ def update_cookies(self, cookies, response_url=None):
if max_age:
try:
delta_seconds = int(max_age)
self._loop.call_later(
delta_seconds, self._expire_cookie, name)
self._expire_cookie(self._loop.time() + delta_seconds,
name)
except ValueError:
cookie["max-age"] = ""

expires = cookie["expires"]
if not cookie["max-age"] and expires:
expire_time = self._parse_date(expires)
if expire_time:
self._loop.call_at(
expire_time.timestamp(),
self._expire_cookie, name)
self._expire_cookie(expire_time.timestamp(),
name)
else:
cookie["expires"] = ""

Expand Down

0 comments on commit 2cb70cc

Please sign in to comment.