mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
parent
ce28721458
commit
db658b12ed
@ -35,6 +35,37 @@ class View:
|
|||||||
raise NotImplementedError() # pragma: no cover
|
raise NotImplementedError() # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
|
def format_pairs(
|
||||||
|
items: typing.Iterable[typing.Tuple[TTextType, TTextType]]
|
||||||
|
)-> typing.Iterator[TViewLine]:
|
||||||
|
|
||||||
|
"""
|
||||||
|
Helper function that accepts a list of (k,v) pairs into a list of
|
||||||
|
[
|
||||||
|
("key", key )
|
||||||
|
("value", value)
|
||||||
|
]
|
||||||
|
where key is padded to a uniform width
|
||||||
|
"""
|
||||||
|
|
||||||
|
max_key_len = max((len(k[0]) for k in items), default=0)
|
||||||
|
max_key_len = min((max_key_len, KEY_MAX), default=0)
|
||||||
|
|
||||||
|
for key, value in items:
|
||||||
|
if isinstance(key, bytes):
|
||||||
|
|
||||||
|
key += b":"
|
||||||
|
else:
|
||||||
|
key += ":"
|
||||||
|
|
||||||
|
key = key.ljust(max_key_len + 2)
|
||||||
|
|
||||||
|
yield [
|
||||||
|
("header", key),
|
||||||
|
("text", value)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def format_dict(
|
def format_dict(
|
||||||
d: typing.Mapping[TTextType, TTextType]
|
d: typing.Mapping[TTextType, TTextType]
|
||||||
) -> typing.Iterator[TViewLine]:
|
) -> typing.Iterator[TViewLine]:
|
||||||
@ -47,18 +78,7 @@ def format_dict(
|
|||||||
entries, where key is padded to a uniform width.
|
entries, where key is padded to a uniform width.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
max_key_len = max((len(k) for k in d.keys()), default=0)
|
return format_pairs(d.items())
|
||||||
max_key_len = min((max_key_len, KEY_MAX), default=0)
|
|
||||||
for key, value in d.items():
|
|
||||||
if isinstance(key, bytes):
|
|
||||||
key += b":"
|
|
||||||
else:
|
|
||||||
key += ":"
|
|
||||||
key = key.ljust(max_key_len + 2)
|
|
||||||
yield [
|
|
||||||
("header", key),
|
|
||||||
("text", value)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def format_text(text: TTextType) -> typing.Iterator[TViewLine]:
|
def format_text(text: TTextType) -> typing.Iterator[TViewLine]:
|
||||||
|
@ -9,6 +9,6 @@ class ViewQuery(base.View):
|
|||||||
def __call__(self, data, **metadata):
|
def __call__(self, data, **metadata):
|
||||||
query = metadata.get("query")
|
query = metadata.get("query")
|
||||||
if query:
|
if query:
|
||||||
return "Query", base.format_dict(query)
|
return "Query", base.format_pairs(query.items(multi=True))
|
||||||
else:
|
else:
|
||||||
return "Query", base.format_text("")
|
return "Query", base.format_text("")
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from mitmproxy.net.http import url
|
from mitmproxy.net.http import url
|
||||||
from mitmproxy.coretypes import multidict
|
|
||||||
from . import base
|
from . import base
|
||||||
|
|
||||||
|
|
||||||
@ -13,4 +12,4 @@ class ViewURLEncoded(base.View):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
d = url.decode(data)
|
d = url.decode(data)
|
||||||
return "URLEncoded form", base.format_dict(multidict.MultiDict(d))
|
return "URLEncoded form", base.format_pairs(d)
|
||||||
|
@ -15,3 +15,18 @@ def test_format_dict():
|
|||||||
f_d = base.format_dict(d)
|
f_d = base.format_dict(d)
|
||||||
with pytest.raises(StopIteration):
|
with pytest.raises(StopIteration):
|
||||||
next(f_d)
|
next(f_d)
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_pairs():
|
||||||
|
d = [("a", "c"), ("b", "d")]
|
||||||
|
f_d = base.format_pairs(d)
|
||||||
|
assert next(f_d)
|
||||||
|
|
||||||
|
d = [("abc", "")]
|
||||||
|
f_d = base.format_pairs(d)
|
||||||
|
assert next(f_d)
|
||||||
|
|
||||||
|
d = []
|
||||||
|
f_d = base.format_pairs(d)
|
||||||
|
with pytest.raises(StopIteration):
|
||||||
|
next(f_d)
|
||||||
|
@ -6,8 +6,8 @@ from . import full_eval
|
|||||||
def test_view_query():
|
def test_view_query():
|
||||||
d = ""
|
d = ""
|
||||||
v = full_eval(query.ViewQuery())
|
v = full_eval(query.ViewQuery())
|
||||||
f = v(d, query=multidict.MultiDict([("foo", "bar")]))
|
f = v(d, query=multidict.MultiDict([("foo", "bar"), ("foo", "baz")]))
|
||||||
assert f[0] == "Query"
|
assert f[0] == "Query"
|
||||||
assert f[1] == [[("header", "foo: "), ("text", "bar")]]
|
assert f[1] == [[("header", "foo: "), ("text", "bar")], [("header", "foo: "), ("text", "baz")]]
|
||||||
|
|
||||||
assert v(d) == ("Query", [])
|
assert v(d) == ("Query", [])
|
||||||
|
Loading…
Reference in New Issue
Block a user