Skip to content

Commit

Permalink
Add explicit notice when data has been auto-decoded.
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Jul 18, 2011
1 parent 669ce8e commit b084938
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions libmproxy/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ def format_flow(f, focus, extended=False, padding=2):
if t:
t = t[0].split(";")[0]
txt.append(("text", " %s"%t))
e = f.response.headers["content-encoding"]
if e:
e = e[0]
else:
e = "identity"
if f.response.content:
txt.append(", %s"%utils.pretty_size(len(f.response.content)))
elif f.error:
Expand Down Expand Up @@ -916,14 +911,17 @@ def _trailer(self, clen, txt):
)
)

def _view_conn_raw(self, content, txt):
def _view_conn_raw(self, content):
txt = []
for i in utils.cleanBin(content[:VIEW_CUTOFF]).splitlines():
txt.append(
urwid.Text(("text", i))
)
self._trailer(len(content), txt)
return txt

def _view_conn_binary(self, content, txt):
def _view_conn_binary(self, content):
txt = []
for offset, hex, s in utils.hexdump(content[:VIEW_CUTOFF]):
txt.append(urwid.Text([
("offset", offset),
Expand All @@ -933,15 +931,19 @@ def _view_conn_binary(self, content, txt):
("text", s),
]))
self._trailer(len(content), txt)
return txt

def _view_conn_xmlish(self, content, txt):
def _view_conn_xmlish(self, content):
txt = []
for i in utils.pretty_xmlish(content[:VIEW_CUTOFF]):
txt.append(
urwid.Text(("text", i)),
)
self._trailer(len(content), txt)
return txt

def _view_conn_json(self, lines, txt):
txt = []
sofar = 0
for i in lines:
sofar += len(i)
Expand All @@ -951,20 +953,22 @@ def _view_conn_json(self, lines, txt):
if sofar > VIEW_CUTOFF:
break
self._trailer(sum(len(i) for i in lines), txt)
return txt

def _view_conn_urlencoded(self, lines):
return [
urwid.Text(("highlight", "URLencoded data:\n")),
urwid.Text(
format_keyvals(
[(k+":", v) for (k, v) in lines],
key = "header",
val = "text"
)
)
]

def _view_conn_urlencoded(self, lines, txt):
txt.append(urwid.Text(("highlight", "URLencoded data:\n")))
txt.append(
urwid.Text(
format_keyvals(
[(k+":", v) for (k, v) in lines],
key = "header",
val = "text"
)
)
)

def _find_pretty_view(self, content, hdrItems, txt):
def _find_pretty_view(self, content, hdrItems):
txt = []
ctype = None
for i in hdrItems:
if i[0].lower() == "content-type":
Expand All @@ -973,20 +977,22 @@ def _find_pretty_view(self, content, hdrItems, txt):
if ctype and "x-www-form-urlencoded" in ctype:
data = utils.urldecode(content)
if data:
return self._view_conn_urlencoded(data, txt)
return self._view_conn_urlencoded(data)
if utils.isXML(content):
return self._view_conn_xmlish(content, txt)
return self._view_conn_xmlish(content)
elif ctype and "application/json" in ctype:
lines = utils.pretty_json(content)
if lines:
return self._view_conn_json(lines, txt)
return self._view_conn_raw(content, txt)
return self._view_conn_json(lines)
return self._view_conn_raw(content)

@utils.LRUCache(20)
def _cached_conn_text(self, e, rawcontent, hdrItems, viewmode):
content = encoding.decode(e, rawcontent)
if content is None:
content = rawcontent
e = None

hdr = []
hdr.extend(
format_keyvals(
Expand All @@ -998,13 +1004,17 @@ def _cached_conn_text(self, e, rawcontent, hdrItems, viewmode):
hdr.append("\n")

txt = [urwid.Text(hdr)]
if e and e != "identity":
txt.append(
urwid.Text(("highlight", "Decoded %s data:\n"%e))
)
if content:
if viewmode == VIEW_BODY_HEX:
self._view_conn_binary(content, txt)
txt.extend(self._view_conn_binary(content))
elif viewmode == VIEW_BODY_PRETTY:
self._find_pretty_view(content, hdrItems, txt)
txt.extend(self._find_pretty_view(content, hdrItems))
else:
self._view_conn_raw(content, txt)
txt.extend(self._view_conn_raw(content))
return urwid.ListBox(txt)

def _readflow(self, path):
Expand Down

0 comments on commit b084938

Please sign in to comment.