From 1cffa5f46b84928f1dd211806122d01700345b40 Mon Sep 17 00:00:00 2001 From: dufferzafar Date: Thu, 28 Jul 2016 07:48:10 -0700 Subject: [PATCH] Use replace while decoding --- mitmproxy/contentviews.py | 5 +++-- test/mitmproxy/test_contentview.py | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 304b52411..dacef36db 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -262,7 +262,7 @@ class ViewHTMLOutline(View): content_types = ["text/html"] def __call__(self, data, **metadata): - data = data.decode("utf-8") + data = data.decode("utf-8", "replace") h = html2text.HTML2Text(baseurl="") h.ignore_images = True h.body_width = 0 @@ -389,7 +389,8 @@ class ViewJavaScript(View): def __call__(self, data, **metadata): opts = jsbeautifier.default_options() opts.indent_size = 2 - res = jsbeautifier.beautify(strutils.native(data), opts) + data = data.decode("utf-8", "replace") + res = jsbeautifier.beautify(data, opts) return "JavaScript", format_text(res) diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py index aad53b372..66cad47bd 100644 --- a/test/mitmproxy/test_contentview.py +++ b/test/mitmproxy/test_contentview.py @@ -78,6 +78,7 @@ class TestContentView: v = cv.ViewHTMLOutline() s = b"


one

" assert v(s) + assert v(b'\xfe') def test_view_json(self): cv.VIEW_CUTOFF = 100 @@ -106,9 +107,10 @@ class TestContentView: def test_view_javascript(self): v = cv.ViewJavaScript() - assert v("[1, 2, 3]") - assert v("[1, 2, 3") - assert v("function(a){[1, 2, 3]}") + assert v(b"[1, 2, 3]") + assert v(b"[1, 2, 3") + assert v(b"function(a){[1, 2, 3]}") + assert v(b"\xfe") # invalid utf-8 def test_view_css(self): v = cv.ViewCSS()