content/base/test/file_CrossSiteXHR_inner.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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 -->
     8 <html>
     9 <head>
    10 <script>
    11 function trimString(stringValue) {
    12   return stringValue.replace(/^\s+|\s+$/g, '');
    13 };
    15 window.addEventListener("message", function(e) {
    17   sendData = null;
    19   req = eval(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   };
    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     }, false);
    36   }
    37   xhr.addEventListener("readystatechange", function(e) {
    38     res.events.push("rs" + xhr.readyState);
    39   }, false);
    40   xhr.addEventListener("progress", function(e) {
    41     res.progressEvents++;
    42   }, false);
    43   if (req.uploadProgress) {
    44     xhr.upload.addEventListener(req.uploadProgress, function(e) {
    45       res.progressEvents++;
    46     }, false);
    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;
    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   }
    81   if (req.withCred)
    82     xhr.withCredentials = true;
    83   if (req.body)
    84     sendData = req.body;
    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 : "aa");
    92   } catch (ex) {
    93     res.didFail = true;
    94     post(e, res);
    95   }
    97   for (header in req.headers) {
    98     xhr.setRequestHeader(header, req.headers[header]);
    99   }
   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   }
   110 }, false);
   112 function post(e, res) {
   113   e.source.postMessage(res.toSource(), "http://mochi.test:8888");
   114 }
   116 </script>
   117 </head>
   118 <body>
   119 Inner page
   120 </body>
   121 </html>

mercurial