aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/console/common.py2
-rw-r--r--mitmproxy/console/flowlist.py6
-rw-r--r--mitmproxy/console/flowview.py4
-rw-r--r--mitmproxy/console/grideditor.py2
-rw-r--r--mitmproxy/console/master.py8
-rw-r--r--mitmproxy/console/options.py46
-rw-r--r--mitmproxy/console/statusbar.py6
-rw-r--r--mitmproxy/flow/master.py3
-rw-r--r--mitmproxy/options.py13
-rw-r--r--test/mitmproxy/test_flow.py8
-rw-r--r--test/mitmproxy/test_options.py11
11 files changed, 56 insertions, 53 deletions
diff --git a/mitmproxy/console/common.py b/mitmproxy/console/common.py
index f15031c2..5d15e0cd 100644
--- a/mitmproxy/console/common.py
+++ b/mitmproxy/console/common.py
@@ -216,7 +216,7 @@ def save_data(path, data):
if not path:
return
try:
- with file(path, "wb") as f:
+ with open(path, "wb") as f:
f.write(data)
except IOError as v:
signals.status_message.send(message=v.strerror)
diff --git a/mitmproxy/console/flowlist.py b/mitmproxy/console/flowlist.py
index ebbe8d21..bc523874 100644
--- a/mitmproxy/console/flowlist.py
+++ b/mitmproxy/console/flowlist.py
@@ -118,7 +118,7 @@ class ConnectionItem(urwid.WidgetWrap):
return common.format_flow(
self.flow,
self.f,
- hostheader = self.master.showhost,
+ hostheader = self.master.options.showhost,
marked=self.state.flow_marked(self.flow)
)
@@ -151,7 +151,7 @@ class ConnectionItem(urwid.WidgetWrap):
if k == "a":
self.master.start_server_playback(
[i.copy() for i in self.master.state.view],
- self.master.killextra, self.master.rheaders,
+ self.master.options.kill, self.master.rheaders,
False, self.master.nopop,
self.master.options.replay_ignore_params,
self.master.options.replay_ignore_content,
@@ -161,7 +161,7 @@ class ConnectionItem(urwid.WidgetWrap):
elif k == "t":
self.master.start_server_playback(
[self.flow.copy()],
- self.master.killextra, self.master.rheaders,
+ self.master.options.kill, self.master.rheaders,
False, self.master.nopop,
self.master.options.replay_ignore_params,
self.master.options.replay_ignore_content,
diff --git a/mitmproxy/console/flowview.py b/mitmproxy/console/flowview.py
index d13e9db0..c85a9f73 100644
--- a/mitmproxy/console/flowview.py
+++ b/mitmproxy/console/flowview.py
@@ -110,7 +110,7 @@ class FlowViewHeader(urwid.WidgetWrap):
f,
False,
extended=True,
- hostheader=self.master.showhost
+ hostheader=self.master.options.showhost
)
signals.flow_change.connect(self.sig_flow_change)
@@ -120,7 +120,7 @@ class FlowViewHeader(urwid.WidgetWrap):
flow,
False,
extended=True,
- hostheader=self.master.showhost
+ hostheader=self.master.options.showhost
)
diff --git a/mitmproxy/console/grideditor.py b/mitmproxy/console/grideditor.py
index f304de57..87700fd7 100644
--- a/mitmproxy/console/grideditor.py
+++ b/mitmproxy/console/grideditor.py
@@ -396,7 +396,7 @@ class GridEditor(urwid.WidgetWrap):
if p:
try:
p = os.path.expanduser(p)
- d = file(p, "rb").read()
+ d = open(p, "rb").read()
self.walker.set_current_value(d, unescaped)
self.walker._modified()
except IOError as v:
diff --git a/mitmproxy/console/master.py b/mitmproxy/console/master.py
index a93eeddf..25a0b83f 100644
--- a/mitmproxy/console/master.py
+++ b/mitmproxy/console/master.py
@@ -220,12 +220,8 @@ class ConsoleMaster(flow.FlowMaster):
self.set_stream_large_bodies(options.stream_large_bodies)
- self.refresh_server_playback = options.refresh_server_playback
- self.anticache = options.anticache
- self.killextra = options.kill
self.rheaders = options.rheaders
self.nopop = options.nopop
- self.showhost = options.showhost
self.palette = options.palette
self.palette_transparent = options.palette_transparent
@@ -373,7 +369,7 @@ class ConsoleMaster(flow.FlowMaster):
if flows:
self.start_server_playback(
flows,
- self.killextra, self.rheaders,
+ self.options.kill, self.rheaders,
False, self.nopop,
self.options.replay_ignore_params,
self.options.replay_ignore_content,
@@ -609,7 +605,7 @@ class ConsoleMaster(flow.FlowMaster):
return
path = os.path.expanduser(path)
try:
- f = file(path, "wb")
+ f = open(path, "wb")
fw = flow.FlowWriter(f)
for i in flows:
fw.add(i)
diff --git a/mitmproxy/console/options.py b/mitmproxy/console/options.py
index afb9186d..e1dd29ee 100644
--- a/mitmproxy/console/options.py
+++ b/mitmproxy/console/options.py
@@ -74,8 +74,8 @@ class Options(urwid.WidgetWrap):
select.Option(
"Show Host",
"w",
- lambda: master.showhost,
- self.toggle_showhost
+ lambda: master.options.showhost,
+ master.options.toggler("showhost")
),
select.Heading("Network"),
@@ -97,25 +97,25 @@ class Options(urwid.WidgetWrap):
"Anti-Cache",
"a",
lambda: master.options.anticache,
- self.toggle_anticache
+ master.options.toggler("anticache")
),
select.Option(
"Anti-Compression",
"o",
lambda: master.options.anticomp,
- self.toggle_anticomp
+ master.options.toggler("anticomp")
),
select.Option(
"Kill Extra",
"x",
- lambda: master.killextra,
- self.toggle_killextra
+ lambda: master.options.kill,
+ master.options.toggler("kill")
),
select.Option(
"No Refresh",
"f",
- lambda: not master.refresh_server_playback,
- self.toggle_refresh_server_playback
+ lambda: not master.options.refresh_server_playback,
+ master.options.toggler("refresh_server_playback")
),
select.Option(
"Sticky Auth",
@@ -152,9 +152,6 @@ class Options(urwid.WidgetWrap):
return super(self.__class__, self).keypress(size, key)
def clearall(self):
- self.master.killextra = False
- self.master.showhost = False
- self.master.refresh_server_playback = True
self.master.server.config.no_upstream_cert = False
self.master.set_ignore_filter([])
self.master.set_tcp_filter([])
@@ -162,9 +159,12 @@ class Options(urwid.WidgetWrap):
self.master.options.update(
anticache = False,
anticomp = False,
+ kill = False,
+ refresh_server_playback = True,
replacements = [],
scripts = [],
setheaders = [],
+ showhost = False,
stickyauth = None,
stickycookie = None
)
@@ -177,33 +177,16 @@ class Options(urwid.WidgetWrap):
expire = 1
)
- def toggle_anticache(self):
- self.master.options.anticache = not self.master.options.anticache
-
- def toggle_anticomp(self):
- self.master.options.anticomp = not self.master.options.anticomp
-
- def toggle_killextra(self):
- self.master.killextra = not self.master.killextra
-
- def toggle_showhost(self):
- self.master.showhost = not self.master.showhost
-
- def toggle_refresh_server_playback(self):
- self.master.refresh_server_playback = not self.master.refresh_server_playback
-
def toggle_upstream_cert(self):
self.master.server.config.no_upstream_cert = not self.master.server.config.no_upstream_cert
signals.update_settings.send(self)
def setheaders(self):
- def _set(shdrs):
- self.master.options.setheaders = shdrs
self.master.view_grideditor(
grideditor.SetHeadersEditor(
self.master,
self.master.options.setheaders,
- _set
+ self.master.options.setter("setheaders")
)
)
@@ -219,14 +202,11 @@ class Options(urwid.WidgetWrap):
)
def replacepatterns(self):
- def _set(replacements):
- self.master.options.replacements = replacements
- signals.update_settings.send(self)
self.master.view_grideditor(
grideditor.ReplaceEditor(
self.master,
self.master.options.replacements,
- _set
+ self.master.options.setter("replacements")
)
)
diff --git a/mitmproxy/console/statusbar.py b/mitmproxy/console/statusbar.py
index 88e2ad0d..8f039e48 100644
--- a/mitmproxy/console/statusbar.py
+++ b/mitmproxy/console/statusbar.py
@@ -194,11 +194,11 @@ class StatusBar(urwid.WidgetWrap):
opts.append("anticache")
if self.master.options.anticomp:
opts.append("anticomp")
- if self.master.showhost:
+ if self.master.options.showhost:
opts.append("showhost")
- if not self.master.refresh_server_playback:
+ if not self.master.options.refresh_server_playback:
opts.append("norefresh")
- if self.master.killextra:
+ if self.master.options.kill:
opts.append("killextra")
if self.master.server.config.no_upstream_cert:
opts.append("no-upstream-cert")
diff --git a/mitmproxy/flow/master.py b/mitmproxy/flow/master.py
index 06ec18b3..64a242ba 100644
--- a/mitmproxy/flow/master.py
+++ b/mitmproxy/flow/master.py
@@ -35,7 +35,6 @@ class FlowMaster(controller.Master):
self.kill_nonreplay = False
self.stream_large_bodies = None # type: Optional[modules.StreamLargeBodies]
- self.refresh_server_playback = False
self.replay_ignore_params = False
self.replay_ignore_content = None
self.replay_ignore_host = False
@@ -120,7 +119,7 @@ class FlowMaster(controller.Master):
return None
response = rflow.response.copy()
response.is_replay = True
- if self.refresh_server_playback:
+ if self.options.refresh_server_playback:
response.refresh()
flow.response = response
return True
diff --git a/mitmproxy/options.py b/mitmproxy/options.py
index a124eaf6..04353dca 100644
--- a/mitmproxy/options.py
+++ b/mitmproxy/options.py
@@ -76,10 +76,23 @@ class Options(object):
self.changed.send(self)
def setter(self, attr):
+ """
+ Generate a setter for a given attribute. This returns a callable
+ taking a single argument.
+ """
if attr not in self._opts:
raise KeyError("No such option: %s" % attr)
return lambda x: self.__setattr__(attr, x)
+ def toggler(self, attr):
+ """
+ Generate a toggler for a boolean attribute. This returns a callable
+ that takes no arguments.
+ """
+ if attr not in self._opts:
+ raise KeyError("No such option: %s" % attr)
+ return lambda: self.__setattr__(attr, not getattr(self, attr))
+
def __repr__(self):
options = pprint.pformat(self._opts, indent=4).strip(" {}")
if "\n" in options:
diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py
index 03935c16..90f7f915 100644
--- a/test/mitmproxy/test_flow.py
+++ b/test/mitmproxy/test_flow.py
@@ -751,7 +751,11 @@ class TestFlowMaster:
f = tutils.tflow(resp=True)
pb = [tutils.tflow(resp=True), f]
- fm = flow.FlowMaster(None, DummyServer(ProxyConfig()), s)
+ fm = flow.FlowMaster(
+ flow.options.Options(),
+ DummyServer(ProxyConfig()),
+ s
+ )
assert not fm.start_server_playback(
pb,
False,
@@ -779,7 +783,7 @@ class TestFlowMaster:
f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request))
pb = [f]
- fm = flow.FlowMaster(None, None, s)
+ fm = flow.FlowMaster(flow.options.Options(), None, s)
fm.refresh_server_playback = True
assert not fm.do_server_playback(tutils.tflow())
diff --git a/test/mitmproxy/test_options.py b/test/mitmproxy/test_options.py
index cdb0d765..af619b27 100644
--- a/test/mitmproxy/test_options.py
+++ b/test/mitmproxy/test_options.py
@@ -52,6 +52,17 @@ def test_setter():
o.setter("nonexistent")
+def test_toggler():
+ o = TO(two=True)
+ f = o.toggler("two")
+ f()
+ assert o.two is False
+ f()
+ assert o.two is True
+ with tutils.raises("no such option"):
+ o.toggler("nonexistent")
+
+
def test_rollback():
o = TO(one="two")