1
0
Fork 0
forked from svrjs/svrjs

Update to SVR.JS 3.4.27

This commit is contained in:
Dorian Niemiec 2023-09-02 19:25:19 +02:00
parent 2b91a22dac
commit d2fa84a969
4 changed files with 375 additions and 434 deletions

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.4.26</title> <title>SVR.JS 3.4.27</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -12,7 +12,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>Welcome to SVR.JS 3.4.26</h1> <h1>Welcome to SVR.JS 3.4.27</h1>
<br/> <br/>
<img src="/logo.png" style="width: 256px;" /> <img src="/logo.png" style="width: 256px;" />
<br/> <br/>
@ -119,8 +119,8 @@
</div> </div>
<p>Changes:</p> <p>Changes:</p>
<ul> <ul>
<li>Changed default SVR.JS configuration.</li> <li>Dropped support for undocumented unused non-standard SVR.JS-specific headers.</li>
<li>Disabled server-side script exposure by default.</li> <li>Fixed bug with <i>wwwredirect</i>.</li>
</ul> </ul>
<p>Bugs:</p> <p>Bugs:</p>
<ul> <ul>

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.4.26 Licenses</title> <title>SVR.JS 3.4.27 Licenses</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -12,8 +12,8 @@
</style> </style>
</head> </head>
<body> <body>
<h1>SVR.JS 3.4.26 Licenses</h1> <h1>SVR.JS 3.4.27 Licenses</h1>
<h2>SVR.JS 3.4.26</h2> <h2>SVR.JS 3.4.27</h2>
<div style="display: inline-block; text-align: left; border-width: 2px; border-style: solid; border-color: gray; padding: 8px;"> <div style="display: inline-block; text-align: left; border-width: 2px; border-style: solid; border-color: gray; padding: 8px;">
MIT License<br/> MIT License<br/>
<br/> <br/>
@ -37,7 +37,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br/> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br/>
SOFTWARE.<br/> SOFTWARE.<br/>
</div> </div>
<h2>Packages used by SVR.JS 3.4.26 and utilities</h2> <h2>Packages used by SVR.JS 3.4.27 and utilities</h2>
<div style="width: 100%; background-color: #ccc; border: 1px solid green; text-align: left; margin: 10px 0;"> <div style="width: 100%; background-color: #ccc; border: 1px solid green; text-align: left; margin: 10px 0;">
<div style="float: right;">License: MIT</div> <div style="float: right;">License: MIT</div>
<div style="font-size: 20px;"> <div style="font-size: 20px;">

789
svr.js
View file

@ -71,7 +71,7 @@ function deleteFolderRecursive(path) {
} }
var os = require("os"); var os = require("os");
var version = "3.4.26"; var version = "3.4.27";
var singlethreaded = false; var singlethreaded = false;
if (process.versions) process.versions.svrjs = version; //Inject SVR.JS into process.versions if (process.versions) process.versions.svrjs = version; //Inject SVR.JS into process.versions
@ -1844,409 +1844,386 @@ if (!cluster.isPrimary) {
if (server2.requestTimeout !== undefined && server2.requestTimeout === 0) server2.requestTimeout = 300000; if (server2.requestTimeout !== undefined && server2.requestTimeout === 0) server2.requestTimeout = 300000;
function redirhandler(req, res) { function redirhandler(req, res) {
if (req.headers["force-insecure"] == "true" || req.headers["x-force-insecure"] == "true" || req.headers["x-svr-js-force-insecure"] == "true") { var reqIdInt = Math.round(Math.random() * 16777216);
reqhandler(req, res, false); var reqId = "0".repeat(6 - reqIdInt.toString(16).length) + reqIdInt.toString(16);
} else { var serverconsole = {
var reqIdInt = Math.round(Math.random() * 16777216); climessage: function (msg) {
var reqId = "0".repeat(6 - reqIdInt.toString(16).length) + reqIdInt.toString(16); if (msg.indexOf("\n") != -1) {
var serverconsole = { var nmsg = msg.split("\n");
climessage: function (msg) { for (var i = 0; i < nmsg.length; i++) {
if (msg.indexOf("\n") != -1) { serverconsole.climessage(nmsg[i]);
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.climessage(nmsg[i]);
}
return;
} }
console.log("SERVER CLI MESSAGE [Request Id: " + reqId + "]: " + msg);
LOG("SERVER CLI MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
reqmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.reqmessage(nmsg[i]);
}
return;
}
console.log("\x1b[34mSERVER REQUEST MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
LOG("SERVER REQUEST MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
resmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.resmessage(nmsg[i]);
}
return;
}
console.log("\x1b[32mSERVER RESPONSE MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
LOG("SERVER RESPONSE MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
errmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.errmessage(nmsg[i]);
}
return;
}
console.log("\x1b[31mSERVER RESPONSE ERROR MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
LOG("SERVER RESPONSE ERROR MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locerrmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locerrmessage(nmsg[i]);
}
return;
}
console.log("\x1b[41mSERVER ERROR MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[40m\x1b[0m");
LOG("SERVER ERROR MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locwarnmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locwarnmessage(nmsg[i]);
}
return;
}
console.log("\x1b[43mSERVER WARNING MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[40m\x1b[0m");
LOG("SERVER WARNING MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locmessage(nmsg[i]);
}
return;
}
console.log("SERVER MESSAGE [Request Id: " + reqId + "]: " + msg);
LOG("SERVER MESSAGE [Request Id: " + reqId + "]: " + msg);
return; return;
} }
}; console.log("SERVER CLI MESSAGE [Request Id: " + reqId + "]: " + msg);
LOG("SERVER CLI MESSAGE [Request Id: " + reqId + "]: " + msg);
function getCustomHeaders() { return;
var ph = JSON.parse(JSON.stringify(customHeaders)); },
var phk = Object.keys(ph); reqmessage: function (msg) {
for (var i = 0; i < phk.length; i++) { if (msg.indexOf("\n") != -1) {
if (typeof ph[phk[i]] == "string") ph[phk[i]] = ph[phk[i]].replace(/\{path\}/g, req.url); var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.reqmessage(nmsg[i]);
}
return;
} }
return ph; console.log("\x1b[34mSERVER REQUEST MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
} LOG("SERVER REQUEST MESSAGE [Request Id: " + reqId + "]: " + msg);
if (req.headers["x-svr-js-from-main-thread"] == "true") { return;
res.writeHead(204, "No Content", getCustomHeaders()); },
res.end(); resmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.resmessage(nmsg[i]);
}
return;
}
console.log("\x1b[32mSERVER RESPONSE MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
LOG("SERVER RESPONSE MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
errmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.errmessage(nmsg[i]);
}
return;
}
console.log("\x1b[31mSERVER RESPONSE ERROR MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[37m\x1b[0m");
LOG("SERVER RESPONSE ERROR MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locerrmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locerrmessage(nmsg[i]);
}
return;
}
console.log("\x1b[41mSERVER ERROR MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[40m\x1b[0m");
LOG("SERVER ERROR MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locwarnmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locwarnmessage(nmsg[i]);
}
return;
}
console.log("\x1b[43mSERVER WARNING MESSAGE [Request Id: " + reqId + "]: " + msg + "\x1b[40m\x1b[0m");
LOG("SERVER WARNING MESSAGE [Request Id: " + reqId + "]: " + msg);
return;
},
locmessage: function (msg) {
if (msg.indexOf("\n") != -1) {
var nmsg = msg.split("\n");
for (var i = 0; i < nmsg.length; i++) {
serverconsole.locmessage(nmsg[i]);
}
return;
}
console.log("SERVER MESSAGE [Request Id: " + reqId + "]: " + msg);
LOG("SERVER MESSAGE [Request Id: " + reqId + "]: " + msg);
return; return;
} }
};
req.url = fixNodeMojibakeURL(req.url);
res.writeHeadNative = res.writeHead;
res.writeHead = function (a, b, c) {
if (parseInt(a) >= 400 && parseInt(a) <= 599) {
serverconsole.errmessage("Server responded with " + a.toString() + " code.");
} else {
serverconsole.resmessage("Server responded with " + a.toString() + " code.");
}
res.writeHeadNative(a, b, c);
};
var finished = false;
res.on("finish", function () {
if (!finished) {
finished = true;
serverconsole.locmessage("Client disconnected.");
}
});
res.on("close", function () {
if (!finished) {
finished = true;
serverconsole.locmessage("Client disconnected.");
}
});
serverconsole.locmessage("Somebody connected to port " + port + "..."); function getCustomHeaders() {
var ph = JSON.parse(JSON.stringify(customHeaders));
if (req.socket == null) { var phk = Object.keys(ph);
serverconsole.errmessage("Client socket is null!!!"); for (var i = 0; i < phk.length; i++) {
return; if (typeof ph[phk[i]] == "string") ph[phk[i]] = ph[phk[i]].replace(/\{path\}/g, req.url);
} }
return ph;
var isProxy = false; }
if (req.url.indexOf("/") != 0 && req.url != "*") isProxy = true; if (req.headers["x-svr-js-from-main-thread"] == "true") {
res.writeHead(204, "No Content", getCustomHeaders());
var head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header res.end();
var foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer return;
var fd = ""; }
function responseEnd(d) { req.url = fixNodeMojibakeURL(req.url);
if (d === undefined) d = fd;
res.write(head + d + foot); res.writeHeadNative = res.writeHead;
res.end(); res.writeHead = function (a, b, c) {
} if (parseInt(a) >= 400 && parseInt(a) <= 599) {
serverconsole.errmessage("Server responded with " + a.toString() + " code.");
//Error descriptions
var serverErrorDescs = {
400: "The request you made is invalid.",
417: "Expectation in Expect property couldn't be satisfied.",
500: "The server had an unexpected error. Below, the error stack is shown: </p><code>{stack}</code><p>Please contact with developer/administrator at <i>{contact}</i>.",
501: "The request requires use of a function, which isn't currently implemented by the server."
};
//Server error calling method
function callServerError(errorCode, extName, stack, ch) {
var errorFile = errorCode.toString() + ".html";
var errorFile2 = "." + errorCode.toString();
if (fs.existsSync(errorFile2)) errorFile = errorFile2;
if (errorCode == 404 && fs.existsSync(page404)) errorFile = page404;
if (Object.prototype.toString.call(stack) === "[object Error]") stack = generateErrorStack(stack);
if (stack === undefined) stack = generateErrorStack(new Error("Unknown error"));
if (errorCode == 500 || errorCode == 502) {
serverconsole.errmessage("There was an error while processing the request!");
serverconsole.errmessage("Stack:");
serverconsole.errmessage(stack);
}
if (stackHidden) stack = "[error stack hidden]";
if (serverErrorDescs[errorCode] === undefined) {
callServerError(501, extName, stack);
} else {
var cheaders = getCustomHeaders();
if (ch) {
var chon = Object.keys(cheaders);
var chn = Object.keys(ch);
for (var i = 0; i < chn.length; i++) {
var nhn = chn[i];
for (var j = 0; j < chon.length; j++) {
if (chon[j].toLowerCase() == chn[i].toLowerCase()) {
nhn = chon[j];
break;
}
}
if (ch[chn[i]]) cheaders[nhn] = ch[chn[i]];
}
}
cheaders["Content-Type"] = "text/html; charset=utf-8";
if (errorCode == 405 && !cheaders["Allow"]) cheaders["Allow"] = "GET, POST, HEAD, OPTIONS";
fs.readFile(errorFile, function (err, data) {
try {
if (err) throw err;
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
fd += data.toString().replace(/{errorMessage}/g, errorCode.toString() + " " + http.STATUS_CODES[errorCode]).replace(/{errorDesc}/g, serverErrorDescs[errorCode]).replace(/{stack}/g, stack.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;")).replace(/{path}/g, req.url.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")).replace(/{server}/g, "" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (extName == undefined ? "" : " " + extName) + ((req.headers.host == undefined || isProxy) ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"))).replace(/{contact}/g, serverAdmin.replace(/\./g, "[dot]").replace(/@/g, "[at]"));
responseEnd();
} catch (ex) {
var additionalError = 500;
if (ex.code == "ENOENT") {
additionalError = 404;
} else if (ex.code == "ENOTDIR") {
additionalError = 404;
} else if (ex.code == "EACCES") {
additionalError = 403;
} else if (ex.code == "ENAMETOOLONG") {
additionalError = 414;
} else if (ex.code == "EMFILE") {
additionalError = 503;
} else if (ex.code == "ELOOP") {
additionalError = 508;
}
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
res.write(("<html><head><title>{errorMessage}</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>" + ((additionalError == 404) ? "" : "<p>Additionally, a {additionalError} error occurred while loading an error page.</p>") + "<p><i>{server}</i></p></body></html>").replace(/{errorMessage}/g, errorCode.toString() + " " + http.STATUS_CODES[errorCode]).replace(/{errorDesc}/g, serverErrorDescs[errorCode]).replace(/{stack}/g, stack.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;")).replace(/{path}/g, req.url.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")).replace(/{server}/g, "" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (extName == undefined ? "" : " " + extName) + ((req.headers.host == undefined || isProxy) ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"))).replace(/{contact}/g, serverAdmin.replace(/\./g, "[dot]").replace(/@/g, "[at]")).replace(/{additionalError}/g, additionalError.toString()));
res.end();
}
});
}
}
var reqport = "";
var reqip = "";
var oldport = "";
var oldip = "";
if (req.headers["x-svr-js-client"] != undefined && enableIPSpoofing) {
var kl = req.headers["x-svr-js-client"].split(":");
reqport = kl.pop();
reqip = kl.join(":");
try {
oldport = req.socket.remotePort;
oldip = req.socket.remoteAddress;
req.socket.realRemotePort = reqport;
req.socket.realRemoteAddress = reqip;
req.socket.originalRemotePort = oldport;
req.socket.originalRemoteAddress = oldip;
res.socket.realRemotePort = reqport;
res.socket.realRemoteAddress = reqip;
res.socket.originalRemotePort = oldport;
res.socket.originalRemoteAddress = oldip;
} catch (ex) {
//Nevermind...
}
} else if (req.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
reqport = null;
reqip = req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "");
if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip;
try {
oldport = req.socket.remotePort;
oldip = req.socket.remoteAddress;
req.socket.realRemotePort = reqport;
req.socket.realRemoteAddress = reqip;
req.socket.originalRemotePort = oldport;
req.socket.originalRemoteAddress = oldip;
res.socket.realRemotePort = reqport;
res.socket.realRemoteAddress = reqip;
res.socket.originalRemotePort = oldport;
res.socket.originalRemoteAddress = oldip;
} catch (ex) {
//Nevermind...
}
} else { } else {
reqip = req.socket.remoteAddress; serverconsole.resmessage("Server responded with " + a.toString() + " code.");
reqport = req.socket.remotePort;
} }
res.writeHeadNative(a, b, c);
};
var finished = false;
res.on("finish", function () {
if (!finished) {
finished = true;
serverconsole.locmessage("Client disconnected.");
}
});
res.on("close", function () {
if (!finished) {
finished = true;
serverconsole.locmessage("Client disconnected.");
}
});
if (!isProxy) serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (req.method == "GET" ? "content in " : (req.method == "POST" ? "to post content in " : (req.method == "PUT" ? "to add content in " : (req.method == "DELETE" ? "to delete content in " : (req.method == "PATCH" ? "to patch content in " : "to access content using " + req.method + " method in "))))) + (req.headers.host == undefined ? "" : req.headers.host) + req.url); serverconsole.locmessage("Somebody connected to port " + port + "...");
else serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (req.method == "GET" ? "content in " : (req.method == "POST" ? "to post content in " : (req.method == "PUT" ? "to add content in " : (req.method == "DELETE" ? "to delete content in " : (req.method == "PATCH" ? "to patch content in " : "to access content using " + req.method + " method in "))))) + req.url);
if (req.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + req.headers["user-agent"]);
try { if (req.socket == null) {
if (req.headers["expect"] && req.headers["expect"] != "100-continue") { serverconsole.errmessage("Client socket is null!!!");
serverconsole.errmessage("Expectation not satified!"); return;
callServerError(417); }
return;
}
var hostx = req.headers.host;
if (hostx === undefined) {
serverconsole.errmessage("Bad request!");
callServerError(400);
return;
}
if (req.method == "CONNECT") { var isProxy = false;
callServerError(501); if (req.url.indexOf("/") != 0 && req.url != "*") isProxy = true;
serverconsole.errmessage("CONNECT requests aren't supported. Your JS runtime probably doesn't support 'connect' handler for HTTP library.");
return;
}
if (isProxy) { var head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header
callServerError(501); var foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer
serverconsole.errmessage("This server will never be a proxy."); var fd = "";
return;
}
function urlParse(uri) { function responseEnd(d) {
if (typeof URL != "undefined" && url.Url) { if (d === undefined) d = fd;
try { res.write(head + d + foot);
var uobject = new URL(uri, "http" + (req.socket.encrypted ? "s" : "") + "://" + (req.headers.host ? req.headers.host : (domain ? domain : "unknown.invalid"))); res.end();
var nuobject = new url.Url(); }
if (uri.indexOf("/") != -1) nuobject.slashes = true;
if (uobject.protocol != "") nuobject.protocol = uobject.protocol; //Error descriptions
if (uobject.username != "" && uobject.password != "") nuobject.auth = uobject.username + ":" + uobject.password; var serverErrorDescs = {
if (uobject.host != "") nuobject.host = uobject.host; 400: "The request you made is invalid.",
if (uobject.hostname != "") nuobject.hostname = uobject.hostname; 417: "Expectation in Expect property couldn't be satisfied.",
if (uobject.port != "") nuobject.port = uobject.port; 500: "The server had an unexpected error. Below, the error stack is shown: </p><code>{stack}</code><p>Please contact with developer/administrator at <i>{contact}</i>.",
if (uobject.pathname != "") nuobject.pathname = uobject.pathname; 501: "The request requires use of a function, which isn't currently implemented by the server."
if (uobject.search != "") nuobject.search = uobject.search; };
if (uobject.hash != "") nuobject.hash = uobject.hash;
if (uobject.href != "") nuobject.href = uobject.href; //Server error calling method
if (uri.indexOf("/") != 0) { function callServerError(errorCode, extName, stack, ch) {
if (nuobject.pathname) { var errorFile = errorCode.toString() + ".html";
nuobject.pathname = nuobject.pathname.substr(1); var errorFile2 = "." + errorCode.toString();
nuobject.href = nuobject.pathname + (nuobject.search ? nuobject.search : ""); if (fs.existsSync(errorFile2)) errorFile = errorFile2;
} if (errorCode == 404 && fs.existsSync(page404)) errorFile = page404;
} if (Object.prototype.toString.call(stack) === "[object Error]") stack = generateErrorStack(stack);
if (nuobject.pathname) { if (stack === undefined) stack = generateErrorStack(new Error("Unknown error"));
nuobject.path = nuobject.pathname + (nuobject.search ? nuobject.search : ""); if (errorCode == 500 || errorCode == 502) {
}
//if(nuobject.path != "" && uobject.password != "") nuobject.path = nuobject.pathname + nuobject.href;
nuobject.query = {};
uobject.searchParams.forEach(function (value, key) {
nuobject.query[key] = value;
});
return nuobject;
} catch (ex) {
return url.parse(uri, true);
}
} else {
return url.parse(uri, true);
}
}
var urlp = urlParse("http://" + hostx);
try {
if (urlp.path.indexOf("//") == 0) {
urlp = urlParse("http:" + url.path);
}
} catch (ex) {
//URL parse error...
}
if (urlp.host == "localhost" || urlp.host == "localhost:" + port.toString() || urlp.host == "127.0.0.1" || urlp.host == "127.0.0.1:" + port.toString() || urlp.host == "::1" || urlp.host == "::1:" + port.toString()) {
urlp.protocol = "https:";
if (sport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + sport.toString();
urlp.port = sport.toString();
}
} else if (urlp.host == host || urlp.host == host + ":" + port.toString()) {
urlp.protocol = "https:";
if (sport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + sport.toString();
urlp.port = sport.toString();
}
} else if (urlp.host == pubip || urlp.host == pubip + ":" + pubport.toString()) {
urlp.protocol = "https:";
if (spubport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + spubport.toString();
urlp.port = spubport.toString();
}
} else if (urlp.hostname == domain || urlp.hostname.indexOf(domain) != -1) {
urlp.protocol = "https:";
if (spubport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + spubport.toString();
urlp.port = spubport.toString();
}
} else {
urlp.protocol = "https:";
}
urlp.path = null;
urlp.pathname = null;
var lloc = url.format(urlp);
var requestURL = req.url;
try {
if (requestURL.split("/")[1].indexOf(".onion") != -1) {
requestURL = requestURL.split("/");
requestURL.shift();
requestURL.shift();
requestURL.unshift("");
requestURL = requestURL.join("/");
}
} catch (ex) {
//Leave URL as it is...
}
var rheaders = getCustomHeaders();
rheaders["Location"] = lloc + requestURL;
res.writeHead(301, "Redirect to HTTPS", rheaders);
res.end();
} catch (ex) {
serverconsole.errmessage("There was an error while processing the request!"); serverconsole.errmessage("There was an error while processing the request!");
serverconsole.errmessage("Stack:"); serverconsole.errmessage("Stack:");
serverconsole.errmessage(generateErrorStack(ex)); serverconsole.errmessage(stack);
callServerError(500, undefined, generateErrorStack(ex)); }
if (stackHidden) stack = "[error stack hidden]";
if (serverErrorDescs[errorCode] === undefined) {
callServerError(501, extName, stack);
} else {
var cheaders = getCustomHeaders();
if (ch) {
var chon = Object.keys(cheaders);
var chn = Object.keys(ch);
for (var i = 0; i < chn.length; i++) {
var nhn = chn[i];
for (var j = 0; j < chon.length; j++) {
if (chon[j].toLowerCase() == chn[i].toLowerCase()) {
nhn = chon[j];
break;
}
}
if (ch[chn[i]]) cheaders[nhn] = ch[chn[i]];
}
}
cheaders["Content-Type"] = "text/html; charset=utf-8";
if (errorCode == 405 && !cheaders["Allow"]) cheaders["Allow"] = "GET, POST, HEAD, OPTIONS";
fs.readFile(errorFile, function (err, data) {
try {
if (err) throw err;
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
fd += data.toString().replace(/{errorMessage}/g, errorCode.toString() + " " + http.STATUS_CODES[errorCode]).replace(/{errorDesc}/g, serverErrorDescs[errorCode]).replace(/{stack}/g, stack.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;")).replace(/{path}/g, req.url.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")).replace(/{server}/g, "" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (extName == undefined ? "" : " " + extName) + ((req.headers.host == undefined || isProxy) ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"))).replace(/{contact}/g, serverAdmin.replace(/\./g, "[dot]").replace(/@/g, "[at]"));
responseEnd();
} catch (ex) {
var additionalError = 500;
if (ex.code == "ENOENT") {
additionalError = 404;
} else if (ex.code == "ENOTDIR") {
additionalError = 404;
} else if (ex.code == "EACCES") {
additionalError = 403;
} else if (ex.code == "ENAMETOOLONG") {
additionalError = 414;
} else if (ex.code == "EMFILE") {
additionalError = 503;
} else if (ex.code == "ELOOP") {
additionalError = 508;
}
res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
res.write(("<html><head><title>{errorMessage}</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>{errorMessage}</h1><p>{errorDesc}</p>" + ((additionalError == 404) ? "" : "<p>Additionally, a {additionalError} error occurred while loading an error page.</p>") + "<p><i>{server}</i></p></body></html>").replace(/{errorMessage}/g, errorCode.toString() + " " + http.STATUS_CODES[errorCode]).replace(/{errorDesc}/g, serverErrorDescs[errorCode]).replace(/{stack}/g, stack.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;")).replace(/{path}/g, req.url.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")).replace(/{server}/g, "" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (extName == undefined ? "" : " " + extName) + ((req.headers.host == undefined || isProxy) ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"))).replace(/{contact}/g, serverAdmin.replace(/\./g, "[dot]").replace(/@/g, "[at]")).replace(/{additionalError}/g, additionalError.toString()));
res.end();
}
});
}
}
var reqport = "";
var reqip = "";
var oldport = "";
var oldip = "";
if (req.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
reqport = null;
reqip = req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "");
if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip;
try {
oldport = req.socket.remotePort;
oldip = req.socket.remoteAddress;
req.socket.realRemotePort = reqport;
req.socket.realRemoteAddress = reqip;
req.socket.originalRemotePort = oldport;
req.socket.originalRemoteAddress = oldip;
res.socket.realRemotePort = reqport;
res.socket.realRemoteAddress = reqip;
res.socket.originalRemotePort = oldport;
res.socket.originalRemoteAddress = oldip;
} catch (ex) {
//Nevermind...
}
} else {
reqip = req.socket.remoteAddress;
reqport = req.socket.remotePort;
}
if (!isProxy) serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (req.method == "GET" ? "content in " : (req.method == "POST" ? "to post content in " : (req.method == "PUT" ? "to add content in " : (req.method == "DELETE" ? "to delete content in " : (req.method == "PATCH" ? "to patch content in " : "to access content using " + req.method + " method in "))))) + (req.headers.host == undefined ? "" : req.headers.host) + req.url);
else serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (req.method == "GET" ? "content in " : (req.method == "POST" ? "to post content in " : (req.method == "PUT" ? "to add content in " : (req.method == "DELETE" ? "to delete content in " : (req.method == "PATCH" ? "to patch content in " : "to access content using " + req.method + " method in "))))) + req.url);
if (req.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + req.headers["user-agent"]);
try {
if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
serverconsole.errmessage("Expectation not satified!");
callServerError(417);
return;
}
var hostx = req.headers.host;
if (hostx === undefined) {
serverconsole.errmessage("Bad request!");
callServerError(400);
return;
} }
if (req.method == "CONNECT") {
callServerError(501);
serverconsole.errmessage("CONNECT requests aren't supported. Your JS runtime probably doesn't support 'connect' handler for HTTP library.");
return;
}
if (isProxy) {
callServerError(501);
serverconsole.errmessage("This server will never be a proxy.");
return;
}
function urlParse(uri) {
if (typeof URL != "undefined" && url.Url) {
try {
var uobject = new URL(uri, "http" + (req.socket.encrypted ? "s" : "") + "://" + (req.headers.host ? req.headers.host : (domain ? domain : "unknown.invalid")));
var nuobject = new url.Url();
if (uri.indexOf("/") != -1) nuobject.slashes = true;
if (uobject.protocol != "") nuobject.protocol = uobject.protocol;
if (uobject.username != "" && uobject.password != "") nuobject.auth = uobject.username + ":" + uobject.password;
if (uobject.host != "") nuobject.host = uobject.host;
if (uobject.hostname != "") nuobject.hostname = uobject.hostname;
if (uobject.port != "") nuobject.port = uobject.port;
if (uobject.pathname != "") nuobject.pathname = uobject.pathname;
if (uobject.search != "") nuobject.search = uobject.search;
if (uobject.hash != "") nuobject.hash = uobject.hash;
if (uobject.href != "") nuobject.href = uobject.href;
if (uri.indexOf("/") != 0) {
if (nuobject.pathname) {
nuobject.pathname = nuobject.pathname.substr(1);
nuobject.href = nuobject.pathname + (nuobject.search ? nuobject.search : "");
}
}
if (nuobject.pathname) {
nuobject.path = nuobject.pathname + (nuobject.search ? nuobject.search : "");
}
//if(nuobject.path != "" && uobject.password != "") nuobject.path = nuobject.pathname + nuobject.href;
nuobject.query = {};
uobject.searchParams.forEach(function (value, key) {
nuobject.query[key] = value;
});
return nuobject;
} catch (ex) {
return url.parse(uri, true);
}
} else {
return url.parse(uri, true);
}
}
var urlp = urlParse("http://" + hostx);
try {
if (urlp.path.indexOf("//") == 0) {
urlp = urlParse("http:" + url.path);
}
} catch (ex) {
//URL parse error...
}
if (urlp.host == "localhost" || urlp.host == "localhost:" + port.toString() || urlp.host == "127.0.0.1" || urlp.host == "127.0.0.1:" + port.toString() || urlp.host == "::1" || urlp.host == "::1:" + port.toString()) {
urlp.protocol = "https:";
if (sport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + sport.toString();
urlp.port = sport.toString();
}
} else if (urlp.host == host || urlp.host == host + ":" + port.toString()) {
urlp.protocol = "https:";
if (sport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + sport.toString();
urlp.port = sport.toString();
}
} else if (urlp.host == pubip || urlp.host == pubip + ":" + pubport.toString()) {
urlp.protocol = "https:";
if (spubport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + spubport.toString();
urlp.port = spubport.toString();
}
} else if (urlp.hostname == domain || urlp.hostname.indexOf(domain) != -1) {
urlp.protocol = "https:";
if (spubport == 443) {
urlp.host = urlp.hostname;
} else {
urlp.host = urlp.hostname + ":" + spubport.toString();
urlp.port = spubport.toString();
}
} else {
urlp.protocol = "https:";
}
urlp.path = null;
urlp.pathname = null;
var lloc = url.format(urlp);
var requestURL = req.url;
try {
if (requestURL.split("/")[1].indexOf(".onion") != -1) {
requestURL = requestURL.split("/");
requestURL.shift();
requestURL.shift();
requestURL.unshift("");
requestURL = requestURL.join("/");
}
} catch (ex) {
//Leave URL as it is...
}
var rheaders = getCustomHeaders();
rheaders["Location"] = lloc + requestURL;
res.writeHead(301, "Redirect to HTTPS", rheaders);
res.end();
} catch (ex) {
serverconsole.errmessage("There was an error while processing the request!");
serverconsole.errmessage("Stack:");
serverconsole.errmessage(generateErrorStack(ex));
callServerError(500, undefined, generateErrorStack(ex));
} }
} }
@ -2874,7 +2851,7 @@ if (!cluster.isPrimary) {
hostname = hostname.join(":"); hostname = hostname.join(":");
} }
if (wwwredirect && hostname == domain && hostname.indexOf("www.") != 0) { if (wwwredirect && hostname == domain && hostname.indexOf("www.") != 0) {
var lloc = (request.socket.encrypted ? "https" : "http") + "://" + hostname + (hostport ? ":" + hostport : ""); var lloc = (request.socket.encrypted ? "https" : "http") + "://www." + hostname + (hostport ? ":" + hostport : "");
try { try {
var rheaders = getCustomHeaders(); var rheaders = getCustomHeaders();
rheaders["Location"] = lloc + (request.url.replace(/\/+/g, "/")); rheaders["Location"] = lloc + (request.url.replace(/\/+/g, "/"));
@ -2914,25 +2891,7 @@ if (!cluster.isPrimary) {
var reqip = ""; var reqip = "";
var oldport = ""; var oldport = "";
var oldip = ""; var oldip = "";
if (request.headers["x-svr-js-client"] != undefined && enableIPSpoofing) { if (request.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
var kl = request.headers["x-svr-js-client"].split(":");
reqport = kl.pop();
reqip = kl.join(":");
try {
oldport = request.socket.remotePort;
oldip = request.socket.remoteAddress;
request.socket.realRemotePort = reqport;
request.socket.realRemoteAddress = reqip;
request.socket.originalRemotePort = oldport;
request.socket.originalRemoteAddress = oldip;
response.socket.realRemotePort = reqport;
response.socket.realRemoteAddress = reqip;
response.socket.originalRemotePort = oldport;
response.socket.originalRemoteAddress = oldip;
} catch (ex) {
//Address setting failed
}
} else if (request.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
reqport = null; reqport = null;
reqip = request.headers["x-forwarded-for"].split(",")[0].replace(/ /g, ""); reqip = request.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "");
if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip; if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip;
@ -3302,25 +3261,7 @@ if (!cluster.isPrimary) {
var reqip = ""; var reqip = "";
var oldport = ""; var oldport = "";
var oldip = ""; var oldip = "";
if (req.headers["x-svr-js-client"] != undefined && enableIPSpoofing) { if (req.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
var kl = req.headers["x-svr-js-client"].split(":");
reqport = kl.pop();
reqip = kl.join(":");
try {
oldport = req.socket.remotePort;
oldip = req.socket.remoteAddress;
req.socket.realRemotePort = reqport;
req.socket.realRemoteAddress = reqip;
req.socket.originalRemotePort = oldport;
req.socket.originalRemoteAddress = oldip;
res.socket.realRemotePort = reqport;
res.socket.realRemoteAddress = reqip;
res.socket.originalRemotePort = oldport;
res.socket.originalRemoteAddress = oldip;
} catch (ex) {
//Nevermind...
}
} else if (req.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
reqport = null; reqport = null;
reqip = req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, ""); reqip = req.headers["x-forwarded-for"].split(",")[0].replace(/ /g, "");
if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip; if (reqip.indexOf(":") == -1) reqip = "::ffff:" + reqip;

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>SVR.JS 3.4.26 Tests</title> <title>SVR.JS 3.4.27 Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<style> <style>
@ -12,7 +12,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>SVR.JS 3.4.26 Tests</h1> <h1>SVR.JS 3.4.27 Tests</h1>
<h2>Directory</h2> <h2>Directory</h2>
<iframe src="/testdir" width="50%" height="300px"></iframe> <iframe src="/testdir" width="50%" height="300px"></iframe>
<h2>Directory (with query)</h2> <h2>Directory (with query)</h2>