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,8 +172,10 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
} 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.split(" ");
code = hso[0]; if (hso[0].match(/^[0-9]{3}$/)) {
if (hso[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(": ");
@ -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"] = ""; 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; if (pubip && (port !== null && port !== undefined)) {
nEnv["SERVER_ADDR"] = pubip.replace(/^::ffff:/i, ""); nEnv["SERVER_PORT"] = port;
if (nEnv["SERVER_ADDR"].indexOf(":") != -1) nEnv["SERVER_ADDR"] = "[" + nEnv["SERVER_ADDR"] + "]"; 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"] = decodeURI(b); 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["GATEWAY_INTERFACE"] = "CGI/1.1";
nEnv["REQUEST_URI"] = req.url; 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_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_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.headers["content-type"]) nEnv["CONTENT_TYPE"] = req.headers["content-type"]; 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) { if (href.indexOf("/cgi-bin") == 0) {
fs.stat("." + href, function (err, stats) { fs.stat("." + href, function (err, stats) {
if (err && err.code == "ENOENT") { if (!err) {
var invokeElseCallback = false; if (!stats.isFile()) {
try { fs.stat("." + href + "/index.php", function (e2, s2) {
invokeElseCallback = stats.isFile(); if (!e2 && s2.isFile()) {
} catch (ex) {} try {
if (invokeElseCallback) { executeCGIWithEnv(
elseCallback(); (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 { } else {
try { try {
executeCGIWithEnv( 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 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("/");
@ -358,8 +411,17 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
rpth: (a !== undefined ? "/" + a : "") rpth: (a !== undefined ? "/" + a : "")
}) })
} else { } else {
b.unshift(cpth.pop()); fs.stat(pth + "/index.php", function (e2, s2) {
return checkPath(cpth.join("/"), cb, b.join("/")); 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("/"));
}
});
} }
}); });
} }
@ -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 { } else {
if (!callServerError) { if (!callServerError) {
res.writeHead(500, "Internal Server Error", abheaders); res.writeHead(500, "Internal Server Error", abheaders);

View file

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