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