From 9c5c3c2b1adfe9e8d79742a1bd5080b3fc1fdcde Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 20 Feb 2011 09:36:13 +1300 Subject: Implement state loading that doesn't change object identity. We need this to let us load state from copied Flows returned from scripts. --- libmproxy/proxy.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'libmproxy/proxy.py') diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 88c62b25..4ab19694 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -148,6 +148,23 @@ class Request(controller.Msg): def is_cached(self): return False + def load_state(self, state): + if state["client_conn"]: + if self.client_conn: + self.client_conn.load_state(state["client_conn"]) + else: + self.client_conn = ClientConnect.from_state(state["client_conn"]) + else: + self.client_conn = None + self.host = state["host"] + self.port = state["port"] + self.scheme = state["scheme"] + self.method = state["method"] + self.path = state["path"] + self.headers = utils.Headers.from_state(state["headers"]) + self.content = base64.decodestring(state["content"]) + self.timestamp = state["timestamp"] + def get_state(self): return dict( client_conn = self.client_conn.get_state() if self.client_conn else None, @@ -164,7 +181,7 @@ class Request(controller.Msg): @classmethod def from_state(klass, state): return klass( - ClientConnect.from_state(state["client_conn"]) if state["client_conn"] else None, + ClientConnect.from_state(state["client_conn"]), state["host"], state["port"], state["scheme"], @@ -249,6 +266,13 @@ class Response(controller.Msg): self.cached = False controller.Msg.__init__(self) + def load_state(self, state): + self.code = state["code"] + self.msg = state["msg"] + self.headers = utils.Headers.from_state(state["headers"]) + self.content = base64.decodestring(state["content"]) + self.timestamp = state["timestamp"] + def get_state(self): return dict( code = self.code, @@ -325,12 +349,21 @@ class ClientConnect(controller.Msg): self.close = False controller.Msg.__init__(self) + def __eq__(self, other): + return self.get_state() == other.get_state() + + def load_state(self, state): + self.address = state + def get_state(self): return list(self.address) if self.address else None @classmethod def from_state(klass, state): - return klass(state) + if state: + return klass(state) + else: + return None def copy(self): return copy.copy(self) @@ -342,6 +375,10 @@ class Error(controller.Msg): self.timestamp = timestamp or time.time() controller.Msg.__init__(self) + def load_state(self, state): + self.msg = state["msg"] + self.timestamp = state["timestamp"] + def copy(self): return copy.copy(self) -- cgit v1.2.3