Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: trap UnicodeEncodeError
Browse files Browse the repository at this point in the history
Closes #606
  • Loading branch information
bbangert committed Aug 23, 2016
1 parent 80f7b83 commit a116def
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 14 additions & 0 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a116def

Please sign in to comment.