diff --git a/index.html b/index.html index cc449ce..fec351f 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@
-Changes:
Bugs:
The server had an unexpected error. Below, error stack is shown:
" + (stackHidden ? "[error stack hidden]" : generateErrorStack(ex)).replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, " ") + "
Please contact with developer/administrator of the website.
" + (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"]) + "
"); + response.stream.write("The server had an unexpected error. Below, error stack is shown:
" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, " ") + "
Please contact with developer/administrator of the website.
" + (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"]) + "
"); response.stream.end(); return; } @@ -2905,7 +2909,7 @@ if (!cluster.isMaster) { try { request.url = encodeURI(Buffer.from(request.url, "latin1").toString("utf8")).replace(/%25/gi, "%"); - } catch (ex) { + } catch (err) { //Request URL not modified... } @@ -2948,11 +2952,11 @@ if (!cluster.isMaster) { rheaders["Location"] = lloc + (request.url.replace(/\/+/g, "/")); response.writeHead(301, "Redirect to WWW", rheaders); response.end(); - } catch (ex) { + } catch (err) { var cheaders = getCustomHeaders(); cheaders["Content-Type"] = "text/html; charset=utf-8"; res.writeHead(500, "Internal Server Error", cheaders); - res.write("The server had an unexpected error. Below, error stack is shown:
" + (stackHidden ? "[error stack hidden]" : generateErrorStack(ex)).replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, " ") + "
Please contact with developer/administrator of the website.
" + (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, "&").replace(//g, ">")) + "
"); + res.write("The server had an unexpected error. Below, error stack is shown:
" + (stackHidden ? "[error stack hidden]" : generateErrorStack(err)).replace(/\r\n/g, "
").replace(/\n/g, "
").replace(/\r/g, "
").replace(/ {2}/g, " ") + "
Please contact with developer/administrator of the website.
" + (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, "&").replace(//g, ">")) + "
"); response.end(); } } else { @@ -2997,7 +3001,7 @@ if (!cluster.isMaster) { response.socket.realRemoteAddress = reqip; response.socket.originalRemotePort = oldport; response.socket.originalRemoteAddress = oldip; - } catch (ex) { + } catch (err) { //Address setting failed } } else if (request.headers["x-forwarded-for"] != undefined && enableIPSpoofing) { @@ -3015,7 +3019,7 @@ if (!cluster.isMaster) { response.socket.realRemoteAddress = reqip; response.socket.originalRemotePort = oldport; response.socket.originalRemoteAddress = oldip; - } catch (ex) { + } catch (err) { //Address setting failed } } else { @@ -3193,16 +3197,18 @@ if (!cluster.isMaster) { response.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, "&").replace(//g, ">").replace(/\r\n/g, "Wait...
\n\n\n\nThis copy of SVR.JS is not genuine."); } return; - } catch (ex) { - callServerError(500, undefined, generateErrorStack(ex)); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); return; } } else if (allowStatus && (href == "/svrjsstatus.svr" || (os.platform() == "win32" && href.toLowerCase() == "/svrjsstatus.svr"))) { @@ -3587,6 +3593,10 @@ if (!cluster.isMaster) { serverconsole.errmessage("Resource not found."); return; } + } else if (err.code == "ENOTDIR") { + callServerError(404); // Assume that file doesn't exist. + serverconsole.errmessage("Resource not found."); + return; } else if (err.code == "EACCES") { callServerError(403); serverconsole.errmessage("Access denied."); @@ -3851,20 +3861,23 @@ if (!cluster.isMaster) { serverconsole.resmessage("Client successfully received content."); }); - } catch (ex) { - if (ex.code == "ENOENT") { + } catch (err) { + if (err.code == "ENOENT") { callServerError(404); serverconsole.errmessage("Resource not found."); - } else if (ex.code == "EACCES") { + } else if (err.code == "ENOTDIR") { + callServerError(404); // Assume that file doesn't exist. + serverconsole.errmessage("Resource not found."); + } else if (err.code == "EACCES") { callServerError(403); serverconsole.errmessage("Access denied."); - } else if (ex.code == "EMFILE") { - callServerError(500, undefined, generateErrorStack(ex)); // Too many file descriptors or open files were reached by the process. It is an internal server issue. - } else if (ex.code == "ELOOP") { + } else if (err.code == "EMFILE") { + callServerError(500, undefined, generateErrorStack(err)); // Too many file descriptors or open files were reached by the process. It is an internal server issue. + } else if (err.code == "ELOOP") { callServerError(508); // The symbolic link loop is detected during file system operations. serverconsole.errmessage("Symbolic link loop detected."); } else { - callServerError(500, undefined, generateErrorStack(ex)); + callServerError(500, undefined, generateErrorStack(err)); } } }); @@ -3885,6 +3898,9 @@ if (!cluster.isMaster) { if (err.code == "ENOENT") { callServerError(404); serverconsole.errmessage("Resource not found."); + } else if (err.code == "ENOTDIR") { + callServerError(404); // Assume that file doesn't exist. + serverconsole.errmessage("Resource not found."); } else if (err.code == "EACCES") { callServerError(403); serverconsole.errmessage("Access denied."); @@ -4003,6 +4019,9 @@ if (!cluster.isMaster) { if (err.code == "ENOENT") { callServerError(404); serverconsole.errmessage("Resource not found."); + } else if (err.code == "ENOTDIR") { + callServerError(404); // Assume that file doesn't exist. + serverconsole.errmessage("Resource not found."); } else if (err.code == "EACCES") { callServerError(403); serverconsole.errmessage("Access denied."); @@ -4019,8 +4038,8 @@ if (!cluster.isMaster) { res.writeHead(206, http.STATUS_CODES[206], rhd); readStream.pipe(res); serverconsole.resmessage("Client successfully received content."); - } catch (ex) { - callServerError(500, undefined, generateErrorStack(ex)); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); } }); } else { @@ -4028,8 +4047,8 @@ if (!cluster.isMaster) { res.end(); } } - } catch (ex) { - callServerError(500, undefined, generateErrorStack(ex)); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); } } else { try { @@ -4055,20 +4074,23 @@ if (!cluster.isMaster) { if (req.method != "HEAD") { var readStream = fs.createReadStream(readFrom); - readStream.on("error", function (ex) { - if (ex.code == "ENOENT") { + readStream.on("error", function (err) { + if (err.code == "ENOENT") { callServerError(404); serverconsole.errmessage("Resource not found."); - } else if (ex.code == "EACCES") { + } else if (err.code == "ENOTDIR") { + callServerError(404); // Assume that file doesn't exist. + serverconsole.errmessage("Resource not found."); + } else if (err.code == "EACCES") { callServerError(403); serverconsole.errmessage("Access denied."); - } else if (ex.code == "EMFILE") { - callServerError(500, undefined, generateErrorStack(ex)); // Too many file descriptors or open files were reached by the process. It is an internal server issue. - } else if (ex.code == "ELOOP") { + } else if (err.code == "EMFILE") { + callServerError(500, undefined, generateErrorStack(err)); // Too many file descriptors or open files were reached by the process. It is an internal server issue. + } else if (err.code == "ELOOP") { callServerError(508); // The symbolic link loop is detected during file system operations. serverconsole.errmessage("Symbolic link loop detected."); } else { - callServerError(500, undefined, generateErrorStack(ex)); + callServerError(500, undefined, generateErrorStack(err)); } }).on("open", function () { try { @@ -4104,8 +4126,8 @@ if (!cluster.isMaster) { readStream.pipe(resStream); } serverconsole.resmessage("Client successfully received content."); - } catch (ex) { - callServerError(500, undefined, generateErrorStack(ex)); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); } }); } else { @@ -4113,8 +4135,8 @@ if (!cluster.isMaster) { res.end(); serverconsole.resmessage("Client successfully received content."); } - } catch (ex) { - callServerError(500, undefined, generateErrorStack(ex)); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); } } }); @@ -4202,7 +4224,7 @@ if (!cluster.isMaster) { try { decodedHref = decodeURIComponent(href); - } catch (ex) { + } catch (err) { //Return 400 error callServerError(400); serverconsole.errmessage("Bad request!"); @@ -4229,7 +4251,7 @@ if (!cluster.isMaster) { ext = ext.substr(1, ext.length); try { decodedHref = decodeURIComponent(href); - } catch (ex) { + } catch (err) { //Return 400 error callServerError(400); serverconsole.errmessage("Bad request!"); @@ -4247,7 +4269,7 @@ if (!cluster.isMaster) { for (var i = 0; i < hk.length; i++) { try { response.setHeader(hk[i], hkh[hk[i]]); - } catch (ex) { + } catch (err) { //Headers will not be set. } } @@ -4276,21 +4298,15 @@ if (!cluster.isMaster) { var regexI = []; if (!isProxy && nonStandardCodes != undefined) { for (var i = 0; i < nonStandardCodes.length; i++) { - var mth = false; + var isMatch = false; if (nonStandardCodes[i].regex) { - var regexObj = nonStandardCodes[i].regex.split("/"); - if (regexObj.length == 0) throw new Error("Invalid regex!"); - var modifiers = regexObj.pop(); - if (!modifiers.match(/i/i) && os.platform() == "win32") modifiers += "i"; - regexObj.shift(); - var searchString = regexObj.join("/"); - var rx = RegExp(searchString, modifiers); - mth = req.url.match(rx) || href.match(rx); - regexI.push(rx); + var createdRegex = createRegex(nonStandardCodes[i].regex, true) + isMatch = req.url.match(createdRegex) || href.match(createdRegex); + regexI.push(createdRegex); } else { - mth = nonStandardCodes[i].url == href || (os.platform() == "win32" && nonStandardCodes[i].url.toLowerCase() == href.toLowerCase()); + isMatch = nonStandardCodes[i].url == href || (os.platform() == "win32" && nonStandardCodes[i].url.toLowerCase() == href.toLowerCase()); } - if (mth) { + if (isMatch) { if (nonStandardCodes[i].scode == 401) { if (authIndex == -1) { authIndex = i; @@ -4298,12 +4314,12 @@ if (!cluster.isMaster) { } else { if (nonscodeIndex == -1) { if ((nonStandardCodes[i].scode == 403 || nonStandardCodes[i].scode == 451) && nonStandardCodes[i].users !== undefined) { - var lpk = false; + var toBreakLoop = false; if (nonStandardCodes[i].users.check(reqip)) { nonscodeIndex = i; - lpk = true; + toBreakLoop = true; } - if (lpk) break; + if (toBreakLoop) break; } else { nonscodeIndex = i; } @@ -4359,21 +4375,21 @@ if (!cluster.isMaster) { serverconsole.errmessage("Content needs authorization."); return; } - var cmatch = credentials.match(/^Basic (.+)$/); - if (!cmatch) { + var credentialsMatch = credentials.match(/^Basic (.+)$/); + if (!credentialsMatch) { callServerError(401, undefined, undefined, ha); serverconsole.errmessage("Malformed credentials."); return; } - var c2 = Buffer.from(cmatch[1], "base64").toString("utf8"); - var c2match = c2.match(/^([^:]*):(.*)$/); - if (!c2match) { + var decodedCredentials = Buffer.from(credentialsMatch[1], "base64").toString("utf8"); + var decodedCredentialsMatch = decodedCredentials.match(/^([^:]*):(.*)$/); + if (!decodedCredentialsMatch) { callServerError(401, undefined, undefined, ha); serverconsole.errmessage("Malformed credentials."); return; } - var username = c2match[1]; - var password = c2match[2]; + var username = decodedCredentialsMatch[1]; + var password = decodedCredentialsMatch[2]; var authorized = false; for (var i = 0; i < users.length; i++) { var hash = sha256(password + users[i].salt); @@ -4446,10 +4462,10 @@ if (!cluster.isMaster) { modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, fd, callServerError, getCustomHeaders, origHref, redirect, parsePostData)); } } - } catch (ex) { + } catch (err) { //CRASH HANDLER - if (ex.message == "Intentionally crashed") throw ex; //If intentionally crashed, then crash SVR.JS - callServerError(500, undefined, generateErrorStack(ex)); //Else just return 500 error + if (err.message == "Intentionally crashed") throw err; //If intentionally crashed, then crash SVR.JS + callServerError(500, undefined, generateErrorStack(err)); //Else just return 500 error } } @@ -4734,9 +4750,9 @@ function start(init) { process.send("Server closed."); process.send("\x12CLOSE"); } - } catch (ex) { - if (cluster.isMaster === undefined) serverconsole.climessage("Cannot close server! Reason: " + ex.message); - else process.send("Cannot close server! Reason: " + ex.message); + } catch (err) { + if (cluster.isMaster === undefined) serverconsole.climessage("Cannot close server! Reason: " + err.message); + else process.send("Cannot close server! Reason: " + err.message); } }, open: function () { @@ -4752,9 +4768,9 @@ function start(init) { process.send("Server opened."); process.send("\x12OPEN"); } - } catch (ex) { - if (cluster.isMaster === undefined) serverconsole.climessage("Cannot open server! Reason: " + ex.message); - else process.send("Cannot open server! Reason: " + ex.message); + } catch (err) { + if (cluster.isMaster === undefined) serverconsole.climessage("Cannot open server! Reason: " + err.message); + else process.send("Cannot open server! Reason: " + err.message); } }, help: function () { @@ -4829,8 +4845,8 @@ function start(init) { try { saveConfig(); serverconsole.locmessage("Configuration saved."); - } catch (ex) { - throw new Error(ex); + } catch (err) { + throw new Error(err); } }, 300000); } else if (cluster.isMaster) { @@ -4842,13 +4858,13 @@ function start(init) { cluster.workers[allClusters[i]].on("message", msgListener); cluster.workers[allClusters[i]].send("\x14SAVECONF"); } - } catch (ex) { + } catch (err) { if (cluster.workers[allClusters[i]]) { cluster.workers[allClusters[i]].removeAllListeners("message"); cluster.workers[allClusters[i]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[i]])); cluster.workers[allClusters[i]].on("message", listenConnListener); } - serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + ex.message); + serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + err.message); } } }, 300000); @@ -4864,8 +4880,8 @@ function start(init) { try { saveConfig(); process.send("\x12SAVEGOOD"); - } catch (ex) { - process.send("\x12SAVEERR" + ex.message); + } catch (err) { + process.send("\x12SAVEERR" + err.message); } process.send("\x12END"); } else if (commands[line.split(" ")[0]] !== undefined && commands[line.split(" ")[0]] !== null) { @@ -4877,7 +4893,7 @@ function start(init) { process.send("Unrecognized command \"" + line.split(" ")[0] + "\"."); process.send("\x12END"); } - } catch (ex) { + } catch (err) { if (line != "") { process.send("Can't execute command \"" + line.split(" ")[0] + "\"."); process.send("\x12END"); @@ -4908,7 +4924,7 @@ function start(init) { if (cluster.workers[allClusters[i]]) { cluster.workers[allClusters[i]].kill(); } - } catch (ex) { + } catch (err) { stopError = true; } } @@ -4919,7 +4935,7 @@ function start(init) { try { var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; //1 core deleted for safety... if (cpus > useAvailableCores) cpus = useAvailableCores; - } catch (ex) { + } catch (err) { //Nevermind... Don't want SVR.JS to fail starting, because os.freemem function is not working. } if (cpus < 1) cpus = 1; //If SVR.JS is run on Haiku or if useAvailableCores = 0 @@ -4951,7 +4967,7 @@ function start(init) { cluster.workers[allClusters[i]].on("message", msgListener); cluster.workers[allClusters[i]].send(line); } - } catch (ex) { + } catch (err) { if (cluster.workers[allClusters[i]]) { cluster.workers[allClusters[i]].removeAllListeners("message"); cluster.workers[allClusters[i]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[i]])); @@ -4973,7 +4989,7 @@ function start(init) { } try { commands[command](argss); - } catch (ex) { + } catch (err) { serverconsole.climessage("Unrecognized command \"" + command + "\"."); } } @@ -4990,7 +5006,7 @@ function start(init) { try { var useAvailableCores = Math.round((os.freemem()) / 50000000) - 1; //1 core deleted for safety... if (cpus > useAvailableCores) cpus = useAvailableCores; - } catch (ex) { + } catch (err) { //Nevermind... Don't want SVR.JS to fail starting, because os.freemem function is not working. } if (cpus < 1) cpus = 1; //If SVR.JS is run on Haiku (os.cpus in Haiku returns empty array) or if useAvailableCores = 0 @@ -5185,8 +5201,8 @@ function saveConfig() { var configString = JSON.stringify(configJSONobj, null, 2); fs.writeFileSync(__dirname + "/config.json", configString); break; - } catch (ex) { - if (i >= 2) throw ex; + } catch (err) { + if (i >= 2) throw err; var now = Date.now(); while (Date.now() - now < 2); } @@ -5195,40 +5211,40 @@ function saveConfig() { //Process event listeners if (cluster.isMaster || cluster.isMaster === undefined) { - process.on("uncaughtException", function (ex) { + process.on("uncaughtException", function (err) { //CRASH HANDLER serverconsole.locerrmessage("SVR.JS master process just crashed!!!"); serverconsole.locerrmessage("Stack:"); - serverconsole.locerrmessage(generateErrorStack(ex)); - process.exit(ex.errno); + serverconsole.locerrmessage(generateErrorStack(err)); + process.exit(err.errno); }); - process.on("unhandledRejection", function (ex) { + process.on("unhandledRejection", function (err) { //CRASH HANDLER serverconsole.locerrmessage("SVR.JS master process just crashed!!!"); serverconsole.locerrmessage("Stack:"); - serverconsole.locerrmessage(ex.stack ? generateErrorStack(ex) : String(ex)); - process.exit(ex.errno); + serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err)); + process.exit(err.errno); }); process.on("exit", function (code) { try { saveConfig(); - } catch (ex) { - serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + ex.message); + } catch (err) { + serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + err.message); } try { deleteFolderRecursive(__dirname + "/temp"); - } catch (ex) { + } catch (err) { //Error! } try { fs.mkdirSync(__dirname + "/temp"); - } catch (ex) { + } catch (err) { //Error! } if (process.isBun) { try { fs.writeFileSync(__dirname + "/temp/serverSideScript.js", "//Placeholder server-side JavaScript to workaround Bun bug.\r\n"); - } catch (ex) { + } catch (err) { //Error! } } @@ -5251,7 +5267,7 @@ if (cluster.isMaster || cluster.isMaster === undefined) { if (cluster.workers[allClusters[i]]) { cluster.workers[allClusters[i]].send("stop"); } - } catch (ex) { + } catch (err) { //Worker will crash with EPIPE anyway. } } @@ -5260,19 +5276,19 @@ if (cluster.isMaster || cluster.isMaster === undefined) { process.exit(); }); } else { - process.on("uncaughtException", function (ex) { + process.on("uncaughtException", function (err) { //CRASH HANDLER serverconsole.locerrmessage("SVR.JS worker just crashed!!!"); serverconsole.locerrmessage("Stack:"); - serverconsole.locerrmessage(generateErrorStack(ex)); - process.exit(ex.errno); + serverconsole.locerrmessage(generateErrorStack(err)); + process.exit(err.errno); }); - process.on("unhandledRejection", function (ex) { + process.on("unhandledRejection", function (err) { //CRASH HANDLER serverconsole.locerrmessage("SVR.JS worker just crashed!!!"); serverconsole.locerrmessage("Stack:"); - serverconsole.locerrmessage(ex.stack ? generateErrorStack(ex) : String(ex)); - process.exit(ex.errno); + serverconsole.locerrmessage(err.stack ? generateErrorStack(err) : String(err)); + process.exit(err.errno); }); process.on("warning", function (warning) { serverconsole.locwarnmessage(warning.message); @@ -5285,11 +5301,11 @@ if (cluster.isMaster || cluster.isMaster === undefined) { //Call start try { start(true); -} catch (ex) { +} catch (err) { serverconsole.locerrmessage("There was a problem starting SVR.JS!!!"); serverconsole.locerrmessage("Stack:"); - serverconsole.locerrmessage(generateErrorStack(ex)); - process.exit(ex.errno); + serverconsole.locerrmessage(generateErrorStack(err)); + process.exit(err.errno); } ////////////////////////////////// diff --git a/tests.html b/tests.html index d529961..1b390d9 100644 --- a/tests.html +++ b/tests.html @@ -1,7 +1,7 @@ -