From ca98d07b4abe8f37a0d8eb51bd4a4043cbc2c3d1 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 10 Dec 2017 16:56:19 -0800 Subject: [PATCH] GUACAMOLE-567: Rely on HTTP or WebSocket status code to determine error if Guacamole-specific reason is missing. Default to server unreachable. --- .../src/main/webapp/modules/Tunnel.js | 84 +++++++------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/guacamole-common-js/src/main/webapp/modules/Tunnel.js b/guacamole-common-js/src/main/webapp/modules/Tunnel.js index c8f8502b5..63e27c52b 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)); + } - 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)); + + // 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(); };