2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacamole-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
42 * Core object providing abstract communication for Guacamole. This object
43 * is a null implementation whose functions do nothing. Guacamole applications
44 * should use {@link Guacamole.HTTPTunnel} instead, or implement their own tunnel based
48 * @see Guacamole.HTTPTunnel
50 Guacamole.Tunnel = function() {
53 * Connect to the tunnel with the given optional data. This data is
54 * typically used for authentication. The format of data accepted is
55 * up to the tunnel implementation.
57 * @param {String} data The data to send to the tunnel when connecting.
59 this.connect = function(data) {};
62 * Disconnect from the tunnel.
64 this.disconnect = function() {};
67 * Send the given message through the tunnel to the service on the other
68 * side. All messages are guaranteed to be received in the order sent.
70 * @param {...} elements The elements of the message to send to the
71 * service on the other side of the tunnel.
73 this.sendMessage = function(elements) {};
76 * Fired whenever an error is encountered by the tunnel.
79 * @param {String} message A human-readable description of the error that
85 * Fired once for every complete Guacamole instruction received, in order.
88 * @param {String} opcode The Guacamole instruction opcode.
89 * @param {Array} parameters The parameters provided for the instruction,
92 this.oninstruction = null;
97 * Guacamole Tunnel implemented over HTTP via XMLHttpRequest.
100 * @augments Guacamole.Tunnel
101 * @param {String} tunnelURL The URL of the HTTP tunneling service.
103 Guacamole.HTTPTunnel = function(tunnelURL) {
106 * Reference to this HTTP tunnel.
112 var TUNNEL_CONNECT = tunnelURL + "?connect";
113 var TUNNEL_READ = tunnelURL + "?read:";
114 var TUNNEL_WRITE = tunnelURL + "?write:";
117 var STATE_CONNECTED = 1;
118 var STATE_DISCONNECTED = 2;
120 var currentState = STATE_IDLE;
122 var POLLING_ENABLED = 1;
123 var POLLING_DISABLED = 0;
125 // Default to polling - will be turned off automatically if not needed
126 var pollingMode = POLLING_ENABLED;
128 var sendingMessages = false;
129 var outputMessageBuffer = "";
131 this.sendMessage = function() {
133 // Do not attempt to send messages if not connected
134 if (currentState != STATE_CONNECTED)
137 // Do not attempt to send empty messages
138 if (arguments.length == 0)
142 * Converts the given value to a length/string pair for use as an
143 * element in a Guacamole instruction.
145 * @param value The value to convert.
146 * @return {String} The converted value.
148 function getElement(value) {
149 var string = new String(value);
150 return string.length + "." + string;
153 // Initialized message with first element
154 var message = getElement(arguments[0]);
156 // Append remaining elements
157 for (var i=1; i<arguments.length; i++)
158 message += "," + getElement(arguments[i]);
163 // Add message to buffer
164 outputMessageBuffer += message;
166 // Send if not currently sending
167 if (!sendingMessages)
168 sendPendingMessages();
172 function sendPendingMessages() {
174 if (outputMessageBuffer.length > 0) {
176 sendingMessages = true;
178 var message_xmlhttprequest = new XMLHttpRequest();
179 message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel_uuid);
180 message_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
182 // Once response received, send next queued event.
183 message_xmlhttprequest.onreadystatechange = function() {
184 if (message_xmlhttprequest.readyState == 4)
185 sendPendingMessages();
188 message_xmlhttprequest.send(outputMessageBuffer);
189 outputMessageBuffer = ""; // Clear buffer
193 sendingMessages = false;
198 function handleResponse(xmlhttprequest) {
201 var nextRequest = null;
203 var dataUpdateEvents = 0;
205 // The location of the last element's terminator
208 // Where to start the next length search or the next element
212 var elements = new Array();
214 function parseResponse() {
216 // Do not handle responses if not connected
217 if (currentState != STATE_CONNECTED) {
219 // Clean up interval if polling
220 if (interval != null)
221 clearInterval(interval);
226 // Do not parse response yet if not ready
227 if (xmlhttprequest.readyState < 2) return;
229 // Attempt to read status
231 try { status = xmlhttprequest.status; }
233 // If status could not be read, assume successful.
234 catch (e) { status = 200; }
236 // Start next request as soon as possible IF request was successful
237 if (nextRequest == null && status == 200)
238 nextRequest = makeRequest();
240 // Parse stream when data is received and when complete.
241 if (xmlhttprequest.readyState == 3 ||
242 xmlhttprequest.readyState == 4) {
244 // Also poll every 30ms (some browsers don't repeatedly call onreadystatechange for new data)
245 if (pollingMode == POLLING_ENABLED) {
246 if (xmlhttprequest.readyState == 3 && interval == null)
247 interval = setInterval(parseResponse, 30);
248 else if (xmlhttprequest.readyState == 4 && interval != null)
249 clearInterval(interval);
252 // If canceled, stop transfer
253 if (xmlhttprequest.status == 0) {
258 // Halt on error during request
259 else if (xmlhttprequest.status != 200) {
261 // Get error message (if any)
262 var message = xmlhttprequest.getResponseHeader("X-Guacamole-Error-Message");
264 message = "Internal server error";
266 // Call error handler
267 if (tunnel.onerror) tunnel.onerror(message);
274 // Attempt to read in-progress data
276 try { current = xmlhttprequest.responseText; }
278 // Do not attempt to parse if data could not be read
279 catch (e) { return; }
281 // While search is within currently received data
282 while (elementEnd < current.length) {
284 // If we are waiting for element data
285 if (elementEnd >= startIndex) {
287 // We now have enough data for the element. Parse.
288 var element = current.substring(startIndex, elementEnd);
289 var terminator = current.substring(elementEnd, elementEnd+1);
291 // Add element to array
292 elements.push(element);
294 // If last element, handle instruction
295 if (terminator == ";") {
298 var opcode = elements.shift();
300 // Call instruction handler.
301 if (tunnel.oninstruction != null)
302 tunnel.oninstruction(opcode, elements);
309 // Start searching for length at character after
310 // element terminator
311 startIndex = elementEnd + 1;
315 // Search for end of length
316 var lengthEnd = current.indexOf(".", startIndex);
317 if (lengthEnd != -1) {
320 var length = parseInt(current.substring(elementEnd+1, lengthEnd));
322 // If we're done parsing, handle the next response.
325 // Clean up interval if polling
326 if (interval != null)
327 clearInterval(interval);
330 xmlhttprequest.onreadystatechange = null;
331 xmlhttprequest.abort();
333 // Start handling next request
335 handleResponse(nextRequest);
342 // Calculate start of element
343 startIndex = lengthEnd + 1;
345 // Calculate location of element terminator
346 elementEnd = startIndex + length;
350 // If no period yet, continue search when more data
353 startIndex = current.length;
363 // If response polling enabled, attempt to detect if still
364 // necessary (via wrapping parseResponse())
365 if (pollingMode == POLLING_ENABLED) {
366 xmlhttprequest.onreadystatechange = function() {
368 // If we receive two or more readyState==3 events,
369 // there is no need to poll.
370 if (xmlhttprequest.readyState == 3) {
372 if (dataUpdateEvents >= 2) {
373 pollingMode = POLLING_DISABLED;
374 xmlhttprequest.onreadystatechange = parseResponse;
382 // Otherwise, just parse
384 xmlhttprequest.onreadystatechange = parseResponse;
391 function makeRequest() {
394 var xmlhttprequest = new XMLHttpRequest();
395 xmlhttprequest.open("POST", TUNNEL_READ + tunnel_uuid);
396 xmlhttprequest.send(null);
398 return xmlhttprequest;
402 this.connect = function(data) {
404 // Start tunnel and connect synchronously
405 var connect_xmlhttprequest = new XMLHttpRequest();
406 connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, false);
407 connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
408 connect_xmlhttprequest.send(data);
410 // If failure, throw error
411 if (connect_xmlhttprequest.status != 200) {
413 var message = connect_xmlhttprequest.getResponseHeader("X-Guacamole-Error-Message");
415 message = "Internal error";
417 throw new Error(message);
421 // Get UUID from response
422 tunnel_uuid = connect_xmlhttprequest.responseText;
424 // Start reading data
425 currentState = STATE_CONNECTED;
426 handleResponse(makeRequest());
430 this.disconnect = function() {
431 currentState = STATE_DISCONNECTED;
436 Guacamole.HTTPTunnel.prototype = new Guacamole.Tunnel();
440 * Guacamole Tunnel implemented over WebSocket via XMLHttpRequest.
443 * @augments Guacamole.Tunnel
444 * @param {String} tunnelURL The URL of the WebSocket tunneling service.
446 Guacamole.WebSocketTunnel = function(tunnelURL) {
449 * Reference to this WebSocket tunnel.
454 * The WebSocket used by this tunnel.
459 * The WebSocket protocol corresponding to the protocol used for the current
468 1000: "Connection closed normally.",
469 1001: "Connection shut down.",
470 1002: "Protocol error.",
471 1003: "Invalid data.",
472 1004: "[UNKNOWN, RESERVED]",
473 1005: "No status code present.",
474 1006: "Connection closed abnormally.",
475 1007: "Inconsistent data type.",
476 1008: "Policy violation.",
477 1009: "Message too large.",
478 1010: "Extension negotiation failed."
482 var STATE_CONNECTED = 1;
483 var STATE_DISCONNECTED = 2;
485 var currentState = STATE_IDLE;
487 // Transform current URL to WebSocket URL
489 // If not already a websocket URL
490 if ( tunnelURL.substring(0, 3) != "ws:"
491 && tunnelURL.substring(0, 4) != "wss:") {
493 var protocol = ws_protocol[window.location.protocol];
495 // If absolute URL, convert to absolute WS URL
496 if (tunnelURL.substring(0, 1) == "/")
499 + "//" + window.location.host
502 // Otherwise, construct absolute from relative URL
505 // Get path from pathname
506 var slash = window.location.pathname.lastIndexOf("/");
507 var path = window.location.pathname.substring(0, slash + 1);
509 // Construct absolute URL
512 + "//" + window.location.host
520 this.sendMessage = function(elements) {
522 // Do not attempt to send messages if not connected
523 if (currentState != STATE_CONNECTED)
526 // Do not attempt to send empty messages
527 if (arguments.length == 0)
531 * Converts the given value to a length/string pair for use as an
532 * element in a Guacamole instruction.
534 * @param value The value to convert.
535 * @return {String} The converted value.
537 function getElement(value) {
538 var string = new String(value);
539 return string.length + "." + string;
542 // Initialized message with first element
543 var message = getElement(arguments[0]);
545 // Append remaining elements
546 for (var i=1; i<arguments.length; i++)
547 message += "," + getElement(arguments[i]);
552 socket.send(message);
556 this.connect = function(data) {
559 socket = new WebSocket(tunnelURL + "?" + data, "guacamole");
561 socket.onopen = function(event) {
562 currentState = STATE_CONNECTED;
565 socket.onclose = function(event) {
567 // If connection closed abnormally, signal error.
568 if (event.code != 1000 && tunnel.onerror)
569 tunnel.onerror(status_code[event.code]);
573 socket.onerror = function(event) {
575 // Call error handler
576 if (tunnel.onerror) tunnel.onerror(event.data);
580 socket.onmessage = function(event) {
582 var message = event.data;
590 // Search for end of length
591 var lengthEnd = message.indexOf(".", startIndex);
592 if (lengthEnd != -1) {
595 var length = parseInt(message.substring(elementEnd+1, lengthEnd));
597 // Calculate start of element
598 startIndex = lengthEnd + 1;
600 // Calculate location of element terminator
601 elementEnd = startIndex + length;
605 // If no period, incomplete instruction.
607 throw new Error("Incomplete instruction.");
609 // We now have enough data for the element. Parse.
610 var element = message.substring(startIndex, elementEnd);
611 var terminator = message.substring(elementEnd, elementEnd+1);
613 // Add element to array
614 elements.push(element);
616 // If last element, handle instruction
617 if (terminator == ";") {
620 var opcode = elements.shift();
622 // Call instruction handler.
623 if (tunnel.oninstruction != null)
624 tunnel.oninstruction(opcode, elements);
631 // Start searching for length at character after
632 // element terminator
633 startIndex = elementEnd + 1;
635 } while (startIndex < message.length);
641 this.disconnect = function() {
642 currentState = STATE_DISCONNECTED;
648 Guacamole.WebSocketTunnel.prototype = new Guacamole.Tunnel();
652 * Guacamole Tunnel which cycles between all specified tunnels until
653 * no tunnels are left. Another tunnel is used if an error occurs but
654 * no instructions have been received. If an instruction has been
655 * received, or no tunnels remain, the error is passed directly out
656 * through the onerror handler (if defined).
659 * @augments Guacamole.Tunnel
660 * @param {...} tunnel_chain The tunnels to use, in order of priority.
662 Guacamole.ChainedTunnel = function(tunnel_chain) {
666 var chained_tunnel = this;
669 * The currently wrapped tunnel, if any.
671 var current_tunnel = null;
674 * Data passed in via connect(), to be used for
675 * wrapped calls to other tunnels' connect() functions.
680 * Array of all tunnels passed to this ChainedTunnel through the
681 * constructor arguments.
685 // Load all tunnels into array
686 for (var i=0; i<arguments.length; i++)
687 tunnels.push(arguments[i]);
690 * Sets the current tunnel
692 function attach(tunnel) {
694 // Set own functions to tunnel's functions
695 chained_tunnel.disconnect = tunnel.disconnect;
696 chained_tunnel.sendMessage = tunnel.sendMessage;
698 // Record current tunnel
699 current_tunnel = tunnel;
701 // Wrap own oninstruction within current tunnel
702 current_tunnel.oninstruction = function(opcode, elements) {
705 chained_tunnel.oninstruction(opcode, elements);
707 // Use handler permanently from now on
708 current_tunnel.oninstruction = chained_tunnel.oninstruction;
710 // Pass through errors (without trying other tunnels)
711 current_tunnel.onerror = chained_tunnel.onerror;
715 // Attach next tunnel on error
716 current_tunnel.onerror = function(message) {
719 var next_tunnel = tunnels.shift();
721 // If there IS a next tunnel, try using it.
725 // Otherwise, call error handler
726 else if (chained_tunnel.onerror)
727 chained_tunnel.onerror(message);
731 // Attempt connection
732 current_tunnel.connect(connect_data);
736 this.connect = function(data) {
738 // Remember connect data
742 var next_tunnel = tunnels.shift();
744 // Attach first tunnel
748 // If there IS no first tunnel, error
749 else if (chained_tunnel.onerror)
750 chained_tunnel.onerror("No tunnels to try.");
756 Guacamole.ChainedTunnel.prototype = new Guacamole.Tunnel();