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