Merge pull request #3106 from cortesi/noprint

Ditch the addon stdout wrapper
This commit is contained in:
Maximilian Hils 2018-05-08 15:24:02 +02:00 committed by GitHub
commit 0c101a4bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 11 additions and 45 deletions

View File

@ -1,11 +1,12 @@
import time import time
from mitmproxy.script import concurrent from mitmproxy.script import concurrent
from mitmproxy import ctx
@concurrent # Remove this and see what happens @concurrent # Remove this and see what happens
def request(flow): def request(flow):
# You don't want to use mitmproxy.ctx from a different thread # You don't want to use mitmproxy.ctx from a different thread
print("handle request: %s%s" % (flow.request.host, flow.request.path)) ctx.log.info("handle request: %s%s" % (flow.request.host, flow.request.path))
time.sleep(5) time.sleep(5)
print("start request: %s%s" % (flow.request.host, flow.request.path)) ctx.log.info("start request: %s%s" % (flow.request.host, flow.request.path))

View File

@ -9,6 +9,7 @@ example cmdline invocation:
mitmdump -T --host --tcp ".*" -q -s examples/tcp_message.py mitmdump -T --host --tcp ".*" -q -s examples/tcp_message.py
""" """
from mitmproxy.utils import strutils from mitmproxy.utils import strutils
from mitmproxy import ctx
def tcp_message(tcp_msg): def tcp_message(tcp_msg):
@ -17,7 +18,7 @@ def tcp_message(tcp_msg):
is_modified = False if modified_msg == tcp_msg.message else True is_modified = False if modified_msg == tcp_msg.message else True
tcp_msg.message = modified_msg tcp_msg.message = modified_msg
print( ctx.log.info(
"[tcp_message{}] from {} {} to {} {}:\r\n{}".format( "[tcp_message{}] from {} {} to {} {}:\r\n{}".format(
" (modified)" if is_modified else "", " (modified)" if is_modified else "",
"client" if tcp_msg.sender == tcp_msg.client_conn else "server", "client" if tcp_msg.sender == tcp_msg.client_conn else "server",

View File

@ -19,8 +19,8 @@ class Filter:
def response(self, flow: http.HTTPFlow) -> None: def response(self, flow: http.HTTPFlow) -> None:
if flowfilter.match(self.filter, flow): if flowfilter.match(self.filter, flow):
print("Flow matches filter:") ctx.log.info("Flow matches filter:")
print(flow) ctx.log.info(flow)
addons = [Filter()] addons = [Filter()]

View File

@ -1,10 +1,7 @@
"""
It is recommended to use `ctx.log` for logging within a script.
print() statements are equivalent to ctx.log.warn().
"""
from mitmproxy import ctx from mitmproxy import ctx
def load(l): def load(l):
ctx.log.info("This is some informative text.") ctx.log.info("This is some informative text.")
ctx.log.warn("This is a warning.")
ctx.log.error("This is an error.") ctx.log.error("This is an error.")

View File

@ -36,25 +36,10 @@ def cut_traceback(tb, func_name):
return tb or tb_orig return tb or tb_orig
class StreamLog:
def __init__(self, lg):
self.log = lg
def write(self, buf):
if buf.strip():
self.log(buf)
def flush(self): # pragma: no cover
# Click uses flush sometimes, so we dummy it up
pass
@contextlib.contextmanager @contextlib.contextmanager
def safecall(): def safecall():
stdout_replacement = StreamLog(lambda message: ctx.log.warn(message))
try: try:
with contextlib.redirect_stdout(stdout_replacement): yield
yield
except (exceptions.AddonHalt, exceptions.OptionsError): except (exceptions.AddonHalt, exceptions.OptionsError):
raise raise
except Exception as e: except Exception as e:

View File

@ -33,6 +33,7 @@ class TBase(tservers.HTTPProxyTest):
s = time.time() s = time.time()
while True: while True:
if flow.response or flow.error: if flow.response or flow.error:
flow.server_conn.close()
break break
time.sleep(0.001) time.sleep(0.001)
if time.time() - s > 5: if time.time() - s > 5:
@ -55,6 +56,7 @@ class TBase(tservers.HTTPProxyTest):
l = self.master.state.flows[-1] l = self.master.state.flows[-1]
assert l.response.status_code == 304 assert l.response.status_code == 304
l.request.path = "/p/305" l.request.path = "/p/305"
l.response = None
cr.start_replay([l]) cr.start_replay([l])
self.wait_response(l) self.wait_response(l)
assert l.response.status_code == 305 assert l.response.status_code == 305

View File

@ -60,17 +60,6 @@ def test_load_fullname(tdata):
assert not hasattr(ns2, "addons") assert not hasattr(ns2, "addons")
@pytest.mark.asyncio
async def test_script_print_stdout(tdata):
with taddons.context() as tctx:
with addonmanager.safecall():
ns = script.load_script(
tdata.path("mitmproxy/data/addonscripts/print.py")
)
ns.load(addonmanager.Loader(tctx.master))
assert await tctx.master.await_log("stdoutprint")
class TestScript: class TestScript:
def test_notfound(self): def test_notfound(self):
with taddons.context(): with taddons.context():

View File

@ -1,2 +0,0 @@
def load(l):
print("stdoutprint")

View File

@ -196,10 +196,3 @@ class D:
def log(self, x): def log(self, x):
self.w = x self.w = x
def test_streamlog():
dummy = D()
s = addonmanager.StreamLog(dummy.log)
s.write("foo")
assert dummy.w == "foo"