From 91bb757660d0cc2417296d6f1e0d60941380f1b4 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 23 Jun 2021 13:46:57 +0200 Subject: [PATCH] commands: add bytes type --- mitmproxy/types.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mitmproxy/types.py b/mitmproxy/types.py index e02a1f670..4ba1dd90a 100644 --- a/mitmproxy/types.py +++ b/mitmproxy/types.py @@ -4,7 +4,7 @@ import typing from mitmproxy import exceptions from mitmproxy import flow -from mitmproxy.utils import emoji +from mitmproxy.utils import emoji, strutils if typing.TYPE_CHECKING: # pragma: no cover from mitmproxy.command import CommandManager @@ -114,6 +114,23 @@ class _StrType(_BaseType): return isinstance(val, str) +class _BytesType(_BaseType): + typ = bytes + display = "bytes" + + def completion(self, manager: "CommandManager", t: type, s: str) -> typing.Sequence[str]: + return [] + + def parse(self, manager: "CommandManager", t: type, s: str) -> bytes: + try: + return strutils.escaped_str_to_bytes(s) + except ValueError as e: + raise exceptions.TypeError(str(e)) + + def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool: + return isinstance(val, bytes) + + class _UnknownType(_BaseType): typ = Unknown display = "unknown" @@ -460,4 +477,5 @@ CommandTypes = TypeManager( _PathType, _StrType, _StrSeqType, + _BytesType, )