aboutsummaryrefslogtreecommitdiffstats
path: root/netlib
diff options
context:
space:
mode:
authorJason <jason.daurus@gmail.com>2016-05-28 01:11:04 +0800
committerJason <jason.daurus@gmail.com>2016-05-28 01:11:04 +0800
commit9e869f0aa17cbd202f72bab1540d866f7274a8a1 (patch)
tree765b55027aba7685264e838d285c0587d18b9205 /netlib
parentfb639c2e11225531172fb7b452250b58295a5a58 (diff)
parent22ecd022a84e1c3762dd425bc9bee2230e393d8d (diff)
downloadmitmproxy-9e869f0aa17cbd202f72bab1540d866f7274a8a1.tar.gz
mitmproxy-9e869f0aa17cbd202f72bab1540d866f7274a8a1.tar.bz2
mitmproxy-9e869f0aa17cbd202f72bab1540d866f7274a8a1.zip
Merge remote-tracking branch 'origin/master' into contentview
Diffstat (limited to 'netlib')
-rw-r--r--netlib/utils.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/netlib/utils.py b/netlib/utils.py
index 7499f71f..648915fa 100644
--- a/netlib/utils.py
+++ b/netlib/utils.py
@@ -425,6 +425,10 @@ def safe_subn(pattern, repl, target, *args, **kwargs):
def bytes_to_escaped_str(data):
"""
Take bytes and return a safe string that can be displayed to the user.
+
+ Single quotes are always escaped, double quotes are never escaped:
+ "'" + bytes_to_escaped_str(...) + "'"
+ gives a valid Python string.
"""
# TODO: We may want to support multi-byte characters without escaping them.
# One way to do would be calling .decode("utf8", "backslashreplace") first
@@ -432,17 +436,23 @@ def bytes_to_escaped_str(data):
if not isinstance(data, bytes):
raise ValueError("data must be bytes")
- return repr(data).lstrip("b")[1:-1]
+ # 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
+ return repr(b'"' + data).lstrip("b")[2:-1]
def escaped_str_to_bytes(data):
"""
Take an escaped string and return the unescaped bytes equivalent.
"""
- if not isinstance(data, str):
+ if not isinstance(data, six.string_types):
+ if six.PY2:
+ raise ValueError("data must be str or unicode")
raise ValueError("data must be str")
if six.PY2:
+ if isinstance(data, unicode):
+ data = data.encode("utf8")
return data.decode("string-escape")
# This one is difficult - we use an undocumented Python API here