From c92992f03bba6553ec39fc42e6716beb942967e3 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 8 Jul 2016 14:16:29 +0530 Subject: [PATCH] Move cookie expiry detection to separate function --- mitmproxy/flow/modules.py | 17 +---------------- netlib/http/cookies.py | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/mitmproxy/flow/modules.py b/mitmproxy/flow/modules.py index 46da5b646..ab41da8d1 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 768a85df5..907893655 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -1,7 +1,8 @@ import collections -import re - import email.utils +import re +import time + 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