aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-03-26 11:26:02 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-03-26 11:26:02 +1300
commita050eeef0537bed695c56ab45e4fb526605e20c0 (patch)
tree07173e7ac925966f05be7fe7500700b583a1837a
parentb6725ee802a761e84e41d394d5d17f11da6e95d1 (diff)
downloadmitmproxy-a050eeef0537bed695c56ab45e4fb526605e20c0.tar.gz
mitmproxy-a050eeef0537bed695c56ab45e4fb526605e20c0.tar.bz2
mitmproxy-a050eeef0537bed695c56ab45e4fb526605e20c0.zip
Add a pretty-viewer for images.
This shows basic image information like dimensions, plus extracted EXIF tags and other metadata.
-rw-r--r--libmproxy/console/contentview.py46
-rw-r--r--test/data/image-err1.jpgbin0 -> 82674 bytes
-rw-r--r--test/data/image.gifbin0 -> 2398 bytes
-rw-r--r--test/data/image.icobin0 -> 11502 bytes
-rw-r--r--test/data/image.jpgbin0 -> 1568 bytes
-rw-r--r--test/data/image.pngbin0 -> 9311 bytes
-rw-r--r--test/test_console_contentview.py7
7 files changed, 52 insertions, 1 deletions
diff --git a/libmproxy/console/contentview.py b/libmproxy/console/contentview.py
index 27addaff..2706358c 100644
--- a/libmproxy/console/contentview.py
+++ b/libmproxy/console/contentview.py
@@ -1,5 +1,7 @@
-import re
+import re, cStringIO
import urwid
+from PIL import Image
+from PIL.ExifTags import TAGS
import common
from .. import utils, encoding, flow
from ..contrib import jsbeautifier
@@ -22,6 +24,7 @@ VIEW_CONTENT_PRETTY_TYPE_XML = 2
VIEW_CONTENT_PRETTY_TYPE_URLENCODED = 3
VIEW_CONTENT_PRETTY_TYPE_MULTIPART = 4
VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT = 5
+VIEW_CONTENT_PRETTY_TYPE_IMAGE = 6
CONTENT_PRETTY_NAMES = {
VIEW_CONTENT_PRETTY_TYPE_JSON: "JSON",
@@ -29,6 +32,7 @@ CONTENT_PRETTY_NAMES = {
VIEW_CONTENT_PRETTY_TYPE_URLENCODED: "URL-encoded",
VIEW_CONTENT_PRETTY_TYPE_MULTIPART: "Multipart Form",
VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT: "JavaScript",
+ VIEW_CONTENT_PRETTY_TYPE_IMAGE: "Image",
}
CONTENT_TYPES_MAP = {
@@ -39,6 +43,11 @@ CONTENT_TYPES_MAP = {
"application/x-www-form-urlencoded": VIEW_CONTENT_PRETTY_TYPE_URLENCODED,
"application/x-javascript": VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT,
"application/javascript": VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT,
+ "text/javascript": VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT,
+ "image/png": VIEW_CONTENT_PRETTY_TYPE_IMAGE,
+ "image/jpeg": VIEW_CONTENT_PRETTY_TYPE_IMAGE,
+ "image/gif": VIEW_CONTENT_PRETTY_TYPE_IMAGE,
+ "image/x-icon": VIEW_CONTENT_PRETTY_TYPE_IMAGE,
}
def trailer(clen, txt):
@@ -164,12 +173,47 @@ def view_javascript(hdrs, content):
return "JavaScript", _view_text(res)
+def view_image(hdrs, content):
+ try:
+ img = Image.open(cStringIO.StringIO(content))
+ except IOError:
+ return None
+ parts = [
+ ("Format", str(img.format_description)),
+ ("Size", "%s x %s px"%img.size),
+ ("Mode", str(img.mode)),
+ ]
+ for i in sorted(img.info.keys()):
+ if i != "exif":
+ parts.append(
+ (str(i), str(img.info[i]))
+ )
+ if hasattr(img, "_getexif"):
+ ex = img._getexif()
+ if ex:
+ for i in sorted(ex.keys()):
+ tag = TAGS.get(i, i)
+ parts.append(
+ (str(tag), str(ex[i]))
+ )
+ clean = []
+ for i in parts:
+ clean.append([utils.cleanBin(i[0]), utils.cleanBin(i[1])])
+ fmt = common.format_keyvals(
+ clean,
+ key = "header",
+ val = "text"
+ )
+ return "%s image"%img.format, fmt
+
+
PRETTY_FUNCTION_MAP = {
VIEW_CONTENT_PRETTY_TYPE_XML: view_xmlish,
VIEW_CONTENT_PRETTY_TYPE_JSON: view_json,
VIEW_CONTENT_PRETTY_TYPE_URLENCODED: view_urlencoded,
VIEW_CONTENT_PRETTY_TYPE_MULTIPART: view_multipart,
VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT: view_javascript,
+ VIEW_CONTENT_PRETTY_TYPE_IMAGE: view_image,
}
def get_view_func(viewmode, pretty_type, hdrs, content):
diff --git a/test/data/image-err1.jpg b/test/data/image-err1.jpg
new file mode 100644
index 00000000..1b251e6e
--- /dev/null
+++ b/test/data/image-err1.jpg
Binary files differ
diff --git a/test/data/image.gif b/test/data/image.gif
new file mode 100644
index 00000000..91c53284
--- /dev/null
+++ b/test/data/image.gif
Binary files differ
diff --git a/test/data/image.ico b/test/data/image.ico
new file mode 100644
index 00000000..4a8421dc
--- /dev/null
+++ b/test/data/image.ico
Binary files differ
diff --git a/test/data/image.jpg b/test/data/image.jpg
new file mode 100644
index 00000000..6cbe081e
--- /dev/null
+++ b/test/data/image.jpg
Binary files differ
diff --git a/test/data/image.png b/test/data/image.png
new file mode 100644
index 00000000..33143e77
--- /dev/null
+++ b/test/data/image.png
Binary files differ
diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py
index e7258545..cc8645df 100644
--- a/test/test_console_contentview.py
+++ b/test/test_console_contentview.py
@@ -83,6 +83,13 @@ class uContentView(libpry.AutoTree):
def test_view_raw(self):
assert cv.view_hex([], "foo")
+ def test_view_image(self):
+ assert cv.view_image([], file("data/image.png").read())
+ assert cv.view_image([], file("data/image.gif").read())
+ assert cv.view_image([], file("data/image-err1.jpg").read())
+ assert cv.view_image([], file("data/image.ico").read())
+ assert not cv.view_image([], "flibble")
+
def test_view_multipart(self):
v = """
--AaB03x