diff --git a/docs/src/content/overview-features.md b/docs/src/content/overview-features.md index 5b1d3d511..00ba18663 100644 --- a/docs/src/content/overview-features.md +++ b/docs/src/content/overview-features.md @@ -131,11 +131,11 @@ then the respective recorded responses are simply replayed by mitmproxy. Otherwise, the unmatched requests is forwarded to the upstream server. If forwarding is not desired, you can use the --kill (-k) switch to prevent that. -## Set Headers +## Modify Headers -The `setheaders` option lets you specify a set of headers to be modified. +The `modify_headers` option lets you specify a set of headers to be modified. New headers can be added, and existing headers can be overwritten or removed. -A `setheaders` expression looks like this: +A `modify_headers` expression looks like this: {{< highlight none >}} /name/value[/filter-expression] diff --git a/mitmproxy/addons/__init__.py b/mitmproxy/addons/__init__.py index ee2389385..2673e7f82 100644 --- a/mitmproxy/addons/__init__.py +++ b/mitmproxy/addons/__init__.py @@ -14,7 +14,7 @@ from mitmproxy.addons import proxyauth from mitmproxy.addons import replace from mitmproxy.addons import script from mitmproxy.addons import serverplayback -from mitmproxy.addons import setheaders +from mitmproxy.addons import modifyheaders from mitmproxy.addons import stickyauth from mitmproxy.addons import stickycookie from mitmproxy.addons import streambodies @@ -40,7 +40,7 @@ def default_addons(): replace.Replace(), script.ScriptLoader(), serverplayback.ServerPlayback(), - setheaders.SetHeaders(), + modifyheaders.ModifyHeaders(), stickyauth.StickyAuth(), stickycookie.StickyCookie(), streambodies.StreamBodies(), diff --git a/mitmproxy/addons/setheaders.py b/mitmproxy/addons/modifyheaders.py similarity index 80% rename from mitmproxy/addons/setheaders.py rename to mitmproxy/addons/modifyheaders.py index 21100459f..043a66a22 100644 --- a/mitmproxy/addons/setheaders.py +++ b/mitmproxy/addons/modifyheaders.py @@ -5,11 +5,11 @@ from mitmproxy import flowfilter from mitmproxy import ctx -def parse_setheader(s): +def parse_modify_headers(s): """ Returns a (header_name, header_value, flow_filter) tuple. - The general form for a setheader hook is as follows: + The general form for a modify_headers hook is as follows: /header_name/header_value/flow_filter @@ -42,29 +42,29 @@ def parse_setheader(s): return header_name, header_value, flow_filter -class SetHeaders: +class ModifyHeaders: def __init__(self): self.lst = [] def load(self, loader): loader.add_option( - "setheaders", typing.Sequence[str], [], + "modify_headers", typing.Sequence[str], [], """ - Header set pattern of the form "/header-name/header-value[/flow-filter]", where the + Header modify pattern of the form "/header-name/header-value[/flow-filter]", where the separator can be any character. An empty header-value removes existing header-name headers. """ ) def configure(self, updated): - if "setheaders" in updated: + if "modify_headers" in updated: self.lst = [] - for shead in ctx.options.setheaders: - header, value, flow_pattern = parse_setheader(shead) + for shead in ctx.options.modify_headers: + header, value, flow_pattern = parse_modify_headers(shead) flow_filter = flowfilter.parse(flow_pattern) if not flow_filter: raise exceptions.OptionsError( - "Invalid setheader filter pattern %s" % flow_pattern + "Invalid modify_headers flow filter %s" % flow_pattern ) self.lst.append((header, value, flow_pattern, flow_filter)) diff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py index 9c8eae8ae..87b7ef2e7 100644 --- a/mitmproxy/tools/cmdline.py +++ b/mitmproxy/tools/cmdline.py @@ -85,9 +85,9 @@ def common_options(parser, opts): group = parser.add_argument_group("Replacements") opts.make_parser(group, "replacements", metavar="PATTERN", short="R") - # Set headers - group = parser.add_argument_group("Set Headers") - opts.make_parser(group, "setheaders", metavar="PATTERN", short="H") + # Modify headers + group = parser.add_argument_group("Modify Headers") + opts.make_parser(group, "modify_headers", metavar="PATTERN", short="H") def mitmproxy(opts): diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index 299d43ac3..6738e401e 100644 --- a/mitmproxy/tools/console/statusbar.py +++ b/mitmproxy/tools/console/statusbar.py @@ -206,7 +206,7 @@ class StatusBar(urwid.WidgetWrap): sreplay = self.master.commands.call("replay.server.count") creplay = self.master.commands.call("replay.client.count") - if len(self.master.options.setheaders): + if len(self.master.options.modify_headers): r.append("[") r.append(("heading_key", "H")) r.append("eaders]") diff --git a/test/mitmproxy/addons/test_setheaders.py b/test/mitmproxy/addons/test_modifyheaders.py similarity index 71% rename from test/mitmproxy/addons/test_setheaders.py rename to test/mitmproxy/addons/test_modifyheaders.py index 23676eaa4..285592937 100644 --- a/test/mitmproxy/addons/test_setheaders.py +++ b/test/mitmproxy/addons/test_modifyheaders.py @@ -3,33 +3,33 @@ import pytest from mitmproxy.test import tflow from mitmproxy.test import taddons -from mitmproxy.addons import setheaders +from mitmproxy.addons import modifyheaders -class TestSetHeaders: - def test_parse_setheaders(self): - x = setheaders.parse_setheader("/foo/bar/voing") +class TestModifyHeaders: + def test_parse_modifyheaders(self): + x = modifyheaders.parse_modify_headers("/foo/bar/voing") assert x == ("foo", "bar", "voing") - x = setheaders.parse_setheader("/foo/bar/vo/ing/") + x = modifyheaders.parse_modify_headers("/foo/bar/vo/ing/") assert x == ("foo", "bar", "vo/ing/") - x = setheaders.parse_setheader("/bar/voing") + x = modifyheaders.parse_modify_headers("/bar/voing") assert x == ("bar", "voing", ".*") with pytest.raises(Exception, match="Invalid replacement"): - setheaders.parse_setheader("/") + modifyheaders.parse_modify_headers("/") def test_configure(self): - sh = setheaders.SetHeaders() + sh = modifyheaders.ModifyHeaders() with taddons.context(sh) as tctx: - with pytest.raises(Exception, match="Invalid setheader filter pattern"): - tctx.configure(sh, setheaders = ["/one/two/~b"]) - tctx.configure(sh, setheaders = ["/foo/bar/voing"]) + with pytest.raises(Exception, match="Invalid modify_headers flow filter"): + tctx.configure(sh, modify_headers = ["/one/two/~b"]) + tctx.configure(sh, modify_headers = ["/foo/bar/voing"]) - def test_setheaders(self): - sh = setheaders.SetHeaders() + def test_modify_headers(self): + sh = modifyheaders.ModifyHeaders() with taddons.context(sh) as tctx: tctx.configure( sh, - setheaders = [ + modify_headers = [ "/one/two/~q", "/one/three/~s" ] @@ -46,7 +46,7 @@ class TestSetHeaders: tctx.configure( sh, - setheaders = [ + modify_headers = [ "/one/two/~s", "/one/three/~s" ] @@ -59,7 +59,7 @@ class TestSetHeaders: tctx.configure( sh, - setheaders = [ + modify_headers = [ "/one/two/~q", "/one/three/~q" ] @@ -69,10 +69,10 @@ class TestSetHeaders: sh.request(f) assert f.request.headers.get_all("one") == ["two", "three"] - # test removal of existing header + # test removal of existing headers tctx.configure( sh, - setheaders = [ + modify_headers = [ "/one//~q", "/one//~s" ] @@ -89,7 +89,7 @@ class TestSetHeaders: tctx.configure( sh, - setheaders = [ + modify_headers = [ "/one/" ] ) diff --git a/test/mitmproxy/tools/console/test_statusbar.py b/test/mitmproxy/tools/console/test_statusbar.py index f1cc67d45..dc5dc807e 100644 --- a/test/mitmproxy/tools/console/test_statusbar.py +++ b/test/mitmproxy/tools/console/test_statusbar.py @@ -8,7 +8,7 @@ def test_statusbar(monkeypatch): o = options.Options() m = master.ConsoleMaster(o) m.options.update( - setheaders=[":~q:foo:bar"], + modify_headers=[":~q:foo:bar"], replacements=[":~q:foo:bar"], ignore_hosts=["example.com", "example.org"], tcp_hosts=["example.tcp"],