This commit is contained in:
Maximilian Hils 2016-07-06 17:31:08 -07:00
parent 811b72cd30
commit 444f0a4c39
6 changed files with 26 additions and 39 deletions

View File

@ -11,7 +11,7 @@ class ViewPigLatin(contentviews.View):
content_types = ["text/html"]
def __call__(self, data, **metadata):
if strutils.isXML(data):
if strutils.is_xml(data):
parser = lxml.etree.HTMLParser(
strip_cdata=True,
remove_blank_text=True
@ -20,7 +20,7 @@ class ViewPigLatin(contentviews.View):
docinfo = d.getroottree().docinfo
def piglify(src):
words = string.split(src)
words = src.split()
ret = ''
for word in words:
idx = -1

View File

@ -13,9 +13,10 @@ def request(context, flow):
# Method 1: Answer with a locally generated response
if flow.request.pretty_host.endswith("example.com"):
resp = HTTPResponse(
"HTTP/1.1", 200, "OK",
b"HTTP/1.1", 200, b"OK",
Headers(Content_Type="text/html"),
"helloworld")
b"helloworld"
)
flow.reply.send(resp)
# Method 2: Redirect the request to a different server

View File

@ -143,11 +143,11 @@ class ViewAuto(View):
ct = "%s/%s" % (ct[0], ct[1])
if ct in content_types_map:
return content_types_map[ct][0](data, **metadata)
elif strutils.isXML(data.decode()):
elif strutils.is_xml(data):
return get("XML")(data, **metadata)
if metadata.get("query"):
return get("Query")(data, **metadata)
if data and strutils.isMostlyBin(data.decode()):
if data and strutils.is_mostly_bin(data):
return get("Hex")(data)
if not data:
return "No content", []
@ -240,7 +240,7 @@ class ViewHTML(View):
content_types = ["text/html"]
def __call__(self, data, **metadata):
if strutils.isXML(data.decode()):
if strutils.is_xml(data):
parser = lxml.etree.HTMLParser(
strip_cdata=True,
remove_blank_text=True

View File

@ -114,24 +114,17 @@ def escaped_str_to_bytes(data):
return codecs.escape_decode(data)[0]
def isBin(s):
"""
Does this string have any non-ASCII characters?
"""
for i in s:
i = ord(i)
if i < 9 or 13 < i < 32 or 126 < i:
return True
return False
def is_mostly_bin(s):
# type: (bytes) -> bool
return sum(
i < 9 or 13 < i < 32 or 126 < i
for i in six.iterbytes(s[:100])
) > 30
def isMostlyBin(s):
s = s[:100]
return sum(isBin(ch) for ch in s) / len(s) > 0.3
def isXML(s):
return s.strip().startswith("<")
def is_xml(s):
# type: (bytes) -> bool
return s.strip().startswith(b"<")
def clean_hanging_newline(t):

View File

@ -73,9 +73,9 @@ def test_add_header():
def test_custom_contentviews():
with example("custom_contentviews.py") as ex:
pig = ex.ctx.contentview
_, fmt = pig("<html>test!</html>")
assert any('esttay!' in val[0][1] for val in fmt)
assert not pig("gobbledygook")
_, fmt = pig(b"<html>test!</html>")
assert any(b'esttay!' in val[0][1] for val in fmt)
assert not pig(b"gobbledygook")
def test_iframe_injector():
@ -103,7 +103,7 @@ def test_modify_form():
def test_modify_querystring():
flow = tutils.tflow(req=netutils.treq(path="/search?q=term"))
flow = tutils.tflow(req=netutils.treq(path=b"/search?q=term"))
with example("modify_querystring.py") as ex:
ex.run("request", flow)
assert flow.request.query["mitmproxy"] == "rocks"
@ -126,7 +126,7 @@ def test_modify_response_body():
def test_redirect_requests():
flow = tutils.tflow(req=netutils.treq(host="example.org"))
flow = tutils.tflow(req=netutils.treq(host=b"example.org"))
with example("redirect_requests.py") as ex:
ex.run("request", flow)
assert flow.request.host == "mitmproxy.org"

View File

@ -68,17 +68,10 @@ def test_escaped_str_to_bytes():
strutils.escaped_str_to_bytes(b"very byte")
def test_isBin():
assert not strutils.isBin("testing\n\r")
assert strutils.isBin("testing\x01")
assert strutils.isBin("testing\x0e")
assert strutils.isBin("testing\x7f")
def test_isXml():
assert not strutils.isXML("foo")
assert strutils.isXML("<foo")
assert strutils.isXML(" \n<foo")
def test_is_xml():
assert not strutils.is_xml(b"foo")
assert strutils.is_xml(b"<foo")
assert strutils.is_xml(b" \n<foo")
def test_clean_hanging_newline():