Replaced some blocking calls in favor of non-blocking ones and also formatted the code.

This commit is contained in:
Dorian Niemiec 2023-08-01 23:37:53 +02:00 committed by svrjs
parent 17c09db695
commit 5c67e6b7a6
2 changed files with 350 additions and 201 deletions

261
index.js
View file

@ -9,23 +9,40 @@ 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 = JSON.parse(fs.readFileSync("config.json")); // Read configuration JSON
function Mod() {} function Mod() {}
Mod.prototype.callback = function callback(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) {
return function () { return function () {
if (!configJSON) configJSON = configJSONS; if (!configJSON) {
configJSON = configJSONS;
}
if (!getCustomHeaders) { if (!getCustomHeaders) {
var bheaders = JSON.parse(JSON.stringify(configJSON.customHeaders)); var bheaders = JSON.parse(JSON.stringify(configJSON.customHeaders));
} else { } else {
var bheaders = getCustomHeaders(); var bheaders = getCustomHeaders();
} }
bheaders["Content-Type"] = "text/html" //HTML output
if (!getCustomHeaders) bheaders["Server"] = "SVR.JS/" + configJSON.version + " (" + os.platform()[0].toUpperCase() + os.platform().slice(1) + ")"; //Add Server header bheaders["Content-Type"] = "text/html"; // HTML output
if (!getCustomHeaders) {
bheaders["Server"] =
"SVR.JS/" +
configJSON.version +
" (" +
os.platform()[0].toUpperCase() +
os.platform().slice(1) +
")"; // Add Server header
}
var abheaders = JSON.parse(JSON.stringify(bheaders)); var abheaders = JSON.parse(JSON.stringify(bheaders));
function executeCGI(fname, req, res, dh, nEnv) { function executeCGI(fname, req, res, dh, nEnv) {
// Function to execute CGI scripts
var env = JSON.parse(JSON.stringify(process.env)); var env = JSON.parse(JSON.stringify(process.env));
var nEnvKeys = Object.keys(nEnv); var nEnvKeys = Object.keys(nEnv);
for (var i = 0; i < nEnvKeys.length; i++) { for (var i = 0; i < nEnvKeys.length; i++) {
@ -44,12 +61,29 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
".pyw": ["python"], ".pyw": ["python"],
".rb": ["ruby"], ".rb": ["ruby"],
".php": ["php-cgi"] ".php": ["php-cgi"]
} };
var exttointerpreteruser = {}; var exttointerpreteruser = {};
fs.readFile(__dirname + "/redbrick-interpreters.json", function (err, data) {
if (!err) {
try { try {
exttointerpreteruser = JSON.parse(fs.readFileSync(__dirname + "/redbrick-interpreters.json")); exttointerpreteruser = JSON.parse(data.toString());
} catch (ex) {} } catch (ex) {}
var script = fs.readFileSync(fname).toString(); }
fs.readFile(fname, function (err, data) {
if (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;
}
var script = data.toString();
var fl = script.replace(/[\r\n]+/g, "\n").split("\n")[0]; var fl = script.replace(/[\r\n]+/g, "\n").split("\n")[0];
if (fl[0] == undefined) fl[0] = ""; if (fl[0] == undefined) fl[0] = "";
var ext = path.extname(fname); var ext = path.extname(fname);
@ -66,15 +100,24 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
var lns = script.split("\n"); var lns = script.split("\n");
if (lns.length < 2) lns = script.split("\r"); if (lns.length < 2) lns = script.split("\r");
if (lns.length > 1) lns.shift(); if (lns.length > 1) lns.shift();
try {
fs.writeFileSync(__dirname + "/../../../temp/" + tempID, lns.join(require("os").EOL)); 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); 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) { } 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" ? "\\" : "/") filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
args = []; args = [];
} else { } else {
args = exttointerpreteruser[ext]; args = exttointerpreteruser[ext];
@ -123,12 +166,14 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
var msg = "OK"; var msg = "OK";
if (bheaders[0].indexOf("HTTP/") == 0) { if (bheaders[0].indexOf("HTTP/") == 0) {
var heada = bheaders.shift(); var heada = bheaders.shift();
code = heada.split(" ")[1]; var hso = heada.split(" ");
if (heada.split(" ")[2] !== undefined) msg = heada.split(" ").splice(2).join(" "); code = hso[1];
if (hso[2] !== undefined) msg = heada.split(" ").splice(2).join(" ");
} else if (bheaders[0].indexOf(":") == -1) { } else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift(); var heada = bheaders.shift();
code = heada.split(" ")[0]; var hso = heada.split(" ");
if (heada.split(" ")[1] !== undefined) msg = heada.split(" ").splice(1).join(" "); code = hso[0];
if (hso[1] !== undefined) msg = heada.split(" ").splice(1).join(" ");
} }
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].split(": ");
@ -165,11 +210,11 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
} else { } else {
if (cned && !res.finished) res.write(data); if (cned && !res.finished) res.write(data);
} }
} };
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) {
stderr += data.toString() stderr += data.toString();
}); });
req.pipe(interpreter.stdin); req.pipe(interpreter.stdin);
interpreter.on("exit", (code, signal) => { interpreter.on("exit", (code, signal) => {
@ -186,20 +231,38 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
} }
}); });
} }
});
});
} }
function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh) { function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh) {
// Function to set up environment variables and execute CGI scripts
var nEnv = {}; var nEnv = {};
if (req.headers.authorization) {
nEnv["AUTH_TYPE"] = req.headers.authorization.split(" ")[0];
if (nEnv["AUTH_TYPE"] == "Basic") {
var remoteCred = req.headers.authorization.split(" ")[1];
if (!remoteCred) {
nEnv["REMOTE_USER"] = "redbrick_cgi_invalid_user";
} else {
var remoteCredDecoded = Buffer.from(remoteCred, "base64").toString("utf8");
nEnv["REMOTE_USER"] = remoteCredDecoded.split(":")[0];
}
} else {
nEnv["REMOTE_USER"] = "svrjs_this_property_is_not_yet_supported_by_redbrick_cgi";
}
}
nEnv["QUERY_STRING"] = req.url.split("?")[1]; nEnv["QUERY_STRING"] = req.url.split("?")[1];
if (nEnv["QUERY_STRING"] == undefined || nEnv["QUERY_STRING"] == "undefined") nEnv["QUERY_STRING"] = ""; if (nEnv["QUERY_STRING"] == undefined || nEnv["QUERY_STRING"] == "undefined") nEnv["QUERY_STRING"] = "";
nEnv["SERVER_SOFTWARE"] = software; nEnv["SERVER_SOFTWARE"] = software;
nEnv["SERVER_PROTOCOL"] = "HTTP/" + req.httpVersion; nEnv["SERVER_PROTOCOL"] = "HTTP/" + req.httpVersion;
nEnv["SERVER_PORT"] = port; nEnv["SERVER_PORT"] = port;
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"] + "]";
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"] = b; nEnv["PATH_INFO"] = decodeURI(b);
nEnv["PATH_TRANSLATED"] = (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 ? decodeURI((process.cwd() + (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"] = req.url; nEnv["REQUEST_URI"] = req.url;
@ -218,94 +281,180 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
} }
executeCGI("." + a, req, res, dh, nEnv); executeCGI("." + a, req, res, dh, nEnv);
} }
if (href.indexOf("/cgi-bin") == 0) { if (href.indexOf("/cgi-bin") == 0) {
try { fs.stat("." + href, function (err, stats) {
if (fs.existsSync("." + href)) { if (err && err.code == "ENOENT") {
var invokeElseCallback = false; var invokeElseCallback = false;
try { try {
invokeElseCallback = !fs.statSync("." + href).isFile(); invokeElseCallback = stats.isFile();
} catch (ex) {} } catch (ex) {}
if (invokeElseCallback) { if (invokeElseCallback) {
elseCallback(); elseCallback();
} else { } else {
try { try {
try { executeCGIWithEnv(
executeCGIWithEnv(href, "", req, res, req.socket.localAddress, req.socket.localPort, (getCustomHeaders ? (getCustomHeaders()["Server"] + " RedBrick/" + version) : ("SVR.JS/" + configJSON.version + " (" + os.platform()[0].toUpperCase() + os.platform().slice(1) + "; Node.JS/" + process.version + ") RedBrick/" + version)), bheaders); href,
"",
req,
res,
req.socket.localAddress,
req.socket.localPort,
getCustomHeaders ?
getCustomHeaders()["Server"] +
" RedBrick/" +
version :
"SVR.JS/" +
configJSON.version +
" (" +
os.platform()[0].toUpperCase() +
os.platform().slice(1) +
"; Node.JS/" +
process.version +
") RedBrick/" +
version,
bheaders
);
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, "Internal Server Error", abheaders); 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 unexcepted execption. Below, stack of the error is shown: </p><code>" + ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, "&nbsp;") + "</code><p>Please contact with 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.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, "&nbsp;") +
"</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(); res.end();
} else { } else {
callServerError(500, "RedBrick/" + version, ex); callServerError(500, "RedBrick/" + version, ex);
} }
} }
} catch (ex) {
if (!callServerError) {
res.writeHead(500);
res.end(ex.stack);
} else {
callServerError(500, "RedBrick/" + version, ex);
} }
} } else if (!err || (err && err.code == "ENOTDIR")) {
} function checkPath(pth, cb, a) {
} else { // Function to check the path of the file and execute CGI script
function checkPath(pth, a) {
var cpth = pth.split("/"); var cpth = pth.split("/");
if (cpth.length < 3) return false; if (cpth.length < 3) {
cb(false);
return;
}
if (!a) b = []; if (!a) b = [];
else var b = a.split("/"); else var b = a.split("/");
var isFile = false; var isFile = false;
try { fs.stat(pth, function (err, stats) {
isFile = fs.statSync(pth).isFile(); if (!err && stats.isFile()) {
} catch (ex) {} cb({
if (fs.existsSync(pth) && isFile) {
return {
fpth: pth, fpth: pth,
rpth: "/" + a rpth: (a !== undefined ? "/" + a : "")
} })
} else { } else {
b.unshift(cpth.pop()); b.unshift(cpth.pop());
return checkPath(cpth.join("/"), b.join("/")); return checkPath(cpth.join("/"), cb, b.join("/"));
} }
});
} }
var pathp = checkPath("." + href); checkPath("." + href, function (pathp) {
if (pathp == false) { if (!pathp) {
elseCallback(); elseCallback();
} else { } else {
try { try {
executeCGIWithEnv(pathp.fpth.substr(1), pathp.rpth, req, res, req.socket.localAddress, req.socket.localPort, (getCustomHeaders ? (getCustomHeaders()["Server"] + " RedBrick/" + version) : ("SVR.JS/" + configJSON.version + " (" + os.platform()[0].toUpperCase() + os.platform().slice(1) + "; Node.JS/" + process.version + ") RedBrick/" + version)), bheaders); executeCGIWithEnv(
pathp.fpth.substr(1),
pathp.rpth,
req,
res,
req.socket.localAddress,
req.socket.localPort,
getCustomHeaders ?
getCustomHeaders()["Server"] +
" RedBrick/" +
version :
"SVR.JS/" +
configJSON.version +
" (" +
os.platform()[0].toUpperCase() +
os.platform().slice(1) +
"; Node.JS/" +
process.version +
") RedBrick/" +
version,
bheaders
);
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, "Internal Server Error", abheaders); 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 unexcepted execption. Below, stack of the error is shown: </p><code>" + ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, "&nbsp;") + "</code><p>Please contact with 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.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, "&nbsp;") +
"</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(); res.end();
} else { } else {
callServerError(500, "RedBrick/" + version, ex); callServerError(500, "RedBrick/" + version, ex);
} }
} }
} }
} });
} catch (ex) { } else {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, "Internal Server Error", abheaders); 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 unexcepted execption. Below, stack of the error is shown: </p><code>" + ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, "&nbsp;") + "</code><p>Please contact with 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.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, "&nbsp;") +
"</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(); res.end();
} else { } else {
callServerError(500, "RedBrick/" + version, ex); callServerError(500, "RedBrick/" + version, err);
} }
} }
});
} else if (href == "/redbrick-interpreters.json" && path.normalize(__dirname + "/../../..") == process.cwd()) { } else if (href == "/redbrick-interpreters.json" && path.normalize(__dirname + "/../../..") == process.cwd()) {
if (!callServerError) { if (!callServerError) {
res.writeHead(403, "Forbidden", abheaders); res.writeHead(200, "OK", {
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>"); "Content-Type": "application/json",
res.end(); "Server": "RedBrick/" + version
});
res.end(JSON.stringify(exttointerpreteruser, null, 2));
} else { } else {
callServerError(403, "RedBrick/" + version); callServerError(200, "RedBrick/" + version, exttointerpreteruser);
} }
} else { } else {
elseCallback(); elseCallback();
} }
} }
} }
module.exports = Mod; module.exports = Mod;

View file

@ -1,4 +1,4 @@
{ {
"name": "DorianTech RedBrick CGI engine for SVR.JS", "name": "DorianTech RedBrick CGI engine for SVR.JS",
"version": "2.1.1" "version": "2.2.0"
} }