1 const DEBUG_all_valid = false;
2 const DEBUG_all_stub = false;
4 function handleRequest(request, response) {
5 // Decode the query string to know what test we're doing.
7 // character 1: 'I' = text/css response, 'J' = text/html response
8 let responseCSS = request.queryString[0] == "I";
10 // character 2: redirection type - we only care about whether we're
11 // ultimately same-origin with the requesting document ('A', 'D') or
14 request.queryString[1] == "A" || request.queryString[1] == "D";
16 // character 3: '1' = syntactically valid, '2' = invalid, '3' = http error
17 let malformed = request.queryString[2] == "2";
18 let httpError = request.queryString[2] == "3";
20 // character 4: loaded with <link> or @import (no action required)
22 // character 5: loading document mode: 'q' = quirks, 's' = standards
23 let quirksMode = request.queryString[4] == "q";
25 // Our response contains a CSS rule that selects an element whose
26 // ID is the first four characters of the query string.
27 let selector = "#" + request.queryString.substring(0, 4);
29 // "Malformed" responses wrap the CSS rule in the construct
30 // <html>{} ... </html>
31 // This mimics what the CSS parser might see if an actual HTML
32 // document were fed to it. Because CSS parsers recover from
33 // errors by skipping tokens until they find something
34 // recognizable, a style rule appearing where I wrote '...' above
36 let leader = malformed ? "<html>{}" : "";
37 let trailer = malformed ? "</html>" : "";
39 // Standards mode documents will ignore the style sheet if it is being
40 // served as text/html (regardless of its contents). Quirks mode
41 // documents will ignore the style sheet if it is being served as
42 // text/html _and_ it is not same-origin. Regardless, style sheets
43 // are ignored if they come as the body of an HTTP error response.
45 // Style sheets that should be ignored paint the element red; those
46 // that should be honored paint it lime.
48 (responseCSS || (quirksMode && sameOrigin)) && !httpError ? "lime" : "red";
50 // For debugging the test itself, we have the capacity to make every style
51 // sheet well-formed, or every style sheet do nothing.
52 if (DEBUG_all_valid) {
53 // In this mode, every test chip should turn blue.
54 response.setHeader("Content-Type", "text/css");
55 response.write(selector + "{background-color:blue}\n");
56 } else if (DEBUG_all_stub) {
57 // In this mode, every test chip for a case where the true test
58 // sheet would be honored, should turn red.
59 response.setHeader("Content-Type", "text/css");
60 response.write(selector + "{}\n");
64 response.setStatusLine(request.httpVersion, 500, "Internal Server Error");
66 response.setHeader("Content-Type", responseCSS ? "text/css" : "text/html");
68 leader + selector + "{background-color:" + color + "}" + trailer + "\n"