mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-24 04:14:57 +08:00
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"
This commit is contained in:
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user