ccd.sjs 3.0 KB

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