diff options
author | Thomas Kriechbaumer <Kriechi@users.noreply.github.com> | 2016-08-15 12:49:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-15 12:49:28 +0200 |
commit | 2419ab153d3ce215e719b434f5f69405be9a6586 (patch) | |
tree | f840f19cde156baa249f806f2e4395b8f9a7a67b /netlib | |
parent | 65677bdd284ef71184185671f4ae0b3713b5a3de (diff) | |
parent | 239eaf0078db0549232ef6eca54dabf75e9752c5 (diff) | |
download | mitmproxy-2419ab153d3ce215e719b434f5f69405be9a6586.tar.gz mitmproxy-2419ab153d3ce215e719b434f5f69405be9a6586.tar.bz2 mitmproxy-2419ab153d3ce215e719b434f5f69405be9a6586.zip |
Merge pull request #1464 from dufferzafar/har
HAR
Diffstat (limited to 'netlib')
-rw-r--r-- | netlib/http/cookies.py | 78 |
1 files changed, 63 insertions, 15 deletions
diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index dd0af99c..1421d8eb 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -26,6 +26,12 @@ variants. Serialization follows RFC6265. http://tools.ietf.org/html/rfc2965 """ +_cookie_params = set(( + 'expires', 'path', 'comment', 'max-age', + 'secure', 'httponly', 'version', +)) + + # TODO: Disallow LHS-only Cookie values @@ -263,27 +269,69 @@ def refresh_set_cookie_header(c, delta): return ret -def is_expired(cookie_attrs): +def get_expiration_ts(cookie_attrs): """ - Determines whether a cookie has expired. + Determines the time when the cookie will be expired. - Returns: boolean - """ + Considering both 'expires' and 'max-age' parameters. - # See if 'expires' time is in the past - expires = False + Returns: timestamp of when the cookie will expire. + None, if no expiration time is set. + """ if 'expires' in cookie_attrs: e = email.utils.parsedate_tz(cookie_attrs["expires"]) if e: - exp_ts = email.utils.mktime_tz(e) + return email.utils.mktime_tz(e) + + elif 'max-age' in cookie_attrs: + try: + max_age = int(cookie_attrs['Max-Age']) + except ValueError: + pass + else: now_ts = time.time() - expires = exp_ts < now_ts + return now_ts + max_age + + return None - # or if Max-Age is 0 - max_age = False - try: - max_age = int(cookie_attrs.get('Max-Age', 1)) == 0 - except ValueError: - pass - return expires or max_age +def is_expired(cookie_attrs): + """ + Determines whether a cookie has expired. + + Returns: boolean + """ + + exp_ts = get_expiration_ts(cookie_attrs) + now_ts = time.time() + + # If no expiration information was provided with the cookie + if exp_ts is None: + return False + else: + return exp_ts <= now_ts + + +def group_cookies(pairs): + """ + Converts a list of pairs to a (name, value, attrs) for each cookie. + """ + + if not pairs: + return [] + + cookie_list = [] + + # First pair is always a new cookie + name, value = pairs[0] + attrs = [] + + for k, v in pairs[1:]: + if k.lower() in _cookie_params: + attrs.append((k, v)) + else: + cookie_list.append((name, value, CookieAttrs(attrs))) + name, value, attrs = k, v, [] + + cookie_list.append((name, value, CookieAttrs(attrs))) + return cookie_list |