From 58e1b3a47f392a5f4f16e30318820f163568f54e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 12:49:19 +1300 Subject: Start refactoring scripts - Move ScriptContext into script module - Use mock module instead of hand-rolled mock objects in tests --- libmproxy/script.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'libmproxy/script.py') diff --git a/libmproxy/script.py b/libmproxy/script.py index f8a0d085..0f1056f6 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -5,6 +5,39 @@ class ScriptError(Exception): pass +class ScriptContext: + def __init__(self, master): + self._master = master + + def log(self, *args, **kwargs): + """ + Logs an event. + + How this is handled depends on the front-end. mitmdump will display + events if the eventlog flag ("-e") was passed. mitmproxy sends + output to the eventlog for display ("v" keyboard shortcut). + """ + self._master.add_event(*args, **kwargs) + + def duplicate_flow(self, f): + """ + Returns a duplicate of the specified flow. The flow is also + injected into the current state, and is ready for editing, replay, + etc. + """ + self._master.pause_scripts = True + f = self._master.duplicate_flow(f) + self._master.pause_scripts = False + return f + + def replay_request(self, f): + """ + Replay the request on the current flow. The response will be added + to the flow object. + """ + self._master.replay_request(f) + + class Script: """ The instantiator should do something along this vein: @@ -12,9 +45,9 @@ class Script: s = Script(argv, master) s.load() """ - def __init__(self, argv, ctx): + def __init__(self, argv, master): self.argv = argv - self.ctx = ctx + self.ctx = ScriptContext(master) self.ns = None def load(self): @@ -82,4 +115,4 @@ def concurrent(fn): def _concurrent(ctx, conn): _handle_concurrent_reply(fn, conn, [ctx, conn]) return _concurrent - raise NotImplementedError("Concurrent decorator not supported for this method.") \ No newline at end of file + raise NotImplementedError("Concurrent decorator not supported for this method.") -- cgit v1.2.3 From e5776b8be3ea36c065beabe416506871f34892e6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 13:59:32 +1300 Subject: Clean up and clarify script API --- libmproxy/script.py | 1 + 1 file changed, 1 insertion(+) (limited to 'libmproxy/script.py') diff --git a/libmproxy/script.py b/libmproxy/script.py index 0f1056f6..747d1567 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -49,6 +49,7 @@ class Script: self.argv = argv self.ctx = ScriptContext(master) self.ns = None + self.load() def load(self): """ -- cgit v1.2.3 From 42d4a2fae96b8b4ba35d3a88e20f278d79a0ccc6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 12 Jan 2014 23:01:59 +1300 Subject: Script refactoring: move script command parsing into script module. --- libmproxy/script.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'libmproxy/script.py') diff --git a/libmproxy/script.py b/libmproxy/script.py index 747d1567..a80f4694 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -1,4 +1,4 @@ -import os, traceback, threading +import os, traceback, threading, shlex import controller class ScriptError(Exception): @@ -45,8 +45,9 @@ class Script: s = Script(argv, master) s.load() """ - def __init__(self, argv, master): - self.argv = argv + def __init__(self, command, master): + self.command = command + self.argv = shlex.split(command, posix=(os.name != "nt")) self.ctx = ScriptContext(master) self.ns = None self.load() @@ -99,7 +100,6 @@ class Script: def _handle_concurrent_reply(fn, o, args=[], kwargs={}): reply = o.reply o.reply = controller.DummyReply() - def run(): fn(*args, **kwargs) reply(o) -- cgit v1.2.3 From 4f69eef8f310b87a45782b8d097dd148e815486a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 13 Jan 2014 14:15:17 +1300 Subject: Extract command parsing and use in script grid editor --- libmproxy/script.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'libmproxy/script.py') diff --git a/libmproxy/script.py b/libmproxy/script.py index a80f4694..0912c9ae 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -47,11 +47,21 @@ class Script: """ def __init__(self, command, master): self.command = command - self.argv = shlex.split(command, posix=(os.name != "nt")) + self.argv = self.parse_command(command) self.ctx = ScriptContext(master) self.ns = None self.load() + @classmethod + def parse_command(klass, command): + args = shlex.split(command, posix=(os.name != "nt")) + args[0] = os.path.expanduser(args[0]) + if not os.path.exists(args[0]): + raise ScriptError("Command not found.") + elif not os.path.isfile(args[0]): + raise ScriptError("Not a file: %s" % args[0]) + return args + def load(self): """ Loads a module. @@ -59,14 +69,9 @@ class Script: Raises ScriptError on failure, with argument equal to an error message that may be a formatted traceback. """ - path = os.path.expanduser(self.argv[0]) - if not os.path.exists(path): - raise ScriptError("No such file: %s" % path) - if not os.path.isfile(path): - raise ScriptError("Not a file: %s" % path) ns = {} try: - execfile(path, ns, ns) + execfile(self.argv[0], ns, ns) except Exception, v: raise ScriptError(traceback.format_exc(v)) self.ns = ns -- cgit v1.2.3