Commit 204675c8 authored by Joel Martin's avatar Joel Martin

Update websockify/websock.js.

This change pulls websockify 6d9deda9c5.

Most note worthy changes:
- Pulls in web-socket-js 7677e7a954 which updates to IETF 6455 (from
  Hixie)
- Binary support detection and use in include/websock.js
- Add ssl and unix target support
- Add multiple target support via config file/dir.
- Idle timeout exit
parent 3435491e
// Copyright: Hiroshi Ichikawa <http://gimite.net/en/> // Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
// License: New BSD License // License: New BSD License
// Reference: http://dev.w3.org/html5/websockets/ // Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol // Reference: http://tools.ietf.org/html/rfc6455
(function() { (function() {
if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) return; if (window.WEB_SOCKET_FORCE_FLASH) {
// Keeps going.
} else if (window.WebSocket) {
return;
} else if (window.MozWebSocket) {
// Firefox.
window.WebSocket = MozWebSocket;
return;
}
var console = window.console; var logger;
if (!console || !console.log || !console.error) { if (window.WEB_SOCKET_LOGGER) {
console = {log: function(){ }, error: function(){ }}; 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(){ }};
} }
if (!swfobject.hasFlashPlayerVersion("10.0.0")) { // swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
console.error("Flash Player >= 10.0.0 is required."); if (swfobject.getFlashPlayerVersion().major < 10) {
logger.error("Flash Player >= 10.0.0 is required.");
return; return;
} }
if (location.protocol == "file:") { if (location.protocol == "file:") {
console.error( logger.error(
"WARNING: web-socket-js doesn't work in file:///... URL " + "WARNING: web-socket-js doesn't work in file:///... URL " +
"unless you set Flash Security Settings properly. " + "unless you set Flash Security Settings properly. " +
"Open the page via Web server i.e. http://..."); "Open the page via Web server i.e. http://...");
} }
/** /**
* This class represents a faux web socket. * Our own implementation of WebSocket class using Flash.
* @param {string} url * @param {string} url
* @param {string} protocol * @param {array or string} protocols
* @param {string} proxyHost * @param {string} proxyHost
* @param {int} proxyPort * @param {int} proxyPort
* @param {string} headers * @param {string} headers
*/ */
WebSocket = function(url, protocol, proxyHost, proxyPort, headers) { window.WebSocket = function(url, protocols, proxyHost, proxyPort, headers) {
var self = this; var self = this;
self.__id = WebSocket.__nextId++; self.__id = WebSocket.__nextId++;
WebSocket.__instances[self.__id] = self; WebSocket.__instances[self.__id] = self;
self.readyState = WebSocket.CONNECTING; self.readyState = WebSocket.CONNECTING;
self.bufferedAmount = 0; self.bufferedAmount = 0;
self.__events = {}; 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. // 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. // Otherwise, when onopen fires immediately, onopen is called before it is set.
setTimeout(function() { self.__createTask = setTimeout(function() {
WebSocket.__addTask(function() { WebSocket.__addTask(function() {
self.__createTask = null;
WebSocket.__flash.create( WebSocket.__flash.create(
self.__id, url, protocol, proxyHost || null, proxyPort || 0, headers || null); self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null);
}); });
}, 0); }, 0);
}; };
...@@ -78,6 +98,12 @@ ...@@ -78,6 +98,12 @@
* Close this web socket gracefully. * Close this web socket gracefully.
*/ */
WebSocket.prototype.close = function() { 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) { if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) {
return; return;
} }
...@@ -131,7 +157,7 @@ ...@@ -131,7 +157,7 @@
events[i](event); events[i](event);
} }
var handler = this["on" + event.type]; var handler = this["on" + event.type];
if (handler) handler(event); if (handler) handler.apply(this, [event]);
}; };
/** /**
...@@ -139,16 +165,22 @@ ...@@ -139,16 +165,22 @@
* @param {Object} flashEvent * @param {Object} flashEvent
*/ */
WebSocket.prototype.__handleEvent = function(flashEvent) { WebSocket.prototype.__handleEvent = function(flashEvent) {
if ("readyState" in flashEvent) { if ("readyState" in flashEvent) {
this.readyState = flashEvent.readyState; this.readyState = flashEvent.readyState;
} }
if ("protocol" in flashEvent) {
this.protocol = flashEvent.protocol;
}
var jsEvent; var jsEvent;
if (flashEvent.type == "open" || flashEvent.type == "error") { if (flashEvent.type == "open" || flashEvent.type == "error") {
jsEvent = this.__createSimpleEvent(flashEvent.type); jsEvent = this.__createSimpleEvent(flashEvent.type);
} else if (flashEvent.type == "close") { } else if (flashEvent.type == "close") {
// TODO implement jsEvent.wasClean
jsEvent = this.__createSimpleEvent("close"); jsEvent = this.__createSimpleEvent("close");
jsEvent.wasClean = flashEvent.wasClean ? true : false;
jsEvent.code = flashEvent.code;
jsEvent.reason = flashEvent.reason;
} else if (flashEvent.type == "message") { } else if (flashEvent.type == "message") {
var data = decodeURIComponent(flashEvent.message); var data = decodeURIComponent(flashEvent.message);
jsEvent = this.__createMessageEvent("message", data); jsEvent = this.__createMessageEvent("message", data);
...@@ -157,6 +189,7 @@ ...@@ -157,6 +189,7 @@
} }
this.dispatchEvent(jsEvent); this.dispatchEvent(jsEvent);
}; };
WebSocket.prototype.__createSimpleEvent = function(type) { WebSocket.prototype.__createSimpleEvent = function(type) {
...@@ -188,6 +221,9 @@ ...@@ -188,6 +221,9 @@
WebSocket.CLOSING = 2; WebSocket.CLOSING = 2;
WebSocket.CLOSED = 3; WebSocket.CLOSED = 3;
// Field to check implementation of WebSocket.
WebSocket.__isFlashImplementation = true;
WebSocket.__initialized = false;
WebSocket.__flash = null; WebSocket.__flash = null;
WebSocket.__instances = {}; WebSocket.__instances = {};
WebSocket.__tasks = []; WebSocket.__tasks = [];
...@@ -207,16 +243,31 @@ ...@@ -207,16 +243,31 @@
* Loads WebSocketMain.swf and creates WebSocketMain object in Flash. * Loads WebSocketMain.swf and creates WebSocketMain object in Flash.
*/ */
WebSocket.__initialize = function() { WebSocket.__initialize = function() {
if (WebSocket.__flash) return;
if (WebSocket.__initialized) return;
WebSocket.__initialized = true;
if (WebSocket.__swfLocation) { if (WebSocket.__swfLocation) {
// For backword compatibility. // For backword compatibility.
window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation; window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
} }
if (!window.WEB_SOCKET_SWF_LOCATION) { if (!window.WEB_SOCKET_SWF_LOCATION) {
console.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf"); logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
return; 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"); var container = document.createElement("div");
container.id = "webSocketContainer"; container.id = "webSocketContainer";
// Hides Flash box. We cannot use display: none or visibility: hidden because it prevents // Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
...@@ -250,9 +301,11 @@ ...@@ -250,9 +301,11 @@
null, null,
function(e) { function(e) {
if (!e.success) { if (!e.success) {
console.error("[WebSocket] swfobject.embedSWF failed"); logger.error("[WebSocket] swfobject.embedSWF failed");
} }
}); }
);
}; };
/** /**
...@@ -287,7 +340,7 @@ ...@@ -287,7 +340,7 @@
WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]); WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
} }
} catch (e) { } catch (e) {
console.error(e); logger.error(e);
} }
}, 0); }, 0);
return true; return true;
...@@ -295,12 +348,12 @@ ...@@ -295,12 +348,12 @@
// Called by Flash. // Called by Flash.
WebSocket.__log = function(message) { WebSocket.__log = function(message) {
console.log(decodeURIComponent(message)); logger.log(decodeURIComponent(message));
}; };
// Called by Flash. // Called by Flash.
WebSocket.__error = function(message) { WebSocket.__error = function(message) {
console.error(decodeURIComponent(message)); logger.error(decodeURIComponent(message));
}; };
WebSocket.__addTask = function(task) { WebSocket.__addTask = function(task) {
...@@ -327,15 +380,12 @@ ...@@ -327,15 +380,12 @@
}; };
if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) { if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) {
if (window.addEventListener) { // NOTE:
window.addEventListener("load", function(){ // This fires immediately if web_socket.js is dynamically loaded after
WebSocket.__initialize(); // the document is loaded.
}, false); swfobject.addDomLoadEvent(function() {
} else {
window.attachEvent("onload", function(){
WebSocket.__initialize(); WebSocket.__initialize();
}); });
} }
}
})(); })();
...@@ -128,9 +128,7 @@ function rQshiftStr(len) { ...@@ -128,9 +128,7 @@ function rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = rQlen(); } if (typeof(len) === 'undefined') { len = rQlen(); }
var arr = rQ.slice(rQi, rQi + len); var arr = rQ.slice(rQi, rQi + len);
rQi += len; rQi += len;
return arr.map(function (num) { return String.fromCharCode.apply(null, arr);
return String.fromCharCode(num); } ).join('');
} }
function rQshiftBytes(len) { function rQshiftBytes(len) {
if (typeof(len) === 'undefined') { len = rQlen(); } if (typeof(len) === 'undefined') { len = rQlen(); }
...@@ -313,11 +311,19 @@ function init(protocols) { ...@@ -313,11 +311,19 @@ function init(protocols) {
throw("WebSocket binary sub-protocol requested but not supported"); throw("WebSocket binary sub-protocol requested but not supported");
} }
if (typeof(protocols) === "object") { if (typeof(protocols) === "object") {
var new_protocols = [];
for (var i = 0; i < protocols.length; i++) { for (var i = 0; i < protocols.length; i++) {
if (protocols[i] === 'binary') { if (protocols[i] === 'binary') {
throw("WebSocket binary sub-protocol requested but not supported"); Util.Error("Skipping unsupported WebSocket binary sub-protocol");
} else {
new_protocols.push(protocols[i]);
} }
} }
if (new_protocols.length > 0) {
protocols = new_protocols;
} else {
throw("Only WebSocket binary sub-protocol was requested and not supported.");
}
} }
} }
......
This diff is collapsed.
This diff is collapsed.
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