diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-09-24 11:21:12 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-09-24 11:21:12 +1200 |
commit | 0d59fd7e01ba74df901cae6f5f2d058235fa1c33 (patch) | |
tree | d4fe874bfc149bf12597060b7dd934c177915cdb | |
parent | 21f74efa10374d99df29a3eb666370b28b30d5b3 (diff) | |
download | mitmproxy-0d59fd7e01ba74df901cae6f5f2d058235fa1c33.tar.gz mitmproxy-0d59fd7e01ba74df901cae6f5f2d058235fa1c33.tar.bz2 mitmproxy-0d59fd7e01ba74df901cae6f5f2d058235fa1c33.zip |
Move cleanBin and hexdump into netutils.
-rw-r--r-- | libmproxy/console/contentview.py | 9 | ||||
-rw-r--r-- | libmproxy/dump.py | 5 | ||||
-rw-r--r-- | libmproxy/utils.py | 37 | ||||
-rw-r--r-- | test/test_utils.py | 11 |
4 files changed, 8 insertions, 54 deletions
diff --git a/libmproxy/console/contentview.py b/libmproxy/console/contentview.py index 29da6030..1e9b9160 100644 --- a/libmproxy/console/contentview.py +++ b/libmproxy/console/contentview.py @@ -3,6 +3,7 @@ import urwid from PIL import Image from PIL.ExifTags import TAGS import lxml.html, lxml.etree +import netlib.utils import common from .. import utils, encoding, flow from ..contrib import jsbeautifier, html2text @@ -22,7 +23,7 @@ def _view_text(content, total, limit): Generates a body for a chunk of text. """ txt = [] - for i in utils.cleanBin(content).splitlines(): + for i in netlib.utils.cleanBin(content).splitlines(): txt.append( urwid.Text(("text", i), wrap="any") ) @@ -76,7 +77,7 @@ class ViewHex: content_types = [] def __call__(self, hdrs, content, limit): txt = [] - for offset, hexa, s in utils.hexdump(content[:limit]): + for offset, hexa, s in netlib.utils.hexdump(content[:limit]): txt.append(urwid.Text([ ("offset", offset), " ", @@ -216,7 +217,7 @@ class ViewMultipart: match = rx.search(parts[1]) if match: keys.append(match.group(1) + ":") - vals.append(utils.cleanBin( + vals.append(netlib.utils.cleanBin( "\n".join(parts[3+parts[2:].index(""):]) )) r = [ @@ -306,7 +307,7 @@ class ViewImage: ) clean = [] for i in parts: - clean.append([utils.cleanBin(i[0]), utils.cleanBin(i[1])]) + clean.append([netlib.utils.cleanBin(i[0]), netlib.utils.cleanBin(i[1])]) fmt = common.format_keyvals( clean, key = "header", diff --git a/libmproxy/dump.py b/libmproxy/dump.py index 98596b53..170c701d 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -14,6 +14,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys, os +import netlib.utils import flow, filt, utils class DumpError(Exception): pass @@ -175,7 +176,7 @@ class DumpMaster(flow.FlowMaster): result = result + "\n\n" + self.indent(4, f.response.headers) if self.o.verbosity > 2: if utils.isBin(f.response.content): - d = utils.hexdump(f.response.content) + d = netlib.utils.hexdump(f.response.content) d = "\n".join("%s\t%s %s"%i for i in d) cont = self.indent(4, d) elif f.response.content: @@ -199,7 +200,7 @@ class DumpMaster(flow.FlowMaster): print >> self.outfile, str_request(f.request) print >> self.outfile, self.indent(4, f.request.headers) if utils.isBin(f.request.content): - print >> self.outfile, self.indent(4, utils.hexdump(f.request.content)) + print >> self.outfile, self.indent(4, netlib.utils.hexdump(f.request.content)) elif f.request.content: print >> self.outfile, self.indent(4, f.request.content) print >> self.outfile diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 69a06a26..6e804887 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -55,24 +55,6 @@ def isXML(s): return False -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): - parts.append(i) - elif i in "\n\r\t" and not fixspacing: - parts.append(i) - else: - parts.append(".") - return "".join(parts) - - def pretty_json(s): try: p = json.loads(s) @@ -96,25 +78,6 @@ def urlencode(s): return urllib.urlencode(s, False) -def hexdump(s): - """ - Returns a set of typles: - (offset, hex, str) - """ - parts = [] - for i in range(0, len(s), 16): - o = "%.10x"%i - part = s[i:i+16] - x = " ".join("%.2x"%ord(i) for i in part) - if len(part) < 16: - x += " " - x += " ".join(" " for i in range(16 - len(part))) - parts.append( - (o, x, cleanBin(part, True)) - ) - return parts - - def del_all(dict, keys): for key in keys: if key in dict: diff --git a/test/test_utils.py b/test/test_utils.py index 49312a4b..db6feb4d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -22,10 +22,6 @@ def test_isXml(): assert utils.isXML(" \n<foo") -def test_hexdump(): - assert utils.hexdump("one\0"*10) - - def test_del_all(): d = dict(a=1, b=2, c=3) utils.del_all(d, ["a", "x", "b"]) @@ -125,13 +121,6 @@ def test_parse_content_type(): assert v == ('text', 'html', {'charset': 'UTF-8'}) -def test_cleanBin(): - assert utils.cleanBin("one") == "one" - assert utils.cleanBin("\00ne") == ".ne" - assert utils.cleanBin("\nne") == "\nne" - assert utils.cleanBin("\nne", True) == ".ne" - - def test_safe_subn(): assert utils.safe_subn("foo", u"bar", "\xc2foo") |