summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs
blob: 0c89f7d538c51bd63e332f0181cc4544950c13fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Redirect handler specifically for the needs of:
 * Bug 1194052 - Append Principal to RedirectChain within LoadInfo before the channel is succesfully openend
 */

function createIframeContent(aQuery) {
  var content = `
  <!DOCTYPE HTML>
  <html>
  <head><meta charset="utf-8">
  <title>Bug 1194052 - LoadInfo redirect chain subtest</title>
  </head>
  <body>
  <script type="text/javascript">
    var myXHR = new XMLHttpRequest();
    myXHR.open("GET", "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?${aQuery}");
    myXHR.onload = function() {
      var loadinfo = SpecialPowers.wrap(myXHR).channel.loadInfo;
      var redirectChain = loadinfo.redirectChain;
      var redirectChainIncludingInternalRedirects = loadinfo.redirectChainIncludingInternalRedirects;
      var resultOBJ = { redirectChain : [], redirectChainIncludingInternalRedirects : [] };
      for (var i = 0; i < redirectChain.length; i++) {
        resultOBJ.redirectChain.push(redirectChain[i].principal.spec);
      }
      for (var i = 0; i < redirectChainIncludingInternalRedirects.length; i++) {
        resultOBJ.redirectChainIncludingInternalRedirects.push(redirectChainIncludingInternalRedirects[i].principal.spec);
      }
      var loadinfoJSON = JSON.stringify(resultOBJ);
      window.parent.postMessage({ loadinfo: loadinfoJSON }, "*");
    }
    myXHR.onerror = function() {
      var resultOBJ = { redirectChain : [], redirectChainIncludingInternalRedirects : [] };
      var loadinfoJSON = JSON.stringify(resultOBJ);
      window.parent.postMessage({ loadinfo: loadinfoJSON }, "*");
    }
    myXHR.send();
  </script>
  </body>
  </html>`;

  return content;
}

function handleRequest(request, response) {
  response.setHeader("Cache-Control", "no-cache", false);
  var queryString = request.queryString;

  if (
    queryString == "iframe-redir-https-2" ||
    queryString == "iframe-redir-err-2"
  ) {
    var query = queryString.replace("iframe-", "");
    // send upgrade-insecure-requests CSP header
    response.setHeader("Content-Type", "text/html", false);
    response.setHeader(
      "Content-Security-Policy",
      "upgrade-insecure-requests",
      false
    );
    response.write(createIframeContent(query));
    return;
  }

  // at the end of the redirectchain we return some text
  // for sanity checking
  if (queryString == "redir-0" || queryString == "redir-https-0") {
    response.setHeader("Content-Type", "text/html", false);
    response.write("checking redirectchain");
    return;
  }

  // special case redir-err-1 and return an error to trigger the fallback
  if (queryString == "redir-err-1") {
    response.setStatusLine("1.1", 404, "Bad request");
    return;
  }

  // must be a redirect
  var newLocation = "";
  switch (queryString) {
    case "redir-err-2":
      newLocation =
        "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-err-1";
      break;

    case "redir-https-2":
      newLocation =
        "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-https-1";
      break;

    case "redir-https-1":
      newLocation =
        "http://example.com/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-https-0";
      break;

    case "redir-2":
      newLocation =
        "http://mochi.test:8888/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-1";
      break;

    case "redir-1":
      newLocation =
        "http://mochi.test:8888/tests/netwerk/test/mochitests/file_loadinfo_redirectchain.sjs?redir-0";
      break;
  }

  response.setStatusLine("1.1", 302, "Found");
  response.setHeader("Location", newLocation, false);
}