partial_content.sjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* Debug and Error wrapper functions for dump().
  6. */
  7. function ERR(response, responseCode, responseCodeStr, msg)
  8. {
  9. // Reset state var.
  10. setState("expectedRequestType", "");
  11. // Dump to console log and send to client in response.
  12. dump("SERVER ERROR: " + msg + "\n");
  13. response.write("HTTP/1.1 " + responseCode + " " + responseCodeStr + "\r\n");
  14. response.write("Content-Type: text/html; charset=UTF-8\r\n");
  15. response.write("Content-Length: " + msg.length + "\r\n");
  16. response.write("\r\n");
  17. response.write(msg);
  18. }
  19. function DBG(msg)
  20. {
  21. // Dump to console only.
  22. dump("SERVER DEBUG: " + msg + "\n");
  23. }
  24. /* Delivers content in parts to test partially cached content: requires two
  25. * requests for the same resource.
  26. *
  27. * First call will respond with partial content, but a 200 header and
  28. * Content-Length equal to the full content length. No Range or If-Range
  29. * headers are allowed in the request.
  30. *
  31. * Second call will require Range and If-Range in the request headers, and
  32. * will respond with the range requested.
  33. */
  34. function handleRequest(request, response)
  35. {
  36. DBG("Trying to seize power");
  37. response.seizePower();
  38. DBG("About to check state vars");
  39. // Get state var to determine if this is the first or second request.
  40. var expectedRequestType;
  41. var lastModified;
  42. if (getState("expectedRequestType") === "") {
  43. DBG("First call: Should be requesting full content.");
  44. expectedRequestType = "fullRequest";
  45. // Set state var for second request.
  46. setState("expectedRequestType", "partialRequest");
  47. // Create lastModified variable for responses.
  48. lastModified = (new Date()).toUTCString();
  49. setState("lastModified", lastModified);
  50. } else if (getState("expectedRequestType") === "partialRequest") {
  51. DBG("Second call: Should be requesting undelivered content.");
  52. expectedRequestType = "partialRequest";
  53. // Reset state var for first request.
  54. setState("expectedRequestType", "");
  55. // Get last modified date and reset state var.
  56. lastModified = getState("lastModified");
  57. } else {
  58. ERR(response, 500, "Internal Server Error",
  59. "Invalid expectedRequestType \"" + expectedRequestType + "\"in " +
  60. "server state db.");
  61. return;
  62. }
  63. // Look for Range and If-Range
  64. var range = request.hasHeader("Range") ? request.getHeader("Range") : "";
  65. var ifRange = request.hasHeader("If-Range") ? request.getHeader("If-Range") : "";
  66. if (expectedRequestType === "fullRequest") {
  67. // Should not have Range or If-Range in first request.
  68. if (range && range.length > 0) {
  69. ERR(response, 400, "Bad Request",
  70. "Should not receive \"Range: " + range + "\" for first, full request.");
  71. return;
  72. }
  73. if (ifRange && ifRange.length > 0) {
  74. ERR(response, 400, "Bad Request",
  75. "Should not receive \"Range: " + range + "\" for first, full request.");
  76. return;
  77. }
  78. } else if (expectedRequestType === "partialRequest") {
  79. // Range AND If-Range should both be present in second request.
  80. if (!range) {
  81. ERR(response, 400, "Bad Request",
  82. "Should receive \"Range: \" for second, partial request.");
  83. return;
  84. }
  85. if (!ifRange) {
  86. ERR(response, 400, "Bad Request",
  87. "Should receive \"If-Range: \" for second, partial request.");
  88. return;
  89. }
  90. } else {
  91. // Somewhat redundant, but a check for errors in this test code.
  92. ERR(response, 500, "Internal Server Error",
  93. "expectedRequestType not set correctly: \"" + expectedRequestType + "\"");
  94. return;
  95. }
  96. // Prepare content in two parts for responses.
  97. var partialContent = "<html><head></head><body><p id=\"firstResponse\">" +
  98. "First response</p>";
  99. var remainderContent = "<p id=\"secondResponse\">Second response</p>" +
  100. "</body></html>";
  101. var totalLength = partialContent.length + remainderContent.length;
  102. DBG("totalLength: " + totalLength);
  103. // Prepare common headers for the two responses.
  104. date = new Date();
  105. DBG("Date: " + date.toUTCString() + ", Last-Modified: " + lastModified);
  106. var commonHeaders = "Date: " + date.toUTCString() + "\r\n" +
  107. "Last-Modified: " + lastModified + "\r\n" +
  108. "Content-Type: text/html; charset=UTF-8\r\n" +
  109. "ETag: abcd0123\r\n" +
  110. "Accept-Ranges: bytes\r\n";
  111. // Prepare specific headers and content for first and second responses.
  112. if (expectedRequestType === "fullRequest") {
  113. DBG("First response: Sending partial content with a full header");
  114. response.write("HTTP/1.1 200 OK\r\n");
  115. response.write(commonHeaders);
  116. // Set Content-Length to full length of resource.
  117. response.write("Content-Length: " + totalLength + "\r\n");
  118. response.write("\r\n");
  119. response.write(partialContent);
  120. } else if (expectedRequestType === "partialRequest") {
  121. DBG("Second response: Sending remaining content with a range header");
  122. response.write("HTTP/1.1 206 Partial Content\r\n");
  123. response.write(commonHeaders);
  124. // Set Content-Length to length of bytes transmitted.
  125. response.write("Content-Length: " + remainderContent.length + "\r\n");
  126. response.write("Content-Range: bytes " + partialContent.length + "-" +
  127. (totalLength - 1) + "/" + totalLength + "\r\n");
  128. response.write("\r\n");
  129. response.write(remainderContent);
  130. } else {
  131. // Somewhat redundant, but a check for errors in this test code.
  132. ERR(response, 500, "Internal Server Error",
  133. "Something very bad happened here: expectedRequestType is invalid " +
  134. "towards the end of handleRequest! - \"" + expectedRequestType + "\"");
  135. return;
  136. }
  137. response.finish();
  138. }