Clean up the code

This commit is contained in:
Dorian Niemiec 2023-11-23 04:34:17 +01:00
parent 1851adc375
commit 92242b757d

818
index.js
View file

@ -18,13 +18,13 @@ var configJSONS = JSON.parse(fs.readFileSync(__dirname + "/../../../config.json"
var exttointerpreteruser = {}; var exttointerpreteruser = {};
var scriptExts = []; var scriptExts = [];
try { try {
exttointerpreteruser = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-interpreters.json")); exttointerpreteruser = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-interpreters.json"));
} catch(ex) { } catch (ex) {
} }
try { try {
scriptExts = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-scriptexts.json")); scriptExts = JSON.parse(fs.readFileSync(__dirname + "/../../../redbrick-scriptexts.json"));
} catch(ex) { } catch (ex) {
} }
@ -72,210 +72,210 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
".rb": ["ruby"], ".rb": ["ruby"],
".php": ["php-cgi"] ".php": ["php-cgi"]
}; };
if(os.platform() == "win32") { if (os.platform() == "win32") {
exttointerpreter[".exe"] = []; exttointerpreter[".exe"] = [];
exttointerpreter[".bat"] = ["cmd", "/c"]; exttointerpreter[".bat"] = ["cmd", "/c"];
exttointerpreter[".vbs"] = ["cscript"]; exttointerpreter[".vbs"] = ["cscript"];
} }
fs.stat(fname, function (err, stats) { fs.stat(fname, function (err, stats) {
if (err) { if (err) {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, { res.writeHead(500, {
"Content-Type": "text/html", "Content-Type": "text/html",
"Server": "RedBrick/" + version "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;
}
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,
}); });
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + err.message + "</p></body></html>");
} else {
callServerError(500, "RedBrick/" + version, err);
}
return;
rl.once("line", function (line) { }
rl.close(); if (stats.size == 0) {
if (line.substr(0, 2) == "#!") { afterShebangCallback(false);
var args = line.substr(2).match(/(?:[^" ]|\\(?:.|$)|"[^"]*(?:"|$))+/g); return;
if (!args) args = []; }
args = args.map(function (arg) { var s = fs.createReadStream(fname);
return arg.replace(/"/g, ""); s.on("error", function (err) {
}); if (!callServerError) {
afterShebangCallback(args); res.writeHead(500, {
} else if (os.platform() != "win32" && line.substr(0, 4) == "\x7fELF") { "Content-Type": "text/html",
afterShebangCallback("binary"); "Server": "RedBrick/" + version
} else {
afterShebangCallback(false);
}
}); });
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) { 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;
args.push(fname); args.push(fname);
if (passedArgs == "binary") { 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 = 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" ? "\\" : "/"); filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/");
args = []; args = [];
} else if (passedArgs) { }
if (os.platform() == "win32") { } else {
args = passedArgs; args = exttointerpreteruser[ext];
args.push((process.cwd() + ("\\" + fname.replace(/\//g, "\\"))).replace(/\\+/, "\\")); if (args === null) {
filename = args.shift(); elseCallback();
} else { return;
filename = (process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"); } else if (!args) {
args = []; args = exttointerpreter[ext];
} if (!args) {
} else {
args = exttointerpreteruser[ext];
if (args === null) {
elseCallback(); elseCallback();
return; return;
} else if (!args) { }
args = exttointerpreter[ext]; }
if (!args) { args.push((process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"));
elseCallback(); filename = args.shift();
return; }
var wd = fname.split("/");
wd[0] = "";
wd[wd.length - 1] = "";
wd = wd.join(os.platform() == "win32" ? "\\" : "/");
var interpreter = childProcess.spawn(filename, args, {
cwd: (process.cwd() + wd).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"),
env: env
});
interpreter.on("error", (error) => {
if (!callServerError) {
res.writeHead(500, {
"Content-Type": "text/html",
"Server": "RedBrick/" + version
});
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + error.message + "</p></body></html>");
} else {
callServerError(500, "RedBrick/" + version, error);
}
});
var dataHandler = function (data) {
buffer += data.toString("latin1");
var m = null;
if (!cned) m = buffer.match(/(?:\r\n\r\n|\n\r\n\r|\n\n|\r\r)/);
if (!cned && m) {
cned = true;
eol = m[0];
headerendline = m.index;
var bheaders = buffer.substr(0, headerendline).split(/(?:\r\n|\n\r|\n|\r)/);
var bheaderso = {};
if (dh) bheaderso = dh;
var code = 200;
var msg = "OK";
var httpMatch = bheaders[0].match(/^HTTP\/[^ ]+ ([0-9]{3})(?: (.*))?$/);
if (httpMatch) {
bheaders.shift();
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
} else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift();
var hso = heada.match(/^([0-9]{3})(?: (.*))?$/);
if (hso) {
code = parseInt(hso[1]);
if (hso[2]) msg = hso[2];
else msg = http.STATUS_CODES[code];
} }
} }
args.push((process.cwd() + (os.platform() == "win32" ? "\\" + fname.replace(/\//g, "\\") : "/" + fname)).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/")); for (var i = 0; i < bheaders.length; i++) {
filename = args.shift(); var headerp = bheaders[i].match(/^([^:]*)(?:: (.*))?/);
} if (!headerp) headerp = [];
var headern = headerp[1];
var wd = fname.split("/"); var headerv = headerp[2];
wd[0] = ""; if (headern.toLowerCase() == "status") {
wd[wd.length - 1] = ""; var httpMatch = headerv.match(/^([0-9]{3})(?: (.*))?$/);
wd = wd.join(os.platform() == "win32" ? "\\" : "/"); if (httpMatch) {
var interpreter = childProcess.spawn(filename, args, { code = parseInt(httpMatch[1]);
cwd: (process.cwd() + wd).replace(os.platform() == "win32" ? /\\+/ : /\/+/, os.platform() == "win32" ? "\\" : "/"), if (httpMatch[2]) msg = httpMatch[2];
env: env
});
interpreter.on("error", (error) => {
if (!callServerError) {
res.writeHead(500, {
"Content-Type": "text/html",
"Server": "RedBrick/" + version
});
res.end("<html><head></head><body><h1>RedBrick Error!</h1><p>Reason: " + error.message + "</p></body></html>");
} else {
callServerError(500, "RedBrick/" + version, error);
}
});
var dataHandler = function (data) {
buffer += data.toString("latin1");
var m = null;
if (!cned) m = buffer.match(/(?:\r\n\r\n|\n\r\n\r|\n\n|\r\r)/);
if (!cned && m) {
cned = true;
eol = m[0];
headerendline = m.index;
var bheaders = buffer.substr(0, headerendline).split(/(?:\r\n|\n\r|\n|\r)/);
var bheaderso = {};
if (dh) bheaderso = dh;
var code = 200;
var msg = "OK";
var httpMatch = bheaders[0].match(/^HTTP\/[^ ]+ ([0-9]{3})(?: (.*))?$/);
if (httpMatch) {
bheaders.shift();
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
} else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift();
var hso = heada.match(/^([0-9]{3})(?: (.*))?$/);
if (hso) {
code = parseInt(hso[1]);
if (hso[2]) msg = hso[2];
else msg = http.STATUS_CODES[code]; else msg = http.STATUS_CODES[code];
} }
} else if (headern.toLowerCase() == "set-cookie") {
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
bheaderso["Set-Cookie"].push(headerv);
} else {
bheaderso[headern] = headerv;
} }
for (var i = 0; i < bheaders.length; i++) { }
var headerp = bheaders[i].match(/^([^:]*)(?:: (.*))?/);
if(!headerp) headerp = [];
var headern = headerp[1];
var headerv = headerp[2];
if (headern.toLowerCase() == "status") {
var httpMatch = headerv.match(/^([0-9]{3})(?: (.*))?$/);
if(httpMatch) {
code = parseInt(httpMatch[1]);
if (httpMatch[2]) msg = httpMatch[2];
else msg = http.STATUS_CODES[code];
}
} else if (headern.toLowerCase() == "set-cookie") {
if (!bheaderso["Set-Cookie"]) bheaderso["Set-Cookie"] = [];
bheaderso["Set-Cookie"].push(headerv);
} else {
bheaderso[headern] = headerv;
}
}
if (code == 200 && (bheaderso["Location"] || bheaderso["location"])) { if (code == 200 && (bheaderso["Location"] || bheaderso["location"])) {
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.substr(headerendline + eol.length), "latin1");
} catch (ex) { } catch (ex) {
if (!callServerError) { if (!callServerError) {
res.writeHead(500); res.writeHead(500);
res.end(ex.stack); res.end(ex.stack);
} else { } else {
callServerError(500, "RedBrick/" + version, ex); callServerError(500, "RedBrick/" + version, ex);
} }
return; return;
}
} else {
if (cned && !res.finished) res.write(data);
}
};
if (interpreter.stdout) {
interpreter.stdout.on("data", dataHandler);
interpreter.stderr.on("data", function (data) {
stderr += data.toString();
});
req.pipe(interpreter.stdin);
interpreter.on("exit", (code, signal) => {
if (!cned && (signal || code !== 0)) {
var ex = new Error("Process execution failed!" + (stderr ? " Reason: " + stderr.trim() : ""));
if (!callServerError) {
res.writeHead(500);
res.end(ex.stack);
} else {
callServerError(500, "RedBrick/" + version, ex);
} }
} else { } else {
if (cned && !res.finished) res.write(data); res.end();
} }
}; });
if (interpreter.stdout) {
interpreter.stdout.on("data", dataHandler);
interpreter.stderr.on("data", function (data) {
stderr += data.toString();
});
req.pipe(interpreter.stdin);
interpreter.on("exit", (code, signal) => {
if (!cned && (signal || code !== 0)) {
var ex = new Error("Process execution failed!" + (stderr ? " Reason: " + stderr.trim() : ""));
if (!callServerError) {
res.writeHead(500);
res.end(ex.stack);
} else {
callServerError(500, "RedBrick/" + version, ex);
}
} else {
res.end();
}
});
}
} }
}
} }
function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh) { function executeCGIWithEnv(a, b, req, res, pubip, port, software, dh) {
@ -304,7 +304,7 @@ 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]") { if (configJSON.serverAdministratorEmail && configJSON.serverAdministratorEmail != "[no contact information]") {
nEnv["SERVER_ADMIN"] = configJSON.serverAdministratorEmail; nEnv["SERVER_ADMIN"] = configJSON.serverAdministratorEmail;
} }
nEnv["SERVER_NAME"] = req.headers.host; nEnv["SERVER_NAME"] = req.headers.host;
@ -315,9 +315,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
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, "");
if(req.socket.realRemoteAddress && req.socket.realRemotePort) { if (req.socket.realRemoteAddress && req.socket.realRemotePort) {
nEnv["REMOTE_PORT"] = req.socket.realRemotePort; nEnv["REMOTE_PORT"] = req.socket.realRemotePort;
} else if(!(req.socket.realRemoteAddress && !req.socket.realRemotePort)) { } else if (!(req.socket.realRemoteAddress && !req.socket.realRemotePort)) {
nEnv["REMOTE_PORT"] = req.socket.remotePort; nEnv["REMOTE_PORT"] = req.socket.remotePort;
} }
nEnv["SCRIPT_NAME"] = a; nEnv["SCRIPT_NAME"] = a;
@ -345,19 +345,19 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
"Server": "RedBrick/" + version "Server": "RedBrick/" + version
}); });
res.write( 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 " + "<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 + configJSON.version +
" (" + " (" +
os.platform()[0].toUpperCase() + os.platform()[0].toUpperCase() +
os.platform().slice(1) + os.platform().slice(1) +
"; Node.JS/" + "; Node.JS/" +
process.version + process.version +
") RedBrick/" + ") RedBrick/" +
version + version +
" " + " " +
(req.headers.host == undefined ? "" : " on " + req.headers.host) + (req.headers.host == undefined ? "" : " on " + req.headers.host) +
"</p></body></html>" "</p></body></html>"
); );
res.end(JSON.stringify(exttointerpreteruser, null, 2)); res.end(JSON.stringify(exttointerpreteruser, null, 2));
} else { } else {
callServerError(403, "RedBrick/" + version); callServerError(403, "RedBrick/" + version);
@ -366,39 +366,22 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
fs.stat("." + href, function (err, stats) { fs.stat("." + href, function (err, stats) {
if (!err) { if (!err) {
if (!stats.isFile()) { if (!stats.isFile()) {
if(isCgiBin || scriptExts.indexOf(".php") != -1) { if (isCgiBin || scriptExts.indexOf(".php") != -1) {
fs.stat("." + href + "/index.php", function (e2, s2) { fs.stat("." + href + "/index.php", function (e2, s2) {
if (!e2 && s2.isFile()) { if (!e2 && s2.isFile()) {
try { try {
executeCGIWithEnv( executeCGIWithEnv(
(href + "/index.php").replace(/\/+/g, "/"), (href + "/index.php").replace(/\/+/g, "/"),
"", "",
req, req,
res, res,
req.socket.localAddress, req.socket.localAddress,
req.socket.localPort, req.socket.localPort,
getCustomHeaders ? getCustomHeaders ?
getCustomHeaders()["Server"] + getCustomHeaders()["Server"] +
" RedBrick/" + " RedBrick/" +
version : version :
"SVR.JS/" + "SVR.JS/" +
configJSON.version +
" (" +
os.platform()[0].toUpperCase() +
os.platform().slice(1) +
"; Node.JS/" +
process.version +
") RedBrick/" +
version,
bheaders
);
} 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, "&nbsp;") +
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
configJSON.version + configJSON.version +
" (" + " (" +
os.platform()[0].toUpperCase() + os.platform()[0].toUpperCase() +
@ -406,32 +389,16 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
"; Node.JS/" + "; Node.JS/" +
process.version + process.version +
") RedBrick/" + ") RedBrick/" +
version + version,
" " + bheaders
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
"</p></body></html>"
); );
res.end(); } catch (ex) {
} else { if (!callServerError) {
callServerError(500, "RedBrick/" + version, ex); 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>" +
} else if(isCgiBin || scriptExts.indexOf(".cgi") != -1) { ex.stack.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ /g, "&nbsp;") +
fs.stat("." + href + "/index.cgi", function (e3, s3) { "</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
if (!e3 && s3.isFile()) {
try {
executeCGIWithEnv(
(href + "/index.cgi").replace(/\/+/g, "/"),
"",
req,
res,
req.socket.localAddress,
req.socket.localPort,
getCustomHeaders ?
getCustomHeaders()["Server"] +
" RedBrick/" +
version :
"SVR.JS/" +
configJSON.version + configJSON.version +
" (" + " (" +
os.platform()[0].toUpperCase() + os.platform()[0].toUpperCase() +
@ -439,16 +406,32 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
"; Node.JS/" + "; Node.JS/" +
process.version + process.version +
") RedBrick/" + ") RedBrick/" +
version, version +
bheaders " " +
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
"</p></body></html>"
); );
} catch (ex) { res.end();
if (!callServerError) { } else {
res.writeHead(500, "Internal Server Error", abheaders); callServerError(500, "RedBrick/" + version, ex);
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;") + } else if (isCgiBin || scriptExts.indexOf(".cgi") != -1) {
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " + fs.stat("." + href + "/index.cgi", function (e3, s3) {
if (!e3 && s3.isFile()) {
try {
executeCGIWithEnv(
(href + "/index.cgi").replace(/\/+/g, "/"),
"",
req,
res,
req.socket.localAddress,
req.socket.localPort,
getCustomHeaders ?
getCustomHeaders()["Server"] +
" RedBrick/" +
version :
"SVR.JS/" +
configJSON.version + configJSON.version +
" (" + " (" +
os.platform()[0].toUpperCase() + os.platform()[0].toUpperCase() +
@ -456,40 +439,74 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
"; Node.JS/" + "; Node.JS/" +
process.version + process.version +
") RedBrick/" + ") RedBrick/" +
version + version,
" " + bheaders
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
"</p></body></html>"
); );
res.end(); } catch (ex) {
} else { if (!callServerError) {
callServerError(500, "RedBrick/" + version, ex); 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, "&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();
} else {
callServerError(500, "RedBrick/" + version, ex);
}
} }
} else {
elseCallback();
} }
} else { });
elseCallback(); } else {
}
});
} else {
elseCallback(); elseCallback();
} }
}); });
} else if(scriptExts.indexOf(".cgi") != -1) { } else if (scriptExts.indexOf(".cgi") != -1) {
fs.stat("." + href + "/index.cgi", function (e3, s3) { fs.stat("." + href + "/index.cgi", function (e3, s3) {
if (!e3 && s3.isFile()) { if (!e3 && s3.isFile()) {
try { try {
executeCGIWithEnv( executeCGIWithEnv(
(href + "/index.cgi").replace(/\/+/g, "/"), (href + "/index.cgi").replace(/\/+/g, "/"),
"", "",
req, req,
res, res,
req.socket.localAddress, req.socket.localAddress,
req.socket.localPort, req.socket.localPort,
getCustomHeaders ? getCustomHeaders ?
getCustomHeaders()["Server"] + getCustomHeaders()["Server"] +
" RedBrick/" + " RedBrick/" +
version : version :
"SVR.JS/" + "SVR.JS/" +
configJSON.version +
" (" +
os.platform()[0].toUpperCase() +
os.platform().slice(1) +
"; Node.JS/" +
process.version +
") RedBrick/" +
version,
bheaders
);
} 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, "&nbsp;") +
"</code><p>Please contact the developer/administrator of the website.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS " +
configJSON.version + configJSON.version +
" (" + " (" +
os.platform()[0].toUpperCase() + os.platform()[0].toUpperCase() +
@ -497,126 +514,29 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
"; Node.JS/" + "; Node.JS/" +
process.version + process.version +
") RedBrick/" + ") RedBrick/" +
version, version +
bheaders " " +
(req.headers.host == undefined ? "" : " on " + req.headers.host) +
"</p></body></html>"
); );
} catch (ex) { res.end();
if (!callServerError) { } else {
res.writeHead(500, "Internal Server Error", abheaders); callServerError(500, "RedBrick/" + version, ex);
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();
} else {
callServerError(500, "RedBrick/" + version, ex);
}
} }
} else {
elseCallback();
} }
}); } else {
elseCallback();
}
});
} else { } else {
elseCallback(); elseCallback();
} }
} else { } else {
if(isCgiBin || isScriptExt) { if (isCgiBin || isScriptExt) {
try {
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
);
} 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, "&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();
} else {
callServerError(500, "RedBrick/" + version, ex);
}
}
} else {
elseCallback();
}
}
} else if (err && err.code == "ENOTDIR") {
function checkPath(pth, cb, a) {
// Function to check the path of the file and execute CGI script
var cpth = pth.split("/");
if (cpth.length < (isCgiBin ? 3 : 2)) {
cb(false);
return;
}
if (!a) b = [];
else var b = a.split("/");
var isFile = false;
fs.stat(pth, function (err, stats) {
if (!err && stats.isFile()) {
cb({
fpth: pth,
rpth: (a !== undefined ? "/" + a : "")
})
} else {
b.unshift(cpth.pop());
return checkPath(cpth.join("/"), cb, b.join("/"));
}
});
}
checkPath("." + href, function (pathp) {
if (!pathp) {
elseCallback();
} else {
var newext = path.extname(pathp.fpth);
if(isCgiBin || scriptExts.indexOf(newext) != -1) {
try { try {
executeCGIWithEnv( executeCGIWithEnv(
pathp.fpth.substr(1), href,
pathp.rpth, "",
req, req,
res, res,
req.socket.localAddress, req.socket.localAddress,
@ -660,9 +580,89 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
callServerError(500, "RedBrick/" + version, ex); callServerError(500, "RedBrick/" + version, ex);
} }
} }
} else { } else {
elseCallback(); elseCallback();
} }
}
} else if (err && err.code == "ENOTDIR") {
function checkPath(pth, cb, a) {
// Function to check the path of the file and execute CGI script
var cpth = pth.split("/");
if (cpth.length < (isCgiBin ? 3 : 2)) {
cb(false);
return;
}
if (!a) b = [];
else var b = a.split("/");
var isFile = false;
fs.stat(pth, function (err, stats) {
if (!err && stats.isFile()) {
cb({
fpth: pth,
rpth: (a !== undefined ? "/" + a : "")
})
} else {
b.unshift(cpth.pop());
return checkPath(cpth.join("/"), cb, b.join("/"));
}
});
}
checkPath("." + href, function (pathp) {
if (!pathp) {
elseCallback();
} else {
var newext = path.extname(pathp.fpth);
if (isCgiBin || scriptExts.indexOf(newext) != -1) {
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
);
} 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, "&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();
} else {
callServerError(500, "RedBrick/" + version, ex);
}
}
} else {
elseCallback();
}
} }
}); });
} else if (err && err.code == "ENOENT") { } else if (err && err.code == "ENOENT") {