Repository: incubator-guacamole-client Updated Branches: refs/heads/master e2c8594b3 -> ff2bd3d4f
GUACAMOLE-437: Custom headers for Guacamole tunnel Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/commit/948b4e80 Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/tree/948b4e80 Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/diff/948b4e80 Branch: refs/heads/master Commit: 948b4e804cd58bcaf1f2f01914ef1f90950f43e6 Parents: e2c8594 Author: Ignasi Barrera <n...@apache.org> Authored: Thu Nov 16 13:03:17 2017 +0100 Committer: Ignasi Barrera <n...@apache.org> Committed: Sun Nov 19 03:45:39 2017 +0100 ---------------------------------------------------------------------- .../src/main/webapp/modules/Tunnel.js | 34 +++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/948b4e80/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 2b1826d..a4e399d 100644 --- a/guacamole-common-js/src/main/webapp/modules/Tunnel.js +++ b/guacamole-common-js/src/main/webapp/modules/Tunnel.js @@ -182,8 +182,13 @@ Guacamole.Tunnel.State = { * Whether tunnel requests will be cross-domain, and thus must use CORS * mechanisms and headers. By default, it is assumed that tunnel requests * will be made to the same domain. + * + * @param {Object} [extraTunnelHeaders={}] + * Key value pairs containing the header names and values of any additional + * headers to be sent in tunnel requests. By default, no extra headers will + * be added. */ -Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) { +Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) { /** * Reference to this HTTP tunnel. @@ -215,6 +220,29 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) { var receive_timeout = null; /** + * Additional headers to be sent in tunnel requests. This dictionary can be + * populated with key/value header pairs to pass information such as authentication + * tokens, etc. + * + * @private + */ + var extraHeaders = extraTunnelHeaders ||Â {} + + /** + * Adds the configured additional headers to the given request. + * + * @params {XMLHttpRequest} request + * The request where the configured extra headers will be added. + * + * @private + */ + function addExtraHeaders(request) { + for (var name in tunnel.extraHeaders) { + request.setRequestHeader(name, tunnel.extraHeaders[name]); + } + } + + /** * Initiates a timeout which, if data is not received, causes the tunnel * to close with an error. * @@ -322,6 +350,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) { var message_xmlhttprequest = new XMLHttpRequest(); message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel.uuid); message_xmlhttprequest.withCredentials = withCredentials; + addExtraHeaders(message_xmlhttprequest); message_xmlhttprequest.setRequestHeader("Content-type", "application/octet-stream"); // Once response received, send next queued event. @@ -553,6 +582,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) { var xmlhttprequest = new XMLHttpRequest(); xmlhttprequest.open("GET", TUNNEL_READ + tunnel.uuid + ":" + (request_id++)); xmlhttprequest.withCredentials = withCredentials; + addExtraHeaders(xmlhttprequest); xmlhttprequest.send(null); return xmlhttprequest; @@ -595,6 +625,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) { connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, true); connect_xmlhttprequest.withCredentials = withCredentials; + addExtraHeaders(connect_xmlhttprequest); connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); connect_xmlhttprequest.send(data); @@ -1137,6 +1168,7 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain) { xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.withCredentials = !!crossDomain; + addExtraHeaders(xhr); xhr.responseType = 'text'; xhr.send(null);