mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-01 15:55:28 +00:00
revert modify headers parameter order
This commit is contained in:
parent
4cffbec291
commit
48dcc6e073
@ -88,17 +88,16 @@ New headers can be added, and existing headers can be overwritten or removed.
|
||||
A `modify_headers` expression looks like this:
|
||||
|
||||
{{< highlight none >}}
|
||||
/name/value[/filter-expression]
|
||||
[/patt]/name/value
|
||||
{{< / highlight >}}
|
||||
|
||||
Here, **name** and **value** are the header name and the value to set respectively,
|
||||
e.g., ``/Host/example.org``. An empty **value** removes existing headers with
|
||||
**name**, e.g., ``/Host/``. The optional **filter-expression** is a mitmproxy
|
||||
[filter expression]({{< relref "concepts-filters">}}) that defines
|
||||
which flows to modify headers on, e.g., only on responses using ``~s``.
|
||||
Existing headers are overwritten by default.
|
||||
This can be changed using filter-expressions, e.g., ``!~h Host:`` to ignore
|
||||
requests and responses with an existing ``Host`` header.
|
||||
Here, **patt** is a mitmproxy [filter expression]({{< relref "concepts-filters">}})
|
||||
that defines which flows to modify headers on, e.g., only on responses using ``~s``.
|
||||
The parameters **name** and **value** are the header name and the value to set
|
||||
respectively, e.g., ``/Host/example.org``. An empty **value** removes existing
|
||||
headers with **name**, e.g., ``/Host/``. Existing headers are overwritten by
|
||||
default. This can be changed using filter-expressions, e.g., ``!~h Host:`` to
|
||||
ignore requests and responses with an existing ``Host`` header.
|
||||
|
||||
|
||||
## Proxy Authentication
|
||||
|
@ -7,23 +7,23 @@ from mitmproxy import ctx
|
||||
|
||||
def parse_modify_headers(s):
|
||||
"""
|
||||
Returns a (header_name, header_value, flow_filter) tuple.
|
||||
Returns a (flow_filter, header_name, header_value) tuple.
|
||||
|
||||
The general form for a modify_headers hook is as follows:
|
||||
|
||||
/header_name/header_value/flow_filter
|
||||
[/flow_filter]/header_name/header_value
|
||||
|
||||
The first character specifies the separator. Example:
|
||||
|
||||
:foo:bar:~q
|
||||
:~q:foo:bar
|
||||
|
||||
If only two clauses are specified, the pattern is set to match
|
||||
universally (i.e. ".*"). Example:
|
||||
|
||||
/foo/bar/
|
||||
/foo/bar
|
||||
|
||||
Clauses are parsed from left to right. Extra separators are taken to be
|
||||
part of the final clause. For instance, the flow filter below is
|
||||
part of the final clause. For instance, the replacement clause below is
|
||||
"foo/bar/":
|
||||
|
||||
/one/two/foo/bar/
|
||||
@ -34,12 +34,12 @@ def parse_modify_headers(s):
|
||||
flow_filter = ".*"
|
||||
header_name, header_value = parts
|
||||
elif len(parts) == 3:
|
||||
header_name, header_value, flow_filter = parts
|
||||
flow_filter, header_name, header_value = parts
|
||||
else:
|
||||
raise exceptions.OptionsError(
|
||||
"Invalid replacement specifier: %s" % s
|
||||
"Invalid modify_headers specifier: %s" % s
|
||||
)
|
||||
return header_name, header_value, flow_filter
|
||||
return flow_filter, header_name, header_value
|
||||
|
||||
|
||||
class ModifyHeaders:
|
||||
@ -50,7 +50,7 @@ class ModifyHeaders:
|
||||
loader.add_option(
|
||||
"modify_headers", typing.Sequence[str], [],
|
||||
"""
|
||||
Header modify pattern of the form "/header-name/header-value[/flow-filter]", where the
|
||||
Header modify pattern of the form "[/flow-filter]/header-name/header-value", where the
|
||||
separator can be any character. An empty header-value removes existing header-name headers.
|
||||
"""
|
||||
)
|
||||
@ -59,20 +59,20 @@ class ModifyHeaders:
|
||||
if "modify_headers" in updated:
|
||||
self.lst = []
|
||||
for shead in ctx.options.modify_headers:
|
||||
header, value, flow_pattern = parse_modify_headers(shead)
|
||||
flow_pattern, header, value = parse_modify_headers(shead)
|
||||
|
||||
flow_filter = flowfilter.parse(flow_pattern)
|
||||
if not flow_filter:
|
||||
raise exceptions.OptionsError(
|
||||
"Invalid modify_headers flow filter %s" % flow_pattern
|
||||
)
|
||||
self.lst.append((header, value, flow_pattern, flow_filter))
|
||||
self.lst.append((flow_pattern, flow_filter, header, value))
|
||||
|
||||
def run(self, f, hdrs):
|
||||
for header, value, _, flow_filter in self.lst:
|
||||
for _, flow_filter, header, value in self.lst:
|
||||
if flow_filter(f):
|
||||
hdrs.pop(header, None)
|
||||
for header, value, _, flow_filter in self.lst:
|
||||
for _, flow_filter, header, value in self.lst:
|
||||
if flow_filter(f) and value:
|
||||
hdrs.add(header, value)
|
||||
|
||||
|
@ -13,15 +13,15 @@ class TestModifyHeaders:
|
||||
x = modifyheaders.parse_modify_headers("/foo/bar/vo/ing/")
|
||||
assert x == ("foo", "bar", "vo/ing/")
|
||||
x = modifyheaders.parse_modify_headers("/bar/voing")
|
||||
assert x == ("bar", "voing", ".*")
|
||||
with pytest.raises(Exception, match="Invalid replacement"):
|
||||
assert x == (".*", "bar", "voing")
|
||||
with pytest.raises(Exception, match="Invalid modify_headers specifier"):
|
||||
modifyheaders.parse_modify_headers("/")
|
||||
|
||||
def test_configure(self):
|
||||
sh = modifyheaders.ModifyHeaders()
|
||||
with taddons.context(sh) as tctx:
|
||||
with pytest.raises(Exception, match="Invalid modify_headers flow filter"):
|
||||
tctx.configure(sh, modify_headers = ["/one/two/~b"])
|
||||
tctx.configure(sh, modify_headers = ["/~b/one/two"])
|
||||
tctx.configure(sh, modify_headers = ["/foo/bar/voing"])
|
||||
|
||||
def test_modify_headers(self):
|
||||
@ -30,8 +30,8 @@ class TestModifyHeaders:
|
||||
tctx.configure(
|
||||
sh,
|
||||
modify_headers = [
|
||||
"/one/two/~q",
|
||||
"/one/three/~s"
|
||||
"/~q/one/two",
|
||||
"/~s/one/three"
|
||||
]
|
||||
)
|
||||
f = tflow.tflow()
|
||||
@ -47,8 +47,8 @@ class TestModifyHeaders:
|
||||
tctx.configure(
|
||||
sh,
|
||||
modify_headers = [
|
||||
"/one/two/~s",
|
||||
"/one/three/~s"
|
||||
"/~s/one/two",
|
||||
"/~s/one/three"
|
||||
]
|
||||
)
|
||||
f = tflow.tflow(resp=True)
|
||||
@ -60,8 +60,8 @@ class TestModifyHeaders:
|
||||
tctx.configure(
|
||||
sh,
|
||||
modify_headers = [
|
||||
"/one/two/~q",
|
||||
"/one/three/~q"
|
||||
"/~q/one/two",
|
||||
"/~q/one/three"
|
||||
]
|
||||
)
|
||||
f = tflow.tflow()
|
||||
@ -73,8 +73,8 @@ class TestModifyHeaders:
|
||||
tctx.configure(
|
||||
sh,
|
||||
modify_headers = [
|
||||
"/one//~q",
|
||||
"/one//~s"
|
||||
"/~q/one/",
|
||||
"/~s/one/"
|
||||
]
|
||||
)
|
||||
f = tflow.tflow()
|
||||
|
Loading…
Reference in New Issue
Block a user