webcam-simulacrum.sjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/
  3. */
  4. var counter = 2;
  5. var frames = ['red.gif', 'blue.gif'];
  6. var timer = Components.classes["@mozilla.org/timer;1"];
  7. var partTimer = timer.createInstance(Components.interfaces.nsITimer);
  8. function getFileAsInputStream(aFilename) {
  9. var file = Components.classes["@mozilla.org/file/directory_service;1"]
  10. .getService(Components.interfaces.nsIProperties)
  11. .get("CurWorkD", Components.interfaces.nsIFile);
  12. file.append("tests");
  13. file.append("image");
  14. file.append("test");
  15. file.append("mochitest");
  16. file.append(aFilename);
  17. var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
  18. .createInstance(Components.interfaces.nsIFileInputStream);
  19. fileStream.init(file, 1, 0, false);
  20. return fileStream;
  21. }
  22. function handleRequest(request, response)
  23. {
  24. response.setHeader("Content-Type",
  25. "multipart/x-mixed-replace;boundary=BOUNDARYOMG", false);
  26. response.setHeader("Cache-Control", "no-cache", false);
  27. response.setStatusLine(request.httpVersion, 200, "OK");
  28. response.processAsync();
  29. response.write("--BOUNDARYOMG\r\n");
  30. while (frames.length > 0) {
  31. sendNextPart(response);
  32. }
  33. response.write("--BOUNDARYOMG--\r\n");
  34. response.finish();
  35. }
  36. function sendNextPart(response) {
  37. var nextPartHead = "Content-Type: image/gif\r\n\r\n";
  38. var inputStream = getFileAsInputStream(frames.shift());
  39. response.bodyOutputStream.write(nextPartHead, nextPartHead.length);
  40. response.bodyOutputStream.writeFrom(inputStream, inputStream.available());
  41. inputStream.close();
  42. // Toss in the boundary, so the browser can know this part is complete
  43. response.write("--BOUNDARYOMG\r\n");
  44. }