1
0
Fork 0
forked from svrjs/svrjs

Remove blocking calls from custom head and custom foot functionality. Also optimize built-in "vres" callbacks.

This commit is contained in:
Dorian Niemiec 2024-05-05 22:07:44 +02:00
parent a1160bd891
commit 45d45c41af

136
svr.js
View file

@ -2661,10 +2661,62 @@ if (!cluster.isPrimary) {
malformedcounter++; malformedcounter++;
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)) + "...");
serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " sent invalid request."); serverconsole.reqmessage("Client " + ((!reqip || reqip == "") ? "[unknown client]" : (reqip + ((reqport && reqport !== 0) && reqport != "" ? ":" + reqport : ""))) + " sent invalid request.");
try {
head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header
foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer
function getCustomHeader(callback) {
fs.readFile(("./.head").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
fs.readFile(("./head.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err);
}
} else {
head = data.toString();
callback();
}
});
} else {
callServerError(500, err);
}
} else {
head = data.toString();
callback();
}
});
}
function getCustomFooter(callback) {
fs.readFile(("./.foot").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
fs.readFile(("./foot.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err);
}
} else {
foot = data.toString();
callback();
}
});
} else {
callServerError(500, err);
}
} else {
foot = data.toString();
callback();
}
});
}
getCustomHeader(function () {
getCustomFooter(function () {
try {
if ((err.code && (err.code.indexOf("ERR_SSL_") == 0 || err.code.indexOf("ERR_TLS_") == 0)) || (!err.code && err.message.indexOf("SSL routines") != -1)) { if ((err.code && (err.code.indexOf("ERR_SSL_") == 0 || err.code.indexOf("ERR_TLS_") == 0)) || (!err.code && err.message.indexOf("SSL routines") != -1)) {
if (err.code == "ERR_SSL_HTTP_REQUEST" || err.message.indexOf("http request") != -1) { if (err.code == "ERR_SSL_HTTP_REQUEST" || err.message.indexOf("http request") != -1) {
serverconsole.errmessage("Client sent HTTP request to HTTPS port."); serverconsole.errmessage("Client sent HTTP request to HTTPS port.");
@ -2753,6 +2805,8 @@ if (!cluster.isPrimary) {
serverconsole.errmessage("There was an error while determining type of malformed request."); serverconsole.errmessage("There was an error while determining type of malformed request.");
callServerError(400); callServerError(400);
} }
});
});
} }
function connhandler(request, socket, head) { function connhandler(request, socket, head) {
@ -2865,13 +2919,12 @@ if (!cluster.isPrimary) {
modFunction(); modFunction();
} }
function vres(req, socket, head, serverconsole) { function vres() {
return function () {
serverconsole.errmessage("SVR.JS doesn't support proxy without proxy mod."); serverconsole.errmessage("SVR.JS doesn't support proxy without proxy mod.");
if (!socket.destroyed) socket.end("HTTP/1.1 501 Not Implemented\n\n"); if (!socket.destroyed) socket.end("HTTP/1.1 501 Not Implemented\n\n");
};
} }
modExecute(mods, vres(req, socket, head, serverconsole));
modExecute(mods, vres);
} }
function reqhandler(req, res, fromMain) { function reqhandler(req, res, fromMain) {
@ -3315,12 +3368,57 @@ if (!cluster.isPrimary) {
}); });
} }
try { function getCustomHeader(callback) {
head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header fs.readFile(("./.head").replace(/\/+/g, "/"), function (err, data) {
foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer if (err) {
} catch (err) { if (err.code == "ENOENT" || err.code == "EISDIR") {
fs.readFile(("./head.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err); callServerError(500, err);
} }
} else {
head = data.toString();
callback();
}
});
} else {
callServerError(500, err);
}
} else {
head = data.toString();
callback();
}
});
}
function getCustomFooter(callback) {
fs.readFile(("./.foot").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
fs.readFile(("./foot.html").replace(/\/+/g, "/"), function (err, data) {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
callback();
} else {
callServerError(500, err);
}
} else {
foot = data.toString();
callback();
}
});
} else {
callServerError(500, err);
}
} else {
foot = data.toString();
callback();
}
});
}
// Function to perform HTTP redirection to a specified destination URL // Function to perform HTTP redirection to a specified destination URL
@ -3394,6 +3492,8 @@ if (!cluster.isPrimary) {
}); });
} }
getCustomHeader( function () {
getCustomFooter( function () {
// Authenticated user variable // Authenticated user variable
var authUser = null; var authUser = null;
@ -3421,6 +3521,7 @@ if (!cluster.isPrimary) {
serverconsole.errmessage("Bad request!"); serverconsole.errmessage("Bad request!");
return; return;
} }
var origHref = href; // Placeholder origHref
if (req.headers["expect"] && req.headers["expect"] != "100-continue") { if (req.headers["expect"] && req.headers["expect"] != "100-continue") {
@ -3453,8 +3554,7 @@ if (!cluster.isPrimary) {
var vresCalled = false; var vresCalled = false;
function vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, callServerError, getCustomHeaders, origHref, redirect, parsePostData, authUser) { function vres() {
return function () {
if (vresCalled) { if (vresCalled) {
process.emitWarning("elseCallback() invoked multiple times.", { process.emitWarning("elseCallback() invoked multiple times.", {
code: "WARN_SVRJS_MULTIPLE_ELSECALLBACK" code: "WARN_SVRJS_MULTIPLE_ELSECALLBACK"
@ -4138,7 +4238,6 @@ if (!cluster.isPrimary) {
} }
} }
}); });
};
} }
try { try {
@ -4356,7 +4455,7 @@ if (!cluster.isPrimary) {
} }
} }
var origHref = href; origHref = href;
// Add web root postfixes // Add web root postfixes
if (!isProxy) { if (!isProxy) {
@ -4801,7 +4900,7 @@ if (!cluster.isPrimary) {
serverconsole.reqmessage("Client is logged in as \"" + String(username).replace(/[\r\n]/g, "") + "\"."); serverconsole.reqmessage("Client is logged in as \"" + String(username).replace(/[\r\n]/g, "") + "\".");
authUser = username; authUser = username;
redirectTrailingSlashes(function () { redirectTrailingSlashes(function () {
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData, authUser)); modExecute(mods, vres);
}); });
} }
} catch (err) { } catch (err) {
@ -4850,7 +4949,7 @@ if (!cluster.isPrimary) {
} }
} else { } else {
redirectTrailingSlashes(function () { redirectTrailingSlashes(function () {
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData, authUser)); modExecute(mods, vres);
}); });
} }
} }
@ -4859,8 +4958,9 @@ if (!cluster.isPrimary) {
} catch (err) { } catch (err) {
callServerError(500, err); callServerError(500, err);
} }
});
});
} }
function serverErrorHandler(err, isRedirect) { function serverErrorHandler(err, isRedirect) {
if(isRedirect) attmtsRedir--; if(isRedirect) attmtsRedir--;
else attmts--; else attmts--;