summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/worker_xhr_headers_server.sjs
blob: c2a944f6efc22ac13a9bbbdda09b5c8b4501762c (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

function handleRequest(request, response) {
  switch (request.method) {
    case "POST":
      try {
        var optionsHost = request.getHeader("options-host");
      } catch (e) {}

      var headerFound = false;
      if (optionsHost) {
        setState("postHost", request.host);
        setState("optionsHost", optionsHost);
        headerFound = true;
      }

      try {
        var emptyHeader = "nada" + request.getHeader("empty");
      } catch (e) {}

      if (emptyHeader && emptyHeader == "nada") {
        setState("emptyHeader", "nada");
        headerFound = true;
      }
      if (headerFound) {
        return;
      }
      break;

    case "OPTIONS":
      if (getState("optionsHost") == request.host) {
        try {
          var optionsHeader = request.getHeader(
            "Access-Control-Request-Headers"
          );
        } catch (e) {}
        setState("optionsHeader", "'" + optionsHeader + "'");
      }
      break;

    case "GET":
      response.setHeader("Cache-Control", "no-cache", false);
      response.setHeader("Content-Type", "text/plain", false);

      if (
        getState("postHost") == request.host &&
        getState("emptyHeader") == "nada"
      ) {
        var result = getState("optionsHeader");
        if (result) {
          response.write(
            "Success: expected OPTIONS request with " + result + " header"
          );
        } else if (getState("badGet") == 1) {
          response.write("Error: unexpected GET request");
        }
      } else {
        setState("badGet", "1");
        response.write("Error: this response should never be seen");
      }
      return;
  }

  response.setStatusLine(request.httpVersion, 501, "Not Implemented");
}