1
0
Fork 0
forked from svrjs/svrjs

Change all of the internal calls to "callServerError"

This commit is contained in:
Dorian Niemiec 2024-03-31 19:15:10 +02:00
parent 51544f5732
commit 9eff118962

93
svr.js
View file

@ -2399,6 +2399,27 @@ if (!cluster.isPrimary) {
// Server error calling method // Server error calling method
function callServerError(errorCode, extName, stack, ch) { function callServerError(errorCode, extName, stack, ch) {
if (typeof errorCode !== "number") {
throw new TypeError("HTTP error code parameter needs to be an integer.");
}
// Handle optional parameters
if (extName && typeof extName === "object") {
ch = stack;
stack = extName;
extName = undefined;
} else if (typeof extName !== "string" && extName !== null && extName !== undefined) {
throw new TypeError("Extension name parameter needs to be a string.");
}
if (stack && typeof stack === "object" && Object.prototype.toString.call(stack) !== "[object Error]") {
ch = stack;
stack = undefined;
} else if (typeof stack !== "object" && typeof stack !== "string" && stack) {
throw new TypeError("Error stack parameter needs to be either a string or an instance of Error object.");
}
// Determine error file
function getErrorFileName(list, callback, _i) { function getErrorFileName(list, callback, _i) {
if (err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) { if (err.code == "ERR_SSL_HTTP_REQUEST" && process.version && parseInt(process.version.split(".")[0].substr(1)) >= 16) {
// Disable custom error page for HTTP SSL error // Disable custom error page for HTTP SSL error
@ -2420,14 +2441,14 @@ if (!cluster.isPrimary) {
callback("." + errorCode.toString()); callback("." + errorCode.toString());
} }
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
}); });
} else { } else {
try { try {
callback(page404); callback(page404);
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
} }
}); });
@ -2440,7 +2461,7 @@ if (!cluster.isPrimary) {
callback("." + errorCode.toString()); callback("." + errorCode.toString());
} }
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
}); });
} }
@ -3069,14 +3090,14 @@ if (!cluster.isPrimary) {
callback("." + errorCode.toString()); callback("." + errorCode.toString());
} }
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
}); });
} else { } else {
try { try {
callback(page404); callback(page404);
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
} }
}); });
@ -3089,7 +3110,7 @@ if (!cluster.isPrimary) {
callback("." + errorCode.toString()); callback("." + errorCode.toString());
} }
} catch (err2) { } catch (err2) {
callServerError(500, undefined, generateErrorStack(err2)); callServerError(500, err2);
} }
}); });
} }
@ -3192,7 +3213,7 @@ if (!cluster.isPrimary) {
head = fs.existsSync("./.head") ? fs.readFileSync("./.head").toString() : (fs.existsSync("./head.html") ? fs.readFileSync("./head.html").toString() : ""); // header 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 foot = fs.existsSync("./.foot") ? fs.readFileSync("./.foot").toString() : (fs.existsSync("./foot.html") ? fs.readFileSync("./foot.html").toString() : ""); // footer
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
// Function to perform HTTP redirection to a specified destination URL // Function to perform HTTP redirection to a specified destination URL
@ -3234,7 +3255,7 @@ if (!cluster.isPrimary) {
customHeaders["Allow"] = "POST"; customHeaders["Allow"] = "POST";
// Call the server error function with 405 status code and custom headers // Call the server error function with 405 status code and custom headers
callServerError(405, undefined, undefined, customHeaders); callServerError(405, customHeaders);
return; return;
} }
@ -3248,7 +3269,7 @@ if (!cluster.isPrimary) {
} }
// If the formidable module had an error, call the server error function with 500 status code and error stack // If the formidable module had an error, call the server error function with 500 status code and error stack
if (formidable._errored) callServerError(500, undefined, generateErrorStack(formidable._errored)); if (formidable._errored) callServerError(500, formidable._errored);
// Create a new formidable form // Create a new formidable form
var form = formidable(formidableOptions); var form = formidable(formidableOptions);
@ -3507,7 +3528,7 @@ if (!cluster.isPrimary) {
serverconsole.errmessage("Symbolic link loop detected."); serverconsole.errmessage("Symbolic link loop detected.");
return; return;
} else { } else {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
return; return;
} }
} }
@ -3765,7 +3786,7 @@ if (!cluster.isPrimary) {
callServerError(508); // The symbolic link loop is detected during file system operations. callServerError(508); // The symbolic link loop is detected during file system operations.
serverconsole.errmessage("Symbolic link loop detected."); serverconsole.errmessage("Symbolic link loop detected.");
} else { } else {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
} }
}); });
@ -3800,7 +3821,7 @@ if (!cluster.isPrimary) {
callServerError(508); // The symbolic link loop is detected during file system operations. callServerError(508); // The symbolic link loop is detected during file system operations.
serverconsole.errmessage("Symbolic link loop detected."); serverconsole.errmessage("Symbolic link loop detected.");
} else { } else {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
return; return;
} }
@ -3837,7 +3858,7 @@ if (!cluster.isPrimary) {
if (ifMatchETag && ifMatchETag !== "*" && ifMatchETag !== fileETag) { if (ifMatchETag && ifMatchETag !== "*" && ifMatchETag !== fileETag) {
var headers = getCustomHeaders(); var headers = getCustomHeaders();
headers.ETag = clientETag; headers.ETag = clientETag;
callServerError(412, undefined, undefined, headers); callServerError(412, headers);
return; return;
} }
} }
@ -3858,7 +3879,7 @@ if (!cluster.isPrimary) {
try { try {
isCompressable = canCompress(href, dontCompress); isCompressable = canCompress(href, dontCompress);
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
return; return;
} }
@ -3879,7 +3900,7 @@ if (!cluster.isPrimary) {
rhd["Content-Range"] = "bytes */" + filelen; rhd["Content-Range"] = "bytes */" + filelen;
var regexmatch = req.headers["range"].match(/bytes=([0-9]*)-([0-9]*)/); var regexmatch = req.headers["range"].match(/bytes=([0-9]*)-([0-9]*)/);
if (!regexmatch) { if (!regexmatch) {
callServerError(416, undefined, undefined, rhd); callServerError(416, rhd);
} else { } else {
// Process the partial content request // Process the partial content request
var beginOrig = regexmatch[1]; var beginOrig = regexmatch[1];
@ -3887,7 +3908,7 @@ if (!cluster.isPrimary) {
var begin = 0; var begin = 0;
var end = filelen - 1; var end = filelen - 1;
if (beginOrig == "" && endOrig == "") { if (beginOrig == "" && endOrig == "") {
callServerError(416, undefined, undefined, rhd); callServerError(416, rhd);
return; return;
} else if (beginOrig == "") { } else if (beginOrig == "") {
begin = end - parseInt(endOrig) + 1; begin = end - parseInt(endOrig) + 1;
@ -3896,7 +3917,7 @@ if (!cluster.isPrimary) {
if (endOrig != "") end = parseInt(endOrig); if (endOrig != "") end = parseInt(endOrig);
} }
if (begin > end || begin < 0 || begin > filelen - 1) { if (begin > end || begin < 0 || begin > filelen - 1) {
callServerError(416, undefined, undefined, rhd); callServerError(416, rhd);
return; return;
} }
if (end > filelen - 1) end = filelen - 1; if (end > filelen - 1) end = filelen - 1;
@ -3928,7 +3949,7 @@ if (!cluster.isPrimary) {
callServerError(508); // The symbolic link loop is detected during file system operations. callServerError(508); // The symbolic link loop is detected during file system operations.
serverconsole.errmessage("Symbolic link loop detected."); serverconsole.errmessage("Symbolic link loop detected.");
} else { } else {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
}).on("open", function () { }).on("open", function () {
try { try {
@ -3936,7 +3957,7 @@ if (!cluster.isPrimary) {
readStream.pipe(res); readStream.pipe(res);
serverconsole.resmessage("Client successfully received content."); serverconsole.resmessage("Client successfully received content.");
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
}); });
} else { } else {
@ -3945,7 +3966,7 @@ if (!cluster.isPrimary) {
} }
} }
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
} else { } else {
try { try {
@ -3988,7 +4009,7 @@ if (!cluster.isPrimary) {
callServerError(508); // The symbolic link loop is detected during file system operations. callServerError(508); // The symbolic link loop is detected during file system operations.
serverconsole.errmessage("Symbolic link loop detected."); serverconsole.errmessage("Symbolic link loop detected.");
} else { } else {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
}).on("open", function () { }).on("open", function () {
try { try {
@ -4025,7 +4046,7 @@ if (!cluster.isPrimary) {
} }
serverconsole.resmessage("Client successfully received content."); serverconsole.resmessage("Client successfully received content.");
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
}); });
} else { } else {
@ -4034,7 +4055,7 @@ if (!cluster.isPrimary) {
serverconsole.resmessage("Client successfully received content."); serverconsole.resmessage("Client successfully received content.");
} }
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
} }
}); });
@ -4230,7 +4251,7 @@ if (!cluster.isPrimary) {
try { try {
callback(); callback();
} catch (err) { } catch (err) {
callServerError(500, undefined, err); callServerError(500, err);
} }
} else { } else {
var destinationURL = uobject; var destinationURL = uobject;
@ -4335,7 +4356,7 @@ if (!cluster.isPrimary) {
// Rewrite URLs // Rewrite URLs
rewriteURL(req.url, rewriteMap, function (err, rewrittenURL) { rewriteURL(req.url, rewriteMap, function (err, rewrittenURL) {
if (err) { if (err) {
callServerError(500, undefined, err); callServerError(500, err);
return; return;
} }
if (rewrittenURL != req.url) { if (rewrittenURL != req.url) {
@ -4520,7 +4541,7 @@ if (!cluster.isPrimary) {
var cacheEntry = null; var cacheEntry = null;
if (list[_i].scrypt) { if (list[_i].scrypt) {
if (!crypto.scrypt) { if (!crypto.scrypt) {
callServerError(500, undefined, new Error("SVR.JS doesn't support scrypt-hashed passwords on Node.JS versions without scrypt hash support.")); callServerError(500, new Error("SVR.JS doesn't support scrypt-hashed passwords on Node.JS versions without scrypt hash support."));
return; return;
} else { } else {
cacheEntry = scryptCache.find(function (entry) { cacheEntry = scryptCache.find(function (entry) {
@ -4531,7 +4552,7 @@ if (!cluster.isPrimary) {
} else { } else {
crypto.scrypt(password, list[_i].salt, 64, function (err, derivedKey) { crypto.scrypt(password, list[_i].salt, 64, function (err, derivedKey) {
if (err) { if (err) {
callServerError(500, undefined, err); callServerError(500, err);
} else { } else {
var key = derivedKey.toString("hex"); var key = derivedKey.toString("hex");
scryptCache.push({ scryptCache.push({
@ -4547,7 +4568,7 @@ if (!cluster.isPrimary) {
} }
} else if (list[_i].pbkdf2) { } else if (list[_i].pbkdf2) {
if (crypto.__disabled__ !== undefined) { if (crypto.__disabled__ !== undefined) {
callServerError(500, undefined, new Error("SVR.JS doesn't support PBKDF2-hashed passwords on Node.JS versions without crypto support.")); callServerError(500, new Error("SVR.JS doesn't support PBKDF2-hashed passwords on Node.JS versions without crypto support."));
return; return;
} else { } else {
cacheEntry = pbkdf2Cache.find(function (entry) { cacheEntry = pbkdf2Cache.find(function (entry) {
@ -4558,7 +4579,7 @@ if (!cluster.isPrimary) {
} else { } else {
crypto.pbkdf2(password, list[_i].salt, 36250, 64, "sha512", function (err, derivedKey) { crypto.pbkdf2(password, list[_i].salt, 36250, 64, "sha512", function (err, derivedKey) {
if (err) { if (err) {
callServerError(500, undefined, err); callServerError(500, err);
} else { } else {
var key = derivedKey.toString("hex"); var key = derivedKey.toString("hex");
pbkdf2Cache.push({ pbkdf2Cache.push({
@ -4583,20 +4604,20 @@ if (!cluster.isPrimary) {
ha["WWW-Authenticate"] = "Basic realm=\"" + (authcode.realm ? authcode.realm.replace(/(\\|")/g, "\\$1") : "SVR.JS HTTP Basic Authorization") + "\", charset=\"UTF-8\""; ha["WWW-Authenticate"] = "Basic realm=\"" + (authcode.realm ? authcode.realm.replace(/(\\|")/g, "\\$1") : "SVR.JS HTTP Basic Authorization") + "\", charset=\"UTF-8\"";
var credentials = req.headers["authorization"]; var credentials = req.headers["authorization"];
if (!credentials) { if (!credentials) {
callServerError(401, undefined, undefined, ha); callServerError(401, ha);
serverconsole.errmessage("Content needs authorization."); serverconsole.errmessage("Content needs authorization.");
return; return;
} }
var credentialsMatch = credentials.match(/^Basic (.+)$/); var credentialsMatch = credentials.match(/^Basic (.+)$/);
if (!credentialsMatch) { if (!credentialsMatch) {
callServerError(401, undefined, undefined, ha); callServerError(401, ha);
serverconsole.errmessage("Malformed credentials."); serverconsole.errmessage("Malformed credentials.");
return; return;
} }
var decodedCredentials = Buffer.from(credentialsMatch[1], "base64").toString("utf8"); var decodedCredentials = Buffer.from(credentialsMatch[1], "base64").toString("utf8");
var decodedCredentialsMatch = decodedCredentials.match(/^([^:]*):(.*)$/); var decodedCredentialsMatch = decodedCredentials.match(/^([^:]*):(.*)$/);
if (!decodedCredentialsMatch) { if (!decodedCredentialsMatch) {
callServerError(401, undefined, undefined, ha); callServerError(401, ha);
serverconsole.errmessage("Malformed credentials."); serverconsole.errmessage("Malformed credentials.");
return; return;
} }
@ -4650,7 +4671,7 @@ if (!cluster.isPrimary) {
} }
} }
} }
callServerError(401, undefined, undefined, ha); callServerError(401, ha);
serverconsole.errmessage("User \"" + String(username).replace(/[\r\n]/g, "") + "\" failed to log in."); serverconsole.errmessage("User \"" + String(username).replace(/[\r\n]/g, "") + "\" failed to log in.");
} else { } else {
if (bruteProtection) { if (bruteProtection) {
@ -4669,12 +4690,12 @@ if (!cluster.isPrimary) {
}); });
} }
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
return; return;
} }
}); });
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
return; return;
} }
} }
@ -4721,7 +4742,7 @@ if (!cluster.isPrimary) {
}); });
} catch (err) { } catch (err) {
callServerError(500, undefined, generateErrorStack(err)); callServerError(500, err);
} }
} }