aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/encoding.py
diff options
context:
space:
mode:
authorStephen Altamirano <stephen@evilrobotstuff.com>2011-07-17 20:16:47 -0700
committerStephen Altamirano <stephen@evilrobotstuff.com>2011-07-17 20:16:47 -0700
commitecd46459886da3a851a6e0c4ef11ab5939673a40 (patch)
treefff247dce2127af687aee304ad2d736438a691b2 /libmproxy/encoding.py
parentb0849387b74c9e289bd2ed4f0dc6bd1b7829c10f (diff)
downloadmitmproxy-ecd46459886da3a851a6e0c4ef11ab5939673a40.tar.gz
mitmproxy-ecd46459886da3a851a6e0c4ef11ab5939673a40.tar.bz2
mitmproxy-ecd46459886da3a851a6e0c4ef11ab5939673a40.zip
Adds encode counterparts to decode functions
Diffstat (limited to 'libmproxy/encoding.py')
-rw-r--r--libmproxy/encoding.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/libmproxy/encoding.py b/libmproxy/encoding.py
index b56c03a6..aab4141b 100644
--- a/libmproxy/encoding.py
+++ b/libmproxy/encoding.py
@@ -10,13 +10,21 @@ ENCODINGS = set(["identity", "gzip", "deflate"])
def decode(encoding, content):
encoding_map = {
- "identity": decode_identity,
+ "identity": identity,
"gzip": decode_gzip,
"deflate": decode_deflate,
}
- return encoding_map.get(encoding, decode_identity)(content)
+ return encoding_map.get(encoding, identity)(content)
-def decode_identity(content):
+def encode(encoding, content):
+ encoding_map = {
+ "identity": identity,
+ "gzip": encode_gzip,
+ "deflate": encode_deflate,
+ }
+ return encoding_map.get(encoding, identity)(content)
+
+def identity(content):
"""
Returns content unchanged. Identity is the default value of
Accept-Encoding headers.
@@ -30,9 +38,16 @@ def decode_gzip(content):
except IOError:
return None
+def encode_gzip(content):
+ s = cStringIO.StringIO()
+ gf = gzip.GzipFile(fileobj=s, mode='wb')
+ gf.write(content)
+ gf.close()
+ return s.getvalue()
+
def decode_deflate(content):
"""
- Returns decompress data for DEFLATE. Some servers may respond with
+ Returns decompressed data for DEFLATE. Some servers may respond with
compressed data without a zlib header or checksum. An undocumented
feature of zlib permits the lenient decompression of data missing both
values.
@@ -46,3 +61,9 @@ def decode_deflate(content):
return zlib.decompress(content, -15)
except zlib.error:
return None
+
+def encode_deflate(content):
+ """
+ Returns compressed content, always including zlib header and checksum.
+ """
+ return zlib.compress(content)