Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def deserialise(cls, flags, data):
"""
Take an on-wire message and turn it into an instance of this class.
"""
if flags != (False, False, False, False):
raise ParseFailure(cls, "Bad flags")
reserved = data.read(7).uint
if reserved:
raise ParseFailure(cls, "Reserved flag used.")
built = cls(session_present=data.read(1).bool,
return_code=data.read(8).uint)
# XXX: Do some more verification, re conn flags
if not data.bitpos == len(data):
# There's some wacky stuff going on here -- data they included, but
# didn't put flags for, maybe?
warnings.warn(("Quirky server CONNACK -- packet length was "
"%d bytes but only had %d bytes of useful data") % (
data.bitpos, len(data)))
return built
def parse_pair():
topic_filter = read_string(data)
reserved = data.read("uint:6")
max_qos = data.read("uint:2")
if reserved:
raise ParseFailure(cls, "Data in QoS Reserved area")
if max_qos not in [0, 1, 2]:
raise ParseFailure(cls, "Invalid QoS")
pairs.append(SubscriptionTopicRequest(topic_filter=topic_filter,
max_qos=max_qos))
built = cls(
username=data.read(1).bool,
password=data.read(1).bool,
will_retain=data.read(1).bool,
will_qos=data.read(2).uint,
will=data.read(1).bool,
clean_session=data.read(1).bool,
reserved=data.read(1).bool
)
# XXX: Do some more conformance checking here
# Need to worry about invalid flag combinations
if built.reserved:
# MQTT-3.1.2-3, reserved flag must not be used
raise ParseFailure(cls, "Reserved flag in CONNECT used")
return built
"""
Disassemble from an on-wire message.
"""
if flags != (False, False, False, False):
raise ParseFailure(cls, "Bad flags")
protocol = read_string(data)
if protocol != "MQTT":
print(protocol)
raise ParseFailure(cls, "Bad protocol name")
protocol_level = data.read('uint:8')
if protocol_level != 4:
raise ParseFailure(cls, "Bad protocol level")
flags = ConnectFlags.deserialise(data.read(8))
# Keep alive, in seconds
keep_alive = data.read('uint:16')
# The client ID
client_id = read_string(data)
if flags.will:
# MQTT-3.1.3-10, topic must be UTF-8
will_topic = read_string(data)
will_message = read_prefixed_data(data)
else:
will_topic = None
will_message = None
def read_string(data):
"""
Reads the next MQTT pascal-style string from `data`.
"""
byte_data = read_prefixed_data(data)
_validator.reset()
if _validator.validate(byte_data)[0]:
decoded = byte_data.decode('utf8', 'strict')
if "\u0000" in decoded:
raise ParseFailure("Invalid UTF-8 string (contains nulls)")
return decoded
else:
raise ParseFailure("Invalid UTF-8 string (contains surrogates)")
def parse_pair():
topic_filter = read_string(data)
reserved = data.read("uint:6")
max_qos = data.read("uint:2")
if reserved:
raise ParseFailure(cls, "Data in QoS Reserved area")
if max_qos not in [0, 1, 2]:
raise ParseFailure(cls, "Invalid QoS")
pairs.append(SubscriptionTopicRequest(topic_filter=topic_filter,
max_qos=max_qos))
def deserialise(cls, flags, data):
if flags != (False, False, True, False):
raise ParseFailure(cls, "Bad flags")
topics = []
packet_identifier = data.read('uint:16')
while not data.bitpos == len(data):
topics.append(read_string(data))
if len(topics) == 0:
raise ParseFailure(cls, "Must contain a payload.")
return cls(packet_identifier=packet_identifier, topics=topics)
def deserialise(cls, flags, data):
"""
Disassemble from an on-wire message.
"""
if flags != (False, False, False, False):
raise ParseFailure(cls, "Bad flags")
protocol = read_string(data)
if protocol != "MQTT":
print(protocol)
raise ParseFailure(cls, "Bad protocol name")
protocol_level = data.read('uint:8')
if protocol_level != 4:
raise ParseFailure(cls, "Bad protocol level")
flags = ConnectFlags.deserialise(data.read(8))
# Keep alive, in seconds
keep_alive = data.read('uint:16')
# The client ID
client_id = read_string(data)
if flags.will:
# MQTT-3.1.3-10, topic must be UTF-8
def deserialise(cls, flags, data):
if flags != (False, False, True, False):
raise ParseFailure(cls, "Bad flags")
topics = []
packet_identifier = data.read('uint:16')
while not data.bitpos == len(data):
topics.append(read_string(data))
if len(topics) == 0:
raise ParseFailure(cls, "Must contain a payload.")
return cls(packet_identifier=packet_identifier, topics=topics)