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