summaryrefslogtreecommitdiffstats
path: root/dom/base/test/iframe_meta_refresh.sjs
blob: 0d881be72b9d4eda579467c1233d9b9ff20398b7 (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
/*
 * Test server for iframe refresh from meta http-equiv
 */

const SHARED_KEY = "iframe_meta_refresh";
const DEFAULT_STATE = { count: 0, referrers: [] };
const REFRESH_PAGE =
  "http://example.com/tests/dom/base/test/iframe_meta_refresh.sjs?action=test";

function createContent(refresh) {
  let metaRefresh = "";
  let scriptMessage = "";

  if (refresh) {
    metaRefresh = `<meta http-equiv="refresh" content="0;URL=${REFRESH_PAGE}">`;
  } else {
    scriptMessage = `
      <script>
          window.addEventListener("load", function() {
            parent.postMessage("childLoadComplete", "http://mochi.test:8888");
          }, false);
      </script>`;
  }

  return `<!DOCTYPE HTML>
         <html>
         <head>
         <meta charset="utf-8">
         ${metaRefresh}
         <title> Test referrer of meta http-equiv refresh</title>
         </head>
         <body>
           ${scriptMessage}
         </body>
         </html>`;
}

function handleRequest(request, response) {
  Components.utils.importGlobalProperties(["URLSearchParams"]);
  let query = new URLSearchParams(request.queryString);

  let action = query.get("action");

  var referrerLevel = "none";
  if (request.hasHeader("Referer")) {
    let referrer = request.getHeader("Referer");
    if (referrer.indexOf("test_meta_refresh_referrer") > 0) {
      referrerLevel = "full";
    } else if (referrer == "http://mochi.test:8888/") {
      referrerLevel = "origin";
    }
  }

  var state = getSharedState(SHARED_KEY);
  if (state === "") {
    state = DEFAULT_STATE;
  } else {
    state = JSON.parse(state);
  }

  response.setStatusLine(request.httpVersion, 200, "OK");

  //avoid confusing cache behaviors
  response.setHeader("Cache-Control", "no-cache", false);

  if (action === "results") {
    response.setHeader("Content-Type", "text/plain", false);
    response.write(JSON.stringify(state));
    return;
  }

  if (action === "reset") {
    //reset server state
    setSharedState(SHARED_KEY, JSON.stringify(DEFAULT_STATE));
    response.write("");
    return;
  }

  if (action === "test") {
    let load = query.get("load");
    state.count++;
    if (state.referrers.indexOf(referrerLevel) < 0) {
      state.referrers.push(referrerLevel);
    }

    // Write frame content
    response.write(createContent(load));
  }

  setSharedState(SHARED_KEY, JSON.stringify(state));
  return;
}