Make it work for PHP-CGI

This commit is contained in:
svrjs 2023-08-02 14:33:36 +02:00
parent 5c67e6b7a6
commit 58fc45fc79
2 changed files with 81 additions and 17 deletions

View file

@ -172,9 +172,11 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
} else if (bheaders[0].indexOf(":") == -1) {
var heada = bheaders.shift();
var hso = heada.split(" ");
if (hso[0].match(/^[0-9]{3}$/)) {
code = hso[0];
if (hso[1] !== undefined) msg = heada.split(" ").splice(1).join(" ");
}
}
for (var i = 0; i < bheaders.length; i++) {
var headerp = bheaders[i].split(": ");
var headern = headerp.shift();
@ -256,9 +258,11 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
if (nEnv["QUERY_STRING"] == undefined || nEnv["QUERY_STRING"] == "undefined") nEnv["QUERY_STRING"] = "";
nEnv["SERVER_SOFTWARE"] = software;
nEnv["SERVER_PROTOCOL"] = "HTTP/" + req.httpVersion;
if (pubip && (port !== null && port !== undefined)) {
nEnv["SERVER_PORT"] = port;
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["DOCUMENT_ROOT"] = process.cwd();
nEnv["PATH_INFO"] = decodeURI(b);
@ -267,7 +271,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
nEnv["GATEWAY_INTERFACE"] = "CGI/1.1";
nEnv["REQUEST_URI"] = req.url;
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.remoteAddress);
nEnv["REMOTE_PORT"] = (req.socket.realRemotePort ? req.socket.realRemotePort : req.socket.remotePort);
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" ? "\\" : "/"));
if (req.headers["content-type"]) nEnv["CONTENT_TYPE"] = req.headers["content-type"];
@ -284,13 +288,62 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
if (href.indexOf("/cgi-bin") == 0) {
fs.stat("." + href, function (err, stats) {
if (err && err.code == "ENOENT") {
var invokeElseCallback = false;
if (!err) {
if (!stats.isFile()) {
fs.stat("." + href + "/index.php", function (e2, s2) {
if (!e2 && s2.isFile()) {
try {
invokeElseCallback = stats.isFile();
} catch (ex) {}
if (invokeElseCallback) {
executeCGIWithEnv(
(href + "/index.php").replace(/\/+/g, "/"),
"",
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 {
try {
executeCGIWithEnv(
@ -340,7 +393,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
}
}
}
} else if (!err || (err && err.code == "ENOTDIR")) {
} 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("/");
@ -357,12 +410,21 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
fpth: pth,
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 {
b.unshift(cpth.pop());
return checkPath(cpth.join("/"), cb, b.join("/"));
}
});
}
});
}
checkPath("." + href, function (pathp) {
if (!pathp) {
elseCallback();
@ -416,6 +478,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
}
}
});
} else if (err && err.code == "ENOENT") {
elseCallback(); //Invoke default error handler
} else {
if (!callServerError) {
res.writeHead(500, "Internal Server Error", abheaders);

View file

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