Source: Status.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * A Guacamole status. Each Guacamole status consists of a status code, defined
  22. * by the protocol, and an optional human-readable message, usually only
  23. * included for debugging convenience.
  24. *
  25. * @constructor
  26. * @param {Number} code
  27. * The Guacamole status code, as defined by Guacamole.Status.Code.
  28. *
  29. * @param {String} [message]
  30. * An optional human-readable message.
  31. */
  32. Guacamole.Status = function(code, message) {
  33. /**
  34. * Reference to this Guacamole.Status.
  35. * @private
  36. */
  37. var guac_status = this;
  38. /**
  39. * The Guacamole status code.
  40. * @see Guacamole.Status.Code
  41. * @type {Number}
  42. */
  43. this.code = code;
  44. /**
  45. * An arbitrary human-readable message associated with this status, if any.
  46. * The human-readable message is not required, and is generally provided
  47. * for debugging purposes only. For user feedback, it is better to translate
  48. * the Guacamole status code into a message.
  49. *
  50. * @type {String}
  51. */
  52. this.message = message;
  53. /**
  54. * Returns whether this status represents an error.
  55. * @returns {Boolean} true if this status represents an error, false
  56. * otherwise.
  57. */
  58. this.isError = function() {
  59. return guac_status.code < 0 || guac_status.code > 0x00FF;
  60. };
  61. };
  62. /**
  63. * Enumeration of all Guacamole status codes.
  64. */
  65. Guacamole.Status.Code = {
  66. /**
  67. * The operation succeeded.
  68. *
  69. * @type {Number}
  70. */
  71. "SUCCESS": 0x0000,
  72. /**
  73. * The requested operation is unsupported.
  74. *
  75. * @type {Number}
  76. */
  77. "UNSUPPORTED": 0x0100,
  78. /**
  79. * The operation could not be performed due to an internal failure.
  80. *
  81. * @type {Number}
  82. */
  83. "SERVER_ERROR": 0x0200,
  84. /**
  85. * The operation could not be performed as the server is busy.
  86. *
  87. * @type {Number}
  88. */
  89. "SERVER_BUSY": 0x0201,
  90. /**
  91. * The operation could not be performed because the upstream server is not
  92. * responding.
  93. *
  94. * @type {Number}
  95. */
  96. "UPSTREAM_TIMEOUT": 0x0202,
  97. /**
  98. * The operation was unsuccessful due to an error or otherwise unexpected
  99. * condition of the upstream server.
  100. *
  101. * @type {Number}
  102. */
  103. "UPSTREAM_ERROR": 0x0203,
  104. /**
  105. * The operation could not be performed as the requested resource does not
  106. * exist.
  107. *
  108. * @type {Number}
  109. */
  110. "RESOURCE_NOT_FOUND": 0x0204,
  111. /**
  112. * The operation could not be performed as the requested resource is
  113. * already in use.
  114. *
  115. * @type {Number}
  116. */
  117. "RESOURCE_CONFLICT": 0x0205,
  118. /**
  119. * The operation could not be performed as the requested resource is now
  120. * closed.
  121. *
  122. * @type {Number}
  123. */
  124. "RESOURCE_CLOSED": 0x0206,
  125. /**
  126. * The operation could not be performed because the upstream server does
  127. * not appear to exist.
  128. *
  129. * @type {Number}
  130. */
  131. "UPSTREAM_NOT_FOUND": 0x0207,
  132. /**
  133. * The operation could not be performed because the upstream server is not
  134. * available to service the request.
  135. *
  136. * @type {Number}
  137. */
  138. "UPSTREAM_UNAVAILABLE": 0x0208,
  139. /**
  140. * The session within the upstream server has ended because it conflicted
  141. * with another session.
  142. *
  143. * @type {Number}
  144. */
  145. "SESSION_CONFLICT": 0x0209,
  146. /**
  147. * The session within the upstream server has ended because it appeared to
  148. * be inactive.
  149. *
  150. * @type {Number}
  151. */
  152. "SESSION_TIMEOUT": 0x020A,
  153. /**
  154. * The session within the upstream server has been forcibly terminated.
  155. *
  156. * @type {Number}
  157. */
  158. "SESSION_CLOSED": 0x020B,
  159. /**
  160. * The operation could not be performed because bad parameters were given.
  161. *
  162. * @type {Number}
  163. */
  164. "CLIENT_BAD_REQUEST": 0x0300,
  165. /**
  166. * Permission was denied to perform the operation, as the user is not yet
  167. * authorized (not yet logged in, for example).
  168. *
  169. * @type {Number}
  170. */
  171. "CLIENT_UNAUTHORIZED": 0x0301,
  172. /**
  173. * Permission was denied to perform the operation, and this permission will
  174. * not be granted even if the user is authorized.
  175. *
  176. * @type {Number}
  177. */
  178. "CLIENT_FORBIDDEN": 0x0303,
  179. /**
  180. * The client took too long to respond.
  181. *
  182. * @type {Number}
  183. */
  184. "CLIENT_TIMEOUT": 0x0308,
  185. /**
  186. * The client sent too much data.
  187. *
  188. * @type {Number}
  189. */
  190. "CLIENT_OVERRUN": 0x030D,
  191. /**
  192. * The client sent data of an unsupported or unexpected type.
  193. *
  194. * @type {Number}
  195. */
  196. "CLIENT_BAD_TYPE": 0x030F,
  197. /**
  198. * The operation failed because the current client is already using too
  199. * many resources.
  200. *
  201. * @type {Number}
  202. */
  203. "CLIENT_TOO_MANY": 0x031D
  204. };
  205. /**
  206. * Returns the Guacamole protocol status code which most closely
  207. * represents the given HTTP status code.
  208. *
  209. * @param {Number} status
  210. * The HTTP status code to translate into a Guacamole protocol status
  211. * code.
  212. *
  213. * @returns {Number}
  214. * The Guacamole protocol status code which most closely represents the
  215. * given HTTP status code.
  216. */
  217. Guacamole.Status.Code.fromHTTPCode = function fromHTTPCode(status) {
  218. // Translate status codes with known equivalents
  219. switch (status) {
  220. // HTTP 400 - Bad request
  221. case 400:
  222. return Guacamole.Status.Code.CLIENT_BAD_REQUEST;
  223. // HTTP 403 - Forbidden
  224. case 403:
  225. return Guacamole.Status.Code.CLIENT_FORBIDDEN;
  226. // HTTP 404 - Resource not found
  227. case 404:
  228. return Guacamole.Status.Code.RESOURCE_NOT_FOUND;
  229. // HTTP 429 - Too many requests
  230. case 429:
  231. return Guacamole.Status.Code.CLIENT_TOO_MANY;
  232. // HTTP 503 - Server unavailable
  233. case 503:
  234. return Guacamole.Status.Code.SERVER_BUSY;
  235. }
  236. // Default all other codes to generic internal error
  237. return Guacamole.Status.Code.SERVER_ERROR;
  238. };
  239. /**
  240. * Returns the Guacamole protocol status code which most closely
  241. * represents the given WebSocket status code.
  242. *
  243. * @param {Number} code
  244. * The WebSocket status code to translate into a Guacamole protocol
  245. * status code.
  246. *
  247. * @returns {Number}
  248. * The Guacamole protocol status code which most closely represents the
  249. * given WebSocket status code.
  250. */
  251. Guacamole.Status.Code.fromWebSocketCode = function fromWebSocketCode(code) {
  252. // Translate status codes with known equivalents
  253. switch (code) {
  254. // Successful disconnect (no error)
  255. case 1000: // Normal Closure
  256. return Guacamole.Status.Code.SUCCESS;
  257. // Codes which indicate the server is not reachable
  258. case 1006: // Abnormal Closure (also signalled by JavaScript when the connection cannot be opened in the first place)
  259. case 1015: // TLS Handshake
  260. return Guacamole.Status.Code.UPSTREAM_NOT_FOUND;
  261. // Codes which indicate the server is reachable but busy/unavailable
  262. case 1001: // Going Away
  263. case 1012: // Service Restart
  264. case 1013: // Try Again Later
  265. case 1014: // Bad Gateway
  266. return Guacamole.Status.Code.UPSTREAM_UNAVAILABLE;
  267. }
  268. // Default all other codes to generic internal error
  269. return Guacamole.Status.Code.SERVER_ERROR;
  270. };