GUACAMOLE-437: Merge fix extraHeaders scope and StaticHTTPTunnel

This commit is contained in:
Nick Couchman
2017-11-20 22:58:07 -05:00

View File

@@ -226,19 +226,22 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) {
*
* @private
*/
var extraHeaders = extraTunnelHeaders || {}
var extraHeaders = extraTunnelHeaders || {};
/**
* Adds the configured additional headers to the given request.
*
* @params {XMLHttpRequest} request
* @param {XMLHttpRequest} request
* The request where the configured extra headers will be added.
*
* @param {Object} headers
* The headers to be added to the request.
*
* @private
*/
function addExtraHeaders(request) {
for (var name in tunnel.extraHeaders) {
request.setRequestHeader(name, tunnel.extraHeaders[name]);
function addExtraHeaders(request, headers) {
for (var name in headers) {
request.setRequestHeader(name, headers[name]);
}
}
@@ -350,7 +353,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) {
var message_xmlhttprequest = new XMLHttpRequest();
message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel.uuid);
message_xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(message_xmlhttprequest);
addExtraHeaders(message_xmlhttprequest, extraHeaders);
message_xmlhttprequest.setRequestHeader("Content-type", "application/octet-stream");
// Once response received, send next queued event.
@@ -582,7 +585,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) {
var xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.open("GET", TUNNEL_READ + tunnel.uuid + ":" + (request_id++));
xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(xmlhttprequest);
addExtraHeaders(xmlhttprequest, extraHeaders);
xmlhttprequest.send(null);
return xmlhttprequest;
@@ -625,7 +628,7 @@ Guacamole.HTTPTunnel = function(tunnelURL, crossDomain, extraTunnelHeaders) {
connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, true);
connect_xmlhttprequest.withCredentials = withCredentials;
addExtraHeaders(connect_xmlhttprequest);
addExtraHeaders(connect_xmlhttprequest, extraHeaders);
connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
connect_xmlhttprequest.send(data);
@@ -1088,8 +1091,13 @@ Guacamole.ChainedTunnel.prototype = new Guacamole.Tunnel();
* 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.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain) {
Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain, extraTunnelHeaders) {
/**
* Reference to this Guacamole.StaticHTTPTunnel.
@@ -1107,6 +1115,32 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain) {
*/
var xhr = 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.
*
* @param {XMLHttpRequest} request
* The request where the configured extra headers will be added.
*
* @param {Object} headers
* The headers to be added to the request.
*
* @private
*/
function addExtraHeaders(request, headers) {
for (var name in headers) {
request.setRequestHeader(name, headers[name]);
}
}
/**
* Returns the Guacamole protocol status code which most closely
* represents the given HTTP status code.
@@ -1168,7 +1202,7 @@ Guacamole.StaticHTTPTunnel = function StaticHTTPTunnel(url, crossDomain) {
xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.withCredentials = !!crossDomain;
addExtraHeaders(xhr);
addExtraHeaders(xhr, extraHeaders);
xhr.responseType = 'text';
xhr.send(null);