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