Commit 38781d93 authored by Solly Ross's avatar Solly Ross

Use Typed Arrays for the Websock receive queue

**This commit removes Base64 (and Flash) support**

This commit converts websock.js to used Typed Arrays for the
receive queue (and tweaks rfb.js to ensure that it continues
to function, since only Firefox implements
`%TypedArray%.prototype.slice`).  Base64 support was removed
to simplify code paths, and pave the way for using Typed Arrays
for the send queue as well.

This provides two advantages: first, we allocate a buffer ahead
of time, meaning the browser doesn't have to do any work dynamically
increasing the receive queue size.  Secondly, we are now able to pass
around Typed Array Views (e.g. `Uint8Array`), which are lightweight, and
don't involve copying.

The downside is that we initially allocate more memory -- we currently
start out with 4 MiB, and then automatically double when it looks like
the amount unused is getting to small.

The commit also explicitly adds a check to the compacting logic that
avoids calling the copy functions if `_rQlen === _rQi`.
parent 6c883653
[submodule "include/web-socket-js-project"]
path = include/web-socket-js-project
url = https://github.com/gimite/web-socket-js.git
...@@ -51,9 +51,6 @@ licenses (all MPL 2.0 compatible): ...@@ -51,9 +51,6 @@ licenses (all MPL 2.0 compatible):
include/jsunzip.js : zlib/libpng license include/jsunzip.js : zlib/libpng license
include/web-socket-js/ : New BSD license (3-clause). Source code at
http://github.com/gimite/web-socket-js
include/chrome-app/tcp-stream.js include/chrome-app/tcp-stream.js
: Apache 2.0 license : Apache 2.0 license
......
...@@ -69,11 +69,7 @@ See more screenshots <a href="http://kanaka.github.com/noVNC/screenshots.html">h ...@@ -69,11 +69,7 @@ See more screenshots <a href="http://kanaka.github.com/noVNC/screenshots.html">h
* HTML5 Canvas (with createImageData): Chrome, Firefox 3.6+, iOS * HTML5 Canvas (with createImageData): Chrome, Firefox 3.6+, iOS
Safari, Opera 11+, Internet Explorer 9+, etc. Safari, Opera 11+, Internet Explorer 9+, etc.
* HTML5 WebSockets: For browsers that do not have builtin * HTML5 WebSockets and Typed Arrays
WebSockets support, the project includes
<a href="http://github.com/gimite/web-socket-js">web-socket-js</a>,
a WebSockets emulator using Adobe Flash. iOS 4.2+ has built-in
WebSocket support.
* Fast Javascript Engine: this is not strictly a requirement, but * Fast Javascript Engine: this is not strictly a requirement, but
without a fast Javascript engine, noVNC might be painfully slow. without a fast Javascript engine, noVNC might be painfully slow.
...@@ -130,7 +126,6 @@ use a WebSockets to TCP socket proxy. There is a python proxy included ...@@ -130,7 +126,6 @@ use a WebSockets to TCP socket proxy. There is a python proxy included
* tight encoding : Michael Tinglof (Mercuri.ca) * tight encoding : Michael Tinglof (Mercuri.ca)
* Included libraries: * Included libraries:
* web-socket-js : Hiroshi Ichikawa (github.com/gimite/web-socket-js)
* as3crypto : Henri Torgemane (code.google.com/p/as3crypto) * as3crypto : Henri Torgemane (code.google.com/p/as3crypto)
* base64 : Martijn Pieters (Digital Creations 2), Samuel Sieb (sieb.net) * base64 : Martijn Pieters (Digital Creations 2), Samuel Sieb (sieb.net)
* DES : Dave Zimmerman (Widget Workshop), Jef Poskanzer (ACME Labs) * DES : Dave Zimmerman (Widget Workshop), Jef Poskanzer (ACME Labs)
......
Some implementation notes:
There is an included flash object (web-socket-js) that is used to
emulate websocket support on browsers without websocket support
(currently only Chrome has WebSocket support).
Javascript doesn't have a bytearray type, so what you get out of
a WebSocket object is just Javascript strings. Javascript has UTF-16
unicode strings and anything sent through the WebSocket gets converted
to UTF-8 and vice-versa. So, one additional (and necessary) function
of websockify is base64 encoding/decoding what is sent to/from the
browser.
Building web-socket-js emulator:
cd include/web-socket-js/flash-src
mxmlc -static-link-runtime-shared-libraries WebSocketMain.as
Rebuilding inflator.js Rebuilding inflator.js
- Download pako from npm - Download pako from npm
......
...@@ -30,6 +30,7 @@ enable_test_mode = function () { ...@@ -30,6 +30,7 @@ enable_test_mode = function () {
this._rfb_port = port; this._rfb_port = port;
this._rfb_password = (password !== undefined) ? password : ""; this._rfb_password = (password !== undefined) ? password : "";
this._rfb_path = (path !== undefined) ? path : ""; this._rfb_path = (path !== undefined) ? path : "";
this._sock.init('binary', 'ws');
this._updateState('ProtocolVersion', "Starting VNC handshake"); this._updateState('ProtocolVersion', "Starting VNC handshake");
}; };
}; };
...@@ -43,7 +44,7 @@ next_iteration = function () { ...@@ -43,7 +44,7 @@ next_iteration = function () {
frame_length = VNC_frame_data.length; frame_length = VNC_frame_data.length;
test_state = 'running'; test_state = 'running';
} }
if (test_state !== 'running') { return; } if (test_state !== 'running') { return; }
iteration += 1; iteration += 1;
......
...@@ -129,7 +129,7 @@ var RFB; ...@@ -129,7 +129,7 @@ var RFB;
'view_only': false, // Disable client mouse/keyboard 'view_only': false, // Disable client mouse/keyboard
'xvp_password_sep': '@', // Separator for XVP password fields 'xvp_password_sep': '@', // Separator for XVP password fields
'disconnectTimeout': 3, // Time (s) to wait for disconnection 'disconnectTimeout': 3, // Time (s) to wait for disconnection
'wsProtocols': ['binary', 'base64'], // Protocols to use in the WebSocket connection 'wsProtocols': ['binary'], // Protocols to use in the WebSocket connection
'repeaterID': '', // [UltraVNC] RepeaterID to connect to 'repeaterID': '', // [UltraVNC] RepeaterID to connect to
'viewportDrag': false, // Move the viewport on mouse drags 'viewportDrag': false, // Move the viewport on mouse drags
...@@ -218,16 +218,8 @@ var RFB; ...@@ -218,16 +218,8 @@ var RFB;
Util.Info("Using native WebSockets"); Util.Info("Using native WebSockets");
this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode); this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
} else { } else {
Util.Warn("Using web-socket-js bridge. Flash version: " + Util.Flash.version); this._cleanupSocket('fatal');
if (!Util.Flash || Util.Flash.version < 9) { throw new Error("WebSocket support is required to use noVNC");
this._cleanupSocket('fatal');
throw new Exception("WebSockets or <a href='http://get.adobe.com/flashplayer'>Adobe Flash</a> is required");
} else if (document.location.href.substr(0, 7) === 'file://') {
this._cleanupSocket('fatal');
throw new Exception("'file://' URL is incompatible with Adobe Flash");
} else {
this._updateState('loaded', 'noVNC ready: WebSockets emulation, ' + rmode);
}
} }
Util.Debug("<< RFB.constructor"); Util.Debug("<< RFB.constructor");
...@@ -363,8 +355,6 @@ var RFB; ...@@ -363,8 +355,6 @@ var RFB;
_init_vars: function () { _init_vars: function () {
// reset state // reset state
this._sock.init();
this._FBU.rects = 0; this._FBU.rects = 0;
this._FBU.subrects = 0; // RRE and HEXTILE this._FBU.subrects = 0; // RRE and HEXTILE
this._FBU.lines = 0; // RAW this._FBU.lines = 0; // RAW
...@@ -760,7 +750,8 @@ var RFB; ...@@ -760,7 +750,8 @@ var RFB;
if (this._sock.rQwait("auth challenge", 16)) { return false; } if (this._sock.rQwait("auth challenge", 16)) { return false; }
var challenge = this._sock.rQshiftBytes(16); // TODO(directxman12): make genDES not require an Array
var challenge = Array.prototype.slice.call(this._sock.rQshiftBytes(16));
var response = RFB.genDES(this._rfb_password, challenge); var response = RFB.genDES(this._rfb_password, challenge);
this._sock.send(response); this._sock.send(response);
this._updateState("SecurityResult"); this._updateState("SecurityResult");
...@@ -1559,11 +1550,21 @@ var RFB; ...@@ -1559,11 +1550,21 @@ var RFB;
rQi += this._FBU.bytes - 1; rQi += this._FBU.bytes - 1;
} else { } else {
if (this._FBU.subencoding & 0x02) { // Background if (this._FBU.subencoding & 0x02) { // Background
this._FBU.background = rQ.slice(rQi, rQi + this._fb_Bpp); if (this._fb_Bpp == 1) {
this._FBU.background = rQ[rQi];
} else {
// fb_Bpp is 4
this._FBU.background = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
}
rQi += this._fb_Bpp; rQi += this._fb_Bpp;
} }
if (this._FBU.subencoding & 0x04) { // Foreground if (this._FBU.subencoding & 0x04) { // Foreground
this._FBU.foreground = rQ.slice(rQi, rQi + this._fb_Bpp); if (this._fb_Bpp == 1) {
this._FBU.foreground = rQ[rQi];
} else {
// this._fb_Bpp is 4
this._FBU.foreground = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
}
rQi += this._fb_Bpp; rQi += this._fb_Bpp;
} }
...@@ -1575,7 +1576,12 @@ var RFB; ...@@ -1575,7 +1576,12 @@ var RFB;
for (var s = 0; s < subrects; s++) { for (var s = 0; s < subrects; s++) {
var color; var color;
if (this._FBU.subencoding & 0x10) { // SubrectsColoured if (this._FBU.subencoding & 0x10) { // SubrectsColoured
color = rQ.slice(rQi, rQi + this._fb_Bpp); if (this._fb_Bpp === 1) {
color = rQ[rQi];
} else {
// _fb_Bpp is 4
color = [rQ[rQi], rQ[rQi + 1], rQ[rQi + 2], rQ[rQi + 3]];
}
rQi += this._fb_Bpp; rQi += this._fb_Bpp;
} else { } else {
color = this._FBU.foreground; color = this._FBU.foreground;
......
Subproject commit c0855c6caec589c33acc22b6ee5e562287e65f3d
* How to try
Assuming you have Web server (e.g. Apache) running at http://example.com/ .
- Download web_socket.rb from:
http://github.com/gimite/web-socket-ruby/tree/master
- Run sample Web Socket server (echo server) in example.com with: (#1)
$ ruby web-socket-ruby/samples/echo_server.rb example.com 10081
- If your server already provides socket policy file at port 843, modify the file to allow access to port 10081. Otherwise you can skip this step. See below for details.
- Publish the web-socket-js directory with your Web server (e.g. put it in ~/public_html).
- Change ws://localhost:10081 to ws://example.com:10081 in sample.html.
- Open sample.html in your browser.
- After "onopen" is shown, input something, click [Send] and confirm echo back.
#1: First argument of echo_server.rb means that it accepts Web Socket connection from HTML pages in example.com.
* Troubleshooting
If it doesn't work, try these:
1. Try Chrome and Firefox 3.x.
- It doesn't work on Chrome:
-- It's likely an issue of your code or the server. Debug your code as usual e.g. using console.log.
- It works on Chrome but it doesn't work on Firefox:
-- It's likely an issue of web-socket-js specific configuration (e.g. 3 and 4 below).
- It works on both Chrome and Firefox, but it doesn't work on your browser:
-- Check "Supported environment" section below. Your browser may not be supported by web-socket-js.
2. Add this line before your code:
WEB_SOCKET_DEBUG = true;
and use Developer Tools (Chrome/Safari) or Firebug (Firefox) to see if console.log outputs any errors.
3. Make sure you do NOT open your HTML page as local file e.g. file:///.../sample.html. web-socket-js doesn't work on local file. Open it via Web server e.g. http:///.../sample.html.
4. If you are NOT using web-socket-ruby as your WebSocket server, you need to place Flash socket policy file on your server. See "Flash socket policy file" section below for details.
5. Check if sample.html bundled with web-socket-js works.
6. Make sure the port used for WebSocket (10081 in example above) is not blocked by your server/client's firewall.
7. Install debugger version of Flash Player available here to see Flash errors:
http://www.adobe.com/support/flashplayer/downloads.html
* Supported environments
It should work on:
- Google Chrome 4 or later (just uses native implementation)
- Firefox 3.x, Internet Explorer 8 + Flash Player 9 or later
It may or may not work on other browsers such as Safari, Opera or IE 6. Patch for these browsers are appreciated, but I will not work on fixing issues specific to these browsers by myself.
* Flash socket policy file
This implementation uses Flash's socket, which means that your server must provide Flash socket policy file to declare the server accepts connections from Flash.
If you use web-socket-ruby available at
http://github.com/gimite/web-socket-ruby/tree/master
, you don't need anything special, because web-socket-ruby handles Flash socket policy file request. But if you already provide socket policy file at port 843, you need to modify the file to allow access to Web Socket port, because it precedes what web-socket-ruby provides.
If you use other Web Socket server implementation, you need to provide socket policy file yourself. See
http://www.lightsphere.com/dev/articles/flash_socket_policy.html
for details and sample script to run socket policy file server. node.js implementation is available here:
http://github.com/LearnBoost/Socket.IO-node/blob/master/lib/socket.io/transports/flashsocket.js
Actually, it's still better to provide socket policy file at port 843 even if you use web-socket-ruby. Flash always try to connect to port 843 first, so providing the file at port 843 makes startup faster.
* Cookie considerations
Cookie is sent if Web Socket host is the same as the origin of JavaScript. Otherwise it is not sent, because I don't know way to send right Cookie (which is Cookie of the host of Web Socket, I heard).
Note that it's technically possible that client sends arbitrary string as Cookie and any other headers (by modifying this library for example) once you place Flash socket policy file in your server. So don't trust Cookie and other headers if you allow connection from untrusted origin.
* Proxy considerations
The WebSocket spec (http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol) specifies instructions for User Agents to support proxied connections by implementing the HTTP CONNECT method.
The AS3 Socket class doesn't implement this mechanism, which renders it useless for the scenarios where the user trying to open a socket is behind a proxy.
The class RFC2817Socket (by Christian Cantrell) effectively lets us implement this, as long as the proxy settings are known and provided by the interface that instantiates the WebSocket. As such, if you want to support proxied conncetions, you'll have to supply this information to the WebSocket constructor when Flash is being used. One way to go about it would be to ask the user for proxy settings information if the initial connection fails.
* How to host HTML file and SWF file in different domains
By default, HTML file and SWF file must be in the same domain. You can follow steps below to allow hosting them in different domain.
WARNING: If you use the method below, HTML files in ANY domains can send arbitrary TCP data to your WebSocket server, regardless of configuration in Flash socket policy file. Arbitrary TCP data means that they can even fake request headers including Origin and Cookie.
- Unzip WebSocketMainInsecure.zip to extract WebSocketMainInsecure.swf.
- Put WebSocketMainInsecure.swf on your server, instead of WebSocketMain.swf.
- In JavaScript, set WEB_SOCKET_SWF_LOCATION to URL of your WebSocketMainInsecure.swf.
* How to build WebSocketMain.swf
Install Flex 4 SDK:
http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4
$ cd flash-src
$ ./build.sh
* License
New BSD License.
/* SWFObject v2.2 <http://code.google.com/p/swfobject/>
is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
var swfobject=function(){var D="undefined",r="object",S="Shockwave Flash",W="ShockwaveFlash.ShockwaveFlash",q="application/x-shockwave-flash",R="SWFObjectExprInst",x="onreadystatechange",O=window,j=document,t=navigator,T=false,U=[h],o=[],N=[],I=[],l,Q,E,B,J=false,a=false,n,G,m=true,M=function(){var aa=typeof j.getElementById!=D&&typeof j.getElementsByTagName!=D&&typeof j.createElement!=D,ah=t.userAgent.toLowerCase(),Y=t.platform.toLowerCase(),ae=Y?/win/.test(Y):/win/.test(ah),ac=Y?/mac/.test(Y):/mac/.test(ah),af=/webkit/.test(ah)?parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,X=!+"\v1",ag=[0,0,0],ab=null;if(typeof t.plugins!=D&&typeof t.plugins[S]==r){ab=t.plugins[S].description;if(ab&&!(typeof t.mimeTypes!=D&&t.mimeTypes[q]&&!t.mimeTypes[q].enabledPlugin)){T=true;X=false;ab=ab.replace(/^.*\s+(\S+\s+\S+$)/,"$1");ag[0]=parseInt(ab.replace(/^(.*)\..*$/,"$1"),10);ag[1]=parseInt(ab.replace(/^.*\.(.*)\s.*$/,"$1"),10);ag[2]=/[a-zA-Z]/.test(ab)?parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/,"$1"),10):0}}else{if(typeof O.ActiveXObject!=D){try{var ad=new ActiveXObject(W);if(ad){ab=ad.GetVariable("$version");if(ab){X=true;ab=ab.split(" ")[1].split(",");ag=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}}catch(Z){}}}return{w3:aa,pv:ag,wk:af,ie:X,win:ae,mac:ac}}(),k=function(){if(!M.w3){return}if((typeof j.readyState!=D&&j.readyState=="complete")||(typeof j.readyState==D&&(j.getElementsByTagName("body")[0]||j.body))){f()}if(!J){if(typeof j.addEventListener!=D){j.addEventListener("DOMContentLoaded",f,false)}if(M.ie&&M.win){j.attachEvent(x,function(){if(j.readyState=="complete"){j.detachEvent(x,arguments.callee);f()}});if(O==top){(function(){if(J){return}try{j.documentElement.doScroll("left")}catch(X){setTimeout(arguments.callee,0);return}f()})()}}if(M.wk){(function(){if(J){return}if(!/loaded|complete/.test(j.readyState)){setTimeout(arguments.callee,0);return}f()})()}s(f)}}();function f(){if(J){return}try{var Z=j.getElementsByTagName("body")[0].appendChild(C("span"));Z.parentNode.removeChild(Z)}catch(aa){return}J=true;var X=U.length;for(var Y=0;Y<X;Y++){U[Y]()}}function K(X){if(J){X()}else{U[U.length]=X}}function s(Y){if(typeof O.addEventListener!=D){O.addEventListener("load",Y,false)}else{if(typeof j.addEventListener!=D){j.addEventListener("load",Y,false)}else{if(typeof O.attachEvent!=D){i(O,"onload",Y)}else{if(typeof O.onload=="function"){var X=O.onload;O.onload=function(){X();Y()}}else{O.onload=Y}}}}}function h(){if(T){V()}else{H()}}function V(){var X=j.getElementsByTagName("body")[0];var aa=C(r);aa.setAttribute("type",q);var Z=X.appendChild(aa);if(Z){var Y=0;(function(){if(typeof Z.GetVariable!=D){var ab=Z.GetVariable("$version");if(ab){ab=ab.split(" ")[1].split(",");M.pv=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}else{if(Y<10){Y++;setTimeout(arguments.callee,10);return}}X.removeChild(aa);Z=null;H()})()}else{H()}}function H(){var ag=o.length;if(ag>0){for(var af=0;af<ag;af++){var Y=o[af].id;var ab=o[af].callbackFn;var aa={success:false,id:Y};if(M.pv[0]>0){var ae=c(Y);if(ae){if(F(o[af].swfVersion)&&!(M.wk&&M.wk<312)){w(Y,true);if(ab){aa.success=true;aa.ref=z(Y);ab(aa)}}else{if(o[af].expressInstall&&A()){var ai={};ai.data=o[af].expressInstall;ai.width=ae.getAttribute("width")||"0";ai.height=ae.getAttribute("height")||"0";if(ae.getAttribute("class")){ai.styleclass=ae.getAttribute("class")}if(ae.getAttribute("align")){ai.align=ae.getAttribute("align")}var ah={};var X=ae.getElementsByTagName("param");var ac=X.length;for(var ad=0;ad<ac;ad++){if(X[ad].getAttribute("name").toLowerCase()!="movie"){ah[X[ad].getAttribute("name")]=X[ad].getAttribute("value")}}P(ai,ah,Y,ab)}else{p(ae);if(ab){ab(aa)}}}}}else{w(Y,true);if(ab){var Z=z(Y);if(Z&&typeof Z.SetVariable!=D){aa.success=true;aa.ref=Z}ab(aa)}}}}}function z(aa){var X=null;var Y=c(aa);if(Y&&Y.nodeName=="OBJECT"){if(typeof Y.SetVariable!=D){X=Y}else{var Z=Y.getElementsByTagName(r)[0];if(Z){X=Z}}}return X}function A(){return !a&&F("6.0.65")&&(M.win||M.mac)&&!(M.wk&&M.wk<312)}function P(aa,ab,X,Z){a=true;E=Z||null;B={success:false,id:X};var ae=c(X);if(ae){if(ae.nodeName=="OBJECT"){l=g(ae);Q=null}else{l=ae;Q=X}aa.id=R;if(typeof aa.width==D||(!/%$/.test(aa.width)&&parseInt(aa.width,10)<310)){aa.width="310"}if(typeof aa.height==D||(!/%$/.test(aa.height)&&parseInt(aa.height,10)<137)){aa.height="137"}j.title=j.title.slice(0,47)+" - Flash Player Installation";var ad=M.ie&&M.win?"ActiveX":"PlugIn",ac="MMredirectURL="+O.location.toString().replace(/&/g,"%26")+"&MMplayerType="+ad+"&MMdoctitle="+j.title;if(typeof ab.flashvars!=D){ab.flashvars+="&"+ac}else{ab.flashvars=ac}if(M.ie&&M.win&&ae.readyState!=4){var Y=C("div");X+="SWFObjectNew";Y.setAttribute("id",X);ae.parentNode.insertBefore(Y,ae);ae.style.display="none";(function(){if(ae.readyState==4){ae.parentNode.removeChild(ae)}else{setTimeout(arguments.callee,10)}})()}u(aa,ab,X)}}function p(Y){if(M.ie&&M.win&&Y.readyState!=4){var X=C("div");Y.parentNode.insertBefore(X,Y);X.parentNode.replaceChild(g(Y),X);Y.style.display="none";(function(){if(Y.readyState==4){Y.parentNode.removeChild(Y)}else{setTimeout(arguments.callee,10)}})()}else{Y.parentNode.replaceChild(g(Y),Y)}}function g(ab){var aa=C("div");if(M.win&&M.ie){aa.innerHTML=ab.innerHTML}else{var Y=ab.getElementsByTagName(r)[0];if(Y){var ad=Y.childNodes;if(ad){var X=ad.length;for(var Z=0;Z<X;Z++){if(!(ad[Z].nodeType==1&&ad[Z].nodeName=="PARAM")&&!(ad[Z].nodeType==8)){aa.appendChild(ad[Z].cloneNode(true))}}}}}return aa}function u(ai,ag,Y){var X,aa=c(Y);if(M.wk&&M.wk<312){return X}if(aa){if(typeof ai.id==D){ai.id=Y}if(M.ie&&M.win){var ah="";for(var ae in ai){if(ai[ae]!=Object.prototype[ae]){if(ae.toLowerCase()=="data"){ag.movie=ai[ae]}else{if(ae.toLowerCase()=="styleclass"){ah+=' class="'+ai[ae]+'"'}else{if(ae.toLowerCase()!="classid"){ah+=" "+ae+'="'+ai[ae]+'"'}}}}}var af="";for(var ad in ag){if(ag[ad]!=Object.prototype[ad]){af+='<param name="'+ad+'" value="'+ag[ad]+'" />'}}aa.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+ah+">"+af+"</object>";N[N.length]=ai.id;X=c(ai.id)}else{var Z=C(r);Z.setAttribute("type",q);for(var ac in ai){if(ai[ac]!=Object.prototype[ac]){if(ac.toLowerCase()=="styleclass"){Z.setAttribute("class",ai[ac])}else{if(ac.toLowerCase()!="classid"){Z.setAttribute(ac,ai[ac])}}}}for(var ab in ag){if(ag[ab]!=Object.prototype[ab]&&ab.toLowerCase()!="movie"){e(Z,ab,ag[ab])}}aa.parentNode.replaceChild(Z,aa);X=Z}}return X}function e(Z,X,Y){var aa=C("param");aa.setAttribute("name",X);aa.setAttribute("value",Y);Z.appendChild(aa)}function y(Y){var X=c(Y);if(X&&X.nodeName=="OBJECT"){if(M.ie&&M.win){X.style.display="none";(function(){if(X.readyState==4){b(Y)}else{setTimeout(arguments.callee,10)}})()}else{X.parentNode.removeChild(X)}}}function b(Z){var Y=c(Z);if(Y){for(var X in Y){if(typeof Y[X]=="function"){Y[X]=null}}Y.parentNode.removeChild(Y)}}function c(Z){var X=null;try{X=j.getElementById(Z)}catch(Y){}return X}function C(X){return j.createElement(X)}function i(Z,X,Y){Z.attachEvent(X,Y);I[I.length]=[Z,X,Y]}function F(Z){var Y=M.pv,X=Z.split(".");X[0]=parseInt(X[0],10);X[1]=parseInt(X[1],10)||0;X[2]=parseInt(X[2],10)||0;return(Y[0]>X[0]||(Y[0]==X[0]&&Y[1]>X[1])||(Y[0]==X[0]&&Y[1]==X[1]&&Y[2]>=X[2]))?true:false}function v(ac,Y,ad,ab){if(M.ie&&M.mac){return}var aa=j.getElementsByTagName("head")[0];if(!aa){return}var X=(ad&&typeof ad=="string")?ad:"screen";if(ab){n=null;G=null}if(!n||G!=X){var Z=C("style");Z.setAttribute("type","text/css");Z.setAttribute("media",X);n=aa.appendChild(Z);if(M.ie&&M.win&&typeof j.styleSheets!=D&&j.styleSheets.length>0){n=j.styleSheets[j.styleSheets.length-1]}G=X}if(M.ie&&M.win){if(n&&typeof n.addRule==r){n.addRule(ac,Y)}}else{if(n&&typeof j.createTextNode!=D){n.appendChild(j.createTextNode(ac+" {"+Y+"}"))}}}function w(Z,X){if(!m){return}var Y=X?"visible":"hidden";if(J&&c(Z)){c(Z).style.visibility=Y}else{v("#"+Z,"visibility:"+Y)}}function L(Y){var Z=/[\\\"<>\.;]/;var X=Z.exec(Y)!=null;return X&&typeof encodeURIComponent!=D?encodeURIComponent(Y):Y}var d=function(){if(M.ie&&M.win){window.attachEvent("onunload",function(){var ac=I.length;for(var ab=0;ab<ac;ab++){I[ab][0].detachEvent(I[ab][1],I[ab][2])}var Z=N.length;for(var aa=0;aa<Z;aa++){y(N[aa])}for(var Y in M){M[Y]=null}M=null;for(var X in swfobject){swfobject[X]=null}swfobject=null})}}();return{registerObject:function(ab,X,aa,Z){if(M.w3&&ab&&X){var Y={};Y.id=ab;Y.swfVersion=X;Y.expressInstall=aa;Y.callbackFn=Z;o[o.length]=Y;w(ab,false)}else{if(Z){Z({success:false,id:ab})}}},getObjectById:function(X){if(M.w3){return z(X)}},embedSWF:function(ab,ah,ae,ag,Y,aa,Z,ad,af,ac){var X={success:false,id:ah};if(M.w3&&!(M.wk&&M.wk<312)&&ab&&ah&&ae&&ag&&Y){w(ah,false);K(function(){ae+="";ag+="";var aj={};if(af&&typeof af===r){for(var al in af){aj[al]=af[al]}}aj.data=ab;aj.width=ae;aj.height=ag;var am={};if(ad&&typeof ad===r){for(var ak in ad){am[ak]=ad[ak]}}if(Z&&typeof Z===r){for(var ai in Z){if(typeof am.flashvars!=D){am.flashvars+="&"+ai+"="+Z[ai]}else{am.flashvars=ai+"="+Z[ai]}}}if(F(Y)){var an=u(aj,am,ah);if(aj.id==ah){w(ah,true)}X.success=true;X.ref=an}else{if(aa&&A()){aj.data=aa;P(aj,am,ah,ac);return}else{w(ah,true)}}if(ac){ac(X)}})}else{if(ac){ac(X)}}},switchOffAutoHideShow:function(){m=false},ua:M,getFlashPlayerVersion:function(){return{major:M.pv[0],minor:M.pv[1],release:M.pv[2]}},hasFlashPlayerVersion:F,createSWF:function(Z,Y,X){if(M.w3){return u(Z,Y,X)}else{return undefined}},showExpressInstall:function(Z,aa,X,Y){if(M.w3&&A()){P(Z,aa,X,Y)}},removeSWF:function(X){if(M.w3){y(X)}},createCSS:function(aa,Z,Y,X){if(M.w3){v(aa,Z,Y,X)}},addDomLoadEvent:K,addLoadEvent:s,getQueryParamValue:function(aa){var Z=j.location.search||j.location.hash;if(Z){if(/\?/.test(Z)){Z=Z.split("?")[1]}if(aa==null){return L(Z)}var Y=Z.split("&");for(var X=0;X<Y.length;X++){if(Y[X].substring(0,Y[X].indexOf("="))==aa){return L(Y[X].substring((Y[X].indexOf("=")+1)))}}}return""},expressInstallCallback:function(){if(a){var X=c(R);if(X&&l){X.parentNode.replaceChild(l,X);if(Q){w(Q,true);if(M.ie&&M.win){l.style.display="block"}}if(E){E(B)}}a=false}}}}();
\ No newline at end of file
// Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
// License: New BSD License
// Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/rfc6455
(function() {
if (window.WEB_SOCKET_FORCE_FLASH) {
// Keeps going.
} else if (window.WebSocket) {
return;
} else if (window.MozWebSocket) {
// Firefox.
window.WebSocket = MozWebSocket;
return;
}
var logger;
if (window.WEB_SOCKET_LOGGER) {
logger = WEB_SOCKET_LOGGER;
} else if (window.console && window.console.log && window.console.error) {
// In some environment, console is defined but console.log or console.error is missing.
logger = window.console;
} else {
logger = {log: function(){ }, error: function(){ }};
}
// swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
if (swfobject.getFlashPlayerVersion().major < 10) {
logger.error("Flash Player >= 10.0.0 is required.");
return;
}
if (location.protocol == "file:") {
logger.error(
"WARNING: web-socket-js doesn't work in file:///... URL " +
"unless you set Flash Security Settings properly. " +
"Open the page via Web server i.e. http://...");
}
/**
* Our own implementation of WebSocket class using Flash.
* @param {string} url
* @param {array or string} protocols
* @param {string} proxyHost
* @param {int} proxyPort
* @param {string} headers
*/
window.WebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
var self = this;
self.__id = WebSocket.__nextId++;
WebSocket.__instances[self.__id] = self;
self.readyState = WebSocket.CONNECTING;
self.bufferedAmount = 0;
self.__events = {};
if (!protocols) {
protocols = [];
} else if (typeof protocols == "string") {
protocols = [protocols];
}
// Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc.
// Otherwise, when onopen fires immediately, onopen is called before it is set.
self.__createTask = setTimeout(function() {
WebSocket.__addTask(function() {
self.__createTask = null;
WebSocket.__flash.create(
self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null);
});
}, 0);
};
/**
* Send data to the web socket.
* @param {string} data The data to send to the socket.
* @return {boolean} True for success, false for failure.
*/
WebSocket.prototype.send = function(data) {
if (this.readyState == WebSocket.CONNECTING) {
throw "INVALID_STATE_ERR: Web Socket connection has not been established";
}
// We use encodeURIComponent() here, because FABridge doesn't work if
// the argument includes some characters. We don't use escape() here
// because of this:
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions
// But it looks decodeURIComponent(encodeURIComponent(s)) doesn't
// preserve all Unicode characters either e.g. "\uffff" in Firefox.
// Note by wtritch: Hopefully this will not be necessary using ExternalInterface. Will require
// additional testing.
var result = WebSocket.__flash.send(this.__id, encodeURIComponent(data));
if (result < 0) { // success
return true;
} else {
this.bufferedAmount += result;
return false;
}
};
/**
* Close this web socket gracefully.
*/
WebSocket.prototype.close = function() {
if (this.__createTask) {
clearTimeout(this.__createTask);
this.__createTask = null;
this.readyState = WebSocket.CLOSED;
return;
}
if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
return;
}
this.readyState = WebSocket.CLOSING;
WebSocket.__flash.close(this.__id);
};
/**
* Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
*
* @param {string} type
* @param {function} listener
* @param {boolean} useCapture
* @return void
*/
WebSocket.prototype.addEventListener = function(type, listener, useCapture) {
if (!(type in this.__events)) {
this.__events[type] = [];
}
this.__events[type].push(listener);
};
/**
* Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
*
* @param {string} type
* @param {function} listener
* @param {boolean} useCapture
* @return void
*/
WebSocket.prototype.removeEventListener = function(type, listener, useCapture) {
if (!(type in this.__events)) return;
var events = this.__events[type];
for (var i = events.length - 1; i >= 0; --i) {
if (events[i] === listener) {
events.splice(i, 1);
break;
}
}
};
/**
* Implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">DOM 2 EventTarget Interface</a>}
*
* @param {Event} event
* @return void
*/
WebSocket.prototype.dispatchEvent = function(event) {
var events = this.__events[event.type] || [];
for (var i = 0; i < events.length; ++i) {
events[i](event);
}
var handler = this["on" + event.type];
if (handler) handler.apply(this, [event]);
};
/**
* Handles an event from Flash.
* @param {Object} flashEvent
*/
WebSocket.prototype.__handleEvent = function(flashEvent) {
if ("readyState" in flashEvent) {
this.readyState = flashEvent.readyState;
}
if ("protocol" in flashEvent) {
this.protocol = flashEvent.protocol;
}
var jsEvent;
if (flashEvent.type == "open" || flashEvent.type == "error") {
jsEvent = this.__createSimpleEvent(flashEvent.type);
} else if (flashEvent.type == "close") {
jsEvent = this.__createSimpleEvent("close");
jsEvent.wasClean = flashEvent.wasClean ? true : false;
jsEvent.code = flashEvent.code;
jsEvent.reason = flashEvent.reason;
} else if (flashEvent.type == "message") {
var data = decodeURIComponent(flashEvent.message);
jsEvent = this.__createMessageEvent("message", data);
} else {
throw "unknown event type: " + flashEvent.type;
}
this.dispatchEvent(jsEvent);
};
WebSocket.prototype.__createSimpleEvent = function(type) {
if (document.createEvent && window.Event) {
var event = document.createEvent("Event");
event.initEvent(type, false, false);
return event;
} else {
return {type: type, bubbles: false, cancelable: false};
}
};
WebSocket.prototype.__createMessageEvent = function(type, data) {
if (document.createEvent && window.MessageEvent && !window.opera) {
var event = document.createEvent("MessageEvent");
event.initMessageEvent("message", false, false, data, null, null, window, null);
return event;
} else {
// IE and Opera, the latter one truncates the data parameter after any 0x00 bytes.
return {type: type, data: data, bubbles: false, cancelable: false};
}
};
/**
* Define the WebSocket readyState enumeration.
*/
WebSocket.CONNECTING = 0;
WebSocket.OPEN = 1;
WebSocket.CLOSING = 2;
WebSocket.CLOSED = 3;
// Field to check implementation of WebSocket.
WebSocket.__isFlashImplementation = true;
WebSocket.__initialized = false;
WebSocket.__flash = null;
WebSocket.__instances = {};
WebSocket.__tasks = [];
WebSocket.__nextId = 0;
/**
* Load a new flash security policy file.
* @param {string} url
*/
WebSocket.loadFlashPolicyFile = function(url){
WebSocket.__addTask(function() {
WebSocket.__flash.loadManualPolicyFile(url);
});
};
/**
* Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
*/
WebSocket.__initialize = function() {
if (WebSocket.__initialized) return;
WebSocket.__initialized = true;
if (WebSocket.__swfLocation) {
// For backword compatibility.
window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
}
if (!window.WEB_SOCKET_SWF_LOCATION) {
logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
return;
}
if (!window.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR &&
!WEB_SOCKET_SWF_LOCATION.match(/(^|\/)WebSocketMainInsecure\.swf(\?.*)?$/) &&
WEB_SOCKET_SWF_LOCATION.match(/^\w+:\/\/([^\/]+)/)) {
var swfHost = RegExp.$1;
if (location.host != swfHost) {
logger.error(
"[WebSocket] You must host HTML and WebSocketMain.swf in the same host " +
"('" + location.host + "' != '" + swfHost + "'). " +
"See also 'How to host HTML file and SWF file in different domains' section " +
"in README.md. If you use WebSocketMainInsecure.swf, you can suppress this message " +
"by WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;");
}
}
var container = document.createElement("div");
container.id = "webSocketContainer";
// Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
// Flash from loading at least in IE. So we move it out of the screen at (-100, -100).
// But this even doesn't work with Flash Lite (e.g. in Droid Incredible). So with Flash
// Lite, we put it at (0, 0). This shows 1x1 box visible at left-top corner but this is
// the best we can do as far as we know now.
container.style.position = "absolute";
if (WebSocket.__isFlashLite()) {
container.style.left = "0px";
container.style.top = "0px";
} else {
container.style.left = "-100px";
container.style.top = "-100px";
}
var holder = document.createElement("div");
holder.id = "webSocketFlash";
container.appendChild(holder);
document.body.appendChild(container);
// See this article for hasPriority:
// http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-7ffd.html
swfobject.embedSWF(
WEB_SOCKET_SWF_LOCATION,
"webSocketFlash",
"1" /* width */,
"1" /* height */,
"10.0.0" /* SWF version */,
null,
null,
{hasPriority: true, swliveconnect : true, allowScriptAccess: "always"},
null,
function(e) {
if (!e.success) {
logger.error("[WebSocket] swfobject.embedSWF failed");
}
}
);
};
/**
* Called by Flash to notify JS that it's fully loaded and ready
* for communication.
*/
WebSocket.__onFlashInitialized = function() {
// We need to set a timeout here to avoid round-trip calls
// to flash during the initialization process.
setTimeout(function() {
WebSocket.__flash = document.getElementById("webSocketFlash");
WebSocket.__flash.setCallerUrl(location.href);
WebSocket.__flash.setDebug(!!window.WEB_SOCKET_DEBUG);
for (var i = 0; i < WebSocket.__tasks.length; ++i) {
WebSocket.__tasks[i]();
}
WebSocket.__tasks = [];
}, 0);
};
/**
* Called by Flash to notify WebSockets events are fired.
*/
WebSocket.__onFlashEvent = function() {
setTimeout(function() {
try {
// Gets events using receiveEvents() instead of getting it from event object
// of Flash event. This is to make sure to keep message order.
// It seems sometimes Flash events don't arrive in the same order as they are sent.
var events = WebSocket.__flash.receiveEvents();
for (var i = 0; i < events.length; ++i) {
WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
}
} catch (e) {
logger.error(e);
}
}, 0);
return true;
};
// Called by Flash.
WebSocket.__log = function(message) {
logger.log(decodeURIComponent(message));
};
// Called by Flash.
WebSocket.__error = function(message) {
logger.error(decodeURIComponent(message));
};
WebSocket.__addTask = function(task) {
if (WebSocket.__flash) {
task();
} else {
WebSocket.__tasks.push(task);
}
};
/**
* Test if the browser is running flash lite.
* @return {boolean} True if flash lite is running, false otherwise.
*/
WebSocket.__isFlashLite = function() {
if (!window.navigator || !window.navigator.mimeTypes) {
return false;
}
var mimeType = window.navigator.mimeTypes["application/x-shockwave-flash"];
if (!mimeType || !mimeType.enabledPlugin || !mimeType.enabledPlugin.filename) {
return false;
}
return mimeType.enabledPlugin.filename.match(/flashlite/i) ? true : false;
};
if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
// NOTE:
// This fires immediately if web_socket.js is dynamically loaded after
// the document is loaded.
swfobject.addDomLoadEvent(function() {
WebSocket.__initialize();
});
}
})();
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
/*jslint browser: true, bitwise: true */ /*jslint browser: true, bitwise: true */
/*global Util, Base64 */ /*global Util*/
// Load Flash WebSocket emulator if needed // Load Flash WebSocket emulator if needed
...@@ -34,29 +34,22 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) { ...@@ -34,29 +34,22 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
/* no builtin WebSocket so load web_socket.js */ /* no builtin WebSocket so load web_socket.js */
Websock_native = false; Websock_native = false;
(function () {
window.WEB_SOCKET_SWF_LOCATION = Util.get_include_uri() +
"web-socket-js/WebSocketMain.swf";
if (Util.Engine.trident) {
Util.Debug("Forcing uncached load of WebSocketMain.swf");
window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random();
}
Util.load_scripts(["web-socket-js/swfobject.js",
"web-socket-js/web_socket.js"]);
})();
} }
function Websock() { function Websock() {
"use strict"; "use strict";
this._websocket = null; // WebSocket object this._websocket = null; // WebSocket object
this._rQ = []; // Receive queue
this._rQi = 0; // Receive queue index this._rQi = 0; // Receive queue index
this._rQmax = 10000; // Max receive queue size before compacting 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;
this._sQ = []; // Send queue this._sQ = []; // Send queue
// called in init: this._rQ = new Uint8Array(this._rQbufferSize);
this._rQ = null; // Receive queue
this._mode = 'base64'; // Current WebSocket mode: 'binary', 'base64' this._mode = 'binary'; // Current WebSocket mode: 'binary', 'base64'
this.maxBufferedAmount = 200; this.maxBufferedAmount = 200;
this._eventHandlers = { this._eventHandlers = {
...@@ -69,6 +62,22 @@ function Websock() { ...@@ -69,6 +62,22 @@ function Websock() {
(function () { (function () {
"use strict"; "use strict";
var typedArrayToString = (function () {
// This is only for PhantomJS, which doesn't like apply-ing
// with Typed Arrays
try {
var arr = new Uint8Array([1, 2, 3]);
String.fromCharCode.apply(null, arr);
return function (a) { return String.fromCharCode.apply(null, a); };
} catch (ex) {
return function (a) {
return String.fromCharCode.apply(
null, Array.prototype.slice.call(a));
};
}
})();
Websock.prototype = { Websock.prototype = {
// Getters and Setters // Getters and Setters
get_sQ: function () { get_sQ: function () {
...@@ -89,7 +98,7 @@ function Websock() { ...@@ -89,7 +98,7 @@ function Websock() {
// Receive Queue // Receive Queue
rQlen: function () { rQlen: function () {
return this._rQ.length - this._rQi; return this._rQlen - this._rQi;
}, },
rQpeek8: function () { rQpeek8: function () {
...@@ -108,15 +117,7 @@ function Websock() { ...@@ -108,15 +117,7 @@ function Websock() {
this._rQi += num; this._rQi += num;
}, },
rQunshift8: function (num) { // TODO(directxman12): test performance with these vs a DataView
if (this._rQi === 0) {
this._rQ.unshift(num);
} else {
this._rQi--;
this._rQ[this._rQi] = num;
}
},
rQshift16: function () { rQshift16: function () {
return (this._rQ[this._rQi++] << 8) + return (this._rQ[this._rQi++] << 8) +
this._rQ[this._rQi++]; this._rQ[this._rQi++];
...@@ -131,22 +132,29 @@ function Websock() { ...@@ -131,22 +132,29 @@ function Websock() {
rQshiftStr: function (len) { rQshiftStr: function (len) {
if (typeof(len) === 'undefined') { len = this.rQlen(); } if (typeof(len) === 'undefined') { len = this.rQlen(); }
var arr = this._rQ.slice(this._rQi, this._rQi + len); var arr = new Uint8Array(this._rQ.buffer, this._rQi, len);
this._rQi += len; this._rQi += len;
return String.fromCharCode.apply(null, arr); return typedArrayToString(arr);
}, },
rQshiftBytes: function (len) { rQshiftBytes: function (len) {
if (typeof(len) === 'undefined') { len = this.rQlen(); } if (typeof(len) === 'undefined') { len = this.rQlen(); }
this._rQi += len; this._rQi += len;
return this._rQ.slice(this._rQi - len, this._rQi); return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
},
rQshiftTo: function (target, len) {
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;
}, },
rQslice: function (start, end) { rQslice: function (start, end) {
if (end) { if (end) {
return this._rQ.slice(this._rQi + start, this._rQi + end); return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
} else { } else {
return this._rQ.slice(this._rQi + start); return new Uint8Array(this._rQ.buffer, this._rQi + start, this._rQlen - this._rQi - start);
} }
}, },
...@@ -154,7 +162,7 @@ function Websock() { ...@@ -154,7 +162,7 @@ function Websock() {
// to be available in the receive queue. Return true if we need to // to be available in the receive queue. Return true if we need to
// wait (and possibly print a debug message), otherwise false. // wait (and possibly print a debug message), otherwise false.
rQwait: function (msg, num, goback) { rQwait: function (msg, num, goback) {
var rQlen = this._rQ.length - this._rQi; // Skip rQlen() function call var rQlen = this._rQlen - this._rQi; // Skip rQlen() function call
if (rQlen < num) { if (rQlen < num) {
if (goback) { if (goback) {
if (this._rQi < goback) { if (this._rQi < goback) {
...@@ -208,8 +216,12 @@ function Websock() { ...@@ -208,8 +216,12 @@ function Websock() {
this._eventHandlers[evt] = handler; this._eventHandlers[evt] = handler;
}, },
_allocate_buffers: function () {
this._rQ = new Uint8Array(this._rQbufferSize);
},
init: function (protocols, ws_schema) { init: function (protocols, ws_schema) {
this._rQ = []; this._allocate_buffers();
this._rQi = 0; this._rQi = 0;
this._sQ = []; this._sQ = [];
this._websocket = null; this._websocket = null;
...@@ -238,35 +250,21 @@ function Websock() { ...@@ -238,35 +250,21 @@ function Websock() {
// Default protocols if not specified // Default protocols if not specified
if (typeof(protocols) === "undefined") { if (typeof(protocols) === "undefined") {
if (wsbt) { protocols = 'binary';
protocols = ['binary', 'base64'];
} else {
protocols = 'base64';
}
} }
if (!wsbt) { if (Array.isArray(protocols) && protocols.indexOf('binary') > -1) {
if (protocols === 'binary') { protocols = 'binary';
throw new Error('WebSocket binary sub-protocol requested but not supported'); }
}
if (typeof(protocols) === 'object') {
var new_protocols = [];
for (var i = 0; i < protocols.length; i++) { if (!wsbt) {
if (protocols[i] === 'binary') { throw new Error("noVNC no longer supports base64 WebSockets. " +
Util.Error('Skipping unsupported WebSocket binary sub-protocol'); "Please use a browser which supports binary WebSockets.");
} else { }
new_protocols.push(protocols[i]);
}
}
if (new_protocols.length > 0) { if (protocols != 'binary') {
protocols = new_protocols; throw new Error("noVNC no longer supports base64 WebSockets. Please " +
} else { "use the binary subprotocol instead.");
throw new Error("Only WebSocket binary sub-protocol was requested and is not supported.");
}
}
} }
return protocols; return protocols;
...@@ -289,9 +287,16 @@ function Websock() { ...@@ -289,9 +287,16 @@ function Websock() {
this._mode = this._websocket.protocol; this._mode = this._websocket.protocol;
Util.Info("Server choose sub-protocol: " + this._websocket.protocol); Util.Info("Server choose sub-protocol: " + this._websocket.protocol);
} else { } else {
this._mode = 'base64'; this._mode = 'binary';
Util.Error('Server select no sub-protocol!: ' + this._websocket.protocol); Util.Error('Server select no sub-protocol!: ' + this._websocket.protocol);
} }
if (this._mode != 'binary') {
throw new Error("noVNC no longer supports base64 WebSockets. Please " +
"use the binary subprotocol instead.");
}
this._eventHandlers.open(); this._eventHandlers.open();
Util.Debug("<< WebSock.onopen"); Util.Debug("<< WebSock.onopen");
}).bind(this); }).bind(this);
...@@ -321,26 +326,15 @@ function Websock() { ...@@ -321,26 +326,15 @@ function Websock() {
// private methods // private methods
_encode_message: function () { _encode_message: function () {
if (this._mode === 'binary') { // Put in a binary arraybuffer
// Put in a binary arraybuffer return (new Uint8Array(this._sQ)).buffer;
return (new Uint8Array(this._sQ)).buffer;
} else {
// base64 encode
return Base64.encode(this._sQ);
}
}, },
_decode_message: function (data) { _decode_message: function (data) {
if (this._mode === 'binary') { // push arraybuffer values onto the end
// push arraybuffer values onto the end var u8 = new Uint8Array(data);
var u8 = new Uint8Array(data); this._rQ.set(u8, this._rQlen);
for (var i = 0; i < u8.length; i++) { this._rQlen += u8.length;
this._rQ.push(u8[i]);
}
} else {
// base64 decode and concat to end
this._rQ = this._rQ.concat(Base64.decode(data, 0));
}
}, },
_recv_message: function (e) { _recv_message: function (e) {
...@@ -349,8 +343,26 @@ function Websock() { ...@@ -349,8 +343,26 @@ function Websock() {
if (this.rQlen() > 0) { if (this.rQlen() > 0) {
this._eventHandlers.message(); this._eventHandlers.message();
// Compact the receive queue // Compact the receive queue
if (this._rQ.length > this._rQmax) { if (this._rQlen == this._rQi) {
this._rQ = this._rQ.slice(this._rQi); this._rQlen = 0;
this._rQi = 0;
} else if (this._rQlen > this._rQmax) {
if (this._rQlen - this._rQi > 0.5 * this._rQbufferSize) {
var old_rQbuffer = this._rQ.buffer;
this._rQbufferSize *= 2;
this._rQmax = this._rQbufferSize / 8;
this._rQ = new Uint8Array(this._rQbufferSize);
this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi));
} else {
if (this._rQ.copyWithin) {
// Firefox only, ATM
this._rQ.copyWithin(0, this._rQi);
} else {
this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi));
}
}
this._rQlen = this._rQlen - this._rQi;
this._rQi = 0; this._rQi = 0;
} }
} else { } else {
......
...@@ -5,7 +5,17 @@ chai.use(function (_chai, utils) { ...@@ -5,7 +5,17 @@ chai.use(function (_chai, utils) {
var data_cl = obj._drawCtx.getImageData(0, 0, obj._viewportLoc.w, obj._viewportLoc.h).data; var data_cl = obj._drawCtx.getImageData(0, 0, obj._viewportLoc.w, obj._viewportLoc.h).data;
// NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray, so work around that
var data = new Uint8Array(data_cl); var data = new Uint8Array(data_cl);
this.assert(utils.eql(data, target_data), var same = true;
for (var i = 0; i < obj.length; i++) {
if (data[i] != target_data[i]) {
same = false;
break;
}
}
if (!same) {
console.log("expected data: %o, actual data: %o", target_data, data);
}
this.assert(same,
"expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}", "expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
"expected #{this} not to have displayed the image #{act}", "expected #{this} not to have displayed the image #{act}",
target_data, target_data,
...@@ -15,10 +25,63 @@ chai.use(function (_chai, utils) { ...@@ -15,10 +25,63 @@ chai.use(function (_chai, utils) {
_chai.Assertion.addMethod('sent', function (target_data) { _chai.Assertion.addMethod('sent', function (target_data) {
var obj = this._obj; var obj = this._obj;
var data = obj._websocket._get_sent_data(); var data = obj._websocket._get_sent_data();
this.assert(utils.eql(data, target_data), var same = true;
for (var i = 0; i < obj.length; i++) {
if (data[i] != target_data[i]) {
same = false;
break;
}
}
if (!same) {
console.log("expected data: %o, actual data: %o", target_data, data);
}
this.assert(same,
"expected #{this} to have sent the data #{exp}, but it actually sent #{act}", "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
"expected #{this} not to have sent the data #{act}", "expected #{this} not to have sent the data #{act}",
target_data, target_data,
data); data);
}); });
_chai.Assertion.addProperty('array', function () {
utils.flag(this, 'array', true);
});
_chai.Assertion.overwriteMethod('equal', function (_super) {
return function assertArrayEqual(target) {
if (utils.flag(this, 'array')) {
var obj = this._obj;
var i;
var same = true;
if (utils.flag(this, 'deep')) {
for (i = 0; i < obj.length; i++) {
if (!utils.eql(obj[i], target[i])) {
same = false;
break;
}
}
this.assert(same,
"expected #{this} to have elements deeply equal to #{exp}",
"expected #{this} not to have elements deeply equal to #{exp}",
Array.prototype.slice.call(target));
} else {
for (i = 0; i < obj.length; i++) {
if (obj[i] != target[i]) {
same = false;
break;
}
}
this.assert(same,
"expected #{this} to have elements equal to #{exp}",
"expected #{this} not to have elements equal to #{exp}",
Array.prototype.slice.call(target));
}
} else {
_super.apply(this, arguments);
}
};
});
}); });
// requires local modules: util, base64, websock, rfb, keyboard, keysym, keysymdef, input, inflator, des, display // requires local modules: util, websock, rfb, keyboard, keysym, keysymdef, input, inflator, des, display
// requires test modules: fake.websocket, assertions // requires test modules: fake.websocket, assertions
/* jshint expr: true */ /* jshint expr: true */
var assert = chai.assert; var assert = chai.assert;
...@@ -18,6 +18,25 @@ describe('Remote Frame Buffer Protocol Client', function() { ...@@ -18,6 +18,25 @@ describe('Remote Frame Buffer Protocol Client', function() {
before(FakeWebSocket.replace); before(FakeWebSocket.replace);
after(FakeWebSocket.restore); after(FakeWebSocket.restore);
before(function () {
this.clock = sinon.useFakeTimers();
// Use a single set of buffers instead of reallocating to
// speed up tests
var sock = new Websock();
var rQ = new Uint8Array(sock._rQbufferSize);
Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
Websock.prototype._allocate_buffers = function () {
this._rQ = rQ;
};
});
after(function () {
Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
this.clock.restore();
});
describe('Public API Basic Behavior', function () { describe('Public API Basic Behavior', function () {
var client; var client;
beforeEach(function () { beforeEach(function () {
...@@ -1826,7 +1845,7 @@ describe('Remote Frame Buffer Protocol Client', function() { ...@@ -1826,7 +1845,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'normal'; client._rfb_state = 'normal';
client._normal_msg = sinon.spy(); client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(Base64.encode([])); client._sock._websocket._receive_data(new Uint8Array([]));
expect(client._normal_msg).to.not.have.been.called; expect(client._normal_msg).to.not.have.been.called;
}); });
...@@ -1834,7 +1853,7 @@ describe('Remote Frame Buffer Protocol Client', function() { ...@@ -1834,7 +1853,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'normal'; client._rfb_state = 'normal';
client._normal_msg = sinon.spy(); client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(Base64.encode([1, 2, 3])); client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._normal_msg).to.have.been.calledOnce; expect(client._normal_msg).to.have.been.calledOnce;
}); });
...@@ -1842,7 +1861,7 @@ describe('Remote Frame Buffer Protocol Client', function() { ...@@ -1842,7 +1861,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.connect('host', 8675); client.connect('host', 8675);
client._rfb_state = 'ProtocolVersion'; client._rfb_state = 'ProtocolVersion';
client._init_msg = sinon.spy(); client._init_msg = sinon.spy();
client._sock._websocket._receive_data(Base64.encode([1, 2, 3])); client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._init_msg).to.have.been.calledOnce; expect(client._init_msg).to.have.been.calledOnce;
}); });
......
// requires local modules: websock, base64, util // requires local modules: websock, util
// requires test modules: fake.websocket // requires test modules: fake.websocket, assertions
/* jshint expr: true */ /* jshint expr: true */
var assert = chai.assert; var assert = chai.assert;
var expect = chai.expect; var expect = chai.expect;
...@@ -9,13 +9,14 @@ describe('Websock', function() { ...@@ -9,13 +9,14 @@ describe('Websock', function() {
describe('Queue methods', function () { describe('Queue methods', function () {
var sock; var sock;
var RQ_TEMPLATE = [0, 1, 2, 3, 4, 5, 6, 7]; var RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
beforeEach(function () { beforeEach(function () {
sock = new Websock(); sock = new Websock();
for (var i = RQ_TEMPLATE.length - 1; i >= 0; i--) { // skip init
sock.rQunshift8(RQ_TEMPLATE[i]); sock._allocate_buffers();
} sock._rQ.set(RQ_TEMPLATE);
sock._rQlen = RQ_TEMPLATE.length;
}); });
describe('rQlen', function () { describe('rQlen', function () {
it('should return the length of the receive queue', function () { it('should return the length of the receive queue', function () {
...@@ -49,14 +50,6 @@ describe('Websock', function() { ...@@ -49,14 +50,6 @@ describe('Websock', function() {
}); });
}); });
describe('rQunshift8', function () {
it('should place a byte at the front of the queue', function () {
sock.rQunshift8(255);
expect(sock.rQpeek8()).to.equal(255);
expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length + 1);
});
});
describe('rQshift16', function () { describe('rQshift16', function () {
it('should pop two bytes from the receive queue and return a single number', function () { it('should pop two bytes from the receive queue and return a single number', function () {
var bef_len = sock.rQlen(); var bef_len = sock.rQlen();
...@@ -84,7 +77,7 @@ describe('Websock', function() { ...@@ -84,7 +77,7 @@ describe('Websock', function() {
var bef_rQi = sock.get_rQi(); var bef_rQi = sock.get_rQi();
var shifted = sock.rQshiftStr(3); var shifted = sock.rQshiftStr(3);
expect(shifted).to.be.a('string'); expect(shifted).to.be.a('string');
expect(shifted).to.equal(String.fromCharCode.apply(null, RQ_TEMPLATE.slice(bef_rQi, bef_rQi + 3))); expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3))));
expect(sock.rQlen()).to.equal(bef_len - 3); expect(sock.rQlen()).to.equal(bef_len - 3);
}); });
...@@ -99,8 +92,8 @@ describe('Websock', function() { ...@@ -99,8 +92,8 @@ describe('Websock', function() {
var bef_len = sock.rQlen(); var bef_len = sock.rQlen();
var bef_rQi = sock.get_rQi(); var bef_rQi = sock.get_rQi();
var shifted = sock.rQshiftBytes(3); var shifted = sock.rQshiftBytes(3);
expect(shifted).to.be.an.instanceof(Array); expect(shifted).to.be.an.instanceof(Uint8Array);
expect(shifted).to.deep.equal(RQ_TEMPLATE.slice(bef_rQi, bef_rQi + 3)); expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3));
expect(sock.rQlen()).to.equal(bef_len - 3); expect(sock.rQlen()).to.equal(bef_len - 3);
}); });
...@@ -123,19 +116,19 @@ describe('Websock', function() { ...@@ -123,19 +116,19 @@ describe('Websock', function() {
it('should return an array containing the given slice of the receive queue', function () { it('should return an array containing the given slice of the receive queue', function () {
var sl = sock.rQslice(0, 2); var sl = sock.rQslice(0, 2);
expect(sl).to.be.an.instanceof(Array); expect(sl).to.be.an.instanceof(Uint8Array);
expect(sl).to.deep.equal(RQ_TEMPLATE.slice(0, 2)); expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
}); });
it('should use the rest of the receive queue if no end is given', function () { it('should use the rest of the receive queue if no end is given', function () {
var sl = sock.rQslice(1); var sl = sock.rQslice(1);
expect(sl).to.have.length(RQ_TEMPLATE.length - 1); expect(sl).to.have.length(RQ_TEMPLATE.length - 1);
expect(sl).to.deep.equal(RQ_TEMPLATE.slice(1)); expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1));
}); });
it('should take the current rQi in to account', function () { it('should take the current rQi in to account', function () {
sock.set_rQi(1); sock.set_rQi(1);
expect(sock.rQslice(0, 2)).to.deep.equal(RQ_TEMPLATE.slice(1, 3)); expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
}); });
}); });
...@@ -257,6 +250,8 @@ describe('Websock', function() { ...@@ -257,6 +250,8 @@ describe('Websock', function() {
WebSocket.CONNECTING = old_WS.CONNECTING; WebSocket.CONNECTING = old_WS.CONNECTING;
WebSocket.CLOSING = old_WS.CLOSING; WebSocket.CLOSING = old_WS.CLOSING;
WebSocket.CLOSED = old_WS.CLOSED; WebSocket.CLOSED = old_WS.CLOSED;
WebSocket.prototype.binaryType = 'arraybuffer';
}); });
describe('opening', function () { describe('opening', function () {
...@@ -265,22 +260,14 @@ describe('Websock', function() { ...@@ -265,22 +260,14 @@ describe('Websock', function() {
}); });
it('should open the actual websocket', function () { it('should open the actual websocket', function () {
sock.open('ws://localhost:8675', 'base64'); sock.open('ws://localhost:8675', 'binary');
expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'base64'); expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
}); });
it('should fail if we try to use binary but do not support it', function () { it('should fail if we specify a protocol besides binary', function () {
expect(function () { sock.open('ws:///', 'binary'); }).to.throw(Error); expect(function () { sock.open('ws:///', 'base64'); }).to.throw(Error);
}); });
it('should fail if we specified an array with only binary and we do not support it', function () {
expect(function () { sock.open('ws:///', ['binary']); }).to.throw(Error);
});
it('should skip binary if we have multiple options for encoding and do not support binary', function () {
sock.open('ws:///', ['binary', 'base64']);
expect(WebSocket).to.have.been.calledWith('ws:///', ['base64']);
});
// it('should initialize the event handlers')? // it('should initialize the event handlers')?
}); });
...@@ -340,16 +327,15 @@ describe('Websock', function() { ...@@ -340,16 +327,15 @@ describe('Websock', function() {
expect(sock._recv_message).to.have.been.calledOnce; expect(sock._recv_message).to.have.been.calledOnce;
}); });
it('should copy the mode over upon opening', function () { it('should fail if a protocol besides binary is requested', function () {
sock._websocket.protocol = 'cheese'; sock._websocket.protocol = 'base64';
sock._websocket.onopen(); expect(sock._websocket.onopen).to.throw(Error);
expect(sock._mode).to.equal('cheese');
}); });
it('should assume base64 if no protocol was available on opening', function () { it('should assume binary if no protocol was available on opening', function () {
sock._websocket.protocol = null; sock._websocket.protocol = null;
sock._websocket.onopen(); sock._websocket.onopen();
expect(sock._mode).to.equal('base64'); expect(sock._mode).to.equal('binary');
}); });
it('should call the open event handler on opening', function () { it('should call the open event handler on opening', function () {
...@@ -377,13 +363,7 @@ describe('Websock', function() { ...@@ -377,13 +363,7 @@ describe('Websock', function() {
var sock; var sock;
beforeEach(function () { beforeEach(function () {
sock = new Websock(); sock = new Websock();
}); sock._allocate_buffers();
it('should support decoding base64 string data to add it to the receive queue', function () {
var msg = { data: Base64.encode([1, 2, 3]) };
sock._mode = 'base64';
sock._recv_message(msg);
expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
}); });
it('should support adding binary Uint8Array data to the receive queue', function () { it('should support adding binary Uint8Array data to the receive queue', function () {
...@@ -395,16 +375,16 @@ describe('Websock', function() { ...@@ -395,16 +375,16 @@ describe('Websock', function() {
it('should call the message event handler if present', function () { it('should call the message event handler if present', function () {
sock._eventHandlers.message = sinon.spy(); sock._eventHandlers.message = sinon.spy();
var msg = { data: Base64.encode([1, 2, 3]) }; var msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'base64'; sock._mode = 'binary';
sock._recv_message(msg); sock._recv_message(msg);
expect(sock._eventHandlers.message).to.have.been.calledOnce; expect(sock._eventHandlers.message).to.have.been.calledOnce;
}); });
it('should not call the message event handler if there is nothing in the receive queue', function () { it('should not call the message event handler if there is nothing in the receive queue', function () {
sock._eventHandlers.message = sinon.spy(); sock._eventHandlers.message = sinon.spy();
var msg = { data: Base64.encode([]) }; var msg = { data: new Uint8Array([]).buffer };
sock._mode = 'base64'; sock._mode = 'binary';
sock._recv_message(msg); sock._recv_message(msg);
expect(sock._eventHandlers.message).not.to.have.been.called; expect(sock._eventHandlers.message).not.to.have.been.called;
}); });
...@@ -412,21 +392,22 @@ describe('Websock', function() { ...@@ -412,21 +392,22 @@ describe('Websock', function() {
it('should compact the receive queue', function () { it('should compact the receive queue', function () {
// NB(sross): while this is an internal implementation detail, it's important to // NB(sross): while this is an internal implementation detail, it's important to
// test, otherwise the receive queue could become very large very quickly // test, otherwise the receive queue could become very large very quickly
sock._rQ = [0, 1, 2, 3, 4, 5]; sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
sock._rQlen = 6;
sock.set_rQi(6); sock.set_rQi(6);
sock._rQmax = 3; sock._rQmax = 3;
var msg = { data: Base64.encode([1, 2, 3]) }; var msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'base64'; sock._mode = 'binary';
sock._recv_message(msg); sock._recv_message(msg);
expect(sock._rQ.length).to.equal(3); expect(sock._rQlen).to.equal(3);
expect(sock.get_rQi()).to.equal(0); expect(sock.get_rQi()).to.equal(0);
}); });
it('should call the error event handler on an exception', function () { it('should call the error event handler on an exception', function () {
sock._eventHandlers.error = sinon.spy(); sock._eventHandlers.error = sinon.spy();
sock._eventHandlers.message = sinon.stub().throws(); sock._eventHandlers.message = sinon.stub().throws();
var msg = { data: Base64.encode([1, 2, 3]) }; var msg = { data: new Uint8Array([1, 2, 3]).buffer };
sock._mode = 'base64'; sock._mode = 'binary';
sock._recv_message(msg); sock._recv_message(msg);
expect(sock._eventHandlers.error).to.have.been.calledOnce; expect(sock._eventHandlers.error).to.have.been.calledOnce;
}); });
...@@ -455,26 +436,5 @@ describe('Websock', function() { ...@@ -455,26 +436,5 @@ describe('Websock', function() {
expect(sock._websocket._get_sent_data()).to.deep.equal([1, 2, 3]); expect(sock._websocket._get_sent_data()).to.deep.equal([1, 2, 3]);
}); });
}); });
describe('as Base64 data', function () {
var sock;
beforeEach(function () {
sock = new Websock();
sock.open('ws://', 'base64');
sock._websocket._open();
});
it('should convert the send queue into a Base64-encoded string', function () {
sock._sQ = [1, 2, 3];
expect(sock._encode_message()).to.equal(Base64.encode([1, 2, 3]));
});
it('should properly pass the encoded data off to the actual WebSocket', function () {
sock.send([1, 2, 3]);
expect(sock._websocket._get_sent_data()).to.deep.equal([1, 2, 3]);
});
});
}); });
}); });
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