aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorChris Czub <chris.czub@gmail.com>2015-11-13 16:55:27 -0500
committerChris Czub <chris.czub@gmail.com>2015-11-13 16:55:27 -0500
commite72a9a62a107ea3f53b6b26d1abe63c554448d17 (patch)
tree6631b484b9029b8391d22a6bf22845058f4f2fce /examples
parentd3feaa3bc6e2d9c2c7ee8286038c69c0b9601869 (diff)
downloadmitmproxy-e72a9a62a107ea3f53b6b26d1abe63c554448d17.tar.gz
mitmproxy-e72a9a62a107ea3f53b6b26d1abe63c554448d17.tar.bz2
mitmproxy-e72a9a62a107ea3f53b6b26d1abe63c554448d17.zip
Feedback from PR #832
Diffstat (limited to 'examples')
-rw-r--r--examples/custom_contentviews.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/custom_contentviews.py b/examples/custom_contentviews.py
new file mode 100644
index 00000000..1a2bcb1e
--- /dev/null
+++ b/examples/custom_contentviews.py
@@ -0,0 +1,66 @@
+import string
+from libmproxy import script, flow, utils
+import libmproxy.contentviews as cv
+from netlib.http import Headers
+import lxml.html
+import lxml.etree
+
+
+class ViewPigLatin(cv.View):
+ name = "pig_latin_HTML"
+ prompt = ("pig latin HTML", "l")
+ content_types = ["text/html"]
+
+ def __call__(self, data, **metadata):
+ if utils.isXML(data):
+ parser = lxml.etree.HTMLParser(
+ strip_cdata=True,
+ remove_blank_text=True
+ )
+ d = lxml.html.fromstring(data, parser=parser)
+ docinfo = d.getroottree().docinfo
+
+ def piglify(src):
+ words = string.split(src)
+ ret = ''
+ for word in words:
+ idx = -1
+ while word[idx] in string.punctuation and (idx * -1) != len(word): idx -= 1
+ if word[0].lower() in 'aeiou':
+ if idx == -1: ret += word[0:] + "hay"
+ else: ret += word[0:len(word)+idx+1] + "hay" + word[idx+1:]
+ else:
+ if idx == -1: ret += word[1:] + word[0] + "ay"
+ else: ret += word[1:len(word)+idx+1] + word[0] + "ay" + word[idx+1:]
+ ret += ' '
+ return ret.strip()
+
+ def recurse(root):
+ if hasattr(root, 'text') and root.text:
+ root.text = piglify(root.text)
+ if hasattr(root, 'tail') and root.tail:
+ root.tail = piglify(root.tail)
+
+ if len(root):
+ for child in root:
+ recurse(child)
+
+ recurse(d)
+
+ s = lxml.etree.tostring(
+ d,
+ pretty_print=True,
+ doctype=docinfo.doctype
+ )
+ return "HTML", cv.format_text(s)
+
+
+pig_view = ViewPigLatin()
+
+
+def start(context, argv):
+ context.add_contentview(pig_view)
+
+
+def stop(context):
+ context.remove_contentview(pig_view)