aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/cmdline.py13
-rw-r--r--mitmproxy/contentviews.py14
-rw-r--r--test/mitmproxy/test_cmdline.py12
-rw-r--r--test/mitmproxy/test_contentview.py54
-rw-r--r--test/mitmproxy/test_custom_contentview.py2
-rw-r--r--tox.ini2
6 files changed, 49 insertions, 48 deletions
diff --git a/mitmproxy/cmdline.py b/mitmproxy/cmdline.py
index 77cdb8ca..d46051a2 100644
--- a/mitmproxy/cmdline.py
+++ b/mitmproxy/cmdline.py
@@ -9,6 +9,7 @@ import configargparse
from mitmproxy import filt
from mitmproxy.proxy import config
from netlib import human
+from netlib import strutils
from netlib import tcp
from netlib import version
from netlib.http import url
@@ -73,7 +74,7 @@ def parse_replace_hook(s):
try:
re.compile(regex)
except re.error as e:
- raise ParseException("Malformed replacement regex: %s" % str(e.message))
+ raise ParseException("Malformed replacement regex: %s" % str(e))
return patt, regex, replacement
@@ -109,7 +110,7 @@ def parse_setheader(s):
def parse_server_spec(spec):
try:
p = url.parse(spec)
- if p[0] not in ("http", "https"):
+ if p[0] not in (b"http", b"https"):
raise ValueError()
except ValueError:
raise configargparse.ArgumentTypeError(
@@ -127,7 +128,7 @@ def parse_upstream_auth(auth):
raise configargparse.ArgumentTypeError(
"Invalid upstream auth specification: %s" % auth
)
- return "Basic" + " " + base64.b64encode(auth)
+ return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
def get_common_options(options):
@@ -147,13 +148,13 @@ def get_common_options(options):
try:
p = parse_replace_hook(i)
except ParseException as e:
- raise configargparse.ArgumentTypeError(e.message)
+ raise configargparse.ArgumentTypeError(e)
reps.append(p)
for i in options.replace_file:
try:
patt, rex, path = parse_replace_hook(i)
except ParseException as e:
- raise configargparse.ArgumentTypeError(e.message)
+ raise configargparse.ArgumentTypeError(e)
try:
v = open(path, "rb").read()
except IOError as e:
@@ -167,7 +168,7 @@ def get_common_options(options):
try:
p = parse_setheader(i)
except ParseException as e:
- raise configargparse.ArgumentTypeError(e.message)
+ raise configargparse.ArgumentTypeError(e)
setheaders.append(p)
if options.outfile and options.outfile[0] == options.rfile:
diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py
index 90dafca0..64c2e6c1 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_cmdline.py b/test/mitmproxy/test_cmdline.py
index e75b7baf..4fe2cf94 100644
--- a/test/mitmproxy/test_cmdline.py
+++ b/test/mitmproxy/test_cmdline.py
@@ -39,11 +39,11 @@ def test_parse_replace_hook():
def test_parse_server_spec():
tutils.raises("Invalid server specification", cmdline.parse_server_spec, "")
assert cmdline.parse_server_spec(
- "http://foo.com:88") == ("http", ("foo.com", 88))
+ "http://foo.com:88") == (b"http", (b"foo.com", 88))
assert cmdline.parse_server_spec(
- "http://foo.com") == ("http", ("foo.com", 80))
+ "http://foo.com") == (b"http", (b"foo.com", 80))
assert cmdline.parse_server_spec(
- "https://foo.com") == ("https", ("foo.com", 443))
+ "https://foo.com") == (b"https", (b"foo.com", 443))
tutils.raises(
"Invalid server specification",
cmdline.parse_server_spec,
@@ -59,9 +59,9 @@ def test_parse_upstream_auth():
tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":")
tutils.raises("Invalid upstream auth specification", cmdline.parse_upstream_auth, ":test")
assert cmdline.parse_upstream_auth(
- "test:test") == "Basic" + " " + base64.b64encode("test:test")
+ "test:test") == b"Basic" + b" " + base64.b64encode(b"test:test")
assert cmdline.parse_upstream_auth(
- "test:") == "Basic" + " " + base64.b64encode("test:")
+ "test:") == b"Basic" + b" " + base64.b64encode(b"test:")
def test_parse_setheaders():
@@ -124,7 +124,7 @@ def test_common():
opts.replace_file = [("/foo/bar/%s" % p)]
v = cmdline.get_common_options(opts)["replacements"]
assert len(v) == 1
- assert v[0][2].strip() == "replacecontents"
+ assert v[0][2].strip() == b"replacecontents"
def test_mitmproxy():
diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py
index 48825bc2..52fceeac 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(
- "<html></html>",
+ b"<html></html>",
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(
- "<xml></xml>",
+ b"<xml></xml>",
headers=Headers(content_type="text/flibble")
)
assert f[0].startswith("XML")
f = v(
- "",
+ b"",
headers=Headers()
)
assert f[0] == "No content"
f = v(
- "",
+ b"",
headers=Headers(),
query=multidict.MultiDict([("foo", "bar")]),
)
@@ -69,29 +69,29 @@ class TestContentView:
def test_view_html(self):
v = cv.ViewHTML()
- s = "<html><br><br></br><p>one</p></html>"
+ s = b"<html><br><br></br><p>one</p></html>"
assert v(s)
- s = "gobbledygook"
+ s = b"gobbledygook"
assert not v(s)
def test_view_html_outline(self):
v = cv.ViewHTMLOutline()
- s = "<html><br><br></br><p>one</p></html>"
+ s = b"<html><br><br></br><p>one</p></html>"
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("<foo></foo>")
- assert not v("<foo>")
- s = """<?xml version="1.0" encoding="UTF-8"?>
+ assert v(b"<foo></foo>")
+ assert not v(b"<foo>")
+ s = b"""<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet title="XSL_formatting"?>
<rss
xmlns:media="http://search.yahoo.com/mrss/"
@@ -103,7 +103,7 @@ class TestContentView:
def test_view_raw(self):
v = cv.ViewRaw()
- assert v("foo")
+ assert v(b"foo")
def test_view_javascript(self):
v = cv.ViewJavaScript()
@@ -133,7 +133,7 @@ class TestContentView:
def test_view_hex(self):
v = cv.ViewHex()
- assert v("foo")
+ assert v(b"foo")
def test_view_image(self):
v = cv.ViewImage()
@@ -149,11 +149,11 @@ class TestContentView:
p = tutils.test_data.path("data/image.ico")
assert v(open(p, "rb").read())
- assert not v("flibble")
+ assert not v(b"flibble")
def test_view_multipart(self):
view = cv.ViewMultipart()
- v = """
+ v = b"""
--AaB03x
Content-Disposition: form-data; name="submit-name"
@@ -182,21 +182,21 @@ Larry
def test_get_content_view(self):
r = cv.get_content_view(
cv.get("Raw"),
- "[1, 2, 3]",
+ b"[1, 2, 3]",
headers=Headers(content_type="application/json")
)
assert "Raw" in r[0]
r = cv.get_content_view(
cv.get("Auto"),
- "[1, 2, 3]",
+ b"[1, 2, 3]",
headers=Headers(content_type="application/json")
)
assert r[0] == "JSON"
r = cv.get_content_view(
cv.get("Auto"),
- "[1, 2",
+ b"[1, 2",
headers=Headers(content_type="application/json")
)
assert "Raw" in r[0]
@@ -205,13 +205,13 @@ Larry
ContentViewException,
cv.get_content_view,
cv.get("AMF"),
- "[1, 2",
+ b"[1, 2",
headers=Headers()
)
r = cv.get_content_view(
cv.get("Auto"),
- encoding.encode('gzip', "[1, 2, 3]"),
+ encoding.encode('gzip', b"[1, 2, 3]"),
headers=Headers(
content_type="application/json",
content_encoding="gzip"
@@ -222,7 +222,7 @@ Larry
r = cv.get_content_view(
cv.get("XML"),
- encoding.encode('gzip', "[1, 2, 3]"),
+ encoding.encode('gzip', b"[1, 2, 3]"),
headers=Headers(
content_type="application/json",
content_encoding="gzip"
@@ -277,7 +277,7 @@ def test_get_by_shortcut():
def test_pretty_json():
- assert cv.pretty_json('{"foo": 1}')
- assert not cv.pretty_json("moo")
+ assert cv.pretty_json(b'{"foo": 1}')
+ assert not cv.pretty_json(b"moo")
assert cv.pretty_json(b'{"foo" : "\xe4\xb8\x96\xe7\x95\x8c"}') # utf8 with chinese characters
assert not cv.pretty_json(b'{"foo" : "\xFF"}')
diff --git a/test/mitmproxy/test_custom_contentview.py b/test/mitmproxy/test_custom_contentview.py
index 479b0b43..889fb8b3 100644
--- a/test/mitmproxy/test_custom_contentview.py
+++ b/test/mitmproxy/test_custom_contentview.py
@@ -40,7 +40,7 @@ def test_custom_views():
cv.remove(view_obj)
r = cv.get_content_view(
cv.get("Auto"),
- "[1, 2, 3]",
+ b"[1, 2, 3]",
headers=Headers(
content_type="text/none"
)
diff --git a/tox.ini b/tox.ini
index 1c48b91f..4837d5b5 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,7 +7,7 @@ deps =
codecov>=2.0.5
passenv = CI TRAVIS_BUILD_ID TRAVIS TRAVIS_BRANCH TRAVIS_JOB_NUMBER TRAVIS_PULL_REQUEST TRAVIS_JOB_ID TRAVIS_REPO_SLUG TRAVIS_COMMIT
setenv =
- PY3TESTS = test/netlib test/pathod/ test/mitmproxy/script
+ PY3TESTS = test/netlib test/pathod/ test/mitmproxy/script test/mitmproxy/test_contentview.py test/mitmproxy/test_custom_contentview.py test/mitmproxy/test_app.py test/mitmproxy/test_controller.py test/mitmproxy/test_fuzzing.py test/mitmproxy/test_script.py test/mitmproxy/test_web_app.py test/mitmproxy/test_utils.py test/mitmproxy/test_stateobject.py test/mitmproxy/test_cmdline.py
[testenv:py27]
commands =