Created
January 29, 2015 10:28
-
-
Save Informatic/5c03b1d8edb41d16fd16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Really simple chunked/gzipped HTTP Request/Response decoder. | |
Might be useful in conjunction with sslsplit and vim. | |
Note: it loads whole body to memory, but might be fixed with zlib.decompressobj | |
""" | |
import sys | |
import zlib | |
from httplib import HTTPResponse | |
from StringIO import StringIO | |
is_gzip = False | |
is_chunked = False | |
for f in iter(sys.stdin.readline, ''): | |
if not f.strip(): | |
break | |
k, _, v = f.partition(':') | |
if k.strip().lower() == 'transfer-encoding' and \ | |
v.strip().lower() == 'chunked': | |
is_chunked = True | |
continue # Ignore that header, because it's no longer relevant | |
if k.strip().lower() == 'content-encoding' and \ | |
v.strip().lower() == 'gzip': | |
is_gzip = True | |
continue | |
sys.stdout.write(f) | |
sys.stdout.write(f) | |
data = '' | |
if is_chunked: | |
for f in iter(sys.stdin.readline, ''): | |
chunk_length = int('0x'+f, 16) | |
data += sys.stdin.read(chunk_length) | |
sys.stdin.readline() | |
else: | |
data = sys.stdin.read() | |
if is_gzip: | |
data = zlib.decompress(data, 16+zlib.MAX_WBITS) | |
sys.stdout.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment