Improve CGI program startup, also added SERVER_ADMIN environment variable
This commit is contained in:
parent
8edef21682
commit
b4de66359d
1 changed files with 55 additions and 26 deletions
81
index.js
81
index.js
|
@ -5,6 +5,8 @@ var url = require("url");
|
|||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var childProcess = require("child_process");
|
||||
var readline = require("readline");
|
||||
|
||||
var version = "UNKNOWN";
|
||||
try {
|
||||
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());
|
||||
} catch (ex) {}
|
||||
}
|
||||
fs.readFile(fname, function (err, data) {
|
||||
fs.stat(fname, function (err, stats) {
|
||||
if (err) {
|
||||
if (!callServerError) {
|
||||
res.writeHead(500, {
|
||||
|
@ -81,43 +83,67 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
|||
callServerError(500, "RedBrick/" + version, err);
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
var script = data.toString();
|
||||
var fl = script.replace(/[\r\n]+/g, "\n").split("\n")[0];
|
||||
if (fl[0] == undefined) fl[0] = "";
|
||||
if (stats.size == 0) {
|
||||
afterShebangCallback(false);
|
||||
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 args = [];
|
||||
var buffer = "";
|
||||
var stderr = "";
|
||||
var headerendline = -1;
|
||||
var cned = false;
|
||||
var tempID = "redbrick" + Math.round(Math.random() * 1000000).toString(16) + ext;
|
||||
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") {
|
||||
args = fl.substr(2).split(" ");
|
||||
var lns = script.split("\n");
|
||||
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);
|
||||
args = passedArgs;
|
||||
args.push((process.cwd() + ("\\" + fname.replace(/\//g, "\\"))).replace(/\\+/, "\\"));
|
||||
filename = args.shift();
|
||||
} else {
|
||||
filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
|
||||
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 {
|
||||
args = exttointerpreteruser[ext];
|
||||
if (!args) {
|
||||
|
@ -127,7 +153,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
|||
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();
|
||||
}
|
||||
|
||||
|
@ -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, "");
|
||||
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["DOCUMENT_ROOT"] = process.cwd();
|
||||
nEnv["PATH_INFO"] = decodeURI(b);
|
||||
|
|
Loading…
Reference in a new issue