From b21b8dc84c2e040f33428847f0e46a2845e7cfec Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 2 Sep 2023 19:35:01 +0200 Subject: [PATCH] Moved and fixed bug with "wwwredirect" --- config.json | 2 +- svr.js | 42 +++++++++++++++++++----------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/config.json b/config.json index 9b62f9d..3be3600 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "port": 80, "pubport": 80, "page404": "404.html", - "timestamp": 1693671292465, + "timestamp": 1693674757734, "blacklist": [], "nonStandardCodes": [], "enableCompression": true, diff --git a/svr.js b/svr.js index b000b72..f5406b3 100644 --- a/svr.js +++ b/svr.js @@ -3282,27 +3282,7 @@ if (!cluster.isPrimary) { } res.writeHeadNative(a, b, c); }; - if (wwwredirect) { - var hostname = req.headers.host.split[":"]; - var hostport = null; - if (hostname.length > 1 && (hostname[0] != "[" || hostname[hostname.length - 1] != "]")) hostport = hostname.pop(); - hostname = hostname.join(":"); - } - if (wwwredirect && hostname == domain && hostname.indexOf("www.") != 0) { - var lloc = (req.socket.encrypted ? "https" : "http") + "://" + hostname + (hostport ? ":" + hostport : ""); - try { - var rheaders = getCustomHeaders(); - rheaders["Location"] = lloc + (req.url.replace(/\/+/g, "/")); - res.writeHead(301, "Redirect to WWW", rheaders); - res.end(); - } catch (err) { - var cheaders = getCustomHeaders(); - cheaders["Content-Type"] = "text/html; charset=utf-8"; - res.writeHead(500, "Internal Server Error", cheaders); - res.write("500 Internal Server Error

500 Internal Server Error

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, ">")) + "

"); - res.end(); - } - } else { + var finished = false; res.on("finish", function () { if (!finished) { @@ -4495,7 +4475,7 @@ if (!cluster.isPrimary) { }); }; } - + try { //scan blacklist if (blacklist.check(reqip) && href != "/favicon.ico") { @@ -4563,6 +4543,22 @@ if (!cluster.isPrimary) { } } + //Handle redirects to addresses with www. + if (wwwredirect) { + var hostname = req.headers.host.split[":"]; + var hostport = null; + if (hostname.length > 1 && (hostname[0] != "[" || hostname[hostname.length - 1] != "]")) hostport = hostname.pop(); + hostname = hostname.join(":"); + if (hostname == domain && hostname.indexOf("www.") != 0) { + try { + redirect((req.socket.encrypted ? "https" : "http") + "://www." + hostname + (hostport ? ":" + hostport : "") + req.url.replace(/\/+/g, "/")); + } catch (err) { + callServerError(500, undefined, generateErrorStack(err)); + return; + } + } + } + //URL REWRITING function rewriteURL(address, map) { var rewrittenAddress = address; @@ -4916,7 +4912,7 @@ if (!cluster.isPrimary) { if (err.message == "Intentionally crashed") throw err; //If intentionally crashed, then crash SVR.JS callServerError(500, undefined, generateErrorStack(err)); //Else just return 500 error } - } + } //Listen port to server