From a116def7bfbeb087bc80845dd2eda438515b3ccb Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Mon, 22 Aug 2016 17:54:01 -0700 Subject: [PATCH] fix: trap UnicodeEncodeError Closes #606 --- autopush/tests/test_websocket.py | 14 ++++++++++++++ autopush/websocket.py | 11 ++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/autopush/tests/test_websocket.py b/autopush/tests/test_websocket.py index cf5f7d54..a83f977d 100644 --- a/autopush/tests/test_websocket.py +++ b/autopush/tests/test_websocket.py @@ -245,6 +245,20 @@ def check_subbed(s): self.proto.processHandshake() eq_(self.proto.factory.externalPort, 80) + def test_handshake_decode_error(self): + self.proto.factory = Mock(externalPort=80) + + def check_subbed(s): + u'\xfe'.encode('ascii') + + self.proto.failHandshake = Mock() + self.proto.parent_class = Mock(**{"processHandshake.side_effect": + check_subbed}) + self.proto.processHandshake() + self.proto.failHandshake.assert_called_with( + "Error reading handshake data" + ) + def test_binary_msg(self): self.proto.onMessage(b"asdfasdf", True) d = Deferred() diff --git a/autopush/websocket.py b/autopush/websocket.py index c3e39e43..f45cfdbb 100644 --- a/autopush/websocket.py +++ b/autopush/websocket.py @@ -384,15 +384,16 @@ def processHandshake(self): track_object(self, msg="processHandshake") port = self.ap_settings.port hide = port != 80 and port != 443 - if not hide: - return self.parent_class.processHandshake(self) - old_port = self.factory.externalPort try: - self.factory.externalPort = None + if hide: + self.factory.externalPort = None return self.parent_class.processHandshake(self) + except UnicodeEncodeError: + self.failHandshake("Error reading handshake data") finally: - self.factory.externalPort = old_port + if hide: + self.factory.externalPort = old_port @log_exception def onMessage(self, payload, isBinary):