GUACAMOLE-437: Merge support for including custom headers within HTTP tunnel requests.

This commit is contained in:
Michael Jumper
2017-11-19 14:25:17 -08:00

View File

@@ -182,8 +182,13 @@ Guacamole.Tunnel.State = {
* Whether tunnel requests will be cross-domain, and thus must use CORS * Whether tunnel requests will be cross-domain, and thus must use CORS
* mechanisms and headers. By default, it is assumed that tunnel requests * mechanisms and headers. By default, it is assumed that tunnel requests
* will be made to the same domain. * 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. * Reference to this HTTP tunnel.
@@ -214,6 +219,29 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) {
*/ */
var receive_timeout = null; 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 * Initiates a timeout which, if data is not received, causes the tunnel
* to close with an error. * to close with an error.
@@ -322,6 +350,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) {
var message_xmlhttprequest = new XMLHttpRequest(); var message_xmlhttprequest = new XMLHttpRequest();
message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel.uuid); message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel.uuid);
message_xmlhttprequest.withCredentials = withCredentials; message_xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(message_xmlhttprequest);
message_xmlhttprequest.setRequestHeader("Content-type", "application/octet-stream"); message_xmlhttprequest.setRequestHeader("Content-type", "application/octet-stream");
// Once response received, send next queued event. // Once response received, send next queued event.
@@ -553,6 +582,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) {
var xmlhttprequest = new XMLHttpRequest(); var xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.open("GET", TUNNEL_READ + tunnel.uuid + ":" + (request_id++)); xmlhttprequest.open("GET", TUNNEL_READ + tunnel.uuid + ":" + (request_id++));
xmlhttprequest.withCredentials = withCredentials; xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(xmlhttprequest);
xmlhttprequest.send(null); xmlhttprequest.send(null);
return xmlhttprequest; return xmlhttprequest;
@@ -595,6 +625,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain) {
connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, true); connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, true);
connect_xmlhttprequest.withCredentials = withCredentials; connect_xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(connect_xmlhttprequest);
connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
connect_xmlhttprequest.send(data); connect_xmlhttprequest.send(data);
@@ -1137,6 +1168,7 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain) {
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open('GET', url); xhr.open('GET', url);
xhr.withCredentials = !!crossDomain; xhr.withCredentials = !!crossDomain;
addExtraHeaders(xhr);
xhr.responseType = 'text'; xhr.responseType = 'text';
xhr.send(null); xhr.send(null);