From 1161256ab963b0b6651f3bc9e832e3a30973bc37 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 24 Aug 2024 17:06:34 +0200 Subject: [PATCH] Create config.generateServerString() function and src/utils/generateServerString.js file. --- src/index.js | 7 ++++--- src/middleware/core.js | 35 ++++++++----------------------- src/utils/generateServerString.js | 19 +++++++++++++++++ 3 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 src/utils/generateServerString.js diff --git a/src/index.js b/src/index.js index cca0305..08404b7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ const http = require("http"); const fs = require("fs"); //const generateErrorStack = require("./utils/generateErrorStack.js"); -const getOS = require("./utils/getOS.js"); +//const getOS = require("./utils/getOS.js"); +const generateServerString = require("./utils/generateServerString.js") const svrjsInfo = require("../svrjs.json"); const version = svrjsInfo.version; //const parseURL = require("./utils/urlParser.js"); @@ -149,7 +150,7 @@ function requestHandler(req, res) { logFacilities.errmessage("Stack:"); logFacilities.errmessage(err.stack); res.writeHead(500, "Internal Server Error", { - Server: (config.exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + Server: generateServerString(config.exposeServerVersion) }); res.end("Error while executing the request handler"); } @@ -158,7 +159,7 @@ function requestHandler(req, res) { if (res.error) res.error(404); else { res.writeHead(404, "Not Found", { - Server: (config.exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") + Server: generateServerString(config.exposeServerVersion) }); res.end("Request handler missing"); } diff --git a/src/middleware/core.js b/src/middleware/core.js index 603617f..dd22894 100644 --- a/src/middleware/core.js +++ b/src/middleware/core.js @@ -2,9 +2,10 @@ const http = require("http"); const fs = require("fs"); const net = require("net"); const generateErrorStack = require("../utils/generateErrorStack.js"); +const generateServerString = require("../utils/generateServerString.js"); const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js"); const fixNodeMojibakeURL = require("../utils/urlMojibakeFixer.js"); -const getOS = require("../utils/getOS.js"); +//const getOS = require("../utils/getOS.js"); const ipMatch = require("../utils/ipMatch.js"); const svrjsInfo = require("../../svrjs.json"); const version = svrjsInfo.version; @@ -37,6 +38,11 @@ module.exports = (req, res, logFacilities, config, next) => { return false; }; + config.generateServerString = () => { + return generateServerString(config.exposeServerVersion); + }; + + // getCustomHeaders() in SVR.JS 3.x config.getCustomHeaders = () => { let ph = Object.assign(config.customHeaders); if (config.customHeadersVHost) { @@ -63,17 +69,7 @@ module.exports = (req, res, logFacilities, config, next) => { if (typeof ph[phk] == "string") ph[phk] = ph[phk].replace(/\{path\}/g, req.url); }); - ph["Server"] = config.exposeServerVersion - ? "SVR.JS/" + - version + - " (" + - getOS() + - "; " + - (process.isBun - ? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version - : "Node.JS/" + process.version) + - ")" - : "SVR.JS"; + ph["Server"] = config.generateServerString(); return ph; }; @@ -588,20 +584,7 @@ module.exports = (req, res, logFacilities, config, next) => { /{server}/g, "" + ( - (config.exposeServerVersion - ? "SVR.JS/" + - version + - " (" + - getOS() + - "; " + - (process.isBun - ? "Bun/v" + - process.versions.bun + - "; like Node.JS/" + - process.version - : "Node.JS/" + process.version) + - ")" - : "SVR.JS") + + config.generateServerString() + (!config.exposeModsInErrorPages || extName == undefined ? "" : " " + extName) diff --git a/src/utils/generateServerString.js b/src/utils/generateServerString.js new file mode 100644 index 0000000..5087791 --- /dev/null +++ b/src/utils/generateServerString.js @@ -0,0 +1,19 @@ +const svrjsInfo = require("../../svrjs.json"); +const version = svrjsInfo.version; +const getOS = require("./getOS.js"); + +function generateServerString(exposeServerVersion) { + return exposeServerVersion + ? "SVR.JS/" + + version + + " (" + + getOS() + + "; " + + (process.isBun + ? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version + : "Node.JS/" + process.version) + + ")" + : "SVR.JS"; +} + +module.exports = generateServerString; \ No newline at end of file