aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/custom_contentviews.py4
-rw-r--r--examples/redirect_requests.py5
-rw-r--r--mitmproxy/contentviews.py6
-rw-r--r--netlib/strutils.py25
-rw-r--r--test/mitmproxy/test_examples.py10
-rw-r--r--test/netlib/test_strutils.py15
6 files changed, 26 insertions, 39 deletions
diff --git a/examples/custom_contentviews.py b/examples/custom_contentviews.py
index 05ebeb69..92fb6a58 100644
--- a/examples/custom_contentviews.py
+++ b/examples/custom_contentviews.py
@@ -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
diff --git a/examples/redirect_requests.py b/examples/redirect_requests.py
index d7db3f1c..af2aa907 100644
--- a/examples/redirect_requests.py
+++ b/examples/redirect_requests.py
@@ -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
diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py
index b7f4d3ec..6f64e360 100644
--- a/mitmproxy/contentviews.py
+++ b/mitmproxy/contentviews.py
@@ -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
diff --git a/netlib/strutils.py b/netlib/strutils.py
index 809f5e17..7ad15c96 100644
--- a/netlib/strutils.py
+++ b/netlib/strutils.py
@@ -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 isMostlyBin(s):
- s = s[:100]
- return sum(isBin(ch) for ch in s) / len(s) > 0.3
+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 isXML(s):
- return s.strip().startswith("<")
+def is_xml(s):
+ # type: (bytes) -> bool
+ return s.strip().startswith(b"<")
def clean_hanging_newline(t):
diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py
index 607d6faf..22d3c425 100644
--- a/test/mitmproxy/test_examples.py
+++ b/test/mitmproxy/test_examples.py
@@ -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"
diff --git a/test/netlib/test_strutils.py b/test/netlib/test_strutils.py
index 33bce714..f88e33ed 100644
--- a/test/netlib/test_strutils.py
+++ b/test/netlib/test_strutils.py
@@ -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():