diff --git a/guacamole-common-js/src/main/webapp/modules/Status.js b/guacamole-common-js/src/main/webapp/modules/Status.js index ceadaa68c..3acfd63c8 100644 --- a/guacamole-common-js/src/main/webapp/modules/Status.js +++ b/guacamole-common-js/src/main/webapp/modules/Status.js @@ -232,3 +232,87 @@ Guacamole.Status.Code = { "CLIENT_TOO_MANY": 0x031D }; + +/** + * Returns the Guacamole protocol status code which most closely + * represents the given HTTP status code. + * + * @param {Number} status + * 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. + */ +Guacamole.Status.Code.fromHTTPCode = function fromHTTPCode(status) { + + // Translate status codes with known equivalents + switch (status) { + + // 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; + +}; + +/** + * Returns the Guacamole protocol status code which most closely + * represents the given WebSocket status code. + * + * @param {Number} code + * The WebSocket status code to translate into a Guacamole protocol + * status code. + * + * @returns {Number} + * The Guacamole protocol status code which most closely represents the + * given WebSocket status code. + */ +Guacamole.Status.Code.fromWebSocketCode = function fromWebSocketCode(code) { + + // Translate status codes with known equivalents + switch (code) { + + // Successful disconnect (no error) + case 1000: // Normal Closure + return Guacamole.Status.Code.SUCCESS; + + // Codes which indicate the server is not reachable + case 1006: // Abnormal Closure (also signalled by JavaScript when the connection cannot be opened in the first place) + case 1015: // TLS Handshake + return Guacamole.Status.Code.UPSTREAM_NOT_FOUND; + + // Codes which indicate the server is reachable but busy/unavailable + case 1001: // Going Away + case 1012: // Service Restart + case 1013: // Try Again Later + case 1014: // Bad Gateway + return Guacamole.Status.Code.UPSTREAM_UNAVAILABLE; + + } + + // Default all other codes to generic internal error + return Guacamole.Status.Code.SERVER_ERROR; + +};