From 0760607a7dd1ab6b904f618ecdda170cd042a21b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 3 Aug 2011 23:02:33 +1200 Subject: [PATCH] Further interface cleaning. --- libmproxy/console.py | 4 ++-- libmproxy/controller.py | 6 +++--- libmproxy/dump.py | 6 +++--- libmproxy/flow.py | 46 ++++++++++++++++------------------------- libmproxy/proxy.py | 20 +++++++++--------- test/test_flow.py | 10 ++++----- test/tutils.py | 2 +- 7 files changed, 42 insertions(+), 52 deletions(-) diff --git a/libmproxy/console.py b/libmproxy/console.py index 69b6d3e46..66efffe91 100644 --- a/libmproxy/console.py +++ b/libmproxy/console.py @@ -475,7 +475,7 @@ class ConnectionView(WWrap): elif part == "r" and self.state.view_flow_mode == VIEW_FLOW_REQUEST: if not conn.acked: response = flow.Response(conn, "200", "OK", flow.Headers(), "") - conn.ack(response) + conn._ack(response) self.view_response() self.master.refresh_connection(self.flow) @@ -1797,7 +1797,7 @@ class ConsoleMaster(flow.FlowMaster): if self.state.intercept and f.match(self.state.intercept) and not f.request.is_replay(): f.intercept() else: - r.ack() + r._ack() self.sync_list_view() self.refresh_connection(f) diff --git a/libmproxy/controller.py b/libmproxy/controller.py index e3d5c8342..524b25135 100644 --- a/libmproxy/controller.py +++ b/libmproxy/controller.py @@ -25,14 +25,14 @@ class Msg: self.q = Queue.Queue() self.acked = False - def ack(self, data=False): + def _ack(self, data=False): self.acked = True if data is None: self.q.put(data) else: self.q.put(data or self) - def send(self, masterq): + def _send(self, masterq): self.acked = False try: masterq.put(self, timeout=3) @@ -94,7 +94,7 @@ class Master: if m: m(msg) else: - msg.ack() + msg._ack() def shutdown(self): global should_exit diff --git a/libmproxy/dump.py b/libmproxy/dump.py index f0379b8bd..86f2fea2b 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -124,7 +124,7 @@ class DumpMaster(flow.FlowMaster): f = flow.FlowMaster.handle_request(self, r) self.add_event("Request: %s"%str_request(r)) if f: - r.ack() + r._ack() return f def indent(self, n, t): @@ -181,14 +181,14 @@ class DumpMaster(flow.FlowMaster): def handle_response(self, msg): f = flow.FlowMaster.handle_response(self, msg) if f: - msg.ack() + msg._ack() self._process_flow(f) return f def handle_error(self, msg): f = flow.FlowMaster.handle_error(self, msg) if f: - msg.ack() + msg._ack() self._process_flow(f) return f diff --git a/libmproxy/flow.py b/libmproxy/flow.py index 1decb7d55..42a234ecf 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -138,9 +138,9 @@ class Headers: class HTTPMsg(controller.Msg): def decode(self): """ - Alters Response object, decoding its content based on the current - Content-Encoding header and changing Content-Encoding header to - 'identity'. + Decodes content based on the current Content-Encoding header, then + removes the header. If there is no Content-Encoding header, no + action is taken. """ ce = self.headers["content-encoding"] if not ce or ce[0] not in encoding.ENCODINGS: @@ -153,10 +153,10 @@ class HTTPMsg(controller.Msg): def encode(self, e): """ - Alters Response object, encoding its content with the specified - coding. This method should only be called on Responses with - Content-Encoding headers of 'identity'. + Encodes content with the encoding e, where e is "gzip", "deflate" + or "identity". """ + # FIXME: Error if there's an existing encoding header? self.content = encoding.encode(e, self.content) self.headers["content-encoding"] = [e] @@ -289,7 +289,7 @@ class Request(HTTPMsg): def is_response(self): return False - def assemble(self, _proxy = False): + def _assemble(self, _proxy = False): """ Assembles the request for transmission to the server. We make some modifications to make sure interception works properly. @@ -436,7 +436,7 @@ class Response(HTTPMsg): def is_response(self): return True - def assemble(self): + def _assemble(self): """ Assembles the response for transmission to the client. We make some modifications to make sure interception works properly. @@ -546,16 +546,6 @@ class Error(controller.Msg): return c - - - - - - - - - - class ClientPlaybackState: def __init__(self, flows, exit): self.flows, self.exit = flows, exit @@ -788,9 +778,9 @@ class Flow: def kill(self, master): self.error = Error(self.request, "Connection killed") if self.request and not self.request.acked: - self.request.ack(None) + self.request._ack(None) elif self.response and not self.response.acked: - self.response.ack(None) + self.response._ack(None) master.handle_error(self.error) self.intercepting = False @@ -800,9 +790,9 @@ class Flow: def accept_intercept(self): if self.request: if not self.request.acked: - self.request.ack() + self.request._ack() elif self.response and not self.response.acked: - self.response.ack() + self.response._ack() self.intercepting = False def replace(self, pattern, repl, *args, **kwargs): @@ -1049,7 +1039,7 @@ class FlowMaster(controller.Master): flow.response = response if self.refresh_server_playback: response.refresh() - flow.request.ack(response) + flow.request._ack(response) return True return None @@ -1099,7 +1089,7 @@ class FlowMaster(controller.Master): if self.kill_nonreplay: f.kill(self) else: - f.request.ack() + f.request._ack() def process_new_response(self, f): if self.stickycookie_state: @@ -1133,7 +1123,7 @@ class FlowMaster(controller.Master): def handle_clientconnect(self, cc): self.run_script("clientconnect", cc) self.add_event("Connect from: %s:%s"%cc.address) - cc.ack() + cc._ack() def handle_clientdisconnect(self, r): self.run_script("clientdisconnect", r) @@ -1146,7 +1136,7 @@ class FlowMaster(controller.Master): self.add_event( " -> error: %s"%r.client_conn.connection_error, "error" ) - r.ack() + r._ack() def handle_error(self, r): f = self.state.add_error(r) @@ -1154,7 +1144,7 @@ class FlowMaster(controller.Master): self.run_script("error", f) if self.client_playback: self.client_playback.clear(f) - r.ack() + r._ack() return f def handle_request(self, r): @@ -1170,7 +1160,7 @@ class FlowMaster(controller.Master): if self.client_playback: self.client_playback.clear(f) if not f: - r.ack() + r._ack() self.process_new_response(f) return f diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index fbc0420c6..81b5aeb4e 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -151,10 +151,10 @@ class RequestReplayThread(threading.Thread): server = ServerConnection(self.flow.request) server.send_request(self.flow.request) response = server.read_response() - response.send(self.masterq) + response._send(self.masterq) except ProxyError, v: err = flow.Error(self.flow.request, v.msg) - err.send(self.masterq) + err._send(self.masterq) class ServerConnection: @@ -182,7 +182,7 @@ class ServerConnection: self.request = request request.close = self.close try: - self.wfile.write(request.assemble()) + self.wfile.write(request._assemble()) self.wfile.flush() except socket.error, err: raise ProxyError(504, 'Error sending data to "%s": %s' % (request.host, err)) @@ -225,11 +225,11 @@ class ProxyHandler(SocketServer.StreamRequestHandler): def handle(self): cc = flow.ClientConnect(self.client_address) - cc.send(self.mqueue) + cc._send(self.mqueue) while not cc.close: self.handle_request(cc) cd = flow.ClientDisconnect(cc) - cd.send(self.mqueue) + cd._send(self.mqueue) self.finish() def handle_request(self, cc): @@ -243,7 +243,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): cc.close = True return cc.requestcount += 1 - request = request.send(self.mqueue) + request = request._send(self.mqueue) if request is None: cc.close = True return @@ -251,7 +251,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): if request.is_response(): response = request request = False - response = response.send(self.mqueue) + response = response._send(self.mqueue) else: server = ServerConnection(request) server.send_request(request) @@ -259,7 +259,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): response = server.read_response() except IOError, v: raise IOError, "Reading response: %s"%v - response = response.send(self.mqueue) + response = response._send(self.mqueue) if response is None: server.terminate() if response is None: @@ -274,7 +274,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): cc.connection_error = "%s: %s"%(e.code, e.msg) if request: err = flow.Error(request, e.msg) - err.send(self.mqueue) + err._send(self.mqueue) self.send_error(e.code, e.msg) if server: server.terminate() @@ -364,7 +364,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): return flow.Request(client_conn, host, port, scheme, method, path, headers, content) def send_response(self, response): - self.wfile.write(response.assemble()) + self.wfile.write(response._assemble()) self.wfile.flush() def terminate(self, connection, wfile, rfile): diff --git a/test/test_flow.py b/test/test_flow.py index 0cfcbc3c1..4c4f7bf88 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -179,7 +179,7 @@ class uFlow(libpry.AutoTree): f.intercept() f.response = tutils.tresp() f.request = f.response.request - f.request.ack() + f.request._ack() assert not f.response.acked f.kill(fm) assert f.response.acked @@ -210,7 +210,7 @@ class uFlow(libpry.AutoTree): f.response = tutils.tresp() f.request = f.response.request f.intercept() - f.request.ack() + f.request._ack() assert not f.response.acked f.accept_intercept() assert f.response.acked @@ -411,7 +411,7 @@ class uSerialize(libpry.AutoTree): f2 = l[0] assert f2._get_state() == f._get_state() - assert f2.request.assemble() == f.request.assemble() + assert f2.request._assemble() == f.request._assemble() def test_load_flows(self): r = self._treader() @@ -575,7 +575,7 @@ class uRequest(libpry.AutoTree): assert r.set_url(u) assert not r.set_url("") assert r.url() == u - assert r.assemble() + assert r._assemble() r2 = r.copy() assert r == r2 @@ -653,7 +653,7 @@ class uResponse(libpry.AutoTree): c = flow.ClientConnect(("addr", 2222)) req = flow.Request(c, "host", 22, "https", "GET", "/", h, "content") resp = flow.Response(req, 200, "msg", h.copy(), "content") - assert resp.assemble() + assert resp._assemble() resp2 = resp.copy() assert resp2 == resp diff --git a/test/tutils.py b/test/tutils.py index a4c5b1fac..f7a495771 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -60,7 +60,7 @@ class TestMaster(controller.Master): def handle(self, m): self.log.append(m) - m.ack() + m._ack() class ProxyThread(threading.Thread):