file_XHRSendData.sjs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. const CC = Components.Constructor;
  2. const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
  3. "nsIBinaryInputStream",
  4. "setInputStream");
  5. function handleRequest(request, response)
  6. {
  7. if (request.hasHeader("Content-Type"))
  8. response.setHeader("Result-Content-Type",
  9. request.getHeader("Content-Type"));
  10. response.setHeader("Content-Type", "text/plain; charset=ISO-8859-1");
  11. var body = new BinaryInputStream(request.bodyInputStream);
  12. var avail;
  13. var bytes = [];
  14. while ((avail = body.available()) > 0)
  15. Array.prototype.push.apply(bytes, body.readByteArray(avail));
  16. var data = String.fromCharCode.apply(null, bytes);
  17. response.setHeader("Result-Content-Length", "" + data.length);
  18. if (data.indexOf("TEST_REDIRECT_STR") >= 0) {
  19. var newURL = "http://" + data.split("&url=")[1];
  20. response.setStatusLine(null, 307, "redirect");
  21. response.setHeader("Location", newURL, false);
  22. }
  23. else {
  24. response.write(data);
  25. }
  26. }