From fb9b3ceeea49423939f74cee3747364988e0b221 Mon Sep 17 00:00:00 2001 From: Etoh Date: Fri, 1 Feb 2019 14:07:08 +0000 Subject: [PATCH] Handle non-UTF8 messages to server (fixes #210, based on #214 by xNinjaKittyx) --- syncplay/messages_de.py | 1 + syncplay/messages_en.py | 1 + syncplay/messages_it.py | 1 + syncplay/messages_ru.py | 1 + syncplay/protocols.py | 13 +++++++++---- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/syncplay/messages_de.py b/syncplay/messages_de.py index e370015..0f3c423 100755 --- a/syncplay/messages_de.py +++ b/syncplay/messages_de.py @@ -444,6 +444,7 @@ de = { # Server errors "unknown-command-server-error": "Unbekannter Befehl {}", # message "not-json-server-error": "Kein JSON-String {}", # message + "line-decode-server-error": "Not a utf-8 string", # TODO: Translate "not-known-server-error": "Der Server muss dich kennen, bevor du diesen Befehl nutzen kannst", "client-drop-server-error": "Client verloren: {} -- {}", # host, error "password-required-server-error": "Passwort nötig", diff --git a/syncplay/messages_en.py b/syncplay/messages_en.py index 2cbf725..9fbbc83 100755 --- a/syncplay/messages_en.py +++ b/syncplay/messages_en.py @@ -448,6 +448,7 @@ en = { # Server errors "unknown-command-server-error": "Unknown command {}", # message "not-json-server-error": "Not a json encoded string {}", # message + "line-decode-server-error": "Not a utf-8 string", "not-known-server-error": "You must be known to server before sending this command", "client-drop-server-error": "Client drop: {} -- {}", # host, error "password-required-server-error": "Password required", diff --git a/syncplay/messages_it.py b/syncplay/messages_it.py index cdc20a0..26dd7f8 100755 --- a/syncplay/messages_it.py +++ b/syncplay/messages_it.py @@ -448,6 +448,7 @@ it = { # Server errors "unknown-command-server-error": "Comando non riconosciuto {}", # message "not-json-server-error": "Non è una stringa in codifica JSON {}", # message + "line-decode-server-error": "Not a utf-8 string", # TODO: Translate "not-known-server-error": "Devi essere autenticato dal server prima di poter inviare questo comando", "client-drop-server-error": "Il client è caduto: {} -- {}", # host, error "password-required-server-error": "È richiesta una password", diff --git a/syncplay/messages_ru.py b/syncplay/messages_ru.py index b836abf..13c3db2 100755 --- a/syncplay/messages_ru.py +++ b/syncplay/messages_ru.py @@ -450,6 +450,7 @@ ru = { # Server errors "unknown-command-server-error": "Неизвестная команда: {}", # message "not-json-server-error": "Не является закодированной json-строкой: {}", # message + "line-decode-server-error": "Not a utf-8 string", # TODO: Translate "not-known-server-error": "Данную команду могут выполнять только авторизованные пользователи.", "client-drop-server-error": "Клиент отключен с ошибкой: {} -- {}", # host, error "password-required-server-error": "Необходимо указать пароль.", diff --git a/syncplay/protocols.py b/syncplay/protocols.py index 96634cc..c4394ac 100755 --- a/syncplay/protocols.py +++ b/syncplay/protocols.py @@ -31,16 +31,21 @@ class JSONCommandProtocol(LineReceiver): self.dropWithError(getMessage("unknown-command-server-error").format(message[1])) # TODO: log, not drop def lineReceived(self, line): - line = line.decode('utf-8').strip() + try: + line = line.decode('utf-8').strip() + except UnicodeDecodeError: + self.dropWithError(getMessage("line-decode-server-error")) + return if not line: return + self.showDebugMessage("client/server << {}".format(line)) try: - self.showDebugMessage("client/server << {}".format(line)) messages = json.loads(line) - except: + except json.decoder.JSONDecodeError: self.dropWithError(getMessage("not-json-server-error").format(line)) return - self.handleMessages(messages) + else: + self.handleMessages(messages) def sendMessage(self, dict_): line = json.dumps(dict_)