From 1e4605066175bc83832a9cc4df6232a882939cee Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 4 Jan 2021 21:35:11 +0100 Subject: Fix mime types for .well-known/matrix files --- webserver.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/webserver.js b/webserver.js index 3405431..e5a62c8 100755 --- a/webserver.js +++ b/webserver.js @@ -84,7 +84,12 @@ for (let i = 0; i < module_list.length; i++) { } -function requestFile(req, res, listdirs, path, origpath) { +// if 'mime' is not null/undefined, it should be the content-type of the file +// options: +// { mime: "content-type value", listdirs: true/false, cors: true/false } +// (defaults are null/false) +function requestFile(req, res, path, origpath, options) { + if (options == null) options = {}; fname = cmn.webfilesdir + path; console.log("Requesting file " + fname); if (!fs.existsSync(fname)) { @@ -93,8 +98,11 @@ function requestFile(req, res, listdirs, path, origpath) { } const stats = fs.statSync(fname); if (stats.isFile()) { - res.sendFile(fname); - } else if (stats.isDirectory() && listdirs) { + const headers = {}; + if (options.mime != null) headers["content-type"] = options.mime; + if (options.cors == true) headers["access-control-allow-origin"] = "*"; + res.sendFile(fname, {headers}); + } else if (stats.isDirectory() && options.listdirs) { const items = fs .readdirSync(fname) .filter(function (f) { return f[0] != "."; }) @@ -144,16 +152,20 @@ app.get("/", function (req, res) { app.get(["/f/univq", "/f/univq/*"], cmn.authgen()); app.get(["/f", "/f/*"], function (req, res) { - requestFile(req, res, false, unescape(makeUrlSafe(req, 2)), req.url); + requestFile(req, res, unescape(makeUrlSafe(req, 2)), req.url); }); app.get(["/ff", "/ff/*"], cmn.authgen()); app.get(["/ff", "/ff/*"], function (req, res) { - requestFile(req, res, true, unescape(makeUrlSafe(req, 3)), req.url); + requestFile(req, res, unescape(makeUrlSafe(req, 3)), req.url, {listdirs: true}); +}); + +app.get("/.well-known/matrix/*", function (req, res) { + requestFile(req, res, "/well-known" + unescape(makeUrlSafe(req, 12)), req.url, {mime: "application/json", cors: true}); }); app.get("/.well-known/*", function (req, res) { - requestFile(req, res, false, "/well-known" + unescape(makeUrlSafe(req, 12)), req.url); + requestFile(req, res, "/well-known" + unescape(makeUrlSafe(req, 12)), req.url); }); app.get("/google*.html", function (req, res) { -- cgit v1.2.3-70-g09d2