1 /* Generic responder that composes a response from
2 * the query string of a request.
4 * It reserves some special prop names:
5 * - body: get's used as the response body
6 * - statusCode: override the 200 OK response code
7 * (response text is set automatically)
9 * Any property names it doesn't know about get converted into
13 * http://test/resource.sjs?Content-Type=text/html&body=<h1>hello</h1>&Hello=hi
17 * Content-Type: text/html
21 //global handleRequest
24 const HTTPStatus = new Map([
26 [101, "Switching Protocol"],
30 [203, "Non-Authoritative Information"],
32 [205, "Reset Content"],
33 [206, "Partial Content"],
34 [300, "Multiple Choice"],
35 [301, "Moved Permanently"],
38 [304, "Not Modified"],
41 [307, "Temporary Redirect"],
42 [308, "Permanent Redirect"],
44 [401, "Unauthorized"],
45 [402, "Payment Required"],
48 [405, "Method Not Allowed"],
49 [406, "Not Acceptable"],
50 [407, "Proxy Authentication Required"],
51 [408, "Request Timeout"],
54 [411, "Length Required"],
55 [412, "Precondition Failed"],
56 [413, "Request Entity Too Large"],
57 [414, "Request-URI Too Long"],
58 [415, "Unsupported Media Type"],
59 [416, "Requested Range Not Satisfiable"],
60 [417, "Expectation Failed"],
61 [500, "Internal Server Error"],
62 [501, "Not Implemented"],
64 [503, "Service Unavailable"],
65 [504, "Gateway Timeout"],
66 [505, "HTTP Version Not Supported"],
69 function handleRequest(request, response) {
70 const queryMap = new URLSearchParams(request.queryString);
71 if (queryMap.has("statusCode")) {
72 let statusCode = parseInt(queryMap.get("statusCode"));
73 let statusText = HTTPStatus.get(statusCode);
74 queryMap.delete("statusCode");
75 response.setStatusLine("1.1", statusCode, statusText);
77 if (queryMap.has("body")) {
78 let body = queryMap.get("body") || "";
79 queryMap.delete("body");
80 response.write(decodeURIComponent(body));
82 for (let [key, value] of queryMap.entries()) {
83 response.setHeader(key, value);