aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-03-27 11:25:50 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-03-27 11:25:50 +1300
commite9ac4bef2065abe545fbc7e8712c027f573082b3 (patch)
tree80dac4ecb44ceb08baceb780b25e82347a061e58
parent690f797da2b22bb48e1d574dde2f02affa6010d3 (diff)
downloadmitmproxy-e9ac4bef2065abe545fbc7e8712c027f573082b3.tar.gz
mitmproxy-e9ac4bef2065abe545fbc7e8712c027f573082b3.tar.bz2
mitmproxy-e9ac4bef2065abe545fbc7e8712c027f573082b3.zip
Add a variant of cleanBin that escapes newlines and tabs.
Use this to fix the hex display option.
-rw-r--r--libmproxy/utils.py13
-rw-r--r--test/test_utils.py10
2 files changed, 20 insertions, 3 deletions
diff --git a/libmproxy/utils.py b/libmproxy/utils.py
index bcf9d141..b4e317c5 100644
--- a/libmproxy/utils.py
+++ b/libmproxy/utils.py
@@ -54,11 +54,18 @@ def isXML(s):
return False
-def cleanBin(s):
+def cleanBin(s, fixspacing=False):
+ """
+ Cleans binary data to make it safe to display. If fixspacing is True,
+ tabs, newlines and so forth will be maintained, if not, they will be
+ replaced with a placeholder.
+ """
parts = []
for i in s:
o = ord(i)
- if (o > 31 and o < 127) or i in "\n\r\t":
+ if (o > 31 and o < 127):
+ parts.append(i)
+ elif i in "\n\r\t" and not fixspacing:
parts.append(i)
else:
parts.append(".")
@@ -147,7 +154,7 @@ def hexdump(s):
x += " "
x += " ".join(" " for i in range(16 - len(part)))
parts.append(
- (o, x, cleanBin(part))
+ (o, x, cleanBin(part, True))
)
return parts
diff --git a/test/test_utils.py b/test/test_utils.py
index d65fa5b4..e445614a 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -224,7 +224,17 @@ class u_parse_content_type(libpry.AutoTree):
assert v == ('text', 'html', {'charset': 'UTF-8'})
+class u_cleanBin(libpry.AutoTree):
+ def test_simple(self):
+ assert utils.cleanBin("one") == "one"
+ assert utils.cleanBin("\00ne") == ".ne"
+ assert utils.cleanBin("\nne") == "\nne"
+ assert utils.cleanBin("\nne", True) == ".ne"
+
+
+
tests = [
+ u_cleanBin(),
u_parse_content_type(),
uformat_timestamp(),
uisBin(),