aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/flow.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-03-16 11:12:56 +1300
committerAldo Cortesi <aldo@nullcube.com>2012-03-16 11:12:56 +1300
commitd51b8cab0c0d1352865155865dfd258f66103ffe (patch)
treef5e950d77224908da351d5d809486d3c0c9c1588 /libmproxy/flow.py
parent8d662e6636b534bdb9fd2f2312589b6e8e453378 (diff)
downloadmitmproxy-d51b8cab0c0d1352865155865dfd258f66103ffe.tar.gz
mitmproxy-d51b8cab0c0d1352865155865dfd258f66103ffe.tar.bz2
mitmproxy-d51b8cab0c0d1352865155865dfd258f66103ffe.zip
Add a decoded context manager.
This simplifies a common chore when modifying traffic - decoding the object, modifying it, then re-encoding it with the same encoding afterwards. You can now simply say: with flow.decoded(request): request.content = "bar"
Diffstat (limited to 'libmproxy/flow.py')
-rw-r--r--libmproxy/flow.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/libmproxy/flow.py b/libmproxy/flow.py
index 450fef30..dbc0c109 100644
--- a/libmproxy/flow.py
+++ b/libmproxy/flow.py
@@ -199,6 +199,34 @@ class ODictCaseless(ODict):
return s.lower()
+class decoded(object):
+ """
+
+ A context manager that decodes a request, response or error, and then
+ re-encodes it with the same encoding after execution of the block.
+
+ Example:
+
+ with decoded(request):
+ request.content = request.content.replace("foo", "bar")
+ """
+ def __init__(self, o):
+ self.o = o
+ ce = o.headers["content-encoding"]
+ if ce and ce[0] in encoding.ENCODINGS:
+ self.ce = ce[0]
+ else:
+ self.ce = None
+
+ def __enter__(self):
+ if self.ce:
+ self.o.decode()
+
+ def __exit__(self, type, value, tb):
+ if self.ce:
+ self.o.encode(self.ce)
+
+
class HTTPMsg(controller.Msg):
def decode(self):
"""