From af9bf140ff5d7e8edb4da1446f76d0da56af66d5 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Mon, 9 Dec 2024 15:44:50 +0100 Subject: [PATCH] fix: use web root from configJSON.wwwroot for SVR.JS 4.2.0 or newer --- index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3a1d8b5..e04c0c2 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,19 @@ if (typeof scgiConfO.multiConfig == "object" && scgiConfO.multiConfig !== null) } 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); +var normalizedWebrootSupported = process.versions.svrjs && process.versions.svrjs.match(/^(?:Nightly-|(?:[5-9]|[1234][0-9])[0-9]*\.|4\.(?:(?:[1][0-9]|[2-9])+\.))/i); + +function normalizeWebroot(currentWebroot) { + if (currentWebroot === undefined) { + return process.cwd(); + } else if (!path.isAbsolute(currentWebroot)) { + return ( + process.cwd() + (os.platform() == "win32" ? "\\" : "/") + currentWebroot + ); + } else { + return currentWebroot; + } +} function Mod() {} @@ -50,6 +63,8 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e configJSON = configJSONS; } + var detectedWwwroot = normalizedWebrootSupported ? normalizeWebroot(configJSON.wwwroot) : process.cwd(); + function checkIfThereIsA401Rule() { var actually401 = false; @@ -254,10 +269,10 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e nEnv["SERVER_ADMIN"] = configJSON.serverAdministratorEmail; } nEnv["SERVER_NAME"] = req.headers.host; - nEnv["DOCUMENT_ROOT"] = process.cwd(); + nEnv["DOCUMENT_ROOT"] = detectedWwwroot; nEnv["SCRIPT_NAME"] = scgiConf.path; nEnv["PATH_INFO"] = decodeURIComponent(b); - 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["PATH_TRANSLATED"] = b ? ((detectedWwwroot + 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["SCGI"] = "1"; nEnv["REQUEST_URI"] = (!origHref || origHref == href) ? req.url : (origHref + (uobject.search ? ((uobject.search[0] == "?" ? "" : "?") + uobject.search) : "")); @@ -351,7 +366,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e callServerError(500, "OrangeCircle/" + version, ex); } } - } else if ((href == "/orangecircle-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/orangecircle-config.json")) && path.normalize(__dirname + "/../../..") == process.cwd()) { + } else if ((href == "/orangecircle-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/orangecircle-config.json")) && path.normalize(__dirname + "/../../..") == detectedWwwroot) { if (!callServerError) { res.writeHead(200, "OK", { "Content-Type": "application/json",