bug1180105.sjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/
  3. */
  4. var counter = 100;
  5. var timer = Components.classes["@mozilla.org/timer;1"];
  6. var partTimer = timer.createInstance(Components.interfaces.nsITimer);
  7. function getFileAsInputStream(aFilename) {
  8. var file = Components.classes["@mozilla.org/file/directory_service;1"]
  9. .getService(Components.interfaces.nsIProperties)
  10. .get("CurWorkD", Components.interfaces.nsIFile);
  11. file.append("tests");
  12. file.append("image");
  13. file.append("test");
  14. file.append("mochitest");
  15. file.append(aFilename);
  16. var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
  17. .createInstance(Components.interfaces.nsIFileInputStream);
  18. fileStream.init(file, 1, 0, false);
  19. return fileStream;
  20. }
  21. function handleRequest(request, response)
  22. {
  23. response.setHeader("Content-Type",
  24. "multipart/x-mixed-replace;boundary=BOUNDARYOMG", false);
  25. response.setHeader("Cache-Control", "no-cache", false);
  26. response.setStatusLine(request.httpVersion, 200, "OK");
  27. // We're sending parts off in a delayed fashion, to let the tests occur.
  28. response.processAsync();
  29. response.write("--BOUNDARYOMG\r\n");
  30. sendParts(response);
  31. }
  32. function sendParts(response) {
  33. if (counter-- == 0) {
  34. sendClose(response);
  35. setSharedState("all-parts-done", "1");
  36. return;
  37. }
  38. sendNextPart(response);
  39. partTimer.initWithCallback(function() {sendParts(response);}, 1,
  40. Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  41. }
  42. function sendClose(response) {
  43. response.write("--BOUNDARYOMG--\r\n");
  44. response.finish();
  45. }
  46. function sendNextPart(response) {
  47. var nextPartHead = "Content-Type: image/jpeg\r\n\r\n";
  48. var inputStream = getFileAsInputStream("damon.jpg");
  49. response.bodyOutputStream.write(nextPartHead, nextPartHead.length);
  50. response.bodyOutputStream.writeFrom(inputStream, inputStream.available());
  51. inputStream.close();
  52. // Toss in the boundary, so the browser can know this part is complete
  53. response.write("--BOUNDARYOMG\r\n");
  54. }