aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-07-29 19:54:44 -0700
committerMaximilian Hils <git@maximilianhils.com>2016-07-29 19:54:44 -0700
commit453436367103dd9deb693da5b5ebb18c2c2c86ce (patch)
tree4edd57812419e4d7c8f41045c43d52f27b783265
parent63f64cd66086f302f53456f29f60b1e28c8ee178 (diff)
downloadmitmproxy-453436367103dd9deb693da5b5ebb18c2c2c86ce.tar.gz
mitmproxy-453436367103dd9deb693da5b5ebb18c2c2c86ce.tar.bz2
mitmproxy-453436367103dd9deb693da5b5ebb18c2c2c86ce.zip
add escape_single_quotes=False arg to bytes_to_escaped_str
-rw-r--r--netlib/strutils.py4
-rw-r--r--pathod/language/base.py8
-rw-r--r--pathod/pathoc.py2
-rw-r--r--test/netlib/test_strutils.py5
4 files changed, 12 insertions, 7 deletions
diff --git a/netlib/strutils.py b/netlib/strutils.py
index 8f27ebb7..4a46b6b1 100644
--- a/netlib/strutils.py
+++ b/netlib/strutils.py
@@ -69,7 +69,7 @@ def escape_control_characters(text, keep_spacing=True):
return text.translate(trans)
-def bytes_to_escaped_str(data, keep_spacing=False):
+def bytes_to_escaped_str(data, keep_spacing=False, escape_single_quotes=False):
"""
Take bytes and return a safe string that can be displayed to the user.
@@ -86,6 +86,8 @@ def bytes_to_escaped_str(data, keep_spacing=False):
# We always insert a double-quote here so that we get a single-quoted string back
# https://stackoverflow.com/questions/29019340/why-does-python-use-different-quotes-for-representing-strings-depending-on-their
ret = repr(b'"' + data).lstrip("b")[2:-1]
+ if not escape_single_quotes:
+ ret = re.sub(r"(?<!\\)(\\\\)*\\'", lambda m: (m.group(1) or "") + "'", ret)
if keep_spacing:
ret = re.sub(
r"(?<!\\)(\\\\)*\\([nrt])",
diff --git a/pathod/language/base.py b/pathod/language/base.py
index 25f3fd1a..39155858 100644
--- a/pathod/language/base.py
+++ b/pathod/language/base.py
@@ -136,7 +136,7 @@ class TokValueLiteral(_TokValueLiteral):
def spec(self):
inner = strutils.bytes_to_escaped_str(self.val)
- inner = inner.replace(r"\'", r"\x27")
+ inner = inner.replace(r"'", r"\x27")
return "'" + inner + "'"
@@ -148,7 +148,7 @@ class TokValueNakedLiteral(_TokValueLiteral):
return e.setParseAction(lambda x: cls(*x))
def spec(self):
- return strutils.bytes_to_escaped_str(self.val)
+ return strutils.bytes_to_escaped_str(self.val, escape_single_quotes=True)
class TokValueGenerate(Token):
@@ -166,7 +166,7 @@ class TokValueGenerate(Token):
def freeze(self, settings):
g = self.get_generator(settings)
- return TokValueLiteral(strutils.bytes_to_escaped_str(g[:]))
+ return TokValueLiteral(strutils.bytes_to_escaped_str(g[:], escape_single_quotes=True))
@classmethod
def expr(cls):
@@ -578,4 +578,4 @@ class NestedMessage(Token):
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
- return self.__class__(TokValueLiteral(strutils.bytes_to_escaped_str(f.encode())))
+ return self.__class__(TokValueLiteral(strutils.bytes_to_escaped_str(f.encode(), escape_single_quotes=True)))
diff --git a/pathod/pathoc.py b/pathod/pathoc.py
index c6783878..5831ba3e 100644
--- a/pathod/pathoc.py
+++ b/pathod/pathoc.py
@@ -444,7 +444,7 @@ class Pathoc(tcp.TCPClient):
finally:
if resp:
lg("<< %s %s: %s bytes" % (
- resp.status_code, strutils.bytes_to_escaped_str(resp.reason.encode()), len(resp.content)
+ resp.status_code, strutils.escape_control_characters(resp.reason) if resp.reason else "", len(resp.content)
))
if resp.status_code in self.ignorecodes:
lg.suppress()
diff --git a/test/netlib/test_strutils.py b/test/netlib/test_strutils.py
index 7c3eacc6..52299e59 100644
--- a/test/netlib/test_strutils.py
+++ b/test/netlib/test_strutils.py
@@ -48,9 +48,12 @@ def test_bytes_to_escaped_str():
assert strutils.bytes_to_escaped_str(b"\b") == r"\x08"
assert strutils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
assert strutils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
- assert strutils.bytes_to_escaped_str(b"'") == r"\'"
+ assert strutils.bytes_to_escaped_str(b"'") == r"'"
assert strutils.bytes_to_escaped_str(b'"') == r'"'
+ assert strutils.bytes_to_escaped_str(b"'", escape_single_quotes=True) == r"\'"
+ assert strutils.bytes_to_escaped_str(b'"', escape_single_quotes=True) == r'"'
+
assert strutils.bytes_to_escaped_str(b"\r\n\t") == "\\r\\n\\t"
assert strutils.bytes_to_escaped_str(b"\r\n\t", True) == "\r\n\t"