diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index ba6ba5b0c..b920a11fe 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -7,6 +7,7 @@ import os from .. import utils from ..protocol.http import CONTENT_MISSING, decoded from . import signals +import netlib.utils try: import pyperclip @@ -379,7 +380,7 @@ def format_flow(f, focus, extended=False, hostheader=False, padding=2): ) if f.response: if f.response.content: - contentdesc = utils.pretty_size(len(f.response.content)) + contentdesc = netlib.utils.pretty_size(len(f.response.content)) elif f.response.content == CONTENT_MISSING: contentdesc = "[content missing]" else: diff --git a/libmproxy/console/contentview.py b/libmproxy/console/contentview.py index 454c992f8..aafba4d77 100644 --- a/libmproxy/console/contentview.py +++ b/libmproxy/console/contentview.py @@ -60,7 +60,7 @@ def trailer(clen, txt, limit): txt.append( urwid.Text( [ - ("highlight", "... %s of data not shown. Press "%utils.pretty_size(rem)), + ("highlight", "... %s of data not shown. Press "%netlib.utils.pretty_size(rem)), ("key", "f"), ("highlight", " to load all data.") ] diff --git a/libmproxy/console/statusbar.py b/libmproxy/console/statusbar.py index 5455ad6ee..37ceef946 100644 --- a/libmproxy/console/statusbar.py +++ b/libmproxy/console/statusbar.py @@ -1,8 +1,8 @@ -import time import os.path import urwid +import netlib.utils from . import pathedit, signals, common from .. import utils @@ -22,7 +22,6 @@ class ActionBar(urwid.WidgetWrap): self.onekey = False self.pathprompt = False - def sig_message(self, sender, message, expire=None): w = urwid.Text(message) self._w = w @@ -191,7 +190,7 @@ class StatusBar(urwid.WidgetWrap): opts.append("following") if self.master.stream_large_bodies: opts.append( - "stream:%s" % utils.pretty_size( + "stream:%s" % netlib.utils.pretty_size( self.master.stream_large_bodies.max_size ) ) diff --git a/libmproxy/dump.py b/libmproxy/dump.py index 59e46dbbb..ac9c273f1 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -207,7 +207,7 @@ class DumpMaster(flow.FlowMaster): if f.response.content == http.CONTENT_MISSING: sz = "(content missing)" else: - sz = utils.pretty_size(len(f.response.content)) + sz = netlib.utils.pretty_size(len(f.response.content)) print(" << %s %s" % (str_response(f.response), sz), file=self.outfile) self._print_message(f.response) diff --git a/libmproxy/protocol/http.py b/libmproxy/protocol/http.py index 852ce393f..8d703786b 100644 --- a/libmproxy/protocol/http.py +++ b/libmproxy/protocol/http.py @@ -691,7 +691,10 @@ class HTTPResponse(HTTPMessage): return f def __repr__(self): - size = utils.pretty_size(len(self.content)) if self.content else "content missing" + if self.content: + size = netlib.utils.pretty_size(len(self.content)) + else: + size = "content missing" return "".format( code=self.code, msg=self.msg, diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 02e8403bf..7d0e369bf 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -96,20 +96,6 @@ def multipartdecode(hdrs, content): return r return [] -def pretty_size(size): - suffixes = [ - ("B", 2**10), - ("kB", 2**20), - ("MB", 2**30), - ] - for suf, lim in suffixes: - if size >= lim: - continue - else: - x = round(size/float(lim/2**10), 2) - if x == int(x): - x = int(x) - return str(x) + suf def pretty_duration(secs): formatters = [ @@ -124,6 +110,7 @@ def pretty_duration(secs): #less than 1 sec return "{:.0f}ms".format(secs*1000) + class Data: def __init__(self, name): m = __import__(name) diff --git a/test/test_utils.py b/test/test_utils.py index ea38b9a36..6b9262a04 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,5 @@ import json -from libmproxy import utils, flow +from libmproxy import utils from netlib import odict import tutils @@ -9,9 +9,11 @@ utils.CERT_SLEEP_TIME = 0 def test_format_timestamp(): assert utils.format_timestamp(utils.timestamp()) + def test_format_timestamp_with_milli(): assert utils.format_timestamp_with_milli(utils.timestamp()) + def test_isBin(): assert not utils.isBin("testing\n\r") assert utils.isBin("testing\x01") @@ -31,13 +33,6 @@ def test_clean_hanging_newline(): assert utils.clean_hanging_newline("foo") == "foo" -def test_pretty_size(): - assert utils.pretty_size(100) == "100B" - assert utils.pretty_size(1024) == "1kB" - assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB" - assert utils.pretty_size(1024*1024) == "1MB" - - def test_pkg_data(): assert utils.pkg_data.path("console") tutils.raises("does not exist", utils.pkg_data.path, "nonexistent") @@ -135,7 +130,7 @@ def test_parse_size(): def test_parse_content_type(): p = utils.parse_content_type assert p("text/html") == ("text", "html", {}) - assert p("text") == None + assert p("text") is None v = p("text/html; charset=UTF-8") assert v == ('text', 'html', {'charset': 'UTF-8'}) @@ -146,4 +141,4 @@ def test_safe_subn(): def test_urlencode(): - assert utils.urlencode([('foo','bar')]) + assert utils.urlencode([('foo', 'bar')])