bug733553.sjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/
  3. */
  4. var bodyPartIndex = -1;
  5. var bodyParts = [
  6. ["red.png", "image/png"],
  7. ["animated-gif2.gif", "image/gif"],
  8. ["red.png", "image/png"],
  9. ["lime100x100.svg", "image/svg+xml"],
  10. ["lime100x100.svg", "image/svg+xml"],
  11. ["animated-gif2.gif", "image/gif"],
  12. ["red.png", "image/png"],
  13. // Mime type intentionally wrong (test for bug 907575)
  14. ["shaver.png", "image/gif"],
  15. ["red.png", "image/png"],
  16. ["damon.jpg", "image/jpeg"],
  17. ["damon.jpg", "application/octet-stream"],
  18. ["damon.jpg", "image/jpeg"],
  19. ["rillybad.jpg", "application/x-unknown-content-type"],
  20. ["damon.jpg", "image/jpeg"],
  21. ["bad.jpg", "image/jpeg"],
  22. ["red.png", "image/png"],
  23. ["invalid.jpg", "image/jpeg"],
  24. ["animated-gif2.gif", "image/gif"]
  25. ];
  26. var timer = Components.classes["@mozilla.org/timer;1"];
  27. var partTimer = timer.createInstance(Components.interfaces.nsITimer);
  28. function getFileAsInputStream(aFilename) {
  29. var file = Components.classes["@mozilla.org/file/directory_service;1"]
  30. .getService(Components.interfaces.nsIProperties)
  31. .get("CurWorkD", Components.interfaces.nsIFile);
  32. file.append("tests");
  33. file.append("image");
  34. file.append("test");
  35. file.append("mochitest");
  36. file.append(aFilename);
  37. var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
  38. .createInstance(Components.interfaces.nsIFileInputStream);
  39. fileStream.init(file, 1, 0, false);
  40. return fileStream;
  41. }
  42. function handleRequest(request, response)
  43. {
  44. if (!getSharedState("next-part")) {
  45. setSharedState("next-part", "-1");
  46. }
  47. response.setHeader("Content-Type",
  48. "multipart/x-mixed-replace;boundary=BOUNDARYOMG", false);
  49. response.setHeader("Cache-Control", "no-cache", false);
  50. response.setStatusLine(request.httpVersion, 200, "OK");
  51. // We're sending parts off in a delayed fashion, to let the tests occur.
  52. response.processAsync();
  53. response.write("--BOUNDARYOMG\r\n");
  54. sendParts(response);
  55. }
  56. function sendParts(response) {
  57. let wait = false;
  58. let nextPart = parseInt(getSharedState("next-part"), 10);
  59. if (nextPart == bodyPartIndex) {
  60. // Haven't been signaled yet, remain in holding pattern
  61. wait = true;
  62. } else {
  63. bodyPartIndex = nextPart;
  64. }
  65. if (bodyParts.length > bodyPartIndex) {
  66. let callback;
  67. if (!wait) {
  68. callback = getSendNextPart(response);
  69. } else {
  70. callback = function () { sendParts(response); };
  71. }
  72. partTimer.initWithCallback(callback, 1000,
  73. Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  74. }
  75. else {
  76. sendClose(response);
  77. }
  78. }
  79. function sendClose(response) {
  80. response.write("--BOUNDARYOMG--\r\n");
  81. response.finish();
  82. }
  83. function getSendNextPart(response) {
  84. var part = bodyParts[bodyPartIndex];
  85. var nextPartHead = "Content-Type: " + part[1] + "\r\n\r\n";
  86. var inputStream = getFileAsInputStream(part[0]);
  87. return function () {
  88. response.bodyOutputStream.write(nextPartHead, nextPartHead.length);
  89. response.bodyOutputStream.writeFrom(inputStream, inputStream.available());
  90. inputStream.close();
  91. // Toss in the boundary, so the browser can know this part is complete
  92. response.write("--BOUNDARYOMG\r\n");
  93. sendParts(response);
  94. }
  95. }