1
0
Fork 0
forked from svrjs/svrjs

Replace "request" with "req" and "response" with "res"

This commit is contained in:
Dorian Niemiec 2023-08-31 23:03:02 +02:00
parent 4a138f73d8
commit bbb8a6f899
3 changed files with 90 additions and 92 deletions

View file

@ -3,7 +3,7 @@
"port": 80, "port": 80,
"pubport": 80, "pubport": 80,
"page404": "404.html", "page404": "404.html",
"timestamp": 1693313769931, "timestamp": 1693515753863,
"blacklist": [], "blacklist": [],
"nonStandardCodes": [], "nonStandardCodes": [],
"enableCompression": true, "enableCompression": true,
@ -89,5 +89,6 @@
"disableToHTTPSRedirect": false, "disableToHTTPSRedirect": false,
"enableETag": true, "enableETag": true,
"disableUnusedWorkerTermination": false, "disableUnusedWorkerTermination": false,
"rewriteDirtyURLs": true "rewriteDirtyURLs": true,
"errorPages": []
} }

175
svr.js
View file

@ -1143,7 +1143,7 @@ function sanitizeURL(resource) {
// Encode certain characters // Encode certain characters
resource = resource.replace(/[<>^`{|}]]/g, function (character) { resource = resource.replace(/[<>^`{|}]]/g, function (character) {
var charCode = character.charCodeAt(0); var charCode = character.charCodeAt(0);
return "%" + (charcode < 16 ? "0" : "") + charCode.toString(16).toUpperCase(); return "%" + (charCode < 16 ? "0" : "") + charCode.toString(16).toUpperCase();
}); });
var sanitizedResource = resource; var sanitizedResource = resource;
// Ensure the resource starts with a slash // Ensure the resource starts with a slash
@ -2992,7 +2992,7 @@ if (!cluster.isPrimary) {
modExecute(mods, vres(req, socket, head, serverconsole)); modExecute(mods, vres(req, socket, head, serverconsole));
} }
function reqhandler(request, response, fromMain) { function reqhandler(req, res, fromMain) {
if (fromMain === undefined) fromMain = true; if (fromMain === undefined) fromMain = true;
var reqIdInt = Math.round(Math.random() * 16777216); var reqIdInt = Math.round(Math.random() * 16777216);
var reqId = "0".repeat(6 - reqIdInt.toString(16).length) + reqIdInt.toString(16); var reqId = "0".repeat(6 - reqIdInt.toString(16).length) + reqIdInt.toString(16);
@ -3079,12 +3079,12 @@ if (!cluster.isPrimary) {
function matchHostname(hostname) { function matchHostname(hostname) {
if (typeof hostname == "undefined" || hostname == "*") { if (typeof hostname == "undefined" || hostname == "*") {
return true; return true;
} else if (request.headers.host && hostname.indexOf("*.") == 0 && hostname != "*.") { } else if (req.headers.host && hostname.indexOf("*.") == 0 && hostname != "*.") {
var hostnamesRoot = hostname.substr(2); var hostnamesRoot = hostname.substr(2);
if (request.headers.host == hostnamesRoot || request.headers.host.indexOf("." + hostnamesRoot) == request.headers.host.length - hostnamesRoot.length - 1) { if (req.headers.host == hostnamesRoot || req.headers.host.indexOf("." + hostnamesRoot) == req.headers.host.length - hostnamesRoot.length - 1) {
return true; return true;
} }
} else if (request.headers.host && request.headers.host == hostname) { } else if (req.headers.host && req.headers.host == hostname) {
return true; return true;
} }
return false; return false;
@ -3110,18 +3110,18 @@ if (!cluster.isPrimary) {
} }
} }
Object.keys(ph).forEach(function (phk) { Object.keys(ph).forEach(function (phk) {
if (typeof ph[phk] == "string") ph[phk] = ph[phk].replace(/\{path\}/g, request.url); if (typeof ph[phk] == "string") ph[phk] = ph[phk].replace(/\{path\}/g, req.url);
}); });
return ph; return ph;
} }
//Make HTTP/1.x API-based scripts compatible with HTTP/2.0 API //Make HTTP/1.x API-based scripts compatible with HTTP/2.0 API
if (configJSON.enableHTTP2 == true && request.httpVersion == "2.0") { if (configJSON.enableHTTP2 == true && req.httpVersion == "2.0") {
try { try {
//Set HTTP/1.x methods (to prevent process warnings) //Set HTTP/1.x methods (to prevent process warnings)
response.writeHeadNodeApi = response.writeHead; res.writeHeadNodeApi = res.writeHead;
response.setHeaderNodeApi = response.setHeader; res.setHeaderNodeApi = res.setHeader;
response.writeHead = function (a, b, c) { res.writeHead = function (a, b, c) {
var table = c; var table = c;
if (typeof (b) == "object") table = b; if (typeof (b) == "object") table = b;
if (table == undefined) table = this.tHeaders; if (table == undefined) table = this.tHeaders;
@ -3133,25 +3133,25 @@ if (!cluster.isPrimary) {
delete table["connection"]; delete table["connection"];
delete table["keep-alive"]; delete table["keep-alive"];
delete table["upgrade"]; delete table["upgrade"];
return response.writeHeadNodeApi(a, table); return res.writeHeadNodeApi(a, table);
}; };
response.setHeader = function (a, b) { res.setHeader = function (a, b) {
if (a != "transfer-encoding" && a != "connection" && a != "keep-alive" && a != "upgrade") return response.setHeaderNodeApi(a, b); if (a != "transfer-encoding" && a != "connection" && a != "keep-alive" && a != "upgrade") return res.setHeaderNodeApi(a, b);
return false; return false;
}; };
//Set HTTP/1.x headers //Set HTTP/1.x headers
if (!request.headers.host) request.headers.host = request.headers[":authority"]; if (!req.headers.host) req.headers.host = req.headers[":authority"];
(request.headers[":path"] == undefined ? (function () {})() : request.url = request.headers[":path"]); (req.headers[":path"] == undefined ? (function () {})() : req.url = req.headers[":path"]);
request.protocol = request.headers[":scheme"]; req.protocol = req.headers[":scheme"];
var headers = [":path", ":method"]; var headers = [":path", ":method"];
for (var i = 0; i < headers.length; i++) { for (var i = 0; i < headers.length; i++) {
if (request.headers[headers[i]] == undefined) { if (req.headers[headers[i]] == undefined) {
var cheaders = getCustomHeaders(); var cheaders = getCustomHeaders();
cheaders["Content-Type"] = "text/html; charset=utf-8"; cheaders["Content-Type"] = "text/html; charset=utf-8";
response.writeHead(400, "Bad Request", cheaders); res.writeHead(400, "Bad Request", cheaders);
response.write("<html><head><title>400 Bad Request</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>400 Bad Request</h1><p>The request you sent is invalid. <p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (request.headers[":authority"] == undefined ? "" : " on " + request.headers[":authority"]) + "</i></p></body></html>"); res.write("<html><head><title>400 Bad Request</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>400 Bad Request</h1><p>The request you sent is invalid. <p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (req.headers[":authority"] == undefined ? "" : " on " + req.headers[":authority"]) + "</i></p></body></html>");
response.end(); res.end();
return; return;
} }
} }
@ -3159,32 +3159,32 @@ if (!cluster.isPrimary) {
var cheaders = getCustomHeaders(); var cheaders = getCustomHeaders();
cheaders["Content-Type"] = "text/html; charset=utf-8"; cheaders["Content-Type"] = "text/html; charset=utf-8";
cheaders[":status"] = "500"; cheaders[":status"] = "500";
response.stream.respond(cheaders); res.stream.respond(cheaders);
response.stream.write("<html><head><title>500 Internal Server Error</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>500 Internal Server Error</h1><p>The server had an unexpected error. Below, error stack is shown: </p><code>" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;") + "</code><p>Please contact with developer/administrator of the website.</p><p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (request.headers[":authority"] == undefined ? "" : " on " + request.headers[":authority"]) + "</i></p></body></html>"); res.stream.write("<html><head><title>500 Internal Server Error</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>500 Internal Server Error</h1><p>The server had an unexpected error. Below, error stack is shown: </p><code>" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;") + "</code><p>Please contact with developer/administrator of the website.</p><p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (req.headers[":authority"] == undefined ? "" : " on " + req.headers[":authority"]) + "</i></p></body></html>");
response.stream.end(); res.stream.end();
return; return;
} }
} }
if (request.headers["x-svr-js-from-main-thread"] == "true" && (!request.socket.remoteAddress || request.socket.remoteAddress == "::1" || request.socket.remoteAddress == "::ffff:127.0.0.1" || request.socket.remoteAddress == "127.0.0.1" || request.socket.remoteAddress == "localhost" || request.socket.remoteAddress == host || request.socket.remoteAddress == "::ffff:" + host)) { if (req.headers["x-svr-js-from-main-thread"] == "true" && (!req.socket.remoteAddress || req.socket.remoteAddress == "::1" || req.socket.remoteAddress == "::ffff:127.0.0.1" || req.socket.remoteAddress == "127.0.0.1" || req.socket.remoteAddress == "localhost" || req.socket.remoteAddress == host || req.socket.remoteAddress == "::ffff:" + host)) {
var headers = getCustomHeaders(); var headers = getCustomHeaders();
response.writeHead(204, "No Content", headers); res.writeHead(204, "No Content", headers);
response.end(); res.end();
return; return;
} }
request.url = fixNodeMojibakeURL(request.url); req.url = fixNodeMojibakeURL(req.url);
var headWritten = false; var headWritten = false;
var lastStatusCode = null; var lastStatusCode = null;
response.writeHeadNative = response.writeHead; res.writeHeadNative = res.writeHead;
response.writeHead = function (a, b, c) { res.writeHead = function (a, b, c) {
if (!(headWritten && process.isBun && a === lastStatusCode && b === undefined && c === undefined)) { if (!(headWritten && process.isBun && a === lastStatusCode && b === undefined && c === undefined)) {
if (headWritten) { if (headWritten) {
process.emitWarning("res.writeHead called multiple times.", { process.emitWarning("res.writeHead called multiple times.", {
code: "WARN_SVRJS_MULTIPLE_WRITEHEAD" code: "WARN_SVRJS_MULTIPLE_WRITEHEAD"
}); });
return response; return res;
} else { } else {
headWritten = true; headWritten = true;
} }
@ -3199,47 +3199,47 @@ if (!cluster.isPrimary) {
} }
lastStatusCode = a; lastStatusCode = a;
} }
response.writeHeadNative(a, b, c); res.writeHeadNative(a, b, c);
}; };
if (wwwredirect) { if (wwwredirect) {
var hostname = request.headers.host.split[":"]; var hostname = req.headers.host.split[":"];
var hostport = null; var hostport = null;
if (hostname.length > 1 && (hostname[0] != "[" || hostname[hostname.length - 1] != "]")) hostport = hostname.pop(); if (hostname.length > 1 && (hostname[0] != "[" || hostname[hostname.length - 1] != "]")) hostport = hostname.pop();
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 = (req.socket.encrypted ? "https" : "http") + "://" + hostname + (hostport ? ":" + hostport : "");
try { try {
var rheaders = getCustomHeaders(); var rheaders = getCustomHeaders();
rheaders["Location"] = lloc + (request.url.replace(/\/+/g, "/")); rheaders["Location"] = lloc + (req.url.replace(/\/+/g, "/"));
response.writeHead(301, "Redirect to WWW", rheaders); res.writeHead(301, "Redirect to WWW", rheaders);
response.end(); res.end();
} catch (err) { } catch (err) {
var cheaders = getCustomHeaders(); var cheaders = getCustomHeaders();
cheaders["Content-Type"] = "text/html; charset=utf-8"; cheaders["Content-Type"] = "text/html; charset=utf-8";
res.writeHead(500, "Internal Server Error", cheaders); res.writeHead(500, "Internal Server Error", cheaders);
res.write("<html><head><title>500 Internal Server Error</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>500 Internal Server Error</h1><p>The server had an unexpected error. Below, error stack is shown: </p><code>" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;") + "</code><p>Please contact with developer/administrator of the website.</p><p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (req.headers.host == undefined ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</i></p></body></html>"); res.write("<html><head><title>500 Internal Server Error</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><h1>500 Internal Server Error</h1><p>The server had an unexpected error. Below, error stack is shown: </p><code>" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>").replace(/\r/g, "<br/>").replace(/ {2}/g, "&nbsp;&nbsp;") + "</code><p>Please contact with developer/administrator of the website.</p><p><i>" + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + (req.headers.host == undefined ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</i></p></body></html>");
response.end(); res.end();
} }
} else { } else {
var finished = false; var finished = false;
response.on("finish", function () { res.on("finish", function () {
if (!finished) { if (!finished) {
finished = true; finished = true;
serverconsole.locmessage("Client disconnected."); serverconsole.locmessage("Client disconnected.");
} }
}); });
response.on("close", function () { res.on("close", function () {
if (!finished) { if (!finished) {
finished = true; finished = true;
serverconsole.locmessage("Client disconnected."); serverconsole.locmessage("Client disconnected.");
} }
}); });
var isProxy = false; var isProxy = false;
if (request.url.indexOf("/") != 0 && request.url != "*") isProxy = true; if (req.url.indexOf("/") != 0 && req.url != "*") isProxy = true;
serverconsole.locmessage("Somebody connected to " + (secure && fromMain ? ((typeof sport == "number" ? "port " : "socket ") + sport) : ((typeof port == "number" ? "port " : "socket ") + port)) + "..."); serverconsole.locmessage("Somebody connected to " + (secure && fromMain ? ((typeof sport == "number" ? "port " : "socket ") + sport) : ((typeof port == "number" ? "port " : "socket ") + port)) + "...");
if (request.socket == null) { if (req.socket == null) {
serverconsole.errmessage("Client socket is null!!!"); serverconsole.errmessage("Client socket is null!!!");
return; return;
} }
@ -3248,54 +3248,54 @@ 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 (req.headers["x-svr-js-client"] != undefined && enableIPSpoofing) {
var kl = request.headers["x-svr-js-client"].split(":"); var kl = req.headers["x-svr-js-client"].split(":");
reqport = kl.pop(); reqport = kl.pop();
reqip = kl.join(":"); reqip = kl.join(":");
try { try {
oldport = request.socket.remotePort; oldport = req.socket.remotePort;
oldip = request.socket.remoteAddress; oldip = req.socket.remoteAddress;
request.socket.realRemotePort = reqport; req.socket.realRemotePort = reqport;
request.socket.realRemoteAddress = reqip; req.socket.realRemoteAddress = reqip;
request.socket.originalRemotePort = oldport; req.socket.originalRemotePort = oldport;
request.socket.originalRemoteAddress = oldip; req.socket.originalRemoteAddress = oldip;
response.socket.realRemotePort = reqport; res.socket.realRemotePort = reqport;
response.socket.realRemoteAddress = reqip; res.socket.realRemoteAddress = reqip;
response.socket.originalRemotePort = oldport; res.socket.originalRemotePort = oldport;
response.socket.originalRemoteAddress = oldip; res.socket.originalRemoteAddress = oldip;
} catch (err) { } catch (err) {
//Address setting failed //Address setting failed
} }
} else if (request.headers["x-forwarded-for"] != undefined && enableIPSpoofing) { } else if (req.headers["x-forwarded-for"] != undefined && enableIPSpoofing) {
reqport = null; reqport = null;
reqip = request.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;
try { try {
oldport = request.socket.remotePort; oldport = req.socket.remotePort;
oldip = request.socket.remoteAddress; oldip = req.socket.remoteAddress;
request.socket.realRemotePort = reqport; req.socket.realRemotePort = reqport;
request.socket.realRemoteAddress = reqip; req.socket.realRemoteAddress = reqip;
request.socket.originalRemotePort = oldport; req.socket.originalRemotePort = oldport;
request.socket.originalRemoteAddress = oldip; req.socket.originalRemoteAddress = oldip;
response.socket.realRemotePort = reqport; res.socket.realRemotePort = reqport;
response.socket.realRemoteAddress = reqip; res.socket.realRemoteAddress = reqip;
response.socket.originalRemotePort = oldport; res.socket.originalRemotePort = oldport;
response.socket.originalRemoteAddress = oldip; res.socket.originalRemoteAddress = oldip;
} catch (err) { } catch (err) {
//Address setting failed //Address setting failed
} }
} else { } else {
reqip = request.socket.remoteAddress; reqip = req.socket.remoteAddress;
reqport = request.socket.remotePort; reqport = req.socket.remotePort;
} }
reqcounter++; reqcounter++;
if (!isProxy) serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " wants " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + (request.headers.host == undefined ? "" : request.headers.host) + request.url); 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 " + (request.method == "GET" ? "content in " : (request.method == "POST" ? "to post content in " : (request.method == "PUT" ? "to add content in " : (request.method == "DELETE" ? "to delete content in " : (request.method == "PATCH" ? "to patch content in " : "to access content using " + request.method + " method in "))))) + request.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 (request.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + request.headers["user-agent"]); if (req.headers["user-agent"] != undefined) serverconsole.reqmessage("Client uses " + req.headers["user-agent"]);
var acceptEncoding = request.headers["accept-encoding"]; var acceptEncoding = req.headers["accept-encoding"];
if (!acceptEncoding) acceptEncoding = ""; if (!acceptEncoding) acceptEncoding = "";
var head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header var head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header
@ -3332,9 +3332,6 @@ if (!cluster.isPrimary) {
// } // }
// }); // });
// } // }
var req = request; // request var is req = request
var res = response; // response var is res = response
//Error descriptions //Error descriptions
var serverErrorDescs = { var serverErrorDescs = {
@ -3503,8 +3500,8 @@ if (!cluster.isPrimary) {
fs.readFile(errorFile, function (err, data) { fs.readFile(errorFile, function (err, data) {
try { try {
if (err) throw err; if (err) throw err;
response.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); 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, request.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 placeholders in error response 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]")); // Replace placeholders in error response
responseEnd(); responseEnd();
} catch (err) { } catch (err) {
var additionalError = 500; var additionalError = 500;
@ -3523,9 +3520,9 @@ if (!cluster.isPrimary) {
additionalError = 508; additionalError = 508;
} }
response.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders); res.writeHead(errorCode, http.STATUS_CODES[errorCode], cheaders);
response.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, request.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())); // Replace placeholders in error response 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())); // Replace placeholders in error response
response.end(); res.end();
} }
}); });
} }
@ -3843,7 +3840,7 @@ if (!cluster.isPrimary) {
var hdhds = getCustomHeaders(); var hdhds = getCustomHeaders();
hdhds["Content-Type"] = "text/html; charset=utf-8"; hdhds["Content-Type"] = "text/html; charset=utf-8";
res.writeHead(200, "OK", hdhds); res.writeHead(200, "OK", hdhds);
res.end((head == "" ? "<html><head><title>SVR.JS status" + (request.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body>" : head.replace(/<head>/i, "<head><title>SVR.JS status" + (request.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title>")) + "<h1>SVR.JS status" + (request.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</h1>Server version: " + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + "<br/><hr/>Current time: " + new Date().toString() + "<br/>Thread start time: " + new Date(new Date() - (process.uptime() * 1000)).toString() + "<br/>Thread uptime: " + formatRelativeTime(Math.floor(process.uptime())) + "<br/>OS uptime: " + formatRelativeTime(os.uptime()) + "<br/>Total request count: " + reqcounter + "<br/>Average request rate: " + (Math.round((reqcounter / process.uptime()) * 100) / 100) + " requests/s" + (process.memoryUsage ? ("<br/>Memory usage of thread: " + sizify(process.memoryUsage().rss) + "B") : "") + (process.cpuUsage ? ("<br/>Total CPU usage by thread: u" + (process.cpuUsage().user / 1000) + "ms s" + (process.cpuUsage().system / 1000) + "ms - " + (Math.round((((process.cpuUsage().user + process.cpuUsage().system) / 1000000) / process.uptime()) * 1000) / 1000) + "%") : "") + "<br/>Thread PID: " + process.pid + "<br/>" + (foot == "" ? "</body></html>" : foot)); res.end((head == "" ? "<html><head><title>SVR.JS status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body>" : head.replace(/<head>/i, "<head><title>SVR.JS status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</title>")) + "<h1>SVR.JS status" + (req.headers.host == undefined ? "" : " for " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</h1>Server version: " + (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + "<br/><hr/>Current time: " + new Date().toString() + "<br/>Thread start time: " + new Date(new Date() - (process.uptime() * 1000)).toString() + "<br/>Thread uptime: " + formatRelativeTime(Math.floor(process.uptime())) + "<br/>OS uptime: " + formatRelativeTime(os.uptime()) + "<br/>Total request count: " + reqcounter + "<br/>Average request rate: " + (Math.round((reqcounter / process.uptime()) * 100) / 100) + " requests/s" + (process.memoryUsage ? ("<br/>Memory usage of thread: " + sizify(process.memoryUsage().rss) + "B") : "") + (process.cpuUsage ? ("<br/>Total CPU usage by thread: u" + (process.cpuUsage().user / 1000) + "ms s" + (process.cpuUsage().system / 1000) + "ms - " + (Math.round((((process.cpuUsage().user + process.cpuUsage().system) / 1000000) / process.uptime()) * 1000) / 1000) + "%") : "") + "<br/>Thread PID: " + process.pid + "<br/>" + (foot == "" ? "</body></html>" : foot));
return; return;
} else if (version.indexOf("Nightly-") === 0 && (href == "/crash.svr" || (os.platform() == "win32" && href.toLowerCase() == "/crash.svr"))) { } else if (version.indexOf("Nightly-") === 0 && (href == "/crash.svr" || (os.platform() == "win32" && href.toLowerCase() == "/crash.svr"))) {
throw new Error("Intentionally crashed"); throw new Error("Intentionally crashed");
@ -3908,13 +3905,13 @@ if (!cluster.isPrimary) {
if (e || !s.isFile()) { if (e || !s.isFile()) {
fs.stat((readFrom + "/index.xhtml").replace(/\/+/g, "/"), function (e, s) { fs.stat((readFrom + "/index.xhtml").replace(/\/+/g, "/"), function (e, s) {
if (e || !s.isFile()) { if (e || !s.isFile()) {
properServe(); properDirectoryListingServe();
} else { } else {
stats = s; stats = s;
pth = (pth + "/index.xhtml").replace(/\/+/g, "/"); pth = (pth + "/index.xhtml").replace(/\/+/g, "/");
ext = "xhtml"; ext = "xhtml";
readFrom = "./" + pth; readFrom = "./" + pth;
properServe(); properDirectoryListingServe();
} }
}); });
} else { } else {
@ -3922,7 +3919,7 @@ if (!cluster.isPrimary) {
pth = (pth + "/index.htm").replace(/\/+/g, "/"); pth = (pth + "/index.htm").replace(/\/+/g, "/");
ext = "htm"; ext = "htm";
readFrom = "./" + pth; readFrom = "./" + pth;
properServe(); properDirectoryListingServe();
} }
}); });
} else { } else {
@ -3930,16 +3927,16 @@ if (!cluster.isPrimary) {
pth = (pth + "/index.html").replace(/\/+/g, "/"); pth = (pth + "/index.html").replace(/\/+/g, "/");
ext = "html"; ext = "html";
readFrom = "./" + pth; readFrom = "./" + pth;
properServe(); properDirectoryListingServe();
} }
}); });
} }
}); });
} else { } else {
properServe(); properDirectoryListingServe();
} }
function properServe() { function properDirectoryListingServe() {
if (stats.isDirectory()) { if (stats.isDirectory()) {
// Check if directory listing is enabled in the configuration // Check if directory listing is enabled in the configuration
if (checkForEnabledDirectoryListing(req.headers.host)) { if (checkForEnabledDirectoryListing(req.headers.host)) {
@ -4445,8 +4442,8 @@ if (!cluster.isPrimary) {
if (blacklist.check(reqip) && href != "/favicon.ico") { if (blacklist.check(reqip) && href != "/favicon.ico") {
var bheaders = getCustomHeaders(); var bheaders = getCustomHeaders();
bheaders["Content-Type"] = "text/html; charset=utf8"; bheaders["Content-Type"] = "text/html; charset=utf8";
response.writeHead(403, "Client blocked", bheaders); res.writeHead(403, "Client blocked", bheaders);
response.write("<!DOCTYPE html><html><head><title>Access denied - SVR.JS</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><br/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><div style=\"height: auto; width: 70%; border-style: solid; border-width: 5; border-color: red; text-align: center; margin: 0 auto;\"><h1>ACCESS DENIED</h1><p style=\"font-size:20px\">Request from " + reqip + " is denied. The client is now in the blacklist.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" + (req.headers.host == undefined ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</p></div></body></html>"); res.write("<!DOCTYPE html><html><head><title>Access denied - SVR.JS</title><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><br/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /></head><body><div style=\"height: auto; width: 70%; border-style: solid; border-width: 5; border-color: red; text-align: center; margin: 0 auto;\"><h1>ACCESS DENIED</h1><p style=\"font-size:20px\">Request from " + reqip + " is denied. The client is now in the blacklist.</p><p style=\"font-style: italic; font-weight: normal;\">SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" + (req.headers.host == undefined ? "" : " on " + String(req.headers.host).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")) + "</p></div></body></html>");
serverconsole.errmessage("Client blocked"); serverconsole.errmessage("Client blocked");
return; return;
} }
@ -4584,7 +4581,7 @@ if (!cluster.isPrimary) {
var hkh = getCustomHeaders(); var hkh = getCustomHeaders();
Object.keys(hkh).forEach(function(hkS) { Object.keys(hkh).forEach(function(hkS) {
try { try {
response.setHeader(hkS, hkh[hkS]); res.setHeader(hkS, hkh[hkS]);
} catch (err) { } catch (err) {
//Headers will not be set. //Headers will not be set.
} }

View file

@ -1 +1 @@
31 33