rename SetHeaders addon to ModifyHeaders

This commit is contained in:
Martin Plattner 2020-06-25 18:08:48 +02:00
parent bcffa674ba
commit 781e0a2e7c
7 changed files with 38 additions and 38 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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