aboutsummaryrefslogtreecommitdiffstats
path: root/examples/har_dump.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-10-22 19:10:44 -0700
committerGitHub <noreply@github.com>2016-10-22 19:10:44 -0700
commitc09cedd0f850a863665c67ecc0a6166488a0f8cb (patch)
tree3f0ab76804d6d59fb6c79ce65a2cc6c95c806d0f /examples/har_dump.py
parent37a05e27526d22a23f7579dd1dbb0a04ea4eedcc (diff)
parenta1a792aeac20eab1fd2f2e1a91d34ba990fc111b (diff)
downloadmitmproxy-c09cedd0f850a863665c67ecc0a6166488a0f8cb.tar.gz
mitmproxy-c09cedd0f850a863665c67ecc0a6166488a0f8cb.tar.bz2
mitmproxy-c09cedd0f850a863665c67ecc0a6166488a0f8cb.zip
Merge pull request #1655 from mhils/fix-encoding
Encoding Fixes
Diffstat (limited to 'examples/har_dump.py')
-rw-r--r--examples/har_dump.py29
1 files changed, 16 insertions, 13 deletions
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))