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

View File

@ -13,9 +13,10 @@ def request(context, flow):
# Method 1: Answer with a locally generated response # Method 1: Answer with a locally generated response
if flow.request.pretty_host.endswith("example.com"): if flow.request.pretty_host.endswith("example.com"):
resp = HTTPResponse( resp = HTTPResponse(
"HTTP/1.1", 200, "OK", b"HTTP/1.1", 200, b"OK",
Headers(Content_Type="text/html"), Headers(Content_Type="text/html"),
"helloworld") b"helloworld"
)
flow.reply.send(resp) flow.reply.send(resp)
# Method 2: Redirect the request to a different server # 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]) ct = "%s/%s" % (ct[0], ct[1])
if ct in content_types_map: if ct in content_types_map:
return content_types_map[ct][0](data, **metadata) return content_types_map[ct][0](data, **metadata)
elif strutils.isXML(data.decode()): elif strutils.is_xml(data):
return get("XML")(data, **metadata) return get("XML")(data, **metadata)
if metadata.get("query"): if metadata.get("query"):
return get("Query")(data, **metadata) return get("Query")(data, **metadata)
if data and strutils.isMostlyBin(data.decode()): if data and strutils.is_mostly_bin(data):
return get("Hex")(data) return get("Hex")(data)
if not data: if not data:
return "No content", [] return "No content", []
@ -240,7 +240,7 @@ class ViewHTML(View):
content_types = ["text/html"] content_types = ["text/html"]
def __call__(self, data, **metadata): def __call__(self, data, **metadata):
if strutils.isXML(data.decode()): if strutils.is_xml(data):
parser = lxml.etree.HTMLParser( parser = lxml.etree.HTMLParser(
strip_cdata=True, strip_cdata=True,
remove_blank_text=True remove_blank_text=True

View File

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

View File

@ -73,9 +73,9 @@ def test_add_header():
def test_custom_contentviews(): def test_custom_contentviews():
with example("custom_contentviews.py") as ex: with example("custom_contentviews.py") as ex:
pig = ex.ctx.contentview pig = ex.ctx.contentview
_, fmt = pig("<html>test!</html>") _, fmt = pig(b"<html>test!</html>")
assert any('esttay!' in val[0][1] for val in fmt) assert any(b'esttay!' in val[0][1] for val in fmt)
assert not pig("gobbledygook") assert not pig(b"gobbledygook")
def test_iframe_injector(): def test_iframe_injector():
@ -103,7 +103,7 @@ def test_modify_form():
def test_modify_querystring(): 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: with example("modify_querystring.py") as ex:
ex.run("request", flow) ex.run("request", flow)
assert flow.request.query["mitmproxy"] == "rocks" assert flow.request.query["mitmproxy"] == "rocks"
@ -126,7 +126,7 @@ def test_modify_response_body():
def test_redirect_requests(): 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: with example("redirect_requests.py") as ex:
ex.run("request", flow) ex.run("request", flow)
assert flow.request.host == "mitmproxy.org" 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") strutils.escaped_str_to_bytes(b"very byte")
def test_isBin(): def test_is_xml():
assert not strutils.isBin("testing\n\r") assert not strutils.is_xml(b"foo")
assert strutils.isBin("testing\x01") assert strutils.is_xml(b"<foo")
assert strutils.isBin("testing\x0e") assert strutils.is_xml(b" \n<foo")
assert strutils.isBin("testing\x7f")
def test_isXml():
assert not strutils.isXML("foo")
assert strutils.isXML("<foo")
assert strutils.isXML(" \n<foo")
def test_clean_hanging_newline(): def test_clean_hanging_newline():