|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * files.js: Sub-app to handle access to static files through |
| 4 | + * files.interlisp.org. |
| 5 | + * |
| 6 | + * 2024-07-07 Frank Halasz |
| 7 | + * |
| 8 | + * |
| 9 | + * Copyright: 2024 by Interlisp.org |
| 10 | + * |
| 11 | + * |
| 12 | + ******************************************************************************/ |
| 13 | +const mFs = require('fs'); |
| 14 | +const mPath = require('node:path'); |
| 15 | +const mExpress = require("express"); |
| 16 | + |
| 17 | +const config = require("./config"); |
| 18 | +const { isText, isBinary, getEncoding } = require('istextorbinary'); |
| 19 | + |
| 20 | +const filesApp = mExpress(); |
| 21 | + |
| 22 | +// Define function to set content type headers for files with no |
| 23 | +// extension. Use isBinary to distinguish octet-stream versus |
| 24 | +// text content types. |
| 25 | +// Used by mExpress.static |
| 26 | +function setContentType(res, path, stat) { |
| 27 | + const ext = mPath.extname(path); |
| 28 | + if(ext.length == 0) { |
| 29 | + const buffer = mFs.readFileSync(path); |
| 30 | + const name = mPath.basename(path); |
| 31 | + if(isInterlispSource(buffer, name)) { |
| 32 | + res.set('Content-Type', 'application/octet-stream'); |
| 33 | + } else if (isText(null, buffer)) { |
| 34 | + res.set('Content-Type', 'text/plain; charset=UTF-8'); |
| 35 | + res.set('X-Content-Type-Options', 'nosniff'); |
| 36 | + } else { |
| 37 | + res.set('Content-Type', 'application/octet-stream'); |
| 38 | + } |
| 39 | + } else { |
| 40 | + switch(ext) { |
| 41 | + case '.awk': |
| 42 | + case '.cmd': |
| 43 | + case '.command': |
| 44 | + case '.dribble': |
| 45 | + case '.iss': |
| 46 | + case '.lisp': |
| 47 | + case '.ps1': |
| 48 | + case '.sh': |
| 49 | + case '.tty': |
| 50 | + case '.sh': |
| 51 | + case '.yaml': |
| 52 | + case '.yml': |
| 53 | + res.set('Content-Type', 'text/plain; charset=UTF-8'); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Does buffer contain an Interlisp source file? |
| 60 | +function isInterlispSource(buffer, name) { |
| 61 | + return ( |
| 62 | + buffer.includes("(DEFINE-FILE-INFO") |
| 63 | + && buffer.includes(name + "COMS") |
| 64 | + ); |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +filesApp.use((req, res, next) => { |
| 70 | + if(req.protocol === 'https' || !config.supportHttps) next(); |
| 71 | + else res.redirect(`https://${req.hostname}:${config.httpsRedirectPort}${req.url}`); |
| 72 | + }); |
| 73 | + |
| 74 | +const staticOptions = { |
| 75 | + dotfiles: "allow", |
| 76 | + index: "index.html", |
| 77 | + setHeaders: setContentType |
| 78 | +}; |
| 79 | + |
| 80 | +filesApp.use(mExpress.static(config.filesHostingPath, staticOptions)); |
| 81 | + |
| 82 | +// can't find file - send error message |
| 83 | +filesApp.use((req, res, next) => { |
| 84 | + if(req.url == "/favicon.ico") { |
| 85 | + res.status(404); |
| 86 | + res.end(); |
| 87 | + } else { |
| 88 | + res.status(404); |
| 89 | + res.send("<!DOCTYPE html><html><body><h3>ERROR: Cannot find file: " + req.originalUrl + "</h3></body></html>"); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | + |
| 94 | +module.exports = filesApp; |
0 commit comments