Răsfoiți Sursa

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.
Joel Martin 15 ani în urmă
părinte
comite
754e0c0bee
1 a modificat fișierele cu 15 adăugiri și 3 ștergeri
  1. 15 3
      include/vnc.js

+ 15 - 3
include/vnc.js

@@ -1135,10 +1135,17 @@ clientCutText: function (text) {
 
 encode_message: function(arr) {
     if (RFB.b64encode) {
+        /* base64 encode */
         RFB.SQ = RFB.SQ + Base64.encode(arr);
     } else {
+        /* UTF-8 encode. 0 -> 256 to avoid WebSockets framing */
         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) {
     var raw, i, length, RQ = RFB.RQ;
     //Util.Debug(">> decode_message: " + data);
     if (RFB.b64encode) {
+        /* base64 decode */
         RFB.RQ = RFB.RQ.concat(Base64.decode(data, 0));
     } else {
-        // A bit faster in firefox
+        /* UTF-8 decode. 256 -> 0 to WebSockets framing */
         length = data.length;
         for (i=0; i < length; i += 1) {
             RQ.push(data.charCodeAt(i) % 256);
@@ -1162,7 +1170,11 @@ recv_message: function(e) {
 
     try {
         RFB.decode_message(e.data);
-        RFB.handle_message();
+        if (RFB.RQ.length > 0) {
+            RFB.handle_message();
+        } else {
+            Util.Debug("Ignoring empty message");
+        }
     } catch (exc) {
         if (typeof exc.stack !== 'undefined') {
             Util.Warn("recv_message, caught exception: " + exc.stack);