From fd20b46b33ad3945ba28e1facb51843507e89670 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 26 Apr 2016 23:10:33 +0200 Subject: [PATCH] Split IPCServer.on_ready_read into two methods --- qutebrowser/misc/ipc.py | 99 +++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/qutebrowser/misc/ipc.py b/qutebrowser/misc/ipc.py index aa219fab7..1e3bcc4c1 100644 --- a/qutebrowser/misc/ipc.py +++ b/qutebrowser/misc/ipc.py @@ -288,6 +288,55 @@ class IPCServer(QObject): self._socket.error.connect(self.on_error) self._socket.disconnectFromServer() + def _handle_data(self, data): + """Handle data (as bytes) we got from on_ready_ready_read.""" + try: + decoded = data.decode('utf-8') + except UnicodeDecodeError: + log.ipc.error("invalid utf-8: {}".format( + binascii.hexlify(data))) + self._handle_invalid_data() + return + + log.ipc.debug("Processing: {}".format(decoded)) + try: + json_data = json.loads(decoded) + except ValueError: + log.ipc.error("invalid json: {}".format(decoded.strip())) + self._handle_invalid_data() + return + + for name in ('args', 'target_arg'): + if name not in json_data: + log.ipc.error("Missing {}: {}".format(name, decoded.strip())) + self._handle_invalid_data() + return + + try: + protocol_version = int(json_data['protocol_version']) + except (KeyError, ValueError): + log.ipc.error("invalid version: {}".format(decoded.strip())) + self._handle_invalid_data() + return + + if protocol_version != PROTOCOL_VERSION: + log.ipc.error("incompatible version: expected {}, got {}".format( + PROTOCOL_VERSION, protocol_version)) + self._handle_invalid_data() + return + + args = json_data['args'] + + target_arg = json_data['target_arg'] + if target_arg is None: + # https://www.riverbankcomputing.com/pipermail/pyqt/2016-April/037375.html + target_arg = '' + + cwd = json_data.get('cwd', '') + assert cwd is not None + + self.got_args.emit(args, target_arg, cwd) + @pyqtSlot() def on_ready_read(self): """Read json data from the client.""" @@ -302,55 +351,7 @@ class IPCServer(QObject): self.got_raw.emit(data) log.ipc.debug("Read from socket 0x{:x}: {}".format( id(self._socket), data)) - - try: - decoded = data.decode('utf-8') - except UnicodeDecodeError: - log.ipc.error("invalid utf-8: {}".format( - binascii.hexlify(data))) - self._handle_invalid_data() - return - - log.ipc.debug("Processing: {}".format(decoded)) - try: - json_data = json.loads(decoded) - except ValueError: - log.ipc.error("invalid json: {}".format(decoded.strip())) - self._handle_invalid_data() - return - - for name in ('args', 'target_arg'): - if name not in json_data: - log.ipc.error("Missing {}: {}".format(name, - decoded.strip())) - self._handle_invalid_data() - return - - try: - protocol_version = int(json_data['protocol_version']) - except (KeyError, ValueError): - log.ipc.error("invalid version: {}".format(decoded.strip())) - self._handle_invalid_data() - return - - if protocol_version != PROTOCOL_VERSION: - log.ipc.error("incompatible version: expected {}, " - "got {}".format( - PROTOCOL_VERSION, protocol_version)) - self._handle_invalid_data() - return - - args = json_data['args'] - - target_arg = json_data['target_arg'] - if target_arg is None: - # https://www.riverbankcomputing.com/pipermail/pyqt/2016-April/037375.html - target_arg = '' - - cwd = json_data.get('cwd', '') - assert cwd is not None - - self.got_args.emit(args, target_arg, cwd) + self._handle_data(data) self._timer.start() @pyqtSlot()