From dbf7cb1a442e2c0823d853ca310395048496996d Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 2 Jul 2016 02:01:46 -0700 Subject: [PATCH] update examples: no decoded() anymore :tada: --- examples/custom_contentviews.py | 2 +- examples/har_extractor.py | 2 +- examples/iframe_injector.py | 24 +++++++++++------------- examples/modify_response_body.py | 10 ++++------ examples/redirect_requests.py | 4 ++-- examples/sslstrip.py | 32 +++++++++++++++----------------- examples/upsidedownternet.py | 20 +++++++++----------- 7 files changed, 43 insertions(+), 51 deletions(-) diff --git a/examples/custom_contentviews.py b/examples/custom_contentviews.py index 05ebeb69f..8a57bf747 100644 --- a/examples/custom_contentviews.py +++ b/examples/custom_contentviews.py @@ -20,7 +20,7 @@ class ViewPigLatin(contentviews.View): docinfo = d.getroottree().docinfo def piglify(src): - words = string.split(src) + words = src.split() ret = '' for word in words: idx = -1 diff --git a/examples/har_extractor.py b/examples/har_extractor.py index d6b50c213..54aa84d39 100644 --- a/examples/har_extractor.py +++ b/examples/har_extractor.py @@ -127,7 +127,7 @@ def response(context, flow): for k, v in flow.request.query or {}] response_body_size = len(flow.response.content) - response_body_decoded_size = len(flow.response.get_decoded_content()) + response_body_decoded_size = len(flow.response.content) response_body_compression = response_body_decoded_size - response_body_size entry = HAR.entries({ diff --git a/examples/iframe_injector.py b/examples/iframe_injector.py index 9495da93d..5803b4c16 100644 --- a/examples/iframe_injector.py +++ b/examples/iframe_injector.py @@ -2,7 +2,6 @@ # (this script works best with --anticache) import sys from bs4 import BeautifulSoup -from mitmproxy.models import decoded def start(context): @@ -14,15 +13,14 @@ def start(context): def response(context, flow): if flow.request.host in context.iframe_url: return - with decoded(flow.response): # Remove content encoding (gzip, ...) - html = BeautifulSoup(flow.response.content, "lxml") - if html.body: - iframe = html.new_tag( - "iframe", - src=context.iframe_url, - frameborder=0, - height=0, - width=0) - html.body.insert(0, iframe) - flow.response.content = str(html) - context.log("Iframe inserted.") + html = BeautifulSoup(flow.response.content, "lxml") + if html.body: + iframe = html.new_tag( + "iframe", + src=context.iframe_url, + frameborder=0, + height=0, + width=0) + html.body.insert(0, iframe) + flow.response.content = str(html) + context.log("Iframe inserted.") diff --git a/examples/modify_response_body.py b/examples/modify_response_body.py index 3034892e0..03dfeaa4b 100644 --- a/examples/modify_response_body.py +++ b/examples/modify_response_body.py @@ -2,8 +2,6 @@ # (this script works best with --anticache) import sys -from mitmproxy.models import decoded - def start(context): if len(sys.argv) != 3: @@ -14,7 +12,7 @@ def start(context): def response(context, flow): - with decoded(flow.response): # automatically decode gzipped responses. - flow.response.content = flow.response.content.replace( - context.old, - context.new) + flow.response.content = flow.response.content.replace( + context.old, + context.new + ) diff --git a/examples/redirect_requests.py b/examples/redirect_requests.py index d7db3f1c6..bb1e69521 100644 --- a/examples/redirect_requests.py +++ b/examples/redirect_requests.py @@ -13,9 +13,9 @@ def request(context, flow): # Method 1: Answer with a locally generated response if flow.request.pretty_host.endswith("example.com"): resp = HTTPResponse( - "HTTP/1.1", 200, "OK", + b"HTTP/1.1", 200, b"OK", Headers(Content_Type="text/html"), - "helloworld") + b"helloworld") flow.reply.send(resp) # Method 2: Redirect the request to a different server diff --git a/examples/sslstrip.py b/examples/sslstrip.py index 8dde8e3ef..77e91cc9f 100644 --- a/examples/sslstrip.py +++ b/examples/sslstrip.py @@ -1,4 +1,3 @@ -from netlib.http import decoded import re from six.moves import urllib @@ -19,22 +18,21 @@ def request(context, flow): def response(context, flow): - with decoded(flow.response): - flow.request.headers.pop('Strict-Transport-Security', None) - flow.request.headers.pop('Public-Key-Pins', None) + flow.request.headers.pop('Strict-Transport-Security', None) + flow.request.headers.pop('Public-Key-Pins', None) - # strip links in response body - flow.response.content = flow.response.content.replace('https://', 'http://') + # strip links in response body + flow.response.content = flow.response.content.replace('https://', 'http://') - # strip links in 'Location' header - if flow.response.headers.get('Location', '').startswith('https://'): - location = flow.response.headers['Location'] - hostname = urllib.parse.urlparse(location).hostname - if hostname: - context.secure_hosts.add(hostname) - flow.response.headers['Location'] = location.replace('https://', 'http://', 1) + # strip links in 'Location' header + if flow.response.headers.get('Location', '').startswith('https://'): + location = flow.response.headers['Location'] + hostname = urllib.parse.urlparse(location).hostname + if hostname: + context.secure_hosts.add(hostname) + flow.response.headers['Location'] = location.replace('https://', 'http://', 1) - # strip secure flag from 'Set-Cookie' headers - cookies = flow.response.headers.get_all('Set-Cookie') - cookies = [re.sub(r';\s*secure\s*', '', s) for s in cookies] - flow.response.headers.set_all('Set-Cookie', cookies) + # strip secure flag from 'Set-Cookie' headers + cookies = flow.response.headers.get_all('Set-Cookie') + cookies = [re.sub(r';\s*secure\s*', '', s) for s in cookies] + flow.response.headers.set_all('Set-Cookie', cookies) diff --git a/examples/upsidedownternet.py b/examples/upsidedownternet.py index 9aac9f05c..58ed53d73 100644 --- a/examples/upsidedownternet.py +++ b/examples/upsidedownternet.py @@ -1,17 +1,15 @@ from six.moves import cStringIO as StringIO from PIL import Image -from mitmproxy.models import decoded def response(context, flow): if flow.response.headers.get("content-type", "").startswith("image"): - with decoded(flow.response): # automatically decode gzipped responses. - try: - s = StringIO(flow.response.content) - img = Image.open(s).rotate(180) - s2 = StringIO() - img.save(s2, "png") - flow.response.content = s2.getvalue() - flow.response.headers["content-type"] = "image/png" - except: # Unknown image types etc. - pass + try: + s = StringIO(flow.response.content) + img = Image.open(s).rotate(180) + s2 = StringIO() + img.save(s2, "png") + flow.response.content = s2.getvalue() + flow.response.headers["content-type"] = "image/png" + except: # Unknown image types etc. + pass