michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: var bodyPartIndex = -1; michael@0: var bodyParts = [ michael@0: ["red.png", "image/png"], michael@0: ["animated-gif2.gif", "image/gif"], michael@0: ["red.png", "image/png"], michael@0: ["lime100x100.svg", "image/svg+xml"], michael@0: ["lime100x100.svg", "image/svg+xml"], michael@0: ["animated-gif2.gif", "image/gif"], michael@0: ["red.png", "image/png"], michael@0: // Mime type intentionally wrong (test for bug 907575) michael@0: ["shaver.png", "image/gif"], michael@0: ["red.png", "image/png"], michael@0: ["damon.jpg", "image/jpeg"], michael@0: ["damon.jpg", "application/octet-stream"], michael@0: ["damon.jpg", "image/jpeg"], michael@0: ["rillybad.jpg", "application/x-unknown-content-type"], michael@0: ["damon.jpg", "image/jpeg"], michael@0: ["bad.jpg", "image/jpeg"], michael@0: ["red.png", "image/png"], michael@0: ["invalid.jpg", "image/jpeg"], michael@0: ["animated-gif2.gif", "image/gif"] michael@0: ]; michael@0: var timer = Components.classes["@mozilla.org/timer;1"]; michael@0: var partTimer = timer.createInstance(Components.interfaces.nsITimer); michael@0: michael@0: function getFileAsInputStream(aFilename) { michael@0: var file = Components.classes["@mozilla.org/file/directory_service;1"] michael@0: .getService(Components.interfaces.nsIProperties) michael@0: .get("CurWorkD", Components.interfaces.nsIFile); michael@0: michael@0: file.append("tests"); michael@0: file.append("image"); michael@0: file.append("test"); michael@0: file.append("mochitest"); michael@0: file.append(aFilename); michael@0: michael@0: var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1'] michael@0: .createInstance(Components.interfaces.nsIFileInputStream); michael@0: fileStream.init(file, 1, 0, false); michael@0: return fileStream; michael@0: } michael@0: michael@0: function handleRequest(request, response) michael@0: { michael@0: if (!getSharedState("next-part")) { michael@0: setSharedState("next-part", "-1"); michael@0: } michael@0: response.setHeader("Content-Type", michael@0: "multipart/x-mixed-replace;boundary=BOUNDARYOMG", false); michael@0: response.setHeader("Cache-Control", "no-cache", false); michael@0: response.setStatusLine(request.httpVersion, 200, "OK"); michael@0: // We're sending parts off in a delayed fashion, to let the tests occur. michael@0: response.processAsync(); michael@0: response.write("--BOUNDARYOMG\r\n"); michael@0: sendParts(response); michael@0: } michael@0: michael@0: function sendParts(response) { michael@0: let wait = false; michael@0: let nextPart = parseInt(getSharedState("next-part"), 10); michael@0: if (nextPart == bodyPartIndex) { michael@0: // Haven't been signaled yet, remain in holding pattern michael@0: wait = true; michael@0: } else { michael@0: bodyPartIndex = nextPart; michael@0: } michael@0: if (bodyParts.length > bodyPartIndex) { michael@0: let callback; michael@0: if (!wait) { michael@0: callback = getSendNextPart(response); michael@0: } else { michael@0: callback = function () { sendParts(response); }; michael@0: } michael@0: partTimer.initWithCallback(callback, 1000, michael@0: Components.interfaces.nsITimer.TYPE_ONE_SHOT); michael@0: } michael@0: else { michael@0: sendClose(response); michael@0: } michael@0: } michael@0: michael@0: function sendClose(response) { michael@0: response.write("--BOUNDARYOMG--\r\n"); michael@0: response.finish(); michael@0: } michael@0: michael@0: function getSendNextPart(response) { michael@0: var part = bodyParts[bodyPartIndex]; michael@0: var nextPartHead = "Content-Type: " + part[1] + "\r\n\r\n"; michael@0: var inputStream = getFileAsInputStream(part[0]); michael@0: return function () { michael@0: response.bodyOutputStream.write(nextPartHead, nextPartHead.length); michael@0: response.bodyOutputStream.writeFrom(inputStream, inputStream.available()); michael@0: inputStream.close(); michael@0: // Toss in the boundary, so the browser can know this part is complete michael@0: response.write("--BOUNDARYOMG\r\n"); michael@0: sendParts(response); michael@0: } michael@0: } michael@0: