diff options
author | Shadab Zafar <dufferzafar0@gmail.com> | 2016-08-04 14:16:35 +0530 |
---|---|---|
committer | Shadab Zafar <dufferzafar0@gmail.com> | 2016-08-15 12:00:23 +0530 |
commit | a479c51465deae7be98ddba07ca2138fe6014d77 (patch) | |
tree | 895a5de63fa78edb568bc9de82a42a88874dd230 | |
parent | 99b32094d81054d7a80adb7d2a032a056465d945 (diff) | |
download | mitmproxy-a479c51465deae7be98ddba07ca2138fe6014d77.tar.gz mitmproxy-a479c51465deae7be98ddba07ca2138fe6014d77.tar.bz2 mitmproxy-a479c51465deae7be98ddba07ca2138fe6014d77.zip |
Add method to group pairs by cookies
-rw-r--r-- | netlib/http/cookies.py | 31 | ||||
-rw-r--r-- | test/netlib/http/test_cookies.py | 20 |
2 files changed, 51 insertions, 0 deletions
diff --git a/netlib/http/cookies.py b/netlib/http/cookies.py index dd0af99c..389dbb26 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 @@ -287,3 +293,28 @@ def is_expired(cookie_attrs): pass return expires or max_age + + +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 diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index 17e21b94..5a0e264e 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -266,3 +266,23 @@ def test_is_expired(): assert not cookies.is_expired(CA([("Max-Age", "nan")])) assert not cookies.is_expired(CA([("Expires", "false")])) + + +def test_group_cookies(): + def group(cookie): + return cookies.group_cookies(cookies.parse_cookie_header(c)) + + c = "one=uno; foo=bar; foo=baz" + assert len(group(c)) == 3 + + c = "one=uno; Path=/; foo=bar; Max-Age=0; foo=baz; expires=24-08-1993" + assert len(group(c)) == 3 + + c = "one=uno;" + assert len(group(c)) == 1 + + c = "one=uno; Path=/; Max-Age=0; Expires=24-08-1993" + assert len(group(c)) == 1 + + c = "path=val; Path=/" + assert group(c) == [("path", "val", cookies.CookieAttrs([("Path", "/")]))] |