Gracefully ignore cookie expiry dates in invalid format

Close #963
This commit is contained in:
Jakub Roztocil 2020-09-20 09:21:10 +02:00
parent 100872b5cf
commit 16ef08a159
2 changed files with 18 additions and 3 deletions

View File

@ -6,7 +6,7 @@ import time
from collections import OrderedDict from collections import OrderedDict
from http.cookiejar import parse_ns_headers from http.cookiejar import parse_ns_headers
from pprint import pformat from pprint import pformat
from typing import List, Tuple from typing import List, Optional, Tuple
import requests.auth import requests.auth
@ -93,7 +93,12 @@ def get_expired_cookies(
headers: List[Tuple[str, str]], headers: List[Tuple[str, str]],
now: float = None now: float = None
) -> List[dict]: ) -> List[dict]:
now = now or time.time() now = now or time.time()
def is_expired(expires: Optional[float]) -> bool:
return expires is not None and expires <= now
attr_sets: List[Tuple[str, str]] = parse_ns_headers( attr_sets: List[Tuple[str, str]] = parse_ns_headers(
value for name, value in headers value for name, value in headers
if name.lower() == 'set-cookie' if name.lower() == 'set-cookie'
@ -103,11 +108,12 @@ def get_expired_cookies(
dict(attrs[1:], name=attrs[0][0]) dict(attrs[1:], name=attrs[0][0])
for attrs in attr_sets for attrs in attr_sets
] ]
return [ return [
{ {
'name': cookie['name'], 'name': cookie['name'],
'path': cookie.get('path', '/') 'path': cookie.get('path', '/')
} }
for cookie in cookies for cookie in cookies
if cookie.get('expires', float('Inf')) <= now if is_expired(expires=cookie.get('expires'))
] ]

View File

@ -366,6 +366,15 @@ class TestExpiredCookies(CookieTestBase):
{'name': 'pea', 'path': '/ab'} {'name': 'pea', 'path': '/ab'}
] ]
), ),
(
# Checks we gracefully ignore expires date in invalid format.
# <https://github.com/httpie/httpie/issues/963>
[
('Set-Cookie', 'pfg=; Expires=Sat, 19-Sep-2020 06:58:14 GMT+0000; Max-Age=0; path=/; domain=.tumblr.com; secure; HttpOnly'),
],
None,
[]
),
( (
[ [
('Set-Cookie', 'hello=world; Path=/; Expires=Fri, 12 Jun 2020 12:28:55 GMT; HttpOnly'), ('Set-Cookie', 'hello=world; Path=/; Expires=Fri, 12 Jun 2020 12:28:55 GMT; HttpOnly'),
@ -373,7 +382,7 @@ class TestExpiredCookies(CookieTestBase):
], ],
datetime(2020, 6, 11).timestamp(), datetime(2020, 6, 11).timestamp(),
[] []
) ),
] ]
) )
def test_get_expired_cookies_manages_multiple_cookie_headers(self, headers, now, expected_expired): def test_get_expired_cookies_manages_multiple_cookie_headers(self, headers, now, expected_expired):