tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sjs_method-test-server.sjs (1093B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2  * License, v. 2.0. If a copy of the MPL was not distributed with this
      3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 "use strict";
      5 
      6 const BinaryInputStream = Components.Constructor(
      7   "@mozilla.org/binaryinputstream;1",
      8   "nsIBinaryInputStream",
      9   "setInputStream"
     10 );
     11 
     12 function handleRequest(request, response) {
     13   response.setStatusLine(request.httpVersion, 200, "Och Aye");
     14   response.setHeader("Content-Type", "text/plain; charset=utf-8", false);
     15 
     16   let body = "";
     17   if (request.method == "POST") {
     18     const bodyStream = new BinaryInputStream(request.bodyInputStream);
     19 
     20     let avail = 0;
     21     while ((avail = bodyStream.available()) > 0) {
     22       body += String.fromCharCode.apply(
     23         String,
     24         bodyStream.readByteArray(avail)
     25       );
     26     }
     27   }
     28 
     29   const contentType = request.hasHeader("content-type")
     30     ? request.getHeader("content-type")
     31     : "";
     32 
     33   const bodyOutput = [request.method, contentType, body].join("\n");
     34   response.bodyOutputStream.write(bodyOutput, bodyOutput.length);
     35 }