Merge pull request #1351 from cortesi/anticache

AntiCache to addon
This commit is contained in:
Aldo Cortesi 2016-07-14 12:09:21 +12:00 committed by GitHub
commit c0cd01b6e3
8 changed files with 43 additions and 21 deletions

View File

@ -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(),
]

View File

@ -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()

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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"