1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/mochitests/partial_content.sjs Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* Debug and Error wrapper functions for dump(). 1.11 + */ 1.12 +function ERR(response, responseCode, responseCodeStr, msg) 1.13 +{ 1.14 + // Reset state var. 1.15 + setState("expectedRequestType", ""); 1.16 + // Dump to console log and send to client in response. 1.17 + dump("SERVER ERROR: " + msg + "\n"); 1.18 + response.write("HTTP/1.1 " + responseCode + " " + responseCodeStr + "\r\n"); 1.19 + response.write("Content-Type: text/html; charset=UTF-8\r\n"); 1.20 + response.write("Content-Length: " + msg.length + "\r\n"); 1.21 + response.write("\r\n"); 1.22 + response.write(msg); 1.23 +} 1.24 + 1.25 +function DBG(msg) 1.26 +{ 1.27 + // Dump to console only. 1.28 + dump("SERVER DEBUG: " + msg + "\n"); 1.29 +} 1.30 + 1.31 +/* Delivers content in parts to test partially cached content: requires two 1.32 + * requests for the same resource. 1.33 + * 1.34 + * First call will respond with partial content, but a 200 header and 1.35 + * Content-Length equal to the full content length. No Range or If-Range 1.36 + * headers are allowed in the request. 1.37 + * 1.38 + * Second call will require Range and If-Range in the request headers, and 1.39 + * will respond with the range requested. 1.40 + */ 1.41 +function handleRequest(request, response) 1.42 +{ 1.43 + DBG("Trying to seize power"); 1.44 + response.seizePower(); 1.45 + 1.46 + DBG("About to check state vars"); 1.47 + // Get state var to determine if this is the first or second request. 1.48 + var expectedRequestType; 1.49 + var lastModified; 1.50 + if (getState("expectedRequestType") === "") { 1.51 + DBG("First call: Should be requesting full content."); 1.52 + expectedRequestType = "fullRequest"; 1.53 + // Set state var for second request. 1.54 + setState("expectedRequestType", "partialRequest"); 1.55 + // Create lastModified variable for responses. 1.56 + lastModified = (new Date()).toUTCString(); 1.57 + setState("lastModified", lastModified); 1.58 + } else if (getState("expectedRequestType") === "partialRequest") { 1.59 + DBG("Second call: Should be requesting undelivered content."); 1.60 + expectedRequestType = "partialRequest"; 1.61 + // Reset state var for first request. 1.62 + setState("expectedRequestType", ""); 1.63 + // Get last modified date and reset state var. 1.64 + lastModified = getState("lastModified"); 1.65 + } else { 1.66 + ERR(response, 500, "Internal Server Error", 1.67 + "Invalid expectedRequestType \"" + expectedRequestType + "\"in " + 1.68 + "server state db."); 1.69 + return; 1.70 + } 1.71 + 1.72 + // Look for Range and If-Range 1.73 + var range = request.hasHeader("Range") ? request.getHeader("Range") : ""; 1.74 + var ifRange = request.hasHeader("If-Range") ? request.getHeader("If-Range") : ""; 1.75 + 1.76 + if (expectedRequestType === "fullRequest") { 1.77 + // Should not have Range or If-Range in first request. 1.78 + if (range && range.length > 0) { 1.79 + ERR(response, 400, "Bad Request", 1.80 + "Should not receive \"Range: " + range + "\" for first, full request."); 1.81 + return; 1.82 + } 1.83 + if (ifRange && ifRange.length > 0) { 1.84 + ERR(response, 400, "Bad Request", 1.85 + "Should not receive \"Range: " + range + "\" for first, full request."); 1.86 + return; 1.87 + } 1.88 + } else if (expectedRequestType === "partialRequest") { 1.89 + // Range AND If-Range should both be present in second request. 1.90 + if (!range) { 1.91 + ERR(response, 400, "Bad Request", 1.92 + "Should receive \"Range: \" for second, partial request."); 1.93 + return; 1.94 + } 1.95 + if (!ifRange) { 1.96 + ERR(response, 400, "Bad Request", 1.97 + "Should receive \"If-Range: \" for second, partial request."); 1.98 + return; 1.99 + } 1.100 + } else { 1.101 + // Somewhat redundant, but a check for errors in this test code. 1.102 + ERR(response, 500, "Internal Server Error", 1.103 + "expectedRequestType not set correctly: \"" + expectedRequestType + "\""); 1.104 + return; 1.105 + } 1.106 + 1.107 + // Prepare content in two parts for responses. 1.108 + var partialContent = "<html><head></head><body><p id=\"firstResponse\">" + 1.109 + "First response</p>"; 1.110 + var remainderContent = "<p id=\"secondResponse\">Second response</p>" + 1.111 + "</body></html>"; 1.112 + var totalLength = partialContent.length + remainderContent.length; 1.113 + 1.114 + DBG("totalLength: " + totalLength); 1.115 + 1.116 + // Prepare common headers for the two responses. 1.117 + date = new Date(); 1.118 + DBG("Date: " + date.toUTCString() + ", Last-Modified: " + lastModified); 1.119 + var commonHeaders = "Date: " + date.toUTCString() + "\r\n" + 1.120 + "Last-Modified: " + lastModified + "\r\n" + 1.121 + "Content-Type: text/html; charset=UTF-8\r\n" + 1.122 + "ETag: abcd0123\r\n" + 1.123 + "Accept-Ranges: bytes\r\n"; 1.124 + 1.125 + 1.126 + // Prepare specific headers and content for first and second responses. 1.127 + if (expectedRequestType === "fullRequest") { 1.128 + DBG("First response: Sending partial content with a full header"); 1.129 + response.write("HTTP/1.1 200 OK\r\n"); 1.130 + response.write(commonHeaders); 1.131 + // Set Content-Length to full length of resource. 1.132 + response.write("Content-Length: " + totalLength + "\r\n"); 1.133 + response.write("\r\n"); 1.134 + response.write(partialContent); 1.135 + } else if (expectedRequestType === "partialRequest") { 1.136 + DBG("Second response: Sending remaining content with a range header"); 1.137 + response.write("HTTP/1.1 206 Partial Content\r\n"); 1.138 + response.write(commonHeaders); 1.139 + // Set Content-Length to length of bytes transmitted. 1.140 + response.write("Content-Length: " + remainderContent.length + "\r\n"); 1.141 + response.write("Content-Range: bytes " + partialContent.length + "-" + 1.142 + (totalLength - 1) + "/" + totalLength + "\r\n"); 1.143 + response.write("\r\n"); 1.144 + response.write(remainderContent); 1.145 + } else { 1.146 + // Somewhat redundant, but a check for errors in this test code. 1.147 + ERR(response, 500, "Internal Server Error", 1.148 + "Something very bad happened here: expectedRequestType is invalid " + 1.149 + "towards the end of handleRequest! - \"" + expectedRequestType + "\""); 1.150 + return; 1.151 + } 1.152 + 1.153 + response.finish(); 1.154 +}