diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py
index 90dafca06..64c2e6c16 100644
--- a/mitmproxy/contentviews.py
+++ b/mitmproxy/contentviews.py
@@ -26,7 +26,7 @@ import lxml.html
import six
from PIL import ExifTags
from PIL import Image
-from six.moves import cStringIO as StringIO
+from six import BytesIO
from mitmproxy import exceptions
from mitmproxy.contrib import jsbeautifier
@@ -64,7 +64,7 @@ KEY_MAX = 30
def pretty_json(s):
# type: (bytes) -> bytes
try:
- p = json.loads(s)
+ p = json.loads(s.decode('utf-8'))
except ValueError:
return None
pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False)
@@ -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):
+ elif strutils.isXML(data.decode()):
return get("XML")(data, **metadata)
if metadata.get("query"):
return get("Query")(data, **metadata)
- if data and strutils.isMostlyBin(data):
+ if data and strutils.isMostlyBin(data.decode()):
return get("Hex")(data)
if not data:
return "No content", []
@@ -209,7 +209,7 @@ class ViewXML(View):
p = p.getprevious()
doctype = docinfo.doctype
if prev:
- doctype += "\n".join(prev).strip()
+ doctype += "\n".join(p.decode() for p in prev).strip()
doctype = doctype.strip()
s = lxml.etree.tostring(
@@ -240,7 +240,7 @@ class ViewHTML(View):
content_types = ["text/html"]
def __call__(self, data, **metadata):
- if strutils.isXML(data):
+ if strutils.isXML(data.decode()):
parser = lxml.etree.HTMLParser(
strip_cdata=True,
remove_blank_text=True
@@ -416,7 +416,7 @@ class ViewImage(View):
def __call__(self, data, **metadata):
try:
- img = Image.open(StringIO(data))
+ img = Image.open(BytesIO(data))
except IOError:
return None
parts = [
diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py
index 48825bc2b..52fceeac0 100644
--- a/test/mitmproxy/test_contentview.py
+++ b/test/mitmproxy/test_contentview.py
@@ -23,37 +23,37 @@ class TestContentView:
def test_view_auto(self):
v = cv.ViewAuto()
f = v(
- "foo",
+ b"foo",
headers=Headers()
)
assert f[0] == "Raw"
f = v(
- "",
+ b"",
headers=Headers(content_type="text/html")
)
assert f[0] == "HTML"
f = v(
- "foo",
+ b"foo",
headers=Headers(content_type="text/flibble")
)
assert f[0] == "Raw"
f = v(
- "
one
" + s = b"one
" assert v(s) - s = "gobbledygook" + s = b"gobbledygook" assert not v(s) def test_view_html_outline(self): v = cv.ViewHTMLOutline() - s = "one
" + s = b"one
" assert v(s) def test_view_json(self): cv.VIEW_CUTOFF = 100 v = cv.ViewJSON() - assert v("{}") - assert not v("{") - assert v("[1, 2, 3, 4, 5]") + assert v(b"{}") + assert not v(b"{") + assert v(b"[1, 2, 3, 4, 5]") def test_view_xml(self): v = cv.ViewXML() - assert v("