summaryrefslogtreecommitdiffstats
path: root/dom/security/test/mixedcontentblocker/file_server.sjs
blob: 4f86c282ee825004f7a77892cded861dbeb3eada (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { NetUtil } = ChromeUtils.importESModule(
  "resource://gre/modules/NetUtil.sys.mjs"
);

function ERR(response, msg) {
  dump("ERROR: " + msg + "\n");
  response.write("HTTP/1.1 400 Bad Request\r\n");
  response.write("Content-Type: text/html; charset=UTF-8\r\n");
  response.write("Content-Length: " + msg.length + "\r\n");
  response.write("\r\n");
  response.write(msg);
}

function loadContentFromFile(path) {
  // Load the content to return in the response from file.
  // Since it's relative to the cwd of the test runner, we start there and
  // append to get to the actual path of the file.
  var testContentFile = Cc["@mozilla.org/file/directory_service;1"]
    .getService(Ci.nsIProperties)
    .get("CurWorkD", Ci.nsIFile);
  var dirs = path.split("/");
  for (var i = 0; i < dirs.length; i++) {
    testContentFile.append(dirs[i]);
  }
  var testContentFileStream = Cc[
    "@mozilla.org/network/file-input-stream;1"
  ].createInstance(Ci.nsIFileInputStream);
  testContentFileStream.init(testContentFile, -1, 0, 0);
  var testContent = NetUtil.readInputStreamToString(
    testContentFileStream,
    testContentFileStream.available()
  );
  return testContent;
}

function handleRequest(request, response) {
  const { scheme, host, path } = request;
  // get the Content-Type to serve from the query string
  var contentType = null;
  var uniqueID = null;
  var showLastRequest = false;
  request.queryString.split("&").forEach(function (val) {
    var [name, value] = val.split("=");
    if (name == "type") {
      contentType = unescape(value);
    }
    if (name == "uniqueID") {
      uniqueID = unescape(value);
    }
    if (name == "lastRequest") {
      showLastRequest = true;
    }
  });

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

  if (showLastRequest) {
    response.setHeader("Content-Type", "text/html", false);

    // We don't want to expose the same lastRequest multiple times.
    var state = getState("lastRequest");
    setState("lastRequest", "");

    if (state == "") {
      ERR(response, "No last request!");
      return;
    }

    response.write(state);
    return;
  }

  if (!uniqueID) {
    ERR(response, "No uniqueID?!?");
    return;
  }

  setState(
    "lastRequest",
    JSON.stringify({
      scheme,
      host,
      path,
      uniqueID,
      contentType: contentType || "other",
    })
  );

  switch (contentType) {
    case "img":
      response.setHeader("Content-Type", "image/png", false);
      response.write(
        loadContentFromFile("tests/image/test/mochitest/blue.png")
      );
      break;

    case "media":
      response.setHeader("Content-Type", "video/ogg", false);
      response.write(loadContentFromFile("tests/dom/media/test/320x240.ogv"));
      break;

    case "iframe":
      response.setHeader("Content-Type", "text/html", false);
      response.write("frame content");
      break;

    case "script":
      response.setHeader("Content-Type", "application/javascript", false);
      break;

    case "stylesheet":
      response.setHeader("Content-Type", "text/css", false);
      break;

    case "object":
      response.setHeader("Content-Type", "application/x-test-match", false);
      break;

    case "xhr":
      response.setHeader("Content-Type", "text/xml", false);
      response.setHeader("Access-Control-Allow-Origin", "https://example.com");
      response.write('<?xml version="1.0" encoding="UTF-8" ?><test></test>');
      break;

    default:
      response.setHeader("Content-Type", "text/html", false);
      response.write("<html><body>Hello World</body></html>");
      break;
  }
}