[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
<!DOCTYPE html>
<html>
<head>
<title>WASD CORS Test JavaScript</title>
<script language="JavaScript">

var host, method, url;
var path = '/wasd_root/exercise/cors.html';
var addHeaders = new Array();

function setConfig ()
{
   host = document.getElementById("host").value.trim();
   if (!host.length) host = window.location.host;
   document.getElementById("host").value = host;
   var port = window.location.port;

   method = document.getElementById("method");
   method = method.options[method.selectedIndex].value;

   var headers = document.getElementById("headers");
   headers = headers.options[headers.selectedIndex].value;
   addHeaders = headers.split(',');
   if (addHeaders[0].length == 0) addHeaders = new Array();

   url = window.location.protocol + '//' + host + path;

   var config = '# WASD_CONFIG_MAP\n';
   config += 'set ' + path + ' \\\n';
   config += '    cors=origin=' + window.location.protocol + '//' +
             window.location.host;
   if (port.length && port != 80 && port != 443) config += ':' + port;
   config += ' \\\n';
   config += '    cors=methods=PUT cors=headers=X-example\n';
   document.getElementById("config").innerHTML = config;

   if (host == window.location.host)
      document.getElementById("notxdr").innerHTML = 'Not a cross-domain request!';
   else
      document.getElementById("notxdr").innerHTML = '';
}

// create the XHR object
function createCORSRequest(method, url)
{
   var xhr = new XMLHttpRequest();
   if ("withCredentials" in xhr) {
      // XHR for Chrome/Firefox/Opera/Safari
      xhr.open(method, url, true);
   } else
   if (typeof XDomainRequest != "undefined") {
      // XDomainRequest for MSIE
      xhr = new XDomainRequest();
      xhr.open(method, url);
   } else {
      // CORS not supported
      xhr = null;
   }
   return xhr;
}

// make the CORS request.
function makeCorsRequest()
{
   reportResponse();

   var xhr = createCORSRequest(method, url);
   if (!xhr) {
      reportError('CORS not supported');
      return;
   }

   xhr.onload = function() {
      var text = xhr.responseText;
      reportResponse(text);
   };

   xhr.onerror = function(msg,url,lnum) {
      reportError('There was an error making the request ' +
                  '(check WATCH and JavaScript console).');
   };

   for (var idx = 0; idx < addHeaders.length; idx++)
      xhr.setRequestHeader(addHeaders[idx], 'value'+idx);

   xhr.send();
}

function reportResponse (text)
{
   var report = document.getElementById("report");
   if (typeof text == 'undefined')
      report.innerHTML = '';
   else
      report.innerHTML += text.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}

function reportError (text)
{
   var report = document.getElementById("report");
   report.innerHTML += '<span style="color:red">' + text + '</span>\n\n';
}

</script>
</head>
<body onload="setConfig()">
<u><b>WASD CORS Test JavaScript</b></u>

<p><pre id="config"></pre>

<p>Method:
<select id="method" onchange="setConfig()">
<option value="GET" selected>GET</option>
<option value="POST">POST</option>
<option value="HEAD">HEAD</option>
<option value="PUT">PUT</option>
</select>

<p>Headers:
<select id="headers" onchange="setConfig()">
<option value="" selected>(none)</option>
<option value="X-example">X-example</option>
<option value="X-an-example">X-an-example</option>
<option value="X-an-example,X-example,X-another-example">(multiple)</option>
</select>

<p>Server:&nbsp;<input id="host" type="text" size="20" onchange="setConfig()">
<span id="notxdr" style="color:red"></span>

<p><input type="button" onclick="makeCorsRequest()" value="makeCorsRequest()">

<p><pre id="report"></pre>

<br>
</body>
</html>