You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@guacamole.apache.org by vn...@apache.org on 2018/05/28 03:07:54 UTC

[2/6] guacamole-client git commit: GUACAMOLE-567: Rely on HTTP or WebSocket status code to determine error if Guacamole-specific reason is missing. Default to server unreachable.

GUACAMOLE-567: Rely on HTTP or WebSocket status code to determine error if Guacamole-specific reason is missing. Default to server unreachable.


Project: http://git-wip-us.apache.org/repos/asf/guacamole-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/guacamole-client/commit/ca98d07b
Tree: http://git-wip-us.apache.org/repos/asf/guacamole-client/tree/ca98d07b
Diff: http://git-wip-us.apache.org/repos/asf/guacamole-client/diff/ca98d07b

Branch: refs/heads/master
Commit: ca98d07b4abe8f37a0d8eb51bd4a4043cbc2c3d1
Parents: a1e59b9
Author: Michael Jumper <mj...@apache.org>
Authored: Sun Dec 10 16:56:19 2017 -0800
Committer: Michael Jumper <mj...@apache.org>
Committed: Sun May 27 12:29:10 2018 -0700

----------------------------------------------------------------------
 .../src/main/webapp/modules/Tunnel.js           | 84 ++++++++------------
 1 file changed, 31 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/ca98d07b/guacamole-common-js/src/main/webapp/modules/Tunnel.js
----------------------------------------------------------------------
diff --git a/guacamole-common-js/src/main/webapp/modules/Tunnel.js b/guacamole-common-js/src/main/webapp/modules/Tunnel.js
index c8f8502..63e27c5 100644
--- a/guacamole-common-js/src/main/webapp/modules/Tunnel.js
+++ b/guacamole-common-js/src/main/webapp/modules/Tunnel.js
@@ -382,10 +382,23 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) {
 
     function handleHTTPTunnelError(xmlhttprequest) {
 
+        // Pull status code directly from headers provided by Guacamole
         var code = parseInt(xmlhttprequest.getResponseHeader("Guacamole-Status-Code"));
-        var message = xmlhttprequest.getResponseHeader("Guacamole-Error-Message");
+        if (code) {
+            var message = xmlhttprequest.getResponseHeader("Guacamole-Error-Message");
+            close_tunnel(new Guacamole.Status(code, message));
+        }
+
+        // Failing that, derive a Guacamole status code from the HTTP status
+        // code provided by the browser
+        else if (xmlhttprequest.status)
+            close_tunnel(new Guacamole.Status(
+                Guacamole.Status.Code.fromHTTPCode(xmlhttprequest.status),
+                    xmlhttprequest.statusText));
 
-        close_tunnel(new Guacamole.Status(code, message));
+        // Otherwise, assume server is unreachable
+        else
+            close_tunnel(new Guacamole.Status(Guacamole.Status.Code.UPSTREAM_NOT_FOUND));
 
     }
 
@@ -808,13 +821,22 @@ Guacamole.WebSocketTunnel = function(tunnelURL) {
         };
 
         socket.onclose = function(event) {
-            close_tunnel(new Guacamole.Status(parseInt(event.reason), event.reason));
+
+            // Pull status code directly from closure reason provided by Guacamole
+            if (event.reason)
+                close_tunnel(new Guacamole.Status(parseInt(event.reason), event.reason));
+
+            // Failing that, derive a Guacamole status code from the WebSocket
+            // status code provided by the browser
+            else if (event.code)
+                close_tunnel(new Guacamole.Status(Guacamole.Status.Code.fromWebSocketCode(event.code)));
+
+            // Otherwise, assume server is unreachable
+            else
+                close_tunnel(new Guacamole.Status(Guacamole.Status.Code.UPSTREAM_NOT_FOUND));
+
         };
         
-        socket.onerror = function(event) {
-            close_tunnel(new Guacamole.Status(Guacamole.Status.Code.SERVER_ERROR, event.data));
-        };
-
         socket.onmessage = function(event) {
 
             reset_timeout();
@@ -1141,51 +1163,6 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain, extraTu
         }
     }
 
-    /**
-     * Returns the Guacamole protocol status code which most closely
-     * represents the given HTTP status code.
-     *
-     * @private
-     * @param {Number} httpStatus
-     *     The HTTP status code to translate into a Guacamole protocol status
-     *     code.
-     *
-     * @returns {Number}
-     *     The Guacamole protocol status code which most closely represents the
-     *     given HTTP status code.
-     */
-    var getGuacamoleStatusCode = function getGuacamoleStatusCode(httpStatus) {
-
-        // Translate status codes with known equivalents
-        switch (httpStatus) {
-
-            // HTTP 400 - Bad request
-            case 400:
-                return Guacamole.Status.Code.CLIENT_BAD_REQUEST;
-
-            // HTTP 403 - Forbidden
-            case 403:
-                return Guacamole.Status.Code.CLIENT_FORBIDDEN;
-
-            // HTTP 404 - Resource not found
-            case 404:
-                return Guacamole.Status.Code.RESOURCE_NOT_FOUND;
-
-            // HTTP 429 - Too many requests
-            case 429:
-                return Guacamole.Status.Code.CLIENT_TOO_MANY;
-
-            // HTTP 503 - Server unavailable
-            case 503:
-                return Guacamole.Status.Code.SERVER_BUSY;
-
-        }
-
-        // Default all other codes to generic internal error
-        return Guacamole.Status.Code.SERVER_ERROR;
-
-    };
-
     this.sendMessage = function sendMessage(elements) {
         // Do nothing
     };
@@ -1248,7 +1225,8 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain, extraTu
 
             // Fail if file could not be downloaded via HTTP
             if (tunnel.onerror)
-                tunnel.onerror(new Guacamole.Status(getGuacamoleStatusCode(xhr.status), xhr.statusText));
+                tunnel.onerror(new Guacamole.Status(
+                    Guacamole.Status.Code.fromHTTPCode(xhr.status), xhr.statusText));
 
             tunnel.disconnect();
         };