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
def piglify(src):
words = string.split(src)
words = src.split()
ret = ''
for word in words:
idx = -1

View File

@ -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({

View File

@ -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.")

View File

@ -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
)

View File

@ -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

View File

@ -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)

View File

@ -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