diff options
author | Thomas Kriechbaumer <Kriechi@users.noreply.github.com> | 2016-04-03 10:16:39 +0200 |
---|---|---|
committer | Thomas Kriechbaumer <Kriechi@users.noreply.github.com> | 2016-04-03 10:16:39 +0200 |
commit | f5fe93bf1317fef0039e5b74e6c0812c78942db8 (patch) | |
tree | a58423514fd1759580ab89f4ddb6b795bbc3a461 /netlib/http/cookies.py | |
parent | 16fdbb4f2fcee8f1db4a516d173ec2881161b3ea (diff) | |
parent | 4e2acc4d9d453126ac0e8aa20f3cb7dcc6a85a8c (diff) | |
download | mitmproxy-f5fe93bf1317fef0039e5b74e6c0812c78942db8.tar.gz mitmproxy-f5fe93bf1317fef0039e5b74e6c0812c78942db8.tar.bz2 mitmproxy-f5fe93bf1317fef0039e5b74e6c0812c78942db8.zip |
Merge pull request #1074 from mitmproxy/move-response-refresh
move HTTPResponse.refresh into netlib
Diffstat (limited to 'netlib/http/cookies.py')
-rw-r--r-- | netlib/http/cookies.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index 18544b5e..caa84ff7 100644 --- a/netlib/http/cookies.py +++ b/netlib/http/cookies.py @@ -1,4 +1,6 @@ +from six.moves import http_cookies as Cookie import re +from email.utils import parsedate_tz, formatdate, mktime_tz from .. import odict @@ -191,3 +193,35 @@ def format_cookie_header(od): Formats a Cookie header value. """ return _format_pairs(od.lst) + + +def refresh_set_cookie_header(c, delta): + """ + Args: + c: A Set-Cookie string + delta: Time delta in seconds + Returns: + A refreshed Set-Cookie string + """ + try: + c = Cookie.SimpleCookie(str(c)) + except Cookie.CookieError: + raise ValueError("Invalid Cookie") + for i in c.values(): + if "expires" in i: + d = parsedate_tz(i["expires"]) + if d: + d = mktime_tz(d) + delta + i["expires"] = formatdate(d) + else: + # This can happen when the expires tag is invalid. + # reddit.com sends a an expires tag like this: "Thu, 31 Dec + # 2037 23:59:59 GMT", which is valid RFC 1123, but not + # strictly correct according to the cookie spec. Browsers + # appear to parse this tolerantly - maybe we should too. + # For now, we just ignore this. + del i["expires"] + ret = c.output(header="").strip() + if not ret: + raise ValueError("Invalid Cookie") + return ret |