1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- var fs = require("fs");
- var pathlib = require("path");
-
- function mimetype(path) {
- }
-
- module.exports = function(root, before) {
-
- return function(req, res, app) {
- var pn = req.urlobj.pathname;
-
- // Send a file
- function send(path) {
- var rs = fs.createReadStream(path);
- rs.on("error", err => {
- app.notice(err);
- res.end(template(app.res404, { pathname: pathname }));
- });
- rs.on("data", d => res.write(d));
- rs.on("end", () => res.end());
- }
-
- res.responded = true;
-
- // Prevent leaking information
- if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
- res.writeHead(403);
- res.end(template(app.res403, { pathname: pn }));
- return;
- }
-
- // Join the web root with the request's path name
- var path = pathlib.join(root, pn.replace(before, ""));
-
- fs.stat(path, (err, stat) => {
-
- // If there's an error stat'ing, just error
- if (err) {
- app.notice(err);
- res.writeHead(404);
- res.end(template(app.res404, { pathname: pn }));
- return;
- }
-
- // If it's a directory, we want the index.html file
- if (stat.isDirectory())
- path = pathlib.join(path, "index.html");
-
- // Send the file
- send(path, pn, res, app);
- });
- }
- }
|