Fix zeroconf txtrecord encoding

Fixes #9146
This commit is contained in:
Philipp Hörist 2018-05-27 21:03:42 +02:00
parent dbbf078229
commit 56fbe32b11
2 changed files with 16 additions and 27 deletions

View File

@ -130,25 +130,21 @@ class Zeroconf:
def txt_array_to_dict(self, txt_array): def txt_array_to_dict(self, txt_array):
txt_dict = {} txt_dict = {}
for els in txt_array: for array in txt_array:
key, val = '', None item = bytes(array)
for c in els: item = item.decode('utf-8')
c = chr(c) item = item.split('=', 1)
if val is None:
if c == '=': if item[0] and (item[0] not in txt_dict):
val = '' if len(item) == 1:
else: txt_dict[item[0]] = None
key += c
else: else:
val += c txt_dict[item[0]] = item[1]
if val is None: # missing '='
val = ''
txt_dict[key] = val
return txt_dict return txt_dict
@staticmethod @staticmethod
def string_to_byte_array(s): def string_to_byte_array(s):
s = s.encode('utf-8')
r = [] r = []
for c in s: for c in s:
@ -157,18 +153,14 @@ class Zeroconf:
return r return r
def dict_to_txt_array(self, txt_dict): def dict_to_txt_array(self, txt_dict):
l = [] array = []
for k,v in txt_dict.items(): for k, v in txt_dict.items():
if isinstance(k, str): item = '%s=%s' % (k, v)
k = k.encode('utf-8') item = item.encode('utf-8')
array.append(self.string_to_byte_array(item))
if isinstance(v, str): return array
v = v.encode('utf-8')
l.append(self.string_to_byte_array("%s=%s" % (k,v)))
return l
def service_resolved_callback(self, interface, protocol, name, stype, domain, def service_resolved_callback(self, interface, protocol, name, stype, domain,
host, aprotocol, address, port, txt, flags): host, aprotocol, address, port, txt, flags):

View File

@ -139,9 +139,6 @@ class Zeroconf:
def query_txt_callback(self, sdRef, flags, interfaceIndex, errorCode, def query_txt_callback(self, sdRef, flags, interfaceIndex, errorCode,
hosttarget, rrtype, rrclass, rdata, ttl): hosttarget, rrtype, rrclass, rdata, ttl):
# Callback from DNSServiceQueryRecord, it does not call decode()
# on rdata
rdata = rdata.decode()
if errorCode != pybonjour.kDNSServiceErr_NoError: if errorCode != pybonjour.kDNSServiceErr_NoError:
log.error('Error in query_record_callback: %s', str(errorCode)) log.error('Error in query_record_callback: %s', str(errorCode))