Start abstracting out sticky cookie state.

This commit is contained in:
Aldo Cortesi 2011-02-24 10:33:39 +13:00
parent 3c1db00ebb
commit 57947b328e
3 changed files with 38 additions and 3 deletions

View File

@ -2,7 +2,7 @@
This module provides more sophisticated flow tracking. These match requests
with their responses, and provide filtering and interception facilities.
"""
import subprocess, base64, sys, json, hashlib
import subprocess, base64, sys, json, hashlib, Cookie, cookielib, copy
import proxy, threading, netstring
import controller
@ -89,6 +89,29 @@ class ServerPlaybackState:
return l.pop(0)
class StickyCookieState:
def __init__(self):
self.jar = {}
def ckey(self, c):
c = copy.copy(c)
del c["expires"]
return str(c)
def add_cookies(self, headers):
for i in headers:
c = Cookie.SimpleCookie(i)
m = c.values()[0]
self.jar[self.ckey(m)] = m
def get_cookies(self, domain, path):
cs = []
for i in self.jar.values():
if cookielib.domain_match(domain, i["domain"]) and path.startswith(i.get("path", "/")):
cs.append(i)
return cs
class Flow:
def __init__(self, request):
self.request = request

View File

@ -85,8 +85,8 @@ def parse_url(url):
else:
port = 80
path = urlparse.urlunparse(('', '', path, params, query, fragment))
if not path:
path = "/"
if not path.startswith("/"):
path = "/" + path
return scheme, host, port, path

View File

@ -4,6 +4,17 @@ import utils
import libpry
class uStickyCookieState(libpry.AutoTree):
def test_simple(self):
s = flow.StickyCookieState()
s.add_cookies(
["SSID=mooo, FOO=bar; Domain=.google.com; Path=/; Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; "]
)
assert len(s.jar) == 1
assert len(s.get_cookies("www.google.com", "/foo")) == 1
assert len(s.get_cookies("www.foo.com", "/foo")) == 0
class uServerPlaybackState(libpry.AutoTree):
def test_hash(self):
s = flow.ServerPlaybackState(None)
@ -345,6 +356,7 @@ class uFlowMaster(libpry.AutoTree):
tests = [
uStickyCookieState(),
uServerPlaybackState(),
uFlow(),
uState(),