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

fix: trap UnicodeEncodeError #619

Merged
merged 1 commit into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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