Compare commits
31 commits
Author | SHA1 | Date | |
---|---|---|---|
f672fcea71 | |||
11b23e99b1 | |||
3d5d57b7b4 | |||
7307229e90 | |||
36e63e9c99 | |||
a7aed50d70 | |||
a94e619f16 | |||
73a6353a98 | |||
fb9bb1e798 | |||
41a9052a07 | |||
4881a2dbd7 | |||
d9056a9f78 | |||
dfe13d711e | |||
92242b757d | |||
1851adc375 | |||
b40e9ad55a | |||
d86d190f0b | |||
cae1d77e4f | |||
f19b2e2f75 | |||
598073594e | |||
afbd0fe4c3 | |||
|
696be56402 | ||
a80d91de48 | |||
9381b3dec0 | |||
b4de66359d | |||
8edef21682 | |||
c3245f287a | |||
4011f36086 | |||
7d20f29c3b | |||
|
b54464e922 | ||
|
a2b40a67ad |
3 changed files with 581 additions and 355 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
||||||
commit.sh
|
|
487
index.js
487
index.js
|
@ -5,22 +5,74 @@ 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;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
// Can't determine version
|
// Can't determine version
|
||||||
}
|
}
|
||||||
var configJSONS = JSON.parse(fs.readFileSync("config.json")); // Read configuration JSON
|
var configJSONS = {};
|
||||||
|
try {
|
||||||
|
configJSONS = JSON.parse(fs.readFileSync(__dirname + "/../../../config.json")); // Read configuration JSON
|
||||||
|
} catch(ex) {
|
||||||
|
//RedBrick will not care about configJSONS in SVR.JS 3.x and newer
|
||||||
|
//SVR.JS 2.x and older will fail to start with broken configuration file anyway...
|
||||||
|
}
|
||||||
|
|
||||||
|
var exttointerpreteruser = {};
|
||||||
|
var scriptExts = [];
|
||||||
|
try {
|
||||||
|
exttointerpreteruser = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-interpreters.json"));
|
||||||
|
} catch (ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
scriptExts = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-scriptexts.json"));
|
||||||
|
} catch (ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var disableModExposeSupported = process.versions.svrjs && process.versions.svrjs.match(/^(?:Nightly-|(?:[4-9]|[123][0-9])[0-9]*\.|3\.(?:[1-9][0-9]+\.|9\.(?:[1-9])|4\.(?:(?:[3-9]|[12][0-9])[0-9]+|29)))/i);
|
||||||
|
|
||||||
function Mod() {}
|
function Mod() {}
|
||||||
|
|
||||||
Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData) {
|
Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData, authUser) {
|
||||||
return function () {
|
return function () {
|
||||||
if (!configJSON) {
|
if (!configJSON) {
|
||||||
configJSON = configJSONS;
|
configJSON = configJSONS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkIfThereIsA401Rule() {
|
||||||
|
var actually401 = false;
|
||||||
|
|
||||||
|
function createRegex(regex) {
|
||||||
|
var regexObj = regex.split("/");
|
||||||
|
if (regexObj.length == 0) throw new Error("Invalid regex!");
|
||||||
|
var modifiers = regexObj.pop();
|
||||||
|
regexObj.shift();
|
||||||
|
var searchString = regexObj.join("/");
|
||||||
|
return new RegExp(searchString, modifiers);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(configJSON.nonStandardCodes) {
|
||||||
|
configJSON.nonStandardCodes.every(function (nonscode) {
|
||||||
|
if (nonscode.scode == 401) {
|
||||||
|
if (nonscode.regex && (req.url.match(createRegex(nonscode.regex)) || href.match(createRegex(nonscode.regex)))) {
|
||||||
|
actually401 = true;
|
||||||
|
return true;
|
||||||
|
} else if (nonscode.url && (nonStandardCodes[i].url == href || (os.platform() == "win32" && nonStandardCodes[i].url.toLowerCase() == href.toLowerCase()))) {
|
||||||
|
actually401 = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return actually401;
|
||||||
|
}
|
||||||
|
|
||||||
if (!getCustomHeaders) {
|
if (!getCustomHeaders) {
|
||||||
var bheaders = JSON.parse(JSON.stringify(configJSON.customHeaders));
|
var bheaders = JSON.parse(JSON.stringify(configJSON.customHeaders));
|
||||||
} else {
|
} else {
|
||||||
|
@ -54,22 +106,16 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
".sh": ["bash"],
|
".sh": ["bash"],
|
||||||
".ksh": ["ksh"],
|
".ksh": ["ksh"],
|
||||||
".csh": ["csh"],
|
".csh": ["csh"],
|
||||||
".bat": ["cmd", "/c"],
|
|
||||||
".cmd": ["cmd", "/c"],
|
|
||||||
".vbs": ["cscript"],
|
|
||||||
".jar": ["java"],
|
|
||||||
".pyw": ["python"],
|
|
||||||
".rb": ["ruby"],
|
".rb": ["ruby"],
|
||||||
".php": ["php-cgi"]
|
".php": ["php-cgi"]
|
||||||
};
|
};
|
||||||
var exttointerpreteruser = {};
|
if (os.platform() == "win32") {
|
||||||
fs.readFile(__dirname + "/redbrick-interpreters.json", function (err, data) {
|
exttointerpreter[".exe"] = [];
|
||||||
if (!err) {
|
exttointerpreter[".bat"] = ["cmd", "/c"];
|
||||||
try {
|
exttointerpreter[".vbs"] = ["cscript"];
|
||||||
exttointerpreteruser = JSON.parse(data.toString());
|
|
||||||
} 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, {
|
||||||
|
@ -77,58 +123,84 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
"Server": "RedBrick/" + version
|
"Server": "RedBrick/" + version
|
||||||
});
|
});
|
||||||
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + err.message + "</p></body></html>");
|
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + err.message + "</p></body></html>");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
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 (os.platform() != "win32" && 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(fname);
|
|
||||||
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 === null) {
|
||||||
|
elseCallback();
|
||||||
|
return;
|
||||||
|
} else if (!args) {
|
||||||
args = exttointerpreter[ext];
|
args = exttointerpreter[ext];
|
||||||
if (!args) {
|
if (!args) {
|
||||||
elseCallback();
|
elseCallback();
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,55 +224,66 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var dataHandler = function (data) {
|
var dataHandler = function (data) {
|
||||||
buffer += data.toString("latin1");
|
if (!cned) buffer += data.toString("latin1");
|
||||||
var m = null;
|
var m = null;
|
||||||
if (!cned) m = buffer.match(/(?:\r\n\r\n|\n\n|\r\r)/);
|
if (!cned) m = buffer.match(/(?:\r\n\r\n|\n\r\n\r|\n\n|\r\r)/);
|
||||||
if (!cned && m) {
|
if (!cned && m) {
|
||||||
cned = true;
|
cned = true;
|
||||||
eol = m[0];
|
eol = m[0];
|
||||||
headerendline = m.index;
|
headerendline = m.index;
|
||||||
var bheaders = buffer.substr(0, headerendline).split(/(?:\r\n|\n|\r)/);
|
var bheaders = buffer.substr(0, headerendline).split(/(?:\r\n|\n\r|\n|\r)/);
|
||||||
var bheaderso = {};
|
var bheaderso = {};
|
||||||
if (dh) bheaderso = dh;
|
if (dh) bheaderso = dh;
|
||||||
var code = 200;
|
var code = 200;
|
||||||
var msg = "OK";
|
var msg = "OK";
|
||||||
if (bheaders[0].indexOf("HTTP/") == 0) {
|
var httpMatch = bheaders[0].match(/^HTTP\/[^ ]+ ([0-9]{3})(?: (.*))?$/);
|
||||||
var heada = bheaders.shift();
|
if (httpMatch) {
|
||||||
var hso = heada.split(" ");
|
bheaders.shift();
|
||||||
code = hso[1];
|
code = parseInt(httpMatch[1]);
|
||||||
if (hso[2] !== undefined) msg = heada.split(" ").splice(2).join(" ");
|
if (httpMatch[2]) msg = httpMatch[2];
|
||||||
|
else msg = http.STATUS_CODES[code];
|
||||||
} else if (bheaders[0].indexOf(":") == -1) {
|
} else if (bheaders[0].indexOf(":") == -1) {
|
||||||
var heada = bheaders.shift();
|
var heada = bheaders.shift();
|
||||||
var hso = heada.split(" ");
|
var hso = heada.match(/^([0-9]{3})(?: (.*))?$/);
|
||||||
if (hso[0].match(/^[0-9]{3}$/)) {
|
if (hso) {
|
||||||
code = hso[0];
|
code = parseInt(hso[1]);
|
||||||
if (hso[1] !== undefined) msg = heada.split(" ").splice(1).join(" ");
|
if (hso[2]) msg = hso[2];
|
||||||
|
else msg = http.STATUS_CODES[code];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var hasLocation = false;
|
||||||
for (var i = 0; i < bheaders.length; i++) {
|
for (var i = 0; i < bheaders.length; i++) {
|
||||||
var headerp = bheaders[i].split(": ");
|
var headerp = bheaders[i].match(/^([^:]*)(?:: (.*))?/);
|
||||||
var headern = headerp.shift();
|
if (!headerp) headerp = [];
|
||||||
var headerv = headerp.join(": ");
|
var headern = headerp[1];
|
||||||
|
var headerv = headerp[2];
|
||||||
if (headern.toLowerCase() == "status") {
|
if (headern.toLowerCase() == "status") {
|
||||||
code = headerv.split(" ")[0];
|
var httpMatch = headerv.match(/^([0-9]{3})(?: (.*))?$/);
|
||||||
if (headerv.split(" ")[1] !== undefined) msg = headerv.split(" ").splice(1).join(" ");
|
if (httpMatch) {
|
||||||
|
code = parseInt(httpMatch[1]);
|
||||||
|
if (httpMatch[2]) msg = httpMatch[2];
|
||||||
|
else msg = http.STATUS_CODES[code];
|
||||||
|
}
|
||||||
} else if (headern.toLowerCase() == "set-cookie") {
|
} else if (headern.toLowerCase() == "set-cookie") {
|
||||||
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
|
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
|
||||||
bheaderso["Set-Cookie"].push(headerv);
|
bheaderso["Set-Cookie"].push(headerv);
|
||||||
} else {
|
} else {
|
||||||
|
if (headern.toLowerCase() == "location") hasLocation = true;
|
||||||
bheaderso[headern] = headerv;
|
bheaderso[headern] = headerv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (code == 200 && (bheaderso["Location"] || bheaderso["location"])) {
|
if ((code < 300 || code > 399) && hasLocation) {
|
||||||
code = 302;
|
code = 302;
|
||||||
msg = "Found";
|
msg = "Found";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
res.writeHead(code, msg, bheaderso);
|
res.writeHead(code, msg, bheaderso);
|
||||||
res.write(buffer.substr(headerendline + eol.length), "latin1");
|
res.write(Buffer.from(buffer.substr(headerendline + eol.length), "latin1"));
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
interpreter.removeAllListeners("exit");
|
||||||
|
interpreter.stdout.removeAllListeners("data");
|
||||||
if (!callServerError) {
|
if (!callServerError) {
|
||||||
res.writeHead(500);
|
res.writeHead(500);
|
||||||
res.end(ex.stack);
|
res.end(ex.stack);
|
||||||
|
@ -210,9 +293,21 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (cned && !res.finished) res.write(data);
|
if (cned && !res.finished) {
|
||||||
|
res.write(data);
|
||||||
|
interpreter.stdout.removeListener("data", dataHandler);
|
||||||
|
interpreter.stdout.pipe(res, {end: false});
|
||||||
|
dataHandler = function () {}; //Prevent event listener memory leaks
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
res.prependListener("close", function() {
|
||||||
|
if(interpreter.stdout) interpreter.stdout.unpipe(res); //Prevent server crashes with write after the end
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on("error", function() {}); //Suppress response stream errors
|
||||||
|
|
||||||
if (interpreter.stdout) {
|
if (interpreter.stdout) {
|
||||||
interpreter.stdout.on("data", dataHandler);
|
interpreter.stdout.on("data", dataHandler);
|
||||||
interpreter.stderr.on("data", function (data) {
|
interpreter.stderr.on("data", function (data) {
|
||||||
|
@ -229,18 +324,29 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
callServerError(500, "RedBrick/" + version, ex);
|
callServerError(500, "RedBrick/" + version, ex);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.end();
|
var preparedStderr = stderr.trim()
|
||||||
|
if(preparedStderr) {
|
||||||
|
serverconsole.errmessage("There were CGI application errors:");
|
||||||
|
serverconsole.errmessage(preparedStderr);
|
||||||
|
}
|
||||||
|
interpreter.stdout.removeListener("data", dataHandler);
|
||||||
|
interpreter.stdout.unpipe(res); //Prevent server crashes with write after the end
|
||||||
|
if(!res.finished) res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh) {
|
function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh, user) {
|
||||||
// Function to set up environment variables and execute CGI scripts
|
// Function to set up environment variables and execute CGI scripts
|
||||||
var nEnv = {};
|
var nEnv = {};
|
||||||
if (req.headers.authorization) {
|
if (typeof user != "undefined") {
|
||||||
|
if (user !== null) {
|
||||||
|
if (req.headers.authorization) nEnv["AUTH_TYPE"] = req.headers.authorization.split(" ")[0];
|
||||||
|
nEnv["REMOTE_USER"] = user;
|
||||||
|
}
|
||||||
|
} else if (req.headers.authorization && (typeof checkIfThereIsA401Rule == "undefined" || checkIfThereIsA401Rule())) {
|
||||||
nEnv["AUTH_TYPE"] = req.headers.authorization.split(" ")[0];
|
nEnv["AUTH_TYPE"] = req.headers.authorization.split(" ")[0];
|
||||||
if (nEnv["AUTH_TYPE"] == "Basic") {
|
if (nEnv["AUTH_TYPE"] == "Basic") {
|
||||||
var remoteCred = req.headers.authorization.split(" ")[1];
|
var remoteCred = req.headers.authorization.split(" ")[1];
|
||||||
|
@ -263,17 +369,25 @@ 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"] = decodeURIComponent(b);
|
||||||
nEnv["PATH_TRANSLATED"] = b ? decodeURI((process.cwd() + (require("os").platform == "win32" ? b.replace(/\//g, "\\") : b)).replace((require("os").platform == "win32" ? /\\\\/g : /\/\//g), (require("os").platform == "win32" ? "\\" : "/"))) : "";
|
nEnv["PATH_TRANSLATED"] = b ? ((process.cwd() + decodeURIComponent(require("os").platform == "win32" ? b.replace(/\//g, "\\") : b)).replace((require("os").platform == "win32" ? /\\\\/g : /\/\//g), (require("os").platform == "win32" ? "\\" : "/"))) : "";
|
||||||
nEnv["REQUEST_METHOD"] = req.method;
|
nEnv["REQUEST_METHOD"] = req.method;
|
||||||
nEnv["GATEWAY_INTERFACE"] = "CGI/1.1";
|
nEnv["GATEWAY_INTERFACE"] = "CGI/1.1";
|
||||||
nEnv["REQUEST_URI"] = (!origHref || origHref == href) ? req.url : (origHref + (uobject.search ? ("?" + uobject.search) : ""));
|
nEnv["REQUEST_URI"] = (!origHref || origHref == href) ? req.url : (origHref + (uobject.search ? ("?" + uobject.search) : ""));
|
||||||
nEnv["REMOTE_ADDR"] = (req.socket.realRemoteAddress ? req.socket.realRemoteAddress : ((req.headers["x-forwarded-for"] && configJSON.enableIPSpoofing) ? req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "") : req.socket.remoteAddress)).replace(/^::ffff:/i, "");
|
nEnv["REMOTE_ADDR"] = (req.socket.realRemoteAddress ? req.socket.realRemoteAddress : ((req.headers["x-forwarded-for"] && configJSON.enableIPSpoofing) ? req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "") : req.socket.remoteAddress)).replace(/^::ffff:/i, "");
|
||||||
nEnv["REMOTE_PORT"] = (req.socket.realRemotePort ? req.socket.realRemotePort : req.socket.remotePort);
|
if (req.socket.realRemoteAddress && req.socket.realRemotePort) {
|
||||||
|
nEnv["REMOTE_PORT"] = req.socket.realRemotePort;
|
||||||
|
} else if (!(req.socket.realRemoteAddress && !req.socket.realRemotePort)) {
|
||||||
|
nEnv["REMOTE_PORT"] = req.socket.remotePort;
|
||||||
|
}
|
||||||
nEnv["SCRIPT_NAME"] = a;
|
nEnv["SCRIPT_NAME"] = a;
|
||||||
nEnv["SCRIPT_FILENAME"] = (process.cwd() + (require("os").platform == "win32" ? a.replace(/\//g, "\\") : a)).replace((require("os").platform == "win32" ? /\\\\/g : /\/\//g), (require("os").platform == "win32" ? "\\" : "/"));
|
nEnv["SCRIPT_FILENAME"] = (process.cwd() + (require("os").platform == "win32" ? a.replace(/\//g, "\\") : a)).replace((require("os").platform == "win32" ? /\\\\/g : /\/\//g), (require("os").platform == "win32" ? "\\" : "/"));
|
||||||
|
if (req.socket.encrypted) nEnv["HTTPS"] = "ON";
|
||||||
if (req.headers["content-type"]) nEnv["CONTENT_TYPE"] = req.headers["content-type"];
|
if (req.headers["content-type"]) nEnv["CONTENT_TYPE"] = req.headers["content-type"];
|
||||||
if (req.headers["content-length"]) nEnv["CONTENT_LENGTH"] = req.headers["content-length"];
|
if (req.headers["content-length"]) nEnv["CONTENT_LENGTH"] = req.headers["content-length"];
|
||||||
var nh = JSON.parse(JSON.stringify(req.headers));
|
var nh = JSON.parse(JSON.stringify(req.headers));
|
||||||
|
@ -286,15 +400,43 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
executeCGI("." + a, req, res, dh, nEnv);
|
executeCGI("." + a, req, res, dh, nEnv);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (href.indexOf("/cgi-bin") == 0) {
|
var isCgiBin = href.match(new RegExp("^/cgi-bin(?:$|[?#/])", os.platform() == "win32" ? "i" : ""));
|
||||||
fs.stat("." + href, function (err, stats) {
|
var isScriptExt = scriptExts.indexOf("." + ext) != -1;
|
||||||
|
|
||||||
|
if ((href == "/redbrick-interpreters.json" || href == "/redbrick-scriptexts.json" || (os.platform() == "win32" && (href.toLowerCase() == "/redbrick-interpreters.json" || href.toLowerCase() == "/redbrick-scriptexts.json"))) && path.normalize(__dirname + "/../../..") == process.cwd()) {
|
||||||
|
if (!callServerError) {
|
||||||
|
res.writeHead(403, "Forbidden", {
|
||||||
|
"Content-Type": "text/html",
|
||||||
|
"Server": "RedBrick/" + version
|
||||||
|
});
|
||||||
|
res.write(
|
||||||
|
"<html><head><title>403 Forbidden</title></head><body><h1>403 Forbidden</h1><p>You don't have access to specific page.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
|
||||||
|
configJSON.version +
|
||||||
|
" (" +
|
||||||
|
os.platform()[0].toUpperCase() +
|
||||||
|
os.platform().slice(1) +
|
||||||
|
"; Node.JS/" +
|
||||||
|
process.version +
|
||||||
|
") RedBrick/" +
|
||||||
|
version +
|
||||||
|
" " +
|
||||||
|
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
|
||||||
|
"</p></body></html>"
|
||||||
|
);
|
||||||
|
res.end(JSON.stringify(exttointerpreteruser, null, 2));
|
||||||
|
} else {
|
||||||
|
callServerError(403, "RedBrick/" + version);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fs.stat("." + decodeURIComponent(href), function (err, stats) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
if (!stats.isFile()) {
|
if (!stats.isFile()) {
|
||||||
fs.stat("." + href + "/index.php", function (e2, s2) {
|
if (isCgiBin || scriptExts.indexOf(".php") != -1) {
|
||||||
|
fs.stat("." + decodeURIComponent(href) + "/index.php", function (e2, s2) {
|
||||||
if (!e2 && s2.isFile()) {
|
if (!e2 && s2.isFile()) {
|
||||||
try {
|
try {
|
||||||
executeCGIWithEnv(
|
executeCGIWithEnv(
|
||||||
(href + "/index.php").replace(/\/+/g, "/"),
|
(decodeURIComponent(href) + "/index.php").replace(/\/+/g, "/"),
|
||||||
"",
|
"",
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
|
@ -302,8 +444,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
req.socket.localPort,
|
req.socket.localPort,
|
||||||
getCustomHeaders ?
|
getCustomHeaders ?
|
||||||
getCustomHeaders()["Server"] +
|
getCustomHeaders()["Server"] +
|
||||||
|
(disableModExposeSupported && (configJSON.exposeModsInErrorPages || configJSON.exposeModsInErrorPages === undefined) ?
|
||||||
" RedBrick/" +
|
" RedBrick/" +
|
||||||
version :
|
version : "") :
|
||||||
"SVR.JS/" +
|
"SVR.JS/" +
|
||||||
configJSON.version +
|
configJSON.version +
|
||||||
" (" +
|
" (" +
|
||||||
|
@ -313,7 +456,60 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
process.version +
|
process.version +
|
||||||
") RedBrick/" +
|
") RedBrick/" +
|
||||||
version,
|
version,
|
||||||
bheaders
|
bheaders,
|
||||||
|
authUser
|
||||||
|
);
|
||||||
|
} catch (ex) {
|
||||||
|
if (!callServerError) {
|
||||||
|
res.writeHead(500, "Internal Server Error", abheaders);
|
||||||
|
res.write(
|
||||||
|
"<html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1><p>A server had unexpected exception. Below, the stack trace of the error is shown:</p><code>" +
|
||||||
|
ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, " ") +
|
||||||
|
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
|
||||||
|
configJSON.version +
|
||||||
|
" (" +
|
||||||
|
os.platform()[0].toUpperCase() +
|
||||||
|
os.platform().slice(1) +
|
||||||
|
"; Node.JS/" +
|
||||||
|
process.version +
|
||||||
|
") RedBrick/" +
|
||||||
|
version +
|
||||||
|
" " +
|
||||||
|
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
|
||||||
|
"</p></body></html>"
|
||||||
|
);
|
||||||
|
res.end();
|
||||||
|
} else {
|
||||||
|
callServerError(500, "RedBrick/" + version, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (isCgiBin || scriptExts.indexOf(".cgi") != -1) {
|
||||||
|
fs.stat("." + decodeURIComponent(href) + "/index.cgi", function (e3, s3) {
|
||||||
|
if (!e3 && s3.isFile()) {
|
||||||
|
try {
|
||||||
|
executeCGIWithEnv(
|
||||||
|
(decodeURIComponent(href) + "/index.cgi").replace(/\/+/g, "/"),
|
||||||
|
"",
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
req.socket.localAddress,
|
||||||
|
req.socket.localPort,
|
||||||
|
getCustomHeaders ?
|
||||||
|
getCustomHeaders()["Server"] +
|
||||||
|
(disableModExposeSupported && (configJSON.exposeModsInErrorPages || configJSON.exposeModsInErrorPages === undefined) ?
|
||||||
|
" RedBrick/" +
|
||||||
|
version : "") :
|
||||||
|
"SVR.JS/" +
|
||||||
|
configJSON.version +
|
||||||
|
" (" +
|
||||||
|
os.platform()[0].toUpperCase() +
|
||||||
|
os.platform().slice(1) +
|
||||||
|
"; Node.JS/" +
|
||||||
|
process.version +
|
||||||
|
") RedBrick/" +
|
||||||
|
version,
|
||||||
|
bheaders,
|
||||||
|
authUser
|
||||||
);
|
);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
if (!callServerError) {
|
if (!callServerError) {
|
||||||
|
@ -340,14 +536,19 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
elseCallback();
|
elseCallback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
elseCallback();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (scriptExts.indexOf(".cgi") != -1) {
|
||||||
|
fs.stat("." + decodeURIComponent(href) + "/index.cgi", function (e3, s3) {
|
||||||
|
if (!e3 && s3.isFile()) {
|
||||||
try {
|
try {
|
||||||
executeCGIWithEnv(
|
executeCGIWithEnv(
|
||||||
href,
|
(decodeURIComponent(href) + "/index.cgi").replace(/\/+/g, "/"),
|
||||||
"",
|
"",
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
|
@ -355,8 +556,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
req.socket.localPort,
|
req.socket.localPort,
|
||||||
getCustomHeaders ?
|
getCustomHeaders ?
|
||||||
getCustomHeaders()["Server"] +
|
getCustomHeaders()["Server"] +
|
||||||
|
(disableModExposeSupported && (configJSON.exposeModsInErrorPages || configJSON.exposeModsInErrorPages === undefined) ?
|
||||||
" RedBrick/" +
|
" RedBrick/" +
|
||||||
version :
|
version : "") :
|
||||||
"SVR.JS/" +
|
"SVR.JS/" +
|
||||||
configJSON.version +
|
configJSON.version +
|
||||||
" (" +
|
" (" +
|
||||||
|
@ -366,7 +568,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
process.version +
|
process.version +
|
||||||
") RedBrick/" +
|
") RedBrick/" +
|
||||||
version,
|
version,
|
||||||
bheaders
|
bheaders,
|
||||||
|
authUser
|
||||||
);
|
);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
if (!callServerError) {
|
if (!callServerError) {
|
||||||
|
@ -392,12 +595,73 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
callServerError(500, "RedBrick/" + version, ex);
|
callServerError(500, "RedBrick/" + version, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
elseCallback();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
elseCallback();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isCgiBin || isScriptExt) {
|
||||||
|
try {
|
||||||
|
executeCGIWithEnv(
|
||||||
|
decodeURIComponent(href),
|
||||||
|
"",
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
req.socket.localAddress,
|
||||||
|
req.socket.localPort,
|
||||||
|
getCustomHeaders ?
|
||||||
|
getCustomHeaders()["Server"] +
|
||||||
|
(disableModExposeSupported && (configJSON.exposeModsInErrorPages || configJSON.exposeModsInErrorPages === undefined) ?
|
||||||
|
" RedBrick/" +
|
||||||
|
version : "") :
|
||||||
|
"SVR.JS/" +
|
||||||
|
configJSON.version +
|
||||||
|
" (" +
|
||||||
|
os.platform()[0].toUpperCase() +
|
||||||
|
os.platform().slice(1) +
|
||||||
|
"; Node.JS/" +
|
||||||
|
process.version +
|
||||||
|
") RedBrick/" +
|
||||||
|
version,
|
||||||
|
bheaders,
|
||||||
|
authUser
|
||||||
|
);
|
||||||
|
} catch (ex) {
|
||||||
|
if (!callServerError) {
|
||||||
|
res.writeHead(500, "Internal Server Error", abheaders);
|
||||||
|
res.write(
|
||||||
|
"<html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1><p>A server had unexpected exception. Below, the stack trace of the error is shown:</p><code>" +
|
||||||
|
ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, " ") +
|
||||||
|
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
|
||||||
|
configJSON.version +
|
||||||
|
" (" +
|
||||||
|
os.platform()[0].toUpperCase() +
|
||||||
|
os.platform().slice(1) +
|
||||||
|
"; Node.JS/" +
|
||||||
|
process.version +
|
||||||
|
") RedBrick/" +
|
||||||
|
version +
|
||||||
|
" " +
|
||||||
|
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
|
||||||
|
"</p></body></html>"
|
||||||
|
);
|
||||||
|
res.end();
|
||||||
|
} else {
|
||||||
|
callServerError(500, "RedBrick/" + version, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
elseCallback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (err && err.code == "ENOTDIR") {
|
} else if (err && err.code == "ENOTDIR") {
|
||||||
function checkPath(pth, cb, a) {
|
function checkPath(pth, cb, a) {
|
||||||
// Function to check the path of the file and execute CGI script
|
// Function to check the path of the file and execute CGI script
|
||||||
var cpth = pth.split("/");
|
var cpth = pth.split("/");
|
||||||
if (cpth.length < 3) {
|
if (cpth.length < (isCgiBin ? 3 : 2)) {
|
||||||
cb(false);
|
cb(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -410,25 +674,18 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
fpth: pth,
|
fpth: pth,
|
||||||
rpth: (a !== undefined ? "/" + a : "")
|
rpth: (a !== undefined ? "/" + a : "")
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
fs.stat(pth + "/index.php", function (e2, s2) {
|
|
||||||
if (!e2 && s2.isFile()) {
|
|
||||||
cb({
|
|
||||||
fpth: (pth + "/index.php").replace(/\/+/g, "/"),
|
|
||||||
rpth: (a !== undefined ? "/" + a : "")
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
b.unshift(cpth.pop());
|
b.unshift(cpth.pop());
|
||||||
return checkPath(cpth.join("/"), cb, b.join("/"));
|
return checkPath(cpth.join("/"), cb, b.join("/"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
checkPath("." + decodeURIComponent(href), function (pathp) {
|
||||||
}
|
|
||||||
checkPath("." + href, function (pathp) {
|
|
||||||
if (!pathp) {
|
if (!pathp) {
|
||||||
elseCallback();
|
elseCallback();
|
||||||
} else {
|
} else {
|
||||||
|
var newext = path.extname(pathp.fpth);
|
||||||
|
if (isCgiBin || scriptExts.indexOf(newext) != -1) {
|
||||||
try {
|
try {
|
||||||
executeCGIWithEnv(
|
executeCGIWithEnv(
|
||||||
pathp.fpth.substr(1),
|
pathp.fpth.substr(1),
|
||||||
|
@ -439,8 +696,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
req.socket.localPort,
|
req.socket.localPort,
|
||||||
getCustomHeaders ?
|
getCustomHeaders ?
|
||||||
getCustomHeaders()["Server"] +
|
getCustomHeaders()["Server"] +
|
||||||
|
(disableModExposeSupported && (configJSON.exposeModsInErrorPages || configJSON.exposeModsInErrorPages === undefined) ?
|
||||||
" RedBrick/" +
|
" RedBrick/" +
|
||||||
version :
|
version : "") :
|
||||||
"SVR.JS/" +
|
"SVR.JS/" +
|
||||||
configJSON.version +
|
configJSON.version +
|
||||||
" (" +
|
" (" +
|
||||||
|
@ -450,7 +708,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
process.version +
|
process.version +
|
||||||
") RedBrick/" +
|
") RedBrick/" +
|
||||||
version,
|
version,
|
||||||
bheaders
|
bheaders,
|
||||||
|
authUser
|
||||||
);
|
);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
if (!callServerError) {
|
if (!callServerError) {
|
||||||
|
@ -476,49 +735,17 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
callServerError(500, "RedBrick/" + version, ex);
|
callServerError(500, "RedBrick/" + version, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (err && err.code == "ENOENT") {
|
|
||||||
elseCallback(); //Invoke default error handler
|
|
||||||
} else {
|
|
||||||
if (!callServerError) {
|
|
||||||
res.writeHead(500, "Internal Server Error", abheaders);
|
|
||||||
res.write(
|
|
||||||
"<html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1><p>A server had unexpected exception. Below, the stack trace of the error is shown:</p><code>" +
|
|
||||||
err.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, " ") +
|
|
||||||
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
|
|
||||||
configJSON.version +
|
|
||||||
" (" +
|
|
||||||
os.platform()[0].toUpperCase() +
|
|
||||||
os.platform().slice(1) +
|
|
||||||
"; Node.JS/" +
|
|
||||||
process.version +
|
|
||||||
") RedBrick/" +
|
|
||||||
version +
|
|
||||||
" " +
|
|
||||||
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
|
|
||||||
"</p></body></html>"
|
|
||||||
);
|
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
callServerError(500, "RedBrick/" + version, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (href == "/redbrick-interpreters.json" && path.normalize(__dirname + "/../../..") == process.cwd()) {
|
|
||||||
if (!callServerError) {
|
|
||||||
res.writeHead(200, "OK", {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Server": "RedBrick/" + version
|
|
||||||
});
|
|
||||||
res.end(JSON.stringify(exttointerpreteruser, null, 2));
|
|
||||||
} else {
|
|
||||||
callServerError(200, "RedBrick/" + version, exttointerpreteruser);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
elseCallback();
|
elseCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
elseCallback(); //Invoke default error handler
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Mod;
|
module.exports = Mod;
|
||||||
|
|
4
mod.info
4
mod.info
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"name": "DorianTech RedBrick CGI engine for SVR.JS",
|
"name": "RedBrick CGI engine for SVR.JS",
|
||||||
"version": "2.3.1"
|
"version": "Nightly-GitMain"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue