diff options
-rw-r--r-- | mitmproxy/flow/modules.py | 17 | ||||
-rw-r--r-- | netlib/http/cookies.py | 24 |
2 files changed, 24 insertions, 17 deletions
diff --git a/mitmproxy/flow/modules.py b/mitmproxy/flow/modules.py index 46da5b64..ab41da8d 100644 --- a/mitmproxy/flow/modules.py +++ b/mitmproxy/flow/modules.py @@ -1,10 +1,8 @@ from __future__ import absolute_import, print_function, division import collections -import email.utils import hashlib import re -import time from six.moves import http_cookiejar from six.moves import urllib @@ -325,20 +323,7 @@ class StickyCookieState: dom_port_path = self.ckey(attrs, f) if self.domain_match(f.request.host, dom_port_path[0]): - - # See if 'expires' time is in the past - expired = False - if 'expires' in attrs: - e = email.utils.parsedate_tz(attrs["expires"]) - if e: - exp_ts = email.utils.mktime_tz(e) - now_ts = time.time() - expired = exp_ts < now_ts - - # or if Max-Age is 0 - expired = expired or (int(attrs.get('Max-Age', 1)) == 0) - - if expired: + if cookies.is_expired(attrs): # Remove the cookie from jar self.jar[dom_port_path].pop(name, None) diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index 768a85df..90789365 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -1,7 +1,8 @@ import collections +import email.utils import re +import time -import email.utils from netlib import multidict """ @@ -260,3 +261,24 @@ def refresh_set_cookie_header(c, delta): if not ret: raise ValueError("Invalid Cookie") return ret + +def is_expired(cookie_attrs): + """ + Determines whether a cookie has expired. + + Returns: boolean + """ + expired = False + + # See if 'expires' time is in the past + if 'expires' in cookie_attrs: + e = email.utils.parsedate_tz(cookie_attrs["expires"]) + if e: + exp_ts = email.utils.mktime_tz(e) + now_ts = time.time() + expired = exp_ts < now_ts + + # or if Max-Age is 0 + expired = expired or (int(cookie_attrs.get('Max-Age', 1)) == 0) + + return expired |