diff options
| -rw-r--r-- | README.rst | 5 | ||||
| -rw-r--r-- | examples/har_dump.py | 29 | ||||
| -rw-r--r-- | pathod/language/generators.py | 2 | ||||
| -rw-r--r-- | test/mitmproxy/test_examples.py | 21 | ||||
| -rw-r--r-- | test/mitmproxy/test_flow.py | 2 | ||||
| -rw-r--r-- | test/mitmproxy/test_utils_strutils.py | 2 | ||||
| -rw-r--r-- | tox.ini | 8 | 
7 files changed, 48 insertions, 21 deletions
| @@ -43,7 +43,7 @@ Join our developer chat on Slack if you would like to hack on mitmproxy itself.  Installation  ------------ -The installation instructions are `here <http://docs.mitmproxy.org/en/stable/install.html>`_. +The installation instructions are `here <http://docs.mitmproxy.org/en/stable/install.html>`__.  If you want to contribute changes, keep on reading. @@ -52,7 +52,7 @@ Hacking  To get started hacking on mitmproxy, make sure you have Python_ 3.5.x or above with  virtualenv_ installed (you can find installation instructions for virtualenv -`here <http://virtualenv.readthedocs.org/en/latest/>`_). Then do the following: +`here <http://virtualenv.readthedocs.org/en/latest/>`__). Then do the following:  .. code-block:: text @@ -188,6 +188,5 @@ PR checks will fail and block merging. We are using this command to check for st  .. _.env: https://github.com/mitmproxy/mitmproxy/blob/master/.env  .. _Sphinx: http://sphinx-doc.org/  .. _sphinx-autobuild: https://pypi.python.org/pypi/sphinx-autobuild -.. _issue_tracker: https://github.com/mitmproxy/mitmproxy/issues  .. _PEP8: https://www.python.org/dev/peps/pep-0008  .. _Google Style Guide: https://google.github.io/styleguide/pyguide.html diff --git a/examples/har_dump.py b/examples/har_dump.py index 560b9adc..efcf9d74 100644 --- a/examples/har_dump.py +++ b/examples/har_dump.py @@ -3,7 +3,6 @@ This inline script can be used to dump flows as HAR files.  """ -import pprint  import json  import sys  import base64 @@ -128,19 +127,22 @@ def response(flow):          "timings": timings,      } -    # Store binay data as base64 +    # Store binary data as base64      if strutils.is_mostly_bin(flow.response.content): -        b64 = base64.b64encode(flow.response.content) -        entry["response"]["content"]["text"] = b64.decode('ascii') +        entry["response"]["content"]["text"] = base64.b64encode(flow.response.content).decode()          entry["response"]["content"]["encoding"] = "base64"      else: -        entry["response"]["content"]["text"] = flow.response.text +        entry["response"]["content"]["text"] = flow.response.get_text(strict=False)      if flow.request.method in ["POST", "PUT", "PATCH"]: +        params = [ +            {"name": a.decode("utf8", "surrogateescape"), "value": b.decode("utf8", "surrogateescape")} +            for a, b in flow.request.urlencoded_form.items(multi=True) +        ]          entry["request"]["postData"] = {              "mimeType": flow.request.headers.get("Content-Type", "").split(";")[0], -            "text": flow.request.content, -            "params": name_value(flow.request.urlencoded_form) +            "text": flow.request.get_text(strict=False), +            "params": params          }      if flow.server_conn: @@ -155,16 +157,17 @@ def done():      """      dump_file = sys.argv[1] +    json_dump = json.dumps(HAR, indent=2)  # type: str +      if dump_file == '-': -        mitmproxy.ctx.log(pprint.pformat(HAR)) +        mitmproxy.ctx.log(json_dump)      else: -        json_dump = json.dumps(HAR, indent=2) - +        raw = json_dump.encode()  # type: bytes          if dump_file.endswith('.zhar'): -            json_dump = zlib.compress(json_dump, 9) +            raw = zlib.compress(raw, 9) -        with open(dump_file, "w") as f: -            f.write(json_dump) +        with open(dump_file, "wb") as f: +            f.write(raw)          mitmproxy.ctx.log("HAR dump finished (wrote %s bytes to file)" % len(json_dump)) diff --git a/pathod/language/generators.py b/pathod/language/generators.py index 4e19ecd9..d716804d 100644 --- a/pathod/language/generators.py +++ b/pathod/language/generators.py @@ -14,7 +14,7 @@ DATATYPES = dict(      punctuation=string.punctuation.encode(),      whitespace=string.whitespace.encode(),      ascii=string.printable.encode(), -    bytes=bytes(bytearray(range(256))) +    bytes=bytes(range(256))  ) diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 6972da0c..3cd29a50 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -160,3 +160,24 @@ class TestHARDump:          f = format_cookies([("n", "v", CA([("expires", "Mon, 24-Aug-2037 00:00:00 GMT")]))])[0]          assert f['expires'] + +    def test_binary(self): + +        f = self.flow() +        f.request.method = "POST" +        f.request.headers["content-type"] = "application/x-www-form-urlencoded" +        f.request.content = b"foo=bar&baz=s%c3%bc%c3%9f" +        f.response.headers["random-junk"] = bytes(range(256)) +        f.response.content = bytes(range(256)) + +        with tutils.tmpdir() as tdir: +            path = os.path.join(tdir, "somefile") + +            m, sc = tscript("har_dump.py", shlex.quote(path)) +            m.addons.invoke(m, "response", f) +            m.addons.remove(sc) + +            with open(path, "r") as inp: +                har = json.load(inp) + +        assert len(har["log"]["entries"]) == 1 diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 6ba23579..ed15a766 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -345,7 +345,7 @@ class TestSerialize:          sio = io.BytesIO()          f = tutils.tflow()          f.marked = True -        f.request.content = bytes(bytearray(range(256))) +        f.request.content = bytes(range(256))          w = mitmproxy.io.FlowWriter(sio)          w.add(f) diff --git a/test/mitmproxy/test_utils_strutils.py b/test/mitmproxy/test_utils_strutils.py index d4c2883c..84281c6b 100644 --- a/test/mitmproxy/test_utils_strutils.py +++ b/test/mitmproxy/test_utils_strutils.py @@ -3,7 +3,7 @@ from mitmproxy.test import tutils  def test_always_bytes(): -    assert strutils.always_bytes(bytes(bytearray(range(256)))) == bytes(bytearray(range(256))) +    assert strutils.always_bytes(bytes(range(256))) == bytes(range(256))      assert strutils.always_bytes("foo") == b"foo"      with tutils.raises(ValueError):          strutils.always_bytes(u"\u2605", "ascii") @@ -18,5 +18,9 @@ changedir = docs  commands = sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html  [testenv:lint] -deps = flake8>=2.6.2, <3.1 -commands = flake8 --jobs 8 --count mitmproxy pathod examples test +deps = +  flake8>=2.6.2, <3.1 +  rstcheck>=2.2, <3.0 +commands = +  flake8 --jobs 8 --count mitmproxy pathod examples test +  rstcheck README.rst
\ No newline at end of file | 
