From 36118973d98f1afa81e79a846f595a7c4782ff9a Mon Sep 17 00:00:00 2001 From: harsh vijay Date: Sat, 29 Apr 2017 03:26:14 +0530 Subject: [PATCH 1/3] extend mypy example/simple --- examples/simple/add_header.py | 5 ++++- examples/simple/add_header_class.py | 5 ++++- examples/simple/custom_contentview.py | 3 ++- examples/simple/filter_flows.py | 6 +++--- examples/simple/io_read_dumpfile.py | 1 + examples/simple/io_write_dumpfile.py | 8 ++++---- examples/simple/modify_body_inject_iframe.py | 4 ++-- examples/simple/modify_form.py | 5 ++++- examples/simple/modify_querystring.py | 5 ++++- examples/simple/redirect_requests.py | 3 ++- examples/simple/send_reply_from_proxy.py | 2 +- examples/simple/upsidedownternet.py | 4 ++-- examples/simple/wsgi_flask_app.py | 2 +- 13 files changed, 34 insertions(+), 19 deletions(-) diff --git a/examples/simple/add_header.py b/examples/simple/add_header.py index 3e0b5f1e3..64fc6267a 100644 --- a/examples/simple/add_header.py +++ b/examples/simple/add_header.py @@ -1,2 +1,5 @@ -def response(flow): +from mitmproxy import http + + +def response(flow: http.HTTPFlow) -> None: flow.response.headers["newheader"] = "foo" diff --git a/examples/simple/add_header_class.py b/examples/simple/add_header_class.py index 5d5c7902a..419c99ac2 100644 --- a/examples/simple/add_header_class.py +++ b/examples/simple/add_header_class.py @@ -1,5 +1,8 @@ +from mitmproxy import http + + class AddHeader: - def response(self, flow): + def response(self, flow: http.HTTPFlow) -> None: flow.response.headers["newheader"] = "foo" diff --git a/examples/simple/custom_contentview.py b/examples/simple/custom_contentview.py index 34fa55414..6fb51aaaf 100644 --- a/examples/simple/custom_contentview.py +++ b/examples/simple/custom_contentview.py @@ -3,6 +3,7 @@ This example shows how one can add a custom contentview to mitmproxy. The content view API is explained in the mitmproxy.contentviews module. """ from mitmproxy import contentviews +from typing import Tuple, Iterable, AnyStr, List class ViewSwapCase(contentviews.View): @@ -13,7 +14,7 @@ class ViewSwapCase(contentviews.View): prompt = ("swap case text", "z") content_types = ["text/plain"] - def __call__(self, data: bytes, **metadata): + def __call__(self, data: bytes, **metadata) -> Tuple[str,Iterable[List[Tuple[str, AnyStr]]]]: return "case-swapped text", contentviews.format_text(data.swapcase()) diff --git a/examples/simple/filter_flows.py b/examples/simple/filter_flows.py index fd49425a9..709795911 100644 --- a/examples/simple/filter_flows.py +++ b/examples/simple/filter_flows.py @@ -2,12 +2,12 @@ This scripts demonstrates how to use mitmproxy's filter pattern in scripts. """ from mitmproxy import flowfilter -from mitmproxy import ctx +from mitmproxy import ctx, http class Filter: def __init__(self): - self.filter = None + self.filter = None # type: flowfilter.TFilter def configure(self, updated): self.filter = flowfilter.parse(ctx.options.flowfilter) @@ -17,7 +17,7 @@ class Filter: "flowfilter", str, "", "Check that flow matches filter." ) - def response(self, flow): + def response(self, flow: http.HTTPFlow) -> None: if flowfilter.match(self.filter, flow): print("Flow matches filter:") print(flow) diff --git a/examples/simple/io_read_dumpfile.py b/examples/simple/io_read_dumpfile.py index edbbe2dd2..87d37c0ff 100644 --- a/examples/simple/io_read_dumpfile.py +++ b/examples/simple/io_read_dumpfile.py @@ -8,6 +8,7 @@ from mitmproxy.exceptions import FlowReadException import pprint import sys + with open(sys.argv[1], "rb") as logfile: freader = io.FlowReader(logfile) pp = pprint.PrettyPrinter(indent=4) diff --git a/examples/simple/io_write_dumpfile.py b/examples/simple/io_write_dumpfile.py index a0956e335..04d943c0b 100644 --- a/examples/simple/io_write_dumpfile.py +++ b/examples/simple/io_write_dumpfile.py @@ -7,18 +7,18 @@ to multiple files in parallel. """ import random import sys -from mitmproxy import io +from mitmproxy import io, http class Writer: - def __init__(self, path): + def __init__(self, path: str) -> None: if path == "-": - f = sys.stdout + f = sys.stdout # type: io.TextIO else: f = open(path, "wb") self.w = io.FlowWriter(f) - def response(self, flow): + def response(self, flow: http.HTTPFlow) -> None: if random.choice([True, False]): self.w.add(flow) diff --git a/examples/simple/modify_body_inject_iframe.py b/examples/simple/modify_body_inject_iframe.py index dff72afa3..595bd9f28 100644 --- a/examples/simple/modify_body_inject_iframe.py +++ b/examples/simple/modify_body_inject_iframe.py @@ -1,6 +1,6 @@ # (this script works best with --anticache) from bs4 import BeautifulSoup -from mitmproxy import ctx +from mitmproxy import ctx, http class Injector: @@ -9,7 +9,7 @@ class Injector: "iframe", str, "", "IFrame to inject" ) - def response(self, flow): + def response(self, flow: http.HTTPFlow) -> None: if ctx.options.iframe: html = BeautifulSoup(flow.response.content, "html.parser") if html.body: diff --git a/examples/simple/modify_form.py b/examples/simple/modify_form.py index b425efb08..8742a976f 100644 --- a/examples/simple/modify_form.py +++ b/examples/simple/modify_form.py @@ -1,4 +1,7 @@ -def request(flow): +from mitmproxy import http + + +def request(flow: http.HTTPFlow) -> None: if flow.request.urlencoded_form: # If there's already a form, one can just add items to the dict: flow.request.urlencoded_form["mitmproxy"] = "rocks" diff --git a/examples/simple/modify_querystring.py b/examples/simple/modify_querystring.py index ee8a89ad8..12b16fda7 100644 --- a/examples/simple/modify_querystring.py +++ b/examples/simple/modify_querystring.py @@ -1,2 +1,5 @@ -def request(flow): +from mitmproxy import http + + +def request(flow: http.HTTPFlow) -> None: flow.request.query["mitmproxy"] = "rocks" diff --git a/examples/simple/redirect_requests.py b/examples/simple/redirect_requests.py index 51876df74..ddb89961f 100644 --- a/examples/simple/redirect_requests.py +++ b/examples/simple/redirect_requests.py @@ -1,9 +1,10 @@ """ This example shows two ways to redirect flows to another server. """ +from mitmproxy import http -def request(flow): +def request(flow: http.HTTPFlow) -> None: # pretty_host takes the "Host" header of the request into account, # which is useful in transparent mode where we usually only have the IP # otherwise. diff --git a/examples/simple/send_reply_from_proxy.py b/examples/simple/send_reply_from_proxy.py index bef2e7e78..5011fd2e5 100644 --- a/examples/simple/send_reply_from_proxy.py +++ b/examples/simple/send_reply_from_proxy.py @@ -5,7 +5,7 @@ without sending any data to the remote server. from mitmproxy import http -def request(flow): +def request(flow: http.HTTPFlow) -> None: # pretty_url takes the "Host" header of the request into account, which # is useful in transparent mode where we usually only have the IP otherwise. diff --git a/examples/simple/upsidedownternet.py b/examples/simple/upsidedownternet.py index 8ba450abd..f150a5c37 100644 --- a/examples/simple/upsidedownternet.py +++ b/examples/simple/upsidedownternet.py @@ -2,11 +2,11 @@ This script rotates all images passing through the proxy by 180 degrees. """ import io - from PIL import Image +from mitmproxy import http -def response(flow): +def response(flow: http.HTTPFlow) -> None: if flow.response.headers.get("content-type", "").startswith("image"): s = io.BytesIO(flow.response.content) img = Image.open(s).rotate(180) diff --git a/examples/simple/wsgi_flask_app.py b/examples/simple/wsgi_flask_app.py index a03ad4c5a..4be380005 100644 --- a/examples/simple/wsgi_flask_app.py +++ b/examples/simple/wsgi_flask_app.py @@ -10,7 +10,7 @@ app = Flask("proxapp") @app.route('/') -def hello_world(): +def hello_world() -> str: return 'Hello World!' From 6e03231d255344f6f84643656ba0be802624aa8c Mon Sep 17 00:00:00 2001 From: harsh vijay Date: Sat, 29 Apr 2017 03:49:08 +0530 Subject: [PATCH 2/3] lint error fixed --- examples/simple/custom_contentview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple/custom_contentview.py b/examples/simple/custom_contentview.py index 6fb51aaaf..289e1efe1 100644 --- a/examples/simple/custom_contentview.py +++ b/examples/simple/custom_contentview.py @@ -14,7 +14,7 @@ class ViewSwapCase(contentviews.View): prompt = ("swap case text", "z") content_types = ["text/plain"] - def __call__(self, data: bytes, **metadata) -> Tuple[str,Iterable[List[Tuple[str, AnyStr]]]]: + def __call__(self, data: bytes, **metadata) -> Tuple[str, Iterable[List[Tuple[str, AnyStr]]]]: return "case-swapped text", contentviews.format_text(data.swapcase()) From 789fbd00d24ee591de592ae5d8143fe4278298d7 Mon Sep 17 00:00:00 2001 From: harsh vijay Date: Sat, 29 Apr 2017 03:54:00 +0530 Subject: [PATCH 3/3] Fixed lint error --- examples/simple/io_write_dumpfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple/io_write_dumpfile.py b/examples/simple/io_write_dumpfile.py index 04d943c0b..7c4c6a7ac 100644 --- a/examples/simple/io_write_dumpfile.py +++ b/examples/simple/io_write_dumpfile.py @@ -13,7 +13,7 @@ from mitmproxy import io, http class Writer: def __init__(self, path: str) -> None: if path == "-": - f = sys.stdout # type: io.TextIO + f = sys.stdout # type: io.TextIO else: f = open(path, "wb") self.w = io.FlowWriter(f)