From 143bf0dfa9138a4340287d636bb68648665b3829 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 14 Jul 2016 11:24:06 +1200 Subject: [PATCH] AntiCache to addon --- mitmproxy/builtins/__init__.py | 2 ++ mitmproxy/builtins/anticache.py | 13 +++++++++++++ mitmproxy/console/options.py | 8 ++++---- mitmproxy/console/statusbar.py | 2 +- mitmproxy/dump.py | 2 -- mitmproxy/flow/master.py | 4 ---- test/mitmproxy/builtins/test_anticache.py | 23 +++++++++++++++++++++++ test/mitmproxy/test_flow.py | 10 ---------- 8 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 mitmproxy/builtins/anticache.py create mode 100644 test/mitmproxy/builtins/test_anticache.py diff --git a/mitmproxy/builtins/__init__.py b/mitmproxy/builtins/__init__.py index 867ebb22b..b54193785 100644 --- a/mitmproxy/builtins/__init__.py +++ b/mitmproxy/builtins/__init__.py @@ -1,11 +1,13 @@ from __future__ import absolute_import, print_function, division +from mitmproxy.builtins import anticache from mitmproxy.builtins import anticomp from mitmproxy.builtins import stickyauth def default_addons(): return [ + anticache.AntiCache(), anticomp.AntiComp(), stickyauth.StickyAuth(), ] diff --git a/mitmproxy/builtins/anticache.py b/mitmproxy/builtins/anticache.py new file mode 100644 index 000000000..f208e2fb6 --- /dev/null +++ b/mitmproxy/builtins/anticache.py @@ -0,0 +1,13 @@ +from __future__ import absolute_import, print_function, division + + +class AntiCache: + def __init__(self): + self.enabled = False + + def configure(self, options): + self.enabled = options.anticache + + def request(self, flow): + if self.enabled: + flow.request.anticache() diff --git a/mitmproxy/console/options.py b/mitmproxy/console/options.py index f63428140..c76a058f5 100644 --- a/mitmproxy/console/options.py +++ b/mitmproxy/console/options.py @@ -96,7 +96,7 @@ class Options(urwid.WidgetWrap): select.Option( "Anti-Cache", "a", - lambda: master.anticache, + lambda: master.options.anticache, self.toggle_anticache ), select.Option( @@ -152,7 +152,6 @@ class Options(urwid.WidgetWrap): return super(self.__class__, self).keypress(size, key) def clearall(self): - self.master.anticache = False self.master.killextra = False self.master.showhost = False self.master.refresh_server_playback = True @@ -164,8 +163,9 @@ class Options(urwid.WidgetWrap): self.master.scripts = [] self.master.set_stickycookie(None) - self.master.options.stickyauth = None + self.master.options.anticache = False self.master.options.anticomp = False + self.master.options.stickyauth = None self.master.state.default_body_view = contentviews.get("Auto") @@ -176,7 +176,7 @@ class Options(urwid.WidgetWrap): ) def toggle_anticache(self): - self.master.anticache = not self.master.anticache + self.master.options.anticache = not self.master.options.anticache def toggle_anticomp(self): self.master.options.anticomp = not self.master.options.anticomp diff --git a/mitmproxy/console/statusbar.py b/mitmproxy/console/statusbar.py index d0a240184..1357d7ca3 100644 --- a/mitmproxy/console/statusbar.py +++ b/mitmproxy/console/statusbar.py @@ -187,7 +187,7 @@ class StatusBar(urwid.WidgetWrap): r.append(":%s]" % self.master.state.default_body_view.name) opts = [] - if self.master.anticache: + if self.master.options.anticache: opts.append("anticache") if self.master.options.anticomp: opts.append("anticomp") diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index f69e3777c..b953d1316 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -63,8 +63,6 @@ class DumpMaster(flow.FlowMaster): self.addons.add(*builtins.default_addons()) self.outfile = outfile self.o = options - self.anticache = options.anticache - self.anticomp = options.anticomp self.showhost = options.showhost self.replay_ignore_params = options.replay_ignore_params self.replay_ignore_content = options.replay_ignore_content diff --git a/mitmproxy/flow/master.py b/mitmproxy/flow/master.py index e469c4991..06e1b4600 100644 --- a/mitmproxy/flow/master.py +++ b/mitmproxy/flow/master.py @@ -42,7 +42,6 @@ class FlowMaster(controller.Master): self.stickycookie_state = None # type: Optional[modules.StickyCookieState] self.stickycookie_txt = None - self.anticache = False self.stream_large_bodies = None # type: Optional[modules.StreamLargeBodies] self.refresh_server_playback = False self.replacehooks = modules.ReplaceHooks() @@ -313,9 +312,6 @@ class FlowMaster(controller.Master): if self.stickycookie_state: self.stickycookie_state.handle_request(f) - if self.anticache: - f.request.anticache() - if self.server_playback: pb = self.do_server_playback(f) if not pb and self.kill_nonreplay: diff --git a/test/mitmproxy/builtins/test_anticache.py b/test/mitmproxy/builtins/test_anticache.py new file mode 100644 index 000000000..5a00af03d --- /dev/null +++ b/test/mitmproxy/builtins/test_anticache.py @@ -0,0 +1,23 @@ +from .. import tutils, mastertest +from mitmproxy.builtins import anticache +from mitmproxy.flow import master +from mitmproxy.flow import state +from mitmproxy import options + + +class TestAntiCache(mastertest.MasterTest): + def test_simple(self): + s = state.State() + m = master.FlowMaster(options.Options(anticache = True), None, s) + sa = anticache.AntiCache() + m.addons.add(sa) + + f = tutils.tflow(resp=True) + self.invoke(m, "request", f) + + f = tutils.tflow(resp=True) + f.request.headers["if-modified-since"] = "test" + f.request.headers["if-none-match"] = "test" + self.invoke(m, "request", f) + assert "if-modified-since" not in f.request.headers + assert "if-none-match" not in f.request.headers diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index a6a3038c9..585dbf932 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -855,7 +855,6 @@ class TestFlowMaster: def test_all(self): s = flow.State() fm = flow.FlowMaster(None, None, s) - fm.anticache = True f = tutils.tflow(req=None) fm.clientconnect(f.client_conn) f.request = HTTPRequest.wrap(netlib.tutils.treq()) @@ -1053,15 +1052,6 @@ class TestRequest: assert r.url == "https://address:22/path" assert r.pretty_url == "https://foo.com:22/path" - def test_anticache(self): - r = HTTPRequest.wrap(netlib.tutils.treq()) - r.headers = Headers() - r.headers["if-modified-since"] = "test" - r.headers["if-none-match"] = "test" - r.anticache() - assert "if-modified-since" not in r.headers - assert "if-none-match" not in r.headers - def test_replace(self): r = HTTPRequest.wrap(netlib.tutils.treq()) r.path = "path/foo"