diff options
author | Maximilian Hils <git@maximilianhils.com> | 2020-01-19 18:36:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-19 18:36:49 +0100 |
commit | b020a11c1af135a36e1c66707923b1e02134724a (patch) | |
tree | 389288e882231b55734ad9b4ad66c3c3a79dd58a | |
parent | 6e57d60f7d821998bf38323a2c0f881eecbbbaa8 (diff) | |
parent | 78dc2094b0d92abcfac97427d88e56568ed9f487 (diff) | |
download | mitmproxy-b020a11c1af135a36e1c66707923b1e02134724a.tar.gz mitmproxy-b020a11c1af135a36e1c66707923b1e02134724a.tar.bz2 mitmproxy-b020a11c1af135a36e1c66707923b1e02134724a.zip |
Merge pull request #3787 from itaisod/console-command-quotes
Fix console command quotes issues
-rw-r--r-- | mitmproxy/command.py | 8 | ||||
-rw-r--r-- | mitmproxy/tools/console/commandexecutor.py | 5 | ||||
-rw-r--r-- | mitmproxy/tools/console/consoleaddons.py | 35 | ||||
-rw-r--r-- | mitmproxy/types.py | 6 |
4 files changed, 29 insertions, 25 deletions
diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 48f9051e..bfec47e1 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -103,7 +103,7 @@ class Command: except TypeError: expected = f'Expected: {str(self.signature.parameters)}' received = f'Received: {str(args)}' - raise exceptions.CommandError(f"Command argument mismatch: \n\t{expected}\n\t{received}") + raise exceptions.CommandError(f"Command argument mismatch: \n {expected}\n {received}") for name, value in bound_arguments.arguments.items(): convert_to = self.signature.parameters[name].annotation @@ -241,7 +241,7 @@ class CommandManager: raise exceptions.CommandError("Unknown command: %s" % command_name) return self.commands[command_name].func(*args) - def _call_strings(self, command_name: str, args: typing.Sequence[str]) -> typing.Any: + def call_strings(self, command_name: str, args: typing.Sequence[str]) -> typing.Any: """ Call a command using a list of string arguments. May raise CommandError. """ @@ -262,7 +262,7 @@ class CommandManager: for part in parts if part.type != mitmproxy.types.Space ] - return self._call_strings(command_name, args) + return self.call_strings(command_name, args) def dump(self, out=sys.stdout) -> None: cmds = list(self.commands.values()) @@ -284,7 +284,7 @@ def parsearg(manager: CommandManager, spec: str, argtype: type) -> typing.Any: try: return t.parse(manager, argtype, spec) except exceptions.TypeError as e: - raise exceptions.CommandError from e + raise exceptions.CommandError(str(e)) from e def command(name: typing.Optional[str] = None): diff --git a/mitmproxy/tools/console/commandexecutor.py b/mitmproxy/tools/console/commandexecutor.py index c738e349..1c6d5aa6 100644 --- a/mitmproxy/tools/console/commandexecutor.py +++ b/mitmproxy/tools/console/commandexecutor.py @@ -2,6 +2,7 @@ import typing from mitmproxy import exceptions from mitmproxy import flow +from mitmproxy import ctx from mitmproxy.tools.console import overlay from mitmproxy.tools.console import signals @@ -15,8 +16,8 @@ class CommandExecutor: if cmd.strip(): try: ret = self.master.commands.execute(cmd) - except exceptions.CommandError as v: - signals.status_message.send(message=str(v)) + except exceptions.CommandError as e: + ctx.log.error(str(e)) else: if ret: if type(ret) == typing.Sequence[flow.Flow]: diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index 7fcd9b48..905653e7 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -160,7 +160,7 @@ class ConsoleAddon: fv = self.master.window.current("options") if not fv: raise exceptions.CommandError("Not viewing options.") - self.master.commands.execute("options.reset.one %s" % fv.current_name()) + self.master.commands.call_strings("options.reset.one", [fv.current_name()]) @command.command("console.nav.start") def nav_start(self) -> None: @@ -248,12 +248,11 @@ class ConsoleAddon: def callback(opt): # We're now outside of the call context... - repl = cmd + " " + " ".join(args) - repl = repl.replace("{choice}", opt) + repl = [arg.replace("{choice}", opt) for arg in args] try: - self.master.commands.execute(repl) + self.master.commands.call_strings(cmd, repl) except exceptions.CommandError as e: - signals.status_message.send(message=str(e)) + ctx.log.error(str(e)) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -276,12 +275,11 @@ class ConsoleAddon: def callback(opt): # We're now outside of the call context... - repl = " ".join(command_lexer.quote(x) for x in args) - repl = repl.replace("{choice}", opt) + repl = [arg.replace("{choice}", opt) for arg in args] try: - self.master.commands.execute(subcmd + " " + repl) + self.master.commands.call_strings(subcmd, repl) except exceptions.CommandError as e: - signals.status_message.send(message=str(e)) + ctx.log.error(str(e)) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -454,8 +452,9 @@ class ConsoleAddon: url = edited_url.rstrip(b"\n") flow.request.url = url.decode() elif flow_part in ["method", "status_code", "reason"]: - self.master.commands.execute( - "console.command flow.set @focus %s " % flow_part + self.master.commands.call_strings( + "console.command", + ["flow.set", "@focus", flow_part] ) def _grideditor(self): @@ -539,10 +538,12 @@ class ConsoleAddon: raise exceptions.CommandError("Invalid flowview mode.") try: - cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, mode) - self.master.commands.execute(cmd) + self.master.commands.call_strings( + "view.settings.setval", + ["@focus", "flowview_mode_%s" % (idx,), mode] + ) except exceptions.CommandError as e: - signals.status_message.send(message=str(e)) + ctx.log.error(str(e)) @command.command("console.flowview.mode.options") def flowview_mode_options(self) -> typing.Sequence[str]: @@ -561,8 +562,10 @@ class ConsoleAddon: raise exceptions.CommandError("Not viewing a flow.") idx = fv.body.tab_offset - cmd = 'view.settings.getval @focus flowview_mode_%s %s' % (idx, self.master.options.console_default_contentview) - return self.master.commands.execute(cmd) + return self.master.commands.call_strings( + "view.settings.getval", + ["@focus", "flowview_mode_%s" % (idx,), self.master.options.console_default_contentview] + ) @command.command("console.key.contexts") def key_contexts(self) -> typing.Sequence[str]: diff --git a/mitmproxy/types.py b/mitmproxy/types.py index ac992217..0d4ffd69 100644 --- a/mitmproxy/types.py +++ b/mitmproxy/types.py @@ -134,7 +134,7 @@ class _IntType(_BaseType): try: return int(s) except ValueError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool: return isinstance(val, int) @@ -328,7 +328,7 @@ class _FlowType(_BaseFlowType): try: flows = manager.execute("view.flows.resolve %s" % (s)) except exceptions.CommandError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e if len(flows) != 1: raise exceptions.TypeError( "Command requires one flow, specification matched %s." % len(flows) @@ -347,7 +347,7 @@ class _FlowsType(_BaseFlowType): try: return manager.execute("view.flows.resolve %s" % (s)) except exceptions.CommandError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool: try: |