From 36fb9f243ad70b69834117ef61b1fb3d1572ea32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Freise?= Date: Tue, 23 Apr 2019 11:42:01 +0200 Subject: [PATCH] Respect binary coded payload data --- coapthon/messages/option.py | 2 +- coapthon/serializer.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/coapthon/messages/option.py b/coapthon/messages/option.py index 8e7104f..aa6f941 100644 --- a/coapthon/messages/option.py +++ b/coapthon/messages/option.py @@ -42,7 +42,7 @@ class Option(object): :return: the option value in the correct format depending on the option """ if type(self._value) is None: - self._value = bytearray() + self._value = bytes() opt_type = defines.OptionRegistry.LIST[self._number].value_type if opt_type == defines.INTEGER: if byte_len(self._value) > 0: diff --git a/coapthon/serializer.py b/coapthon/serializer.py index 34a2e0f..df11a87 100644 --- a/coapthon/serializer.py +++ b/coapthon/serializer.py @@ -113,13 +113,17 @@ class Serializer(object): raise AttributeError("Packet length %s, pos %s" % (length_packet, pos)) message.payload = "" payload = values[pos:] - try: - if message.payload_type == defines.Content_types["application/octet-stream"]: - message.payload = payload - else: + if hasattr(message, 'payload_type') and message.payload_type in [ + defines.Content_types["application/octet-stream"], + defines.Content_types["application/exi"], + defines.Content_types["application/cbor"] + ]: + message.payload = payload + else: + try: message.payload = payload.decode("utf-8") - except AttributeError: - message.payload = payload.decode("utf-8") + except AttributeError: + message.payload = payload pos += len(payload) return message @@ -323,7 +327,7 @@ class Serializer(object): @staticmethod def convert_to_raw(number, value, length): """ - Get the value of an option as a ByteArray. + Get the value of an option as bytes. :param number: the option number :param value: the option value @@ -350,11 +354,11 @@ class Serializer(object): if isinstance(value, str): value = str(value) if isinstance(value, str): - return bytearray(value, "utf-8") + return bytes(value, "utf-8") elif isinstance(value, int): return value else: - return bytearray(value) + return bytes(value) @staticmethod def as_sorted_list(options):