Skip to content

Commit

Permalink
update websockify to latest trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Oct 30, 2012
1 parent 5ba8486 commit fcb6c33
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 164 deletions.
5 changes: 5 additions & 0 deletions third_party/websockify/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

0.2.1 - Oct 15, 2012
--------------------

* re-released with updated version number

0.2.0 - Sep 17, 2012
--------------------

Expand Down
11 changes: 8 additions & 3 deletions third_party/websockify/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
websockify is licensed under the LGPL version 3 (see docs/LICENSE.GPL-3 and
docs/LICENSE.LGPL-3) with the following exceptions:

include/base64.js : Choice of MIT 1.1, GPL-2 or LGPL-2.1

include/web-socket-js/ : New BSD license. Source code at
include/websock.js : MPL 2.0

include/base64.js : MPL 2.0

include/des.js : Various BSD style licenses

include/web-socket-js/ : New BSD license (3-clause). Source code at
https://github.com/gimite/web-socket-js

other/kumina.c : Simplified BSD license (2 clause).
Original source at
https://github.com/kumina/wsproxy

21 changes: 17 additions & 4 deletions third_party/websockify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ the target in both directions.
### WebSockets binary data

Websockify supports all versions of the WebSockets protocol (Hixie and
HyBI). The older Hixie versions of the protocol only support UTF-8
HyBi). The older Hixie versions of the protocol only support UTF-8
text payloads. In order to transport binary data over UTF-8 an
encoding must used to encapsulate the data within UTF-8. Websockify
uses base64 to encode all traffic to and from the client. This does
not affect the data between websockify and the server.
encoding must used to encapsulate the data within UTF-8.

With Hixie clients, Websockify uses base64 to encode all traffic to
and from the client. This does not affect the data between websockify
and the server.

With HyBi clients, websockify negotiates whether to base64 encode
traffic to and from the client via the subprotocol header
(Sec-WebSocket-Protocol). The valid subprotocol values are 'binary'
and 'base64' and if the client sends both then the server (the python
implementation) will prefer 'binary'. The 'binary' subprotocol
indicates that the data will be sent raw using binary WebSocket
frames. Some HyBi clients (such as the Flash fallback and older Chrome
and iOS versions) do not support binary data which is why the
negotiation is necessary.


### Encrypted WebSocket connections (wss://)

Expand Down
99 changes: 33 additions & 66 deletions third_party/websockify/include/base64.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,46 @@
/*
* Modified from:
* http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
*/

/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla XML-RPC Client component.
*
* The Initial Developer of the Original Code is
* Digital Creations 2, Inc.
* Portions created by the Initial Developer are Copyright (C) 2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Martijn Pieters <[email protected]> (original author)
* Samuel Sieb <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js

/*jslint white: false, bitwise: false, plusplus: false */
/*global console */

var Base64 = {

/* Convert data (an array of integers) to a Base64 string. */
toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(''),
base64Pad : '=',

encode: function (data) {
"use strict";
var result = '',
chrTable = Base64.toBase64Table.split(''),
pad = Base64.base64Pad,
length = data.length,
i;
var result = '';
var toBase64Table = Base64.toBase64Table;
var base64Pad = Base64.base64Pad;
var length = data.length;
var i;
// Convert every three bytes to 4 ascii characters.
/* BEGIN LOOP */
for (i = 0; i < (length - 2); i += 3) {
result += chrTable[data[i] >> 2];
result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
result += chrTable[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
result += chrTable[data[i+2] & 0x3f];
result += toBase64Table[data[i] >> 2];
result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
result += toBase64Table[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)];
result += toBase64Table[data[i+2] & 0x3f];
}
/* END LOOP */

// Convert the remaining 1 or 2 bytes, pad out to 4 characters.
if (length%3) {
i = length - (length%3);
result += chrTable[data[i] >> 2];
result += toBase64Table[data[i] >> 2];
if ((length%3) === 2) {
result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
result += chrTable[(data[i+1] & 0x0f) << 2];
result += pad;
result += toBase64Table[((data[i] & 0x03) << 4) + (data[i+1] >> 4)];
result += toBase64Table[(data[i+1] & 0x0f) << 2];
result += base64Pad;
} else {
result += chrTable[(data[i] & 0x03) << 4];
result += pad + pad;
result += toBase64Table[(data[i] & 0x03) << 4];
result += base64Pad + base64Pad;
}
}

Expand All @@ -97,12 +62,12 @@ toBinaryTable : [
decode: function (data, offset) {
"use strict";
offset = typeof(offset) !== 'undefined' ? offset : 0;
var binTable = Base64.toBinaryTable,
pad = Base64.base64Pad,
result, result_length, idx, i, c, padding,
leftbits = 0, // number of bits decoded, but yet to be appended
leftdata = 0, // bits decoded, but yet to be appended
data_length = data.indexOf('=') - offset;
var toBinaryTable = Base64.toBinaryTable;
var base64Pad = Base64.base64Pad;
var result, result_length, idx, i, c, padding;
var leftbits = 0; // number of bits decoded, but yet to be appended
var leftdata = 0; // bits decoded, but yet to be appended
var data_length = data.indexOf('=') - offset;

if (data_length < 0) { data_length = data.length - offset; }

Expand All @@ -111,9 +76,10 @@ decode: function (data, offset) {
result = new Array(result_length);

// Convert one by one.
/* BEGIN LOOP */
for (idx = 0, i = offset; i < data.length; i++) {
c = binTable[data.charCodeAt(i) & 0x7f];
padding = (data.charAt(i) === pad);
c = toBinaryTable[data.charCodeAt(i) & 0x7f];
padding = (data.charAt(i) === base64Pad);
// Skip illegal characters and whitespace
if (c === -1) {
console.error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
Expand All @@ -134,6 +100,7 @@ decode: function (data, offset) {
leftdata &= (1 << leftbits) - 1;
}
}
/* END LOOP */

// If there are any bits left, the base64 string was corrupted
if (leftbits) {
Expand Down
62 changes: 59 additions & 3 deletions third_party/websockify/include/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* noVNC: HTML5 VNC client
* Copyright (C) 2011 Joel Martin
* Licensed under LGPL-3 (see LICENSE.txt)
* from noVNC: HTML5 VNC client
* Copyright (C) 2012 Joel Martin
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
Expand Down Expand Up @@ -57,6 +57,21 @@ if (!Array.prototype.map)
};
}

//
// requestAnimationFrame shim with setTimeout fallback
//

window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();

/*
* ------------------------------------------------------
* Namespaced in Util
Expand Down Expand Up @@ -131,6 +146,8 @@ Util.conf_default = function(cfg, api, defaults, v, mode, type, defval, desc) {
}
} else if (type in {'integer':1, 'int':1}) {
val = parseInt(val, 10);
} else if (type === 'str') {
val = String(val);
} else if (type === 'func') {
if (!val) {
val = function () {};
Expand Down Expand Up @@ -190,6 +207,45 @@ Util.conf_defaults = function(cfg, api, defaults, arr) {
* Cross-browser routines
*/


// Dynamically load scripts without using document.write()
// Reference: http://unixpapa.com/js/dyna.html
//
// Handles the case where load_scripts is invoked from a script that
// itself is loaded via load_scripts. Once all scripts are loaded the
// window.onscriptsloaded handler is called (if set).
Util.get_include_uri = function() {
return (typeof INCLUDE_URI !== "undefined") ? INCLUDE_URI : "include/";
}
Util._pending_scripts = [];
Util.load_scripts = function(files) {
var head = document.getElementsByTagName('head')[0],
ps = Util._pending_scripts;
for (var f=0; f<files.length; f++) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = Util.get_include_uri() + files[f];
//console.log("loading script: " + Util.get_include_uri() + files[f]);
head.appendChild(script);
ps.push(script);
script.onload = script.onreadystatechange = function (e) {
if (!this.readyState ||
this.readyState == 'complete' ||
this.readyState == 'loaded') {
this.onload = this.onreadystatechange = null;
if (ps.indexOf(this) >= 0) {
//console.log("loaded script: " + this.src);
ps.splice(ps.indexOf(this), 1);
}
// Call window.onscriptsload after last script loads
if (ps.length === 0 && window.onscriptsload) {
window.onscriptsload();
}
}
}
}
}

// Get DOM element position on page
Util.getPosition = function (obj) {
var x = 0, y = 0;
Expand Down
25 changes: 10 additions & 15 deletions third_party/websockify/include/websock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Websock: high-performance binary WebSockets
* Copyright (C) 2012 Joel Martin
* Licensed under LGPL-3 (see LICENSE.txt)
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* Websock is similar to the standard WebSocket object but Websock
* enables communication with raw TCP sockets (i.e. the binary stream)
Expand Down Expand Up @@ -35,23 +35,14 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {

Websock_native = false;
(function () {
function get_INCLUDE_URI() {
return (typeof INCLUDE_URI !== "undefined") ?
INCLUDE_URI : "include/";
}

var start = "<script src='" + get_INCLUDE_URI(),
end = "'><\/script>", extra = "";

window.WEB_SOCKET_SWF_LOCATION = get_INCLUDE_URI() +
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();
}
extra += start + "web-socket-js/swfobject.js" + end;
extra += start + "web-socket-js/web_socket.js" + end;
document.write(extra);
Util.load_scripts(["web-socket-js/swfobject.js",
"web-socket-js/web_socket.js"]);
}());
}

Expand Down Expand Up @@ -181,7 +172,10 @@ function decode_message(data) {
//Util.Debug(">> decode_message: " + data);
if (mode === 'binary') {
// push arraybuffer values onto the end
rQ.push.apply(rQ, (new Uint8Array(data)));
var u8 = new Uint8Array(data);
for (var i = 0; i < u8.length; i++) {
rQ.push(u8[i]);
}
} else {
// base64 decode and concat to the end
rQ = rQ.concat(Base64.decode(data, 0));
Expand Down Expand Up @@ -380,8 +374,9 @@ function close() {

// Override internal functions for testing
// Takes a send function, returns reference to recv function
function testMode(override_send) {
function testMode(override_send, data_mode) {
test_mode = true;
mode = data_mode;
api.send = override_send;
api.close = function () {};
return recv_message;
Expand Down
Loading

0 comments on commit fcb6c33

Please sign in to comment.