diff --git a/controllers/middlewares/ServeLiveDirectory.js b/controllers/middlewares/ServeLiveDirectory.js index 91a0851..dbcdf29 100644 --- a/controllers/middlewares/ServeLiveDirectory.js +++ b/controllers/middlewares/ServeLiveDirectory.js @@ -62,7 +62,13 @@ class ServeLiveDirectory { this.#options = options } - handler (req, res, file) { + get (path) { + const file = this.instance.get(path) + + return file + } + + handler (req, res, path, file) { // Set Content-Type res.type(file.extension) @@ -93,12 +99,24 @@ class ServeLiveDirectory { return next() } - const file = this.instance.get(req.path) + // If root path is set, ensure it matches the request + let path = req.path + if (this.#options.root) { + if (path.indexOf(this.#options.root) === 0) { + // Re-map path for internal .get() + path = path.replace(this.#options.root, '') + } else { + // Immediately proceed to next middleware otherwise + return next() + } + } + + const file = this.get(path) if (file === undefined) { return next() } - return this.handler(req, res, file) + return this.handler(req, res, path, file) } #setHeaders (req, res, file) { diff --git a/controllers/middlewares/ServeStaticQuick.js b/controllers/middlewares/ServeStaticQuick.js index cf3acf9..c03a1d6 100644 --- a/controllers/middlewares/ServeStaticQuick.js +++ b/controllers/middlewares/ServeStaticQuick.js @@ -77,9 +77,17 @@ class ServeStaticQuick { this.#options = options } - handler (req, res, stat) { + get (path) { + const stat = this.files.get(path) + + if (!stat || stat.isDirectory()) return + + return stat + } + + handler (req, res, path, stat) { // Set Content-Type - res.type(req.path) + res.type(path) // Set header fields this.#setHeaders(req, res, stat) @@ -106,7 +114,7 @@ class ServeStaticQuick { res.end() } - return this.#stream(req, res, stat, result) + return this.#stream(req, res, path, stat, result) } // Returns a promise which resolves to true once ServeStaticQuick is ready @@ -154,26 +162,30 @@ class ServeStaticQuick { }) } - #get (path) { - const stat = this.files.get(path) - - if (!stat || stat.isDirectory()) return - - return stat - } - #middleware (req, res, next) { // Only process GET and HEAD requests if (req.method !== 'GET' && req.method !== 'HEAD') { return next() } - const stat = this.#get(req.path) + // If root path is set, ensure it matches the request + let path = req.path + if (this.#options.root) { + if (path.indexOf(this.#options.root) === 0) { + // Re-map path for internal .get() + path = path.replace(this.#options.root, '') + } else { + // Immediately proceed to next middleware otherwise + return next() + } + } + + const stat = this.get(path) if (stat === undefined) { return next() } - return this.handler(req, res, stat) + return this.handler(req, res, path, stat) } #setHeaders (req, res, stat) { @@ -198,8 +210,8 @@ class ServeStaticQuick { } } - #stream (req, res, stat, result) { - const fullPath = this.directory + req.path + #stream (req, res, path, stat, result) { + const fullPath = this.directory + path const readStream = fs.createReadStream(fullPath, result.options) readStream.on('error', error => { diff --git a/controllers/utils/serveUtils.js b/controllers/utils/serveUtils.js index f34f30d..b9bdf29 100644 --- a/controllers/utils/serveUtils.js +++ b/controllers/utils/serveUtils.js @@ -24,7 +24,7 @@ self.relativePath = (root, path) => { * Based on https://github.com/pillarjs/send/blob/0.18.0/index.js * Copyright(c) 2012 TJ Holowaychuk * Copyright(c) 2014-2022 Douglas Christopher Wilson - * MIT Licensed + * MIT License */ self.isRangeFresh = (req, res) => { diff --git a/lolisafe.js b/lolisafe.js index bc5ea87..31f919a 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -268,9 +268,9 @@ safe.use('/api', api) safe.use((req, res, next) => { if (req.method === 'GET' || req.method === 'HEAD') { const page = req.path === '/' ? 'home' : req.path.substring(1) - const customPage = serveLiveDirectoryCustomPagesInstance.instance.get(`${page}.html`) + const customPage = serveLiveDirectoryCustomPagesInstance.get(`${page}.html`) if (customPage) { - return serveLiveDirectoryCustomPagesInstance.handler(req, res, customPage) + return serveLiveDirectoryCustomPagesInstance.handler(req, res, req.path, customPage) } else if (config.pages.includes(page)) { // These rendered pages are persistently cached during production return res.render(page, {