tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

file_CrossSiteXHR_inner.html (3312B)


      1 <!DOCTYPE HTML>
      2 <!--
      3  NOTE! The content of this file is duplicated in file_CrossSiteXHR_inner.jar
      4  and file_CrossSiteXHR_inner_data.sjs
      5  Please update those files if you update this one.
      6 -->
      7 
      8 <html>
      9 <head>
     10 <script>
     11 function trimString(stringValue) {
     12  return stringValue.replace(/^\s+|\s+$/g, '');
     13 };
     14 
     15 window.addEventListener("message", function(e) {
     16 
     17  sendData = null;
     18 
     19  req = JSON.parse(e.data);
     20  var res = {
     21    didFail: false,
     22    events: [],
     23    progressEvents: 0,
     24    status: 0,
     25    responseText: "",
     26    statusText: "",
     27    responseXML: null,
     28    sendThrew: false
     29  };
     30  
     31  var xhr = new XMLHttpRequest();
     32  for (type of ["load", "abort", "error", "loadstart", "loadend"]) {
     33    xhr.addEventListener(type, function(e) {
     34      res.events.push(e.type);
     35    });
     36  }
     37  xhr.addEventListener("readystatechange", function(e) {
     38    res.events.push("rs" + xhr.readyState);
     39  });
     40  xhr.addEventListener("progress", function(e) {
     41    res.progressEvents++;
     42  });
     43  if (req.uploadProgress) {
     44    xhr.upload.addEventListener(req.uploadProgress, function(e) {
     45      res.progressEvents++;
     46    });
     47  }
     48  xhr.onerror = function(e) {
     49    res.didFail = true;
     50  };
     51  xhr.onloadend = function (event) {
     52    res.status = xhr.status;
     53    try {
     54      res.statusText = xhr.statusText;
     55    } catch (e) {
     56      delete(res.statusText);
     57    }
     58    res.responseXML = xhr.responseXML ?
     59      (new XMLSerializer()).serializeToString(xhr.responseXML) :
     60      null;
     61    res.responseText = xhr.responseText;
     62 
     63    res.responseHeaders = {};
     64    for (responseHeader in req.responseHeaders) {
     65      res.responseHeaders[responseHeader] =
     66        xhr.getResponseHeader(responseHeader);
     67    }
     68    res.allResponseHeaders = {};
     69    var splitHeaders = xhr.getAllResponseHeaders().split("\r\n");
     70    for (var i = 0; i < splitHeaders.length; i++) {
     71      var headerValuePair = splitHeaders[i].split(":");
     72        if(headerValuePair[1] != null) {
     73          var headerName = trimString(headerValuePair[0]);
     74          var headerValue = trimString(headerValuePair[1]);
     75          res.allResponseHeaders[headerName] = headerValue;
     76        }
     77    }
     78    post(e, res);
     79  }
     80 
     81  if (req.withCred)
     82    xhr.withCredentials = true;
     83  if (req.body)
     84    sendData = req.body;
     85 
     86  res.events.push("opening");
     87  // Allow passign in falsy usernames/passwords so we can test them
     88  try {
     89    xhr.open(req.method, req.url, true,
     90             ("username" in req) ? req.username : "",
     91             ("password" in req) ? req.password : "");
     92  } catch (ex) {
     93    res.didFail = true;
     94    post(e, res);
     95  }
     96 
     97  for (header in req.headers) {
     98    xhr.setRequestHeader(header, req.headers[header]);
     99  }
    100 
    101  res.events.push("sending");
    102  try {
    103    xhr.send(sendData);
    104  } catch (ex) {
    105    res.didFail = true;
    106    res.sendThrew = true;
    107    post(e, res);
    108  }
    109 
    110 });
    111 
    112 function post(e, res) {
    113  // For test_CrossSiteXHR.html https based default and xorigin test runs
    114  if (self.location.protocol == "https:") {
    115    testOrigin = (self.location.origin ) == "https://example.com" ? "https://example.org" : "https://example.com";
    116  }
    117  // For test_CrossSiteXHR_origin.html and test_CrossSiteXHR_cache.html http based test runs
    118  else {
    119    testOrigin = "http://mochi.test:8888";
    120  }
    121  e.source.postMessage(JSON.stringify(res), testOrigin);
    122 }
    123 
    124 </script>
    125 </head>
    126 <body>
    127 Inner page
    128 </body>
    129 </html>