michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Debug and Error wrapper functions for dump(). michael@0: */ michael@0: function ERR(response, responseCode, responseCodeStr, msg) michael@0: { michael@0: // Reset state var. michael@0: setState("expectedRequestType", ""); michael@0: // Dump to console log and send to client in response. michael@0: dump("SERVER ERROR: " + msg + "\n"); michael@0: response.write("HTTP/1.1 " + responseCode + " " + responseCodeStr + "\r\n"); michael@0: response.write("Content-Type: text/html; charset=UTF-8\r\n"); michael@0: response.write("Content-Length: " + msg.length + "\r\n"); michael@0: response.write("\r\n"); michael@0: response.write(msg); michael@0: } michael@0: michael@0: function DBG(msg) michael@0: { michael@0: // Dump to console only. michael@0: dump("SERVER DEBUG: " + msg + "\n"); michael@0: } michael@0: michael@0: /* Delivers content in parts to test partially cached content: requires two michael@0: * requests for the same resource. michael@0: * michael@0: * First call will respond with partial content, but a 200 header and michael@0: * Content-Length equal to the full content length. No Range or If-Range michael@0: * headers are allowed in the request. michael@0: * michael@0: * Second call will require Range and If-Range in the request headers, and michael@0: * will respond with the range requested. michael@0: */ michael@0: function handleRequest(request, response) michael@0: { michael@0: DBG("Trying to seize power"); michael@0: response.seizePower(); michael@0: michael@0: DBG("About to check state vars"); michael@0: // Get state var to determine if this is the first or second request. michael@0: var expectedRequestType; michael@0: var lastModified; michael@0: if (getState("expectedRequestType") === "") { michael@0: DBG("First call: Should be requesting full content."); michael@0: expectedRequestType = "fullRequest"; michael@0: // Set state var for second request. michael@0: setState("expectedRequestType", "partialRequest"); michael@0: // Create lastModified variable for responses. michael@0: lastModified = (new Date()).toUTCString(); michael@0: setState("lastModified", lastModified); michael@0: } else if (getState("expectedRequestType") === "partialRequest") { michael@0: DBG("Second call: Should be requesting undelivered content."); michael@0: expectedRequestType = "partialRequest"; michael@0: // Reset state var for first request. michael@0: setState("expectedRequestType", ""); michael@0: // Get last modified date and reset state var. michael@0: lastModified = getState("lastModified"); michael@0: } else { michael@0: ERR(response, 500, "Internal Server Error", michael@0: "Invalid expectedRequestType \"" + expectedRequestType + "\"in " + michael@0: "server state db."); michael@0: return; michael@0: } michael@0: michael@0: // Look for Range and If-Range michael@0: var range = request.hasHeader("Range") ? request.getHeader("Range") : ""; michael@0: var ifRange = request.hasHeader("If-Range") ? request.getHeader("If-Range") : ""; michael@0: michael@0: if (expectedRequestType === "fullRequest") { michael@0: // Should not have Range or If-Range in first request. michael@0: if (range && range.length > 0) { michael@0: ERR(response, 400, "Bad Request", michael@0: "Should not receive \"Range: " + range + "\" for first, full request."); michael@0: return; michael@0: } michael@0: if (ifRange && ifRange.length > 0) { michael@0: ERR(response, 400, "Bad Request", michael@0: "Should not receive \"Range: " + range + "\" for first, full request."); michael@0: return; michael@0: } michael@0: } else if (expectedRequestType === "partialRequest") { michael@0: // Range AND If-Range should both be present in second request. michael@0: if (!range) { michael@0: ERR(response, 400, "Bad Request", michael@0: "Should receive \"Range: \" for second, partial request."); michael@0: return; michael@0: } michael@0: if (!ifRange) { michael@0: ERR(response, 400, "Bad Request", michael@0: "Should receive \"If-Range: \" for second, partial request."); michael@0: return; michael@0: } michael@0: } else { michael@0: // Somewhat redundant, but a check for errors in this test code. michael@0: ERR(response, 500, "Internal Server Error", michael@0: "expectedRequestType not set correctly: \"" + expectedRequestType + "\""); michael@0: return; michael@0: } michael@0: michael@0: // Prepare content in two parts for responses. michael@0: var partialContent = "

" + michael@0: "First response

"; michael@0: var remainderContent = "

Second response

" + michael@0: ""; michael@0: var totalLength = partialContent.length + remainderContent.length; michael@0: michael@0: DBG("totalLength: " + totalLength); michael@0: michael@0: // Prepare common headers for the two responses. michael@0: date = new Date(); michael@0: DBG("Date: " + date.toUTCString() + ", Last-Modified: " + lastModified); michael@0: var commonHeaders = "Date: " + date.toUTCString() + "\r\n" + michael@0: "Last-Modified: " + lastModified + "\r\n" + michael@0: "Content-Type: text/html; charset=UTF-8\r\n" + michael@0: "ETag: abcd0123\r\n" + michael@0: "Accept-Ranges: bytes\r\n"; michael@0: michael@0: michael@0: // Prepare specific headers and content for first and second responses. michael@0: if (expectedRequestType === "fullRequest") { michael@0: DBG("First response: Sending partial content with a full header"); michael@0: response.write("HTTP/1.1 200 OK\r\n"); michael@0: response.write(commonHeaders); michael@0: // Set Content-Length to full length of resource. michael@0: response.write("Content-Length: " + totalLength + "\r\n"); michael@0: response.write("\r\n"); michael@0: response.write(partialContent); michael@0: } else if (expectedRequestType === "partialRequest") { michael@0: DBG("Second response: Sending remaining content with a range header"); michael@0: response.write("HTTP/1.1 206 Partial Content\r\n"); michael@0: response.write(commonHeaders); michael@0: // Set Content-Length to length of bytes transmitted. michael@0: response.write("Content-Length: " + remainderContent.length + "\r\n"); michael@0: response.write("Content-Range: bytes " + partialContent.length + "-" + michael@0: (totalLength - 1) + "/" + totalLength + "\r\n"); michael@0: response.write("\r\n"); michael@0: response.write(remainderContent); michael@0: } else { michael@0: // Somewhat redundant, but a check for errors in this test code. michael@0: ERR(response, 500, "Internal Server Error", michael@0: "Something very bad happened here: expectedRequestType is invalid " + michael@0: "towards the end of handleRequest! - \"" + expectedRequestType + "\""); michael@0: return; michael@0: } michael@0: michael@0: response.finish(); michael@0: }