aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/command.py
diff options
context:
space:
mode:
authorHenrique <typoon@gmail.com>2019-11-12 21:27:02 -0500
committerHenrique <typoon@gmail.com>2019-11-12 21:27:02 -0500
commit561415cea99c46dd5df892bcac148931f70ff3b0 (patch)
tree9a962f7c4d50ad72e08e09c979e9a90d5474cec8 /mitmproxy/command.py
parentb321e07279f8e1be4b76beb9dff608bb09ce485e (diff)
downloadmitmproxy-561415cea99c46dd5df892bcac148931f70ff3b0.tar.gz
mitmproxy-561415cea99c46dd5df892bcac148931f70ff3b0.tar.bz2
mitmproxy-561415cea99c46dd5df892bcac148931f70ff3b0.zip
Created a lexer for the command bar
Diffstat (limited to 'mitmproxy/command.py')
-rw-r--r--mitmproxy/command.py55
1 files changed, 5 insertions, 50 deletions
diff --git a/mitmproxy/command.py b/mitmproxy/command.py
index cf345c22..625e87e5 100644
--- a/mitmproxy/command.py
+++ b/mitmproxy/command.py
@@ -11,49 +11,9 @@ import functools
import sys
from mitmproxy import exceptions
+from mitmproxy import lexer
import mitmproxy.types
-def escape_and_quote(value):
- """
- This function takes the output from the lexer and puts it between quotes
- in the following cases:
- * There is a space in the string: The only way a token from the lexer can have a space in it is if it was between quotes
- * There is one or more quotes in the middle of the string: The only way for a token to have a quote in it that is not escaped is if it was escaped prior to being processed by the lexer. For example, the string `"s1 \" s2"` would come back from the lexer as `s1 " s2`.
-
- Any quotes that are in the middle of the string and that are not escaped will also be escaped (by placing a \ in front of it).
- This function only deals with double quotes and they are the only ones that should be used.
- """
-
- new_value = ""
- last_pos = len(value) - 1
-
- for pos, char in enumerate(value):
- if pos == 0:
- new_value += char
- continue
-
- # if pos == last_pos:
- # new_value += char
- # break
-
- if char in " \n\r\t":
- new_value += char
- continue
-
- if char == '"':
- if value[pos-1] != '\\':
- new_value += '\\'
-
- new_value += char
-
- value = new_value
-
- if ((" " in value) or ('"' in value)) and not (value.startswith("\"") or value.startswith("'")):
- return "\"%s\"" % value
-
- return value
-
-
def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None:
sig = inspect.signature(f)
try:
@@ -62,13 +22,8 @@ def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None:
raise exceptions.CommandError("command argument mismatch: %s" % v.args[0])
-def lexer(s):
- # mypy mis-identifies shlex.shlex as abstract
- lex = shlex.shlex(s, posix=True) # type: ignore
- lex.wordchars += "."
- lex.whitespace_split = True
- lex.commenters = ''
- return lex
+def get_lexer(s):
+ return lexer.Lexer(s)
def typename(t: type) -> str:
@@ -199,7 +154,7 @@ class CommandManager(mitmproxy.types._CommandBase):
"""
buf = io.StringIO(cmdstr)
parts: typing.List[str] = []
- lex = lexer(buf)
+ lex = get_lexer(buf)
while 1:
remainder = cmdstr[buf.tell():]
try:
@@ -245,7 +200,7 @@ class CommandManager(mitmproxy.types._CommandBase):
# ctx.log.info('[gilga] before parse.append. value = %s' % parts[i])
parse.append(
ParseResult(
- value=escape_and_quote(parts[i]),
+ value=parts[i],
type=typ,
valid=valid,
)