fiorix/cyclone — pull request #61 by alexbrasetvik (2012-07-22 17:25:26+00:00)
Title: WebSocketProtocol17.sendMessage vs unicode

Not everything sent to sendMessage is necessarily valid unicode. For example, when Firefox closes the connection, a control frame is sent that is not valid utf-8. (The one Chrome sends is apparently ok)

Also, checking the length of the unicode-string is wrong, as it can be shorter than the encoded length.

Without this patch, try running the echo-demo. Opening it in Firefox (14.0.1 here) and then closing the tab gives a traceback like

...
File "[snip]/cyclone-1.0_rc8-py2.7.egg/cyclone/websocket.py", line 268, in sendMessage
message = unicode(message, "utf-8")
exceptions.UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 1: unexpected end of data

Reopening and trying to echo unicode (e.g. "æøå") results in a closed connection without anything echoed back.

Trying the same in Chrome, it usually behaves the same – but I got it to echo a truncated message once as well.