This commit is contained in:
Maximilian Hils 2016-07-07 17:50:26 -07:00
parent f62e976e1e
commit 76473d44e0
7 changed files with 20 additions and 19 deletions

View File

@ -24,5 +24,5 @@ def response(context, flow):
height=0,
width=0)
html.body.insert(0, iframe)
flow.response.content = str(html)
flow.response.content = str(html).encode("utf8")
context.log("Iframe inserted.")

View File

@ -10,7 +10,7 @@ def start(context):
raise ValueError('Usage: -s "modify_response_body.py old new"')
# You may want to use Python's argparse for more sophisticated argument
# parsing.
context.old, context.new = sys.argv[1], sys.argv[2]
context.old, context.new = sys.argv[1].encode(), sys.argv[2].encode()
def response(context, flow):

View File

@ -75,7 +75,7 @@ def python_code(flow):
data = ""
if flow.request.body:
json_obj = is_json(flow.request.headers, flow.request.body)
json_obj = is_json(flow.request.headers, flow.request.content)
if json_obj:
data = "\njson = %s\n" % dictstr(sorted(json_obj.items()), " ")
args += "\n json=json,"
@ -100,11 +100,12 @@ def raw_request(flow):
def is_json(headers, content):
# type: (netlib.http.Headers, bytes) -> bool
if headers:
ct = netlib.http.parse_content_type(headers.get("content-type", ""))
if ct and "%s/%s" % (ct[0], ct[1]) == "application/json":
try:
return json.loads(content)
return json.loads(content.decode("utf8", "surrogateescape"))
except ValueError:
return False
return False

View File

@ -84,11 +84,11 @@ def test_iframe_injector():
with example("iframe_injector.py") as ex:
pass
flow = tutils.tflow(resp=netutils.tresp(content="<html>mitmproxy</html>"))
flow = tutils.tflow(resp=netutils.tresp(content=b"<html>mitmproxy</html>"))
with example("iframe_injector.py http://example.org/evil_iframe") as ex:
ex.run("response", flow)
content = flow.response.content
assert 'iframe' in content and 'evil_iframe' in content
assert b'iframe' in content and b'evil_iframe' in content
def test_modify_form():
@ -96,11 +96,11 @@ def test_modify_form():
flow = tutils.tflow(req=netutils.treq(headers=form_header))
with example("modify_form.py") as ex:
ex.run("request", flow)
assert flow.request.urlencoded_form["mitmproxy"] == "rocks"
assert flow.request.urlencoded_form[b"mitmproxy"] == b"rocks"
flow.request.headers["content-type"] = ""
ex.run("request", flow)
assert list(flow.request.urlencoded_form.items()) == [("foo", "bar")]
assert list(flow.request.urlencoded_form.items()) == [(b"foo", b"bar")]
def test_modify_querystring():
@ -119,11 +119,11 @@ def test_modify_response_body():
with example("modify_response_body.py"):
assert True
flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy"))
flow = tutils.tflow(resp=netutils.tresp(content=b"I <3 mitmproxy"))
with example("modify_response_body.py mitmproxy rocks") as ex:
assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks"
assert ex.ctx.old == b"mitmproxy" and ex.ctx.new == b"rocks"
ex.run("response", flow)
assert flow.response.content == "I <3 rocks"
assert flow.response.content == b"I <3 rocks"
def test_redirect_requests():

View File

@ -60,7 +60,7 @@ class TestExportPythonCode():
def test_post_json(self):
p = req_post()
p.content = '{"name": "example", "email": "example@example.com"}'
p.content = b'{"name": "example", "email": "example@example.com"}'
p.headers = Headers(content_type="application/json")
flow = tutils.tflow(req=p)
python_equals("data/test_flow_export/python_post_json.py", export.python_code(flow))
@ -112,7 +112,7 @@ class TestExportLocustCode():
def test_post(self):
p = req_post()
p.content = '''content'''
p.content = b'content'
p.headers = ''
flow = tutils.tflow(req=p)
python_equals("data/test_flow_export/locust_post.py", export.locust_code(flow))
@ -142,14 +142,14 @@ class TestIsJson():
def test_json_type(self):
headers = Headers(content_type="application/json")
assert export.is_json(headers, "foobar") is False
assert export.is_json(headers, b"foobar") is False
def test_valid(self):
headers = Headers(content_type="application/foobar")
j = export.is_json(headers, '{"name": "example", "email": "example@example.com"}')
j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}')
assert j is False
def test_valid2(self):
headers = Headers(content_type="application/json")
j = export.is_json(headers, '{"name": "example", "email": "example@example.com"}')
j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}')
assert isinstance(j, dict)

View File

@ -24,7 +24,7 @@ def test_assemble_request():
def test_assemble_request_head():
c = assemble_request_head(treq(content="foo"))
c = assemble_request_head(treq(content=b"foo"))
assert b"GET" in c
assert b"qvalue" in c
assert b"content-length" in c

View File

@ -7,8 +7,8 @@ from netlib.tutils import tresp
def _test_passthrough_attr(message, attr):
assert getattr(message, attr) == getattr(message.data, attr)
setattr(message, attr, "foo")
assert getattr(message.data, attr) == "foo"
setattr(message, attr, b"foo")
assert getattr(message.data, attr) == b"foo"
def _test_decoded_attr(message, attr):