From d688166022700c76ee8cf43c1129fd171462e410 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 24 Aug 2024 17:32:27 +0200 Subject: [PATCH] Add middleware responsible for setting response headers (res.setHeader function). --- src/index.js | 4 +++- src/middleware/core.js | 2 -- src/middleware/responseHeaders.js | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 src/middleware/responseHeaders.js diff --git a/src/index.js b/src/index.js index d66d091..a6cc5de 100644 --- a/src/index.js +++ b/src/index.js @@ -108,8 +108,10 @@ let middleware = [ require("./middleware/core.js"), require("./middleware/urlSanitizer.js"), require("./middleware/redirects.js"), + // TODO: blocklist require("./middleware/webRootPostfixes.js"), - require("./middleware/rewriteURL.js") + require("./middleware/rewriteURL.js"), + require("./middleware/responseHeaders.js") ]; function addMiddleware(mw) { diff --git a/src/middleware/core.js b/src/middleware/core.js index fcfae35..3d8afff 100644 --- a/src/middleware/core.js +++ b/src/middleware/core.js @@ -7,8 +7,6 @@ const serverHTTPErrorDescs = require("../res/httpErrorDescriptions.js"); const fixNodeMojibakeURL = require("../utils/urlMojibakeFixer.js"); const ipMatch = require("../utils/ipMatch.js"); const matchHostname = require("../utils/matchHostname.js"); -const svrjsInfo = require("../../svrjs.json"); -const version = svrjsInfo.version; if (!process.err4xxcounter) process.err4xxcounter = 0; if (!process.err5xxcounter) process.err5xxcounter = 0; diff --git a/src/middleware/responseHeaders.js b/src/middleware/responseHeaders.js new file mode 100644 index 0000000..4cf6346 --- /dev/null +++ b/src/middleware/responseHeaders.js @@ -0,0 +1,14 @@ +module.exports = (req, res, logFacilities, config, next) => { + if (!req.isProxy) { + var hkh = config.getCustomHeaders(); + Object.keys(hkh).forEach(function (hkS) { + try { + res.setHeader(hkS, hkh[hkS]); + } catch (err) { + // Headers will not be set. + } + }); + } + + next(); +};