update examples: no decoded() anymore 🎉

This commit is contained in:
Maximilian Hils 2016-07-02 02:01:46 -07:00
parent 6032c4f235
commit dbf7cb1a44
7 changed files with 43 additions and 51 deletions

View File

@ -20,7 +20,7 @@ class ViewPigLatin(contentviews.View):
docinfo = d.getroottree().docinfo docinfo = d.getroottree().docinfo
def piglify(src): def piglify(src):
words = string.split(src) words = src.split()
ret = '' ret = ''
for word in words: for word in words:
idx = -1 idx = -1

View File

@ -127,7 +127,7 @@ def response(context, flow):
for k, v in flow.request.query or {}] for k, v in flow.request.query or {}]
response_body_size = len(flow.response.content) 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 response_body_compression = response_body_decoded_size - response_body_size
entry = HAR.entries({ entry = HAR.entries({

View File

@ -2,7 +2,6 @@
# (this script works best with --anticache) # (this script works best with --anticache)
import sys import sys
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from mitmproxy.models import decoded
def start(context): def start(context):
@ -14,15 +13,14 @@ def start(context):
def response(context, flow): def response(context, flow):
if flow.request.host in context.iframe_url: if flow.request.host in context.iframe_url:
return return
with decoded(flow.response): # Remove content encoding (gzip, ...) html = BeautifulSoup(flow.response.content, "lxml")
html = BeautifulSoup(flow.response.content, "lxml") if html.body:
if html.body: iframe = html.new_tag(
iframe = html.new_tag( "iframe",
"iframe", src=context.iframe_url,
src=context.iframe_url, frameborder=0,
frameborder=0, height=0,
height=0, width=0)
width=0) html.body.insert(0, iframe)
html.body.insert(0, iframe) flow.response.content = str(html)
flow.response.content = str(html) context.log("Iframe inserted.")
context.log("Iframe inserted.")

View File

@ -2,8 +2,6 @@
# (this script works best with --anticache) # (this script works best with --anticache)
import sys import sys
from mitmproxy.models import decoded
def start(context): def start(context):
if len(sys.argv) != 3: if len(sys.argv) != 3:
@ -14,7 +12,7 @@ def start(context):
def response(context, flow): def response(context, flow):
with decoded(flow.response): # automatically decode gzipped responses. flow.response.content = flow.response.content.replace(
flow.response.content = flow.response.content.replace( context.old,
context.old, context.new
context.new) )

View File

@ -13,9 +13,9 @@ def request(context, flow):
# Method 1: Answer with a locally generated response # Method 1: Answer with a locally generated response
if flow.request.pretty_host.endswith("example.com"): if flow.request.pretty_host.endswith("example.com"):
resp = HTTPResponse( resp = HTTPResponse(
"HTTP/1.1", 200, "OK", b"HTTP/1.1", 200, b"OK",
Headers(Content_Type="text/html"), Headers(Content_Type="text/html"),
"helloworld") b"helloworld")
flow.reply.send(resp) flow.reply.send(resp)
# Method 2: Redirect the request to a different server # Method 2: Redirect the request to a different server

View File

@ -1,4 +1,3 @@
from netlib.http import decoded
import re import re
from six.moves import urllib from six.moves import urllib
@ -19,22 +18,21 @@ def request(context, flow):
def response(context, flow): def response(context, flow):
with decoded(flow.response): flow.request.headers.pop('Strict-Transport-Security', None)
flow.request.headers.pop('Strict-Transport-Security', None) flow.request.headers.pop('Public-Key-Pins', None)
flow.request.headers.pop('Public-Key-Pins', None)
# strip links in response body # strip links in response body
flow.response.content = flow.response.content.replace('https://', 'http://') flow.response.content = flow.response.content.replace('https://', 'http://')
# strip links in 'Location' header # strip links in 'Location' header
if flow.response.headers.get('Location', '').startswith('https://'): if flow.response.headers.get('Location', '').startswith('https://'):
location = flow.response.headers['Location'] location = flow.response.headers['Location']
hostname = urllib.parse.urlparse(location).hostname hostname = urllib.parse.urlparse(location).hostname
if hostname: if hostname:
context.secure_hosts.add(hostname) context.secure_hosts.add(hostname)
flow.response.headers['Location'] = location.replace('https://', 'http://', 1) flow.response.headers['Location'] = location.replace('https://', 'http://', 1)
# strip secure flag from 'Set-Cookie' headers # strip secure flag from 'Set-Cookie' headers
cookies = flow.response.headers.get_all('Set-Cookie') cookies = flow.response.headers.get_all('Set-Cookie')
cookies = [re.sub(r';\s*secure\s*', '', s) for s in cookies] cookies = [re.sub(r';\s*secure\s*', '', s) for s in cookies]
flow.response.headers.set_all('Set-Cookie', cookies) flow.response.headers.set_all('Set-Cookie', cookies)

View File

@ -1,17 +1,15 @@
from six.moves import cStringIO as StringIO from six.moves import cStringIO as StringIO
from PIL import Image from PIL import Image
from mitmproxy.models import decoded
def response(context, flow): def response(context, flow):
if flow.response.headers.get("content-type", "").startswith("image"): if flow.response.headers.get("content-type", "").startswith("image"):
with decoded(flow.response): # automatically decode gzipped responses. try:
try: s = StringIO(flow.response.content)
s = StringIO(flow.response.content) img = Image.open(s).rotate(180)
img = Image.open(s).rotate(180) s2 = StringIO()
s2 = StringIO() img.save(s2, "png")
img.save(s2, "png") flow.response.content = s2.getvalue()
flow.response.content = s2.getvalue() flow.response.headers["content-type"] = "image/png"
flow.response.headers["content-type"] = "image/png" except: # Unknown image types etc.
except: # Unknown image types etc. pass
pass