1// Test server for bug 1268962
2"use strict";
3Components.utils.importGlobalProperties(["URLSearchParams"]);
4const HTTPStatus = new Map([
5  [100, "Continue"],
6  [101, "Switching Protocol"],
7  [200, "OK"],
8  [201, "Created"],
9  [202, "Accepted"],
10  [203, "Non-Authoritative Information"],
11  [204, "No Content"],
12  [205, "Reset Content"],
13  [206, "Partial Content"],
14  [300, "Multiple Choice"],
15  [301, "Moved Permanently"],
16  [302, "Found"],
17  [303, "See Other"],
18  [304, "Not Modified"],
19  [305, "Use Proxy"],
20  [306, "unused"],
21  [307, "Temporary Redirect"],
22  [308, "Permanent Redirect"],
23  [400, "Bad Request"],
24  [401, "Unauthorized"],
25  [402, "Payment Required"],
26  [403, "Forbidden"],
27  [404, "Not Found"],
28  [405, "Method Not Allowed"],
29  [406, "Not Acceptable"],
30  [407, "Proxy Authentication Required"],
31  [408, "Request Timeout"],
32  [409, "Conflict"],
33  [410, "Gone"],
34  [411, "Length Required"],
35  [412, "Precondition Failed"],
36  [413, "Request Entity Too Large"],
37  [414, "Request-URI Too Long"],
38  [415, "Unsupported Media Type"],
39  [416, "Requested Range Not Satisfiable"],
40  [417, "Expectation Failed"],
41  [500, "Internal Server Error"],
42  [501, "Not Implemented"],
43  [502, "Bad Gateway"],
44  [503, "Service Unavailable"],
45  [504, "Gateway Timeout"],
46  [505, "HTTP Version Not Supported"],
47]);
48
49const SAME_ORIGIN =
50  "http://mochi.test:8888/tests/dom/base/test/file_bug1268962.sjs";
51const CROSS_ORIGIN =
52  "http://example.com/tests/dom/base/test/file_bug1268962.sjs";
53
54function handleRequest(request, response) {
55  const queryMap = new URLSearchParams(request.queryString);
56
57  // Check redirection before everything else.
58  if (queryMap.has("redirect")) {
59    let redirect = queryMap.get("redirect");
60    let location;
61    if (redirect == "sameorigin") {
62      location = SAME_ORIGIN;
63    } else if (redirect == "crossorigin") {
64      location = CROSS_ORIGIN;
65    }
66
67    if (location) {
68      // Use HTTP 302 redirection.
69      response.setStatusLine("1.1", 302, HTTPStatus.get(302));
70
71      // Forward query strings except the redirect option.
72      queryMap.delete("redirect");
73      response.setHeader("Location", location + "?" + queryMap.toString());
74
75      return;
76    }
77  }
78
79  if (queryMap.has("statusCode")) {
80    let statusCode = parseInt(queryMap.get("statusCode"));
81    let statusText = HTTPStatus.get(statusCode);
82    response.setStatusLine("1.1", statusCode, statusText);
83  }
84  if (queryMap.has("cacheControl")) {
85    let cacheControl = queryMap.get("cacheControl");
86    response.setHeader("Cache-Control", cacheControl);
87  }
88  if (queryMap.has("allowOrigin")) {
89    let allowOrigin = queryMap.get("allowOrigin");
90    response.setHeader("Access-Control-Allow-Origin", allowOrigin);
91  }
92}
93