From 214498f01c86bd2c84d379880f5eeac521a55e66 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 14 Apr 2018 10:24:03 +1200 Subject: [PATCH] asyncio: adjust readfile.py addon for async --- mitmproxy/addons/readfile.py | 30 +++++++++++++++++------------- mitmproxy/tools/main.py | 1 - 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/mitmproxy/addons/readfile.py b/mitmproxy/addons/readfile.py index aaf02d01b..f97e297b2 100644 --- a/mitmproxy/addons/readfile.py +++ b/mitmproxy/addons/readfile.py @@ -1,3 +1,4 @@ +import asyncio import os.path import sys import typing @@ -17,12 +18,12 @@ class ReadFile: "Read flows from file." ) - def load_flows(self, fo: typing.IO[bytes]) -> int: + async def load_flows(self, fo: typing.IO[bytes]) -> int: cnt = 0 freader = io.FlowReader(fo) try: for flow in freader.stream(): - ctx.master.load_flow(flow) + await ctx.master.load_flow(flow) cnt += 1 except (IOError, exceptions.FlowReadException) as e: if cnt: @@ -33,29 +34,32 @@ class ReadFile: else: return cnt - def load_flows_from_path(self, path: str) -> int: + async def load_flows_from_path(self, path: str) -> int: path = os.path.expanduser(path) try: with open(path, "rb") as f: - return self.load_flows(f) + return await self.load_flows(f) except IOError as e: ctx.log.error("Cannot load flows: {}".format(e)) raise exceptions.FlowReadException(str(e)) from e + async def doread(self, rfile): + try: + await self.load_flows_from_path(ctx.options.rfile) + except exceptions.FlowReadException as e: + raise exceptions.OptionsError(e) from e + finally: + ctx.master.addons.trigger("processing_complete") + def running(self): if ctx.options.rfile: - try: - self.load_flows_from_path(ctx.options.rfile) - except exceptions.FlowReadException as e: - raise exceptions.OptionsError(e) from e - finally: - ctx.master.addons.trigger("processing_complete") + asyncio.get_event_loop().create_task(self.doread(ctx.options.rfile)) class ReadFileStdin(ReadFile): """Support the special case of "-" for reading from stdin""" - def load_flows_from_path(self, path: str) -> int: + async def load_flows_from_path(self, path: str) -> int: if path == "-": - return self.load_flows(sys.stdin.buffer) + return await self.load_flows(sys.stdin.buffer) else: - return super().load_flows_from_path(path) + return await super().load_flows_from_path(path) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index 53c236bba..b424490ac 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -150,7 +150,6 @@ def mitmdump(args=None): # pragma: no cover if args.filter_args: v = " ".join(args.filter_args) return dict( - view_filter=v, save_stream_filter=v, ) return {}