]> git.proxmox.com Git - mirror_novnc.git/blobdiff - core/websock.js
Add try catch in every place that uses decodeUTF8
[mirror_novnc.git] / core / websock.js
index ab63a3a91c34e4ece9df220bbd1df3e9adcfb3ec..8fef0b2818b63e39480debaf6644f275a8958e9e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Websock: high-performance binary WebSockets
- * Copyright (C) 2018 The noVNC Authors
+ * Copyright (C) 2019 The noVNC Authors
  * Licensed under MPL 2.0 (see LICENSE.txt)
  *
  * Websock is similar to the standard WebSocket object but with extra
@@ -17,6 +17,8 @@ import * as Log from './util/logging.js';
 // this has performance issues in some versions Chromium, and
 // doesn't gain a tremendous amount of performance increase in Firefox
 // at the moment.  It may be valuable to turn it on in the future.
+// Also copyWithin() for TypedArrays is not supported in IE 11 or
+// Safari 13 (at the moment we want to support Safari 11).
 const ENABLE_COPYWITHIN = false;
 const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024;  // 40 MiB
 
@@ -27,7 +29,6 @@ export default class Websock {
         this._rQi = 0;           // Receive queue index
         this._rQlen = 0;         // Next write position in the receive queue
         this._rQbufferSize = 1024 * 1024 * 4; // Receive queue buffer size (4 MiB)
-        this._rQmax = this._rQbufferSize / 8;
         // called in init: this._rQ = new Uint8Array(this._rQbufferSize);
         this._rQ = null; // Receive queue
 
@@ -45,24 +46,24 @@ export default class Websock {
     }
 
     // Getters and Setters
-    get_sQ() {
+    get sQ() {
         return this._sQ;
     }
 
-    get_rQ() {
+    get rQ() {
         return this._rQ;
     }
 
-    get_rQi() {
+    get rQi() {
         return this._rQi;
     }
 
-    set_rQi(val) {
+    set rQi(val) {
         this._rQi = val;
     }
 
     // Receive Queue
-    rQlen() {
+    get rQlen() {
         return this._rQlen - this._rQi;
     }
 
@@ -70,33 +71,33 @@ export default class Websock {
         return this._rQ[this._rQi];
     }
 
-    rQshift8() {
-        return this._rQ[this._rQi++];
-    }
-
-    rQskip8() {
-        this._rQi++;
+    rQskipBytes(bytes) {
+        this._rQi += bytes;
     }
 
-    rQskipBytes(num) {
-        this._rQi += num;
+    rQshift8() {
+        return this._rQshift(1);
     }
 
-    // TODO(directxman12): test performance with these vs a DataView
     rQshift16() {
-        return (this._rQ[this._rQi++] << 8) +
-               this._rQ[this._rQi++];
+        return this._rQshift(2);
     }
 
     rQshift32() {
-        return (this._rQ[this._rQi++] << 24) +
-               (this._rQ[this._rQi++] << 16) +
-               (this._rQ[this._rQi++] << 8) +
-               this._rQ[this._rQi++];
+        return this._rQshift(4);
+    }
+
+    // TODO(directxman12): test performance with these vs a DataView
+    _rQshift(bytes) {
+        let res = 0;
+        for (let byte = bytes - 1; byte >= 0; byte--) {
+            res += this._rQ[this._rQi++] << (byte * 8);
+        }
+        return res;
     }
 
     rQshiftStr(len) {
-        if (typeof(len) === 'undefined') { len = this.rQlen(); }
+        if (typeof(len) === 'undefined') { len = this.rQlen; }
         let str = "";
         // Handle large arrays in steps to avoid long strings on the stack
         for (let i = 0; i < len; i += 4096) {
@@ -107,36 +108,27 @@ export default class Websock {
     }
 
     rQshiftBytes(len) {
-        if (typeof(len) === 'undefined') { len = this.rQlen(); }
+        if (typeof(len) === 'undefined') { len = this.rQlen; }
         this._rQi += len;
         return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
     }
 
     rQshiftTo(target, len) {
-        if (len === undefined) { len = this.rQlen(); }
+        if (len === undefined) { len = this.rQlen; }
         // TODO: make this just use set with views when using a ArrayBuffer to store the rQ
         target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
         this._rQi += len;
     }
 
-    rQwhole() {
-        return new Uint8Array(this._rQ.buffer, 0, this._rQlen);
-    }
-
-    rQslice(start, end) {
-        if (end) {
-            return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
-        } else {
-            return new Uint8Array(this._rQ.buffer, this._rQi + start, this._rQlen - this._rQi - start);
-        }
+    rQslice(start, end = this.rQlen) {
+        return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
     }
 
     // Check to see if we must wait for 'num' bytes (default to FBU.bytes)
     // to be available in the receive queue. Return true if we need to
     // wait (and possibly print a debug message), otherwise false.
     rQwait(msg, num, goback) {
-        const rQlen = this._rQlen - this._rQi; // Skip rQlen() function call
-        if (rQlen < num) {
+        if (this.rQlen < num) {
             if (goback) {
                 if (this._rQi < goback) {
                     throw new Error("rQwait cannot backup " + goback + " bytes");
@@ -234,36 +226,40 @@ export default class Websock {
         return new Uint8Array(this._sQ.buffer, 0, this._sQlen);
     }
 
+    // We want to move all the unread data to the start of the queue,
+    // e.g. compacting.
+    // The function also expands the receive que if needed, and for
+    // performance reasons we combine these two actions to avoid
+    // unneccessary copying.
     _expand_compact_rQ(min_fit) {
-        const resizeNeeded = min_fit || this._rQlen - this._rQi > this._rQbufferSize / 2;
+        // if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place
+        // instead of resizing
+        const required_buffer_size =  (this._rQlen - this._rQi + min_fit) * 8;
+        const resizeNeeded = this._rQbufferSize < required_buffer_size;
+
         if (resizeNeeded) {
-            if (!min_fit) {
-                // just double the size if we need to do compaction
-                this._rQbufferSize *= 2;
-            } else {
-                // otherwise, make sure we satisy rQlen - rQi + min_fit < rQbufferSize / 8
-                this._rQbufferSize = (this._rQlen - this._rQi + min_fit) * 8;
-            }
+            // Make sure we always *at least* double the buffer size, and have at least space for 8x
+            // the current amount of data
+            this._rQbufferSize = Math.max(this._rQbufferSize * 2, required_buffer_size);
         }
 
         // we don't want to grow unboundedly
         if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
             this._rQbufferSize = MAX_RQ_GROW_SIZE;
-            if (this._rQbufferSize - this._rQlen - this._rQi < min_fit) {
+            if (this._rQbufferSize - this.rQlen < min_fit) {
                 throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
             }
         }
 
         if (resizeNeeded) {
             const old_rQbuffer = this._rQ.buffer;
-            this._rQmax = this._rQbufferSize / 8;
             this._rQ = new Uint8Array(this._rQbufferSize);
-            this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi));
+            this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi, this._rQlen - this._rQi));
         } else {
             if (ENABLE_COPYWITHIN) {
-                this._rQ.copyWithin(0, this._rQi);
+                this._rQ.copyWithin(0, this._rQi, this._rQlen);
             } else {
-                this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi));
+                this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi, this._rQlen - this._rQi));
             }
         }
 
@@ -271,8 +267,8 @@ export default class Websock {
         this._rQi = 0;
     }
 
+    // push arraybuffer values onto the end of the receive que
     _decode_message(data) {
-        // push arraybuffer values onto the end
         const u8 = new Uint8Array(data);
         if (u8.length > this._rQbufferSize - this._rQlen) {
             this._expand_compact_rQ(u8.length);
@@ -283,14 +279,13 @@ export default class Websock {
 
     _recv_message(e) {
         this._decode_message(e.data);
-        if (this.rQlen() > 0) {
+        if (this.rQlen > 0) {
             this._eventHandlers.message();
-            // Compact the receive queue
             if (this._rQlen == this._rQi) {
+                // All data has now been processed, this means we
+                // can reset the receive queue.
                 this._rQlen = 0;
                 this._rQi = 0;
-            } else if (this._rQlen > this._rQmax) {
-                this._expand_compact_rQ();
             }
         } else {
             Log.Debug("Ignoring empty message");