mirror of https://github.com/Desuuuu/klipper.git
webhooks: Convert to Python3 string encoding
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
adeb869f56
commit
b8c91914b7
|
@ -14,15 +14,17 @@ REQUEST_LOG_SIZE = 20
|
||||||
#
|
#
|
||||||
# https://stackoverflow.com/questions/956867/
|
# https://stackoverflow.com/questions/956867/
|
||||||
#
|
#
|
||||||
def byteify(data, ignore_dicts=False):
|
json_loads_byteify = None
|
||||||
if isinstance(data, unicode):
|
if sys.version_info.major < 3:
|
||||||
return data.encode('utf-8')
|
def json_loads_byteify(data, ignore_dicts=False):
|
||||||
if isinstance(data, list):
|
if isinstance(data, unicode):
|
||||||
return [byteify(i, True) for i in data]
|
return data.encode('utf-8')
|
||||||
if isinstance(data, dict) and not ignore_dicts:
|
if isinstance(data, list):
|
||||||
return {byteify(k, True): byteify(v, True)
|
return [json_loads_byteify(i, True) for i in data]
|
||||||
for k, v in data.items()}
|
if isinstance(data, dict) and not ignore_dicts:
|
||||||
return data
|
return {json_loads_byteify(k, True): json_loads_byteify(v, True)
|
||||||
|
for k, v in data.items()}
|
||||||
|
return data
|
||||||
|
|
||||||
class WebRequestError(gcode.CommandError):
|
class WebRequestError(gcode.CommandError):
|
||||||
def __init__(self, message,):
|
def __init__(self, message,):
|
||||||
|
@ -40,7 +42,7 @@ class WebRequest:
|
||||||
error = WebRequestError
|
error = WebRequestError
|
||||||
def __init__(self, client_conn, request):
|
def __init__(self, client_conn, request):
|
||||||
self.client_conn = client_conn
|
self.client_conn = client_conn
|
||||||
base_request = json.loads(request, object_hook=byteify)
|
base_request = json.loads(request, object_hook=json_loads_byteify)
|
||||||
if type(base_request) != dict:
|
if type(base_request) != dict:
|
||||||
raise ValueError("Not a top-level dictionary")
|
raise ValueError("Not a top-level dictionary")
|
||||||
self.id = base_request.get('id', None)
|
self.id = base_request.get('id', None)
|
||||||
|
@ -170,7 +172,7 @@ class ClientConnection:
|
||||||
self.sock = sock
|
self.sock = sock
|
||||||
self.fd_handle = self.reactor.register_fd(
|
self.fd_handle = self.reactor.register_fd(
|
||||||
self.sock.fileno(), self.process_received)
|
self.sock.fileno(), self.process_received)
|
||||||
self.partial_data = self.send_buffer = ""
|
self.partial_data = self.send_buffer = b""
|
||||||
self.is_sending_data = False
|
self.is_sending_data = False
|
||||||
self.set_client_info("?", "New connection")
|
self.set_client_info("?", "New connection")
|
||||||
self.request_log = collections.deque([], REQUEST_LOG_SIZE)
|
self.request_log = collections.deque([], REQUEST_LOG_SIZE)
|
||||||
|
@ -216,14 +218,14 @@ class ClientConnection:
|
||||||
# If bad file descriptor allow connection to be
|
# If bad file descriptor allow connection to be
|
||||||
# closed by the data check
|
# closed by the data check
|
||||||
if e.errno == errno.EBADF:
|
if e.errno == errno.EBADF:
|
||||||
data = ''
|
data = b""
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
if data == '':
|
if not data:
|
||||||
# Socket Closed
|
# Socket Closed
|
||||||
self.close()
|
self.close()
|
||||||
return
|
return
|
||||||
requests = data.split('\x03')
|
requests = data.split(b'\x03')
|
||||||
requests[0] = self.partial_data + requests[0]
|
requests[0] = self.partial_data + requests[0]
|
||||||
self.partial_data = requests.pop()
|
self.partial_data = requests.pop()
|
||||||
for req in requests:
|
for req in requests:
|
||||||
|
@ -255,7 +257,8 @@ class ClientConnection:
|
||||||
self.send(result)
|
self.send(result)
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
self.send_buffer += json.dumps(data, separators=(',', ':')) + "\x03"
|
jmsg = json.dumps(data, separators=(',', ':'))
|
||||||
|
self.send_buffer += jmsg.encode() + b"\x03"
|
||||||
if not self.is_sending_data:
|
if not self.is_sending_data:
|
||||||
self.is_sending_data = True
|
self.is_sending_data = True
|
||||||
self.reactor.register_callback(self._do_send)
|
self.reactor.register_callback(self._do_send)
|
||||||
|
|
Loading…
Reference in New Issue