mitmproxy/netlib/utils.py

222 lines
6.5 KiB
Python
Raw Normal View History

from __future__ import absolute_import, print_function, division
import os.path
2015-08-05 19:32:53 +00:00
import re
2016-01-31 11:15:44 +00:00
import codecs
2015-09-12 15:03:09 +00:00
import unicodedata
2016-02-15 15:34:38 +00:00
import importlib
import inspect
2016-02-08 03:16:58 +00:00
2015-09-15 17:12:15 +00:00
import six
2015-09-20 22:44:17 +00:00
def always_bytes(unicode_or_bytes, *encode_args):
if isinstance(unicode_or_bytes, six.text_type):
return unicode_or_bytes.encode(*encode_args)
return unicode_or_bytes
2015-09-21 16:34:43 +00:00
def native(s, *encoding_opts):
2015-09-20 22:44:17 +00:00
"""
Convert :py:class:`bytes` or :py:class:`unicode` to the native
:py:class:`str` type, using latin1 encoding if conversion is necessary.
https://www.python.org/dev/peps/pep-3333/#a-note-on-string-types
"""
if not isinstance(s, (six.binary_type, six.text_type)):
raise TypeError("%r is neither bytes nor unicode" % s)
if six.PY3:
if isinstance(s, six.binary_type):
2015-09-21 16:34:43 +00:00
return s.decode(*encoding_opts)
2015-09-20 22:44:17 +00:00
else:
if isinstance(s, six.text_type):
2015-09-21 16:34:43 +00:00
return s.encode(*encoding_opts)
2015-09-20 22:44:17 +00:00
return s
2015-09-12 15:03:09 +00:00
def clean_bin(s, keep_spacing=True):
"""
2015-09-12 15:03:09 +00:00
Cleans binary data to make it safe to display.
Args:
keep_spacing: If False, tabs and newlines will also be replaced.
"""
2015-09-12 15:03:09 +00:00
if isinstance(s, six.text_type):
if keep_spacing:
keep = u" \n\r\t"
2015-09-12 15:03:09 +00:00
else:
keep = u" "
return u"".join(
ch if (unicodedata.category(ch)[0] not in "CZ" or ch in keep) else u"."
2015-09-12 15:03:09 +00:00
for ch in s
)
else:
if keep_spacing:
2015-09-15 22:04:23 +00:00
keep = (9, 10, 13) # \t, \n, \r,
else:
2015-09-15 22:04:23 +00:00
keep = ()
2015-09-12 15:03:09 +00:00
return b"".join(
2015-09-15 17:12:15 +00:00
six.int2byte(ch) if (31 < ch < 127 or ch in keep) else b"."
for ch in six.iterbytes(s)
2015-09-12 15:03:09 +00:00
)
def hexdump(s):
"""
2015-09-17 13:16:12 +00:00
Returns:
A generator of (offset, hex, str) tuples
"""
for i in range(0, len(s), 16):
offset = "{:0=10x}".format(i).encode()
2014-06-25 18:31:10 +00:00
part = s[i:i + 16]
x = b" ".join("{:0=2x}".format(i).encode() for i in six.iterbytes(part))
2015-09-17 13:16:12 +00:00
x = x.ljust(47) # 16*2 + 15
yield (offset, x, clean_bin(part, False))
def setbit(byte, offset, value):
"""
Set a bit in a byte to 1 if value is truthy, 0 if not.
"""
if value:
return byte | (1 << offset)
else:
return byte & ~(1 << offset)
def getbit(byte, offset):
mask = 1 << offset
2015-09-17 13:16:12 +00:00
return bool(byte & mask)
class BiDi(object):
"""
A wee utility class for keeping bi-directional mappings, like field
constants in protocols. Names are attributes on the object, dict-like
access maps values to names:
CONST = BiDi(a=1, b=2)
assert CONST.a == 1
assert CONST.get_name(1) == "a"
"""
def __init__(self, **kwargs):
self.names = kwargs
self.values = {}
for k, v in kwargs.items():
self.values[v] = k
if len(self.names) != len(self.values):
raise ValueError("Duplicate values not allowed.")
def __getattr__(self, k):
if k in self.names:
return self.names[k]
raise AttributeError("No such attribute: %s", k)
def get_name(self, n, default=None):
return self.values.get(n, default)
class Data(object):
2015-08-10 18:44:36 +00:00
def __init__(self, name):
2016-02-15 15:34:38 +00:00
m = importlib.import_module(name)
dirname = os.path.dirname(inspect.getsourcefile(m))
self.dirname = os.path.abspath(dirname)
def path(self, path):
"""
Returns a path to the package data housed at 'path' under this
module.Path can be a path to a file, or to a directory.
This function will raise ValueError if the path does not exist.
"""
2016-02-15 15:34:38 +00:00
fullpath = os.path.join(self.dirname, path)
if not os.path.exists(fullpath):
raise ValueError("dataPath: %s does not exist." % fullpath)
return fullpath
2015-07-29 09:27:43 +00:00
2015-09-15 17:12:15 +00:00
_label_valid = re.compile(b"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
2015-08-01 08:39:14 +00:00
def is_valid_host(host):
2015-09-20 17:40:09 +00:00
"""
Checks if a hostname is valid.
Args:
host (bytes): The hostname
"""
2015-08-01 08:39:14 +00:00
try:
host.decode("idna")
except ValueError:
return False
2015-09-15 17:12:15 +00:00
if len(host) > 255:
return False
2015-09-17 13:16:12 +00:00
if host[-1] == b".":
2015-09-15 17:12:15 +00:00
host = host[:-1]
return all(_label_valid.match(x) for x in host.split(b"."))
def is_valid_port(port):
return 0 <= port <= 65535
2015-07-29 09:27:43 +00:00
def hostport(scheme, host, port):
"""
Returns the host component, with a port specifcation if needed.
"""
2015-09-25 22:39:04 +00:00
if (port, scheme) in [(80, "http"), (443, "https"), (80, b"http"), (443, b"https")]:
2015-07-29 09:27:43 +00:00
return host
else:
2015-09-25 22:39:04 +00:00
if isinstance(host, six.binary_type):
return b"%s:%d" % (host, port)
else:
return "%s:%d" % (host, port)
2015-08-01 08:39:14 +00:00
2015-08-10 18:44:36 +00:00
def safe_subn(pattern, repl, target, *args, **kwargs):
"""
There are Unicode conversion problems with re.subn. We try to smooth
that over by casting the pattern and replacement to strings. We really
need a better solution that is aware of the actual content ecoding.
"""
return re.subn(str(pattern), str(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
# and then escaping UTF8 control chars (see clean_bin).
if not isinstance(data, bytes):
2016-05-29 17:28:59 +00:00
raise ValueError("data must be bytes, but is {}".format(data.__class__.__name__))
# 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
2016-05-26 03:31:32 +00:00
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, six.string_types):
if six.PY2:
2016-05-29 17:28:59 +00:00
raise ValueError("data must be str or unicode, but is {}".format(data.__class__.__name__))
raise ValueError("data must be str, but is {}".format(data.__class__.__name__))
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
# as per http://stackoverflow.com/a/23151714/934719
return codecs.escape_decode(data)[0]