Improve CGI program startup, also added SERVER_ADMIN environment variable

This commit is contained in:
Dorian Niemiec 2023-08-27 09:01:30 +02:00
parent 8edef21682
commit b4de66359d

View file

@ -5,6 +5,8 @@ var url = require("url");
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var childProcess = require("child_process"); var childProcess = require("child_process");
var readline = require("readline");
var version = "UNKNOWN"; var version = "UNKNOWN";
try { try {
version = JSON.parse(fs.readFileSync(__dirname + "/mod.info")).version; version = JSON.parse(fs.readFileSync(__dirname + "/mod.info")).version;
@ -69,7 +71,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
exttointerpreteruser = JSON.parse(data.toString()); exttointerpreteruser = JSON.parse(data.toString());
} catch (ex) {} } catch (ex) {}
} }
fs.readFile(fname, function (err, data) { fs.stat(fname, function (err, stats) {
if (err) { if (err) {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, { res.writeHead(500, {
@ -81,43 +83,67 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
callServerError(500, "RedBrick/" + version, err); callServerError(500, "RedBrick/" + version, err);
} }
return; return;
} }
var script = data.toString(); if (stats.size == 0) {
var fl = script.replace(/[\r\n]+/g, "\n").split("\n")[0]; afterShebangCallback(false);
if (fl[0] == undefined) fl[0] = ""; return;
}
var s = fs.createReadStream(fname);
s.on("error", function (err) {
if (!callServerError) {
res.writeHead(500, {
"Content-Type": "text/html",
"Server": "RedBrick/" + version
});
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + err.message + "</p></body></html>");
} else {
callServerError(500, "RedBrick/" + version, err);
}
return;
}).on("open", function () {
var rl = readline.createInterface({
input: s,
});
rl.once("line", function (line) {
rl.close();
if (line.substr(0, 2) == "#!") {
var args = line.substr(2).match(/(?:[^" ]|\\(?:.|$)|"[^"]*(?:"|$))+/g);
if (!args) args = [];
args = args.map(function (arg) {
return arg.replace(/"/g, "");
});
afterShebangCallback(args);
} else if (line.substr(0, 2) == "MZ" || line.substr(0, 4) == "\x7fELF") {
afterShebangCallback("binary");
} else {
afterShebangCallback(false);
}
});
});
});
function afterShebangCallback(passedArgs) {
var ext = path.extname(fname); var ext = path.extname(fname);
var args = []; var args = [];
var buffer = ""; var buffer = "";
var stderr = ""; var stderr = "";
var headerendline = -1; var headerendline = -1;
var cned = false; var cned = false;
var tempID = "redbrick" + Math.round(Math.random() * 1000000).toString(16) + ext;
args.push(fname); args.push(fname);
if (fl.indexOf("#!") == 0) { if (passedArgs == "binary") {
filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
args = [];
} else if (passedArgs) {
if (os.platform() == "win32") { if (os.platform() == "win32") {
args = fl.substr(2).split(" "); args = passedArgs;
var lns = script.split("\n"); args.push((process.cwd() + ("\\" + fname.replace(/\//g, "\\"))).replace(/\\+/, "\\"));
if (lns.length < 2) lns = script.split("\r");
if (lns.length > 1) lns.shift();
try {
fs.writeFileSync(__dirname + "/../../../temp/" + tempID, lns.join(require("os").EOL));
} catch (ex) {
res.writeHead(500, {
"Content-Type": "text/html",
"Server": "RedBrick/" + version
});
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + ex.message + "</p></body></html>");
return;
}
args.push(__dirname + "/../../../temp/" + tempID);
filename = args.shift(); filename = args.shift();
} else { } else {
filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"); filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
args = []; args = [];
} }
} else if (fl.indexOf("\x7fELF") == 0 || fl.indexOf("MZ") == 0) {
filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
args = [];
} else { } else {
args = exttointerpreteruser[ext]; args = exttointerpreteruser[ext];
if (!args) { if (!args) {
@ -127,7 +153,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
return; return;
} }
} }
args.push(fname); args.push((process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"));
filename = args.shift(); filename = args.shift();
} }
@ -232,7 +258,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
} }
}); });
} }
}); }
}); });
} }
@ -262,6 +288,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
nEnv["SERVER_ADDR"] = pubip.replace(/^::ffff:/i, ""); nEnv["SERVER_ADDR"] = pubip.replace(/^::ffff:/i, "");
if (nEnv["SERVER_ADDR"].indexOf(":") != -1) nEnv["SERVER_ADDR"] = "[" + nEnv["SERVER_ADDR"] + "]"; if (nEnv["SERVER_ADDR"].indexOf(":") != -1) nEnv["SERVER_ADDR"] = "[" + nEnv["SERVER_ADDR"] + "]";
} }
if(configJSON.serverAdministratorEmail && configJSON.serverAdministratorEmail != "[no contact information]") {
nEnv["SERVER_ADMIN"] = configJSON.serverAdministratorEmail;
}
nEnv["SERVER_NAME"] = req.headers.host; nEnv["SERVER_NAME"] = req.headers.host;
nEnv["DOCUMENT_ROOT"] = process.cwd(); nEnv["DOCUMENT_ROOT"] = process.cwd();
nEnv["PATH_INFO"] = decodeURI(b); nEnv["PATH_INFO"] = decodeURI(b);