Commit 754e0c0b authored by Joel Martin's avatar Joel Martin

UTF-8: send 0 as 256 during encoding too.

0 is valid UTF-8, but in order to avoid WebSockets framing, we
encode/decode it as 256.

Also, be tolerant of 0 length messages.
parent d798572d
...@@ -1135,10 +1135,17 @@ clientCutText: function (text) { ...@@ -1135,10 +1135,17 @@ clientCutText: function (text) {
encode_message: function(arr) { encode_message: function(arr) {
if (RFB.b64encode) { if (RFB.b64encode) {
/* base64 encode */
RFB.SQ = RFB.SQ + Base64.encode(arr); RFB.SQ = RFB.SQ + Base64.encode(arr);
} else { } else {
/* UTF-8 encode. 0 -> 256 to avoid WebSockets framing */
RFB.SQ = RFB.SQ + arr.map(function (num) { RFB.SQ = RFB.SQ + arr.map(function (num) {
return String.fromCharCode(num); } ).join(''); if (num === 0) {
return String.fromCharCode(256);
} else {
return String.fromCharCode(num);
}
} ).join('');
} }
}, },
...@@ -1146,9 +1153,10 @@ decode_message: function(data) { ...@@ -1146,9 +1153,10 @@ decode_message: function(data) {
var raw, i, length, RQ = RFB.RQ; var raw, i, length, RQ = RFB.RQ;
//Util.Debug(">> decode_message: " + data); //Util.Debug(">> decode_message: " + data);
if (RFB.b64encode) { if (RFB.b64encode) {
/* base64 decode */
RFB.RQ = RFB.RQ.concat(Base64.decode(data, 0)); RFB.RQ = RFB.RQ.concat(Base64.decode(data, 0));
} else { } else {
// A bit faster in firefox /* UTF-8 decode. 256 -> 0 to WebSockets framing */
length = data.length; length = data.length;
for (i=0; i < length; i += 1) { for (i=0; i < length; i += 1) {
RQ.push(data.charCodeAt(i) % 256); RQ.push(data.charCodeAt(i) % 256);
...@@ -1162,7 +1170,11 @@ recv_message: function(e) { ...@@ -1162,7 +1170,11 @@ recv_message: function(e) {
try { try {
RFB.decode_message(e.data); RFB.decode_message(e.data);
RFB.handle_message(); if (RFB.RQ.length > 0) {
RFB.handle_message();
} else {
Util.Debug("Ignoring empty message");
}
} catch (exc) { } catch (exc) {
if (typeof exc.stack !== 'undefined') { if (typeof exc.stack !== 'undefined') {
Util.Warn("recv_message, caught exception: " + exc.stack); Util.Warn("recv_message, caught exception: " + exc.stack);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment