Skip to content

Commit

Permalink
sendMessage should accept bytes and encode unicode
Browse files Browse the repository at this point in the history
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.

Also, checking the length of the unicode-string is wrong, as it can
be shorter than the encoded length.
  • Loading branch information
alexbrasetvik committed Jul 22, 2012
1 parent 06f768e commit d8af62a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cyclone/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ def _extractMessageFromFrame(self, data):
return str(payload)

def sendMessage(self, message, code=0x81):
message = unicode(message, "utf-8")
if isinstance(message, unicode):
message = message.encode('utf8')
length = len(message)
newFrame = []
newFrame.append(code)
Expand All @@ -261,7 +262,7 @@ def sendMessage(self, message, code=0x81):
newFrame.append(127)
newFrame += struct.pack('!Q', length)

newFrame += message.encode('utf-8')
newFrame += message
self.transport.write(str(newFrame))


Expand Down

0 comments on commit d8af62a

Please sign in to comment.