bug767779.sjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/
  3. */
  4. var timer = Components.classes["@mozilla.org/timer;1"];
  5. var partTimer = timer.createInstance(Components.interfaces.nsITimer);
  6. function getFileAsInputStream(aFilename) {
  7. var file = Components.classes["@mozilla.org/file/directory_service;1"]
  8. .getService(Components.interfaces.nsIProperties)
  9. .get("CurWorkD", Components.interfaces.nsIFile);
  10. file.append("tests");
  11. file.append("image");
  12. file.append("test");
  13. file.append("mochitest");
  14. file.append(aFilename);
  15. var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
  16. .createInstance(Components.interfaces.nsIFileInputStream);
  17. fileStream.init(file, 1, 0, false);
  18. return fileStream;
  19. }
  20. function handleRequest(request, response)
  21. {
  22. response.setHeader("Content-Type", "image/gif", false);
  23. response.setHeader("Cache-Control", "no-cache", false);
  24. response.setStatusLine(request.httpVersion, 200, "OK");
  25. // We're sending data off in a delayed fashion
  26. response.processAsync();
  27. var inputStream = getFileAsInputStream("animated-gif_trailing-garbage.gif");
  28. var available = inputStream.available(); // = 4029 bytes
  29. // Send the good data at once
  30. response.bodyOutputStream.writeFrom(inputStream, 285);
  31. sendParts(inputStream, response);
  32. }
  33. function sendParts(inputStream, response) {
  34. // 3744 left, send in 8 chunks of 468 each
  35. partTimer.initWithCallback(getSendNextPart(inputStream, response), 500,
  36. Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  37. }
  38. function getSendNextPart(inputStream, response) {
  39. return function () {
  40. response.bodyOutputStream.writeFrom(inputStream, 468);
  41. if (!inputStream.available()) {
  42. inputStream.close();
  43. response.finish();
  44. } else {
  45. sendParts(inputStream, response);
  46. }
  47. };
  48. }