1 const CC = Components.Constructor;
2 const BinaryInputStream = CC(
3 "@mozilla.org/binaryinputstream;1",
4 "nsIBinaryInputStream",
8 function utf8decode(s) {
9 return decodeURIComponent(escape(s));
12 function utf8encode(s) {
13 return unescape(encodeURIComponent(s));
16 function handleRequest(request, response) {
17 var bodyStream = new BinaryInputStream(request.bodyInputStream);
20 while ((bodyAvail = bodyStream.available()) > 0) {
21 requestBody += bodyStream.readBytes(bodyAvail);
24 if (request.method == "POST") {
25 var contentTypeParams = {};
27 .getHeader("Content-Type")
29 .forEach(function (s) {
30 if (s.indexOf("=") >= 0) {
31 let [name, value] = s.split("=");
32 contentTypeParams[name] = value;
34 contentTypeParams[""] = s;
39 contentTypeParams[""] == "multipart/form-data" &&
40 request.queryString == ""
43 .split("--" + contentTypeParams.boundary)
45 .forEach(function (s) {
47 let headerEnd = s.indexOf("\r\n\r\n");
48 s.substr(2, headerEnd - 2)
50 .forEach(function (str) {
51 // We're assuming UTF8 for now
52 let [name, value] = str.split(": ");
53 headers[name] = utf8decode(value);
56 let body = s.substring(headerEnd + 4, s.length - 2);
58 !headers["Content-Type"] ||
59 headers["Content-Type"] == "text/plain"
61 // We're assuming UTF8 for now
62 body = utf8decode(body);
64 result.push({ headers, body });
68 contentTypeParams[""] == "text/plain" &&
69 request.queryString == "plain"
71 result = utf8decode(requestBody);
74 contentTypeParams[""] == "application/x-www-form-urlencoded" &&
75 request.queryString == "url"
79 } else if (request.method == "GET") {
80 result = request.queryString;
84 response.setHeader("Content-Type", "text/plain; charset=utf-8", false);
85 response.write(utf8encode(JSON.stringify(result)));