|
|
@@ -1,7 +1,54 @@ |
|
|
|
var fs = require("fs"); |
|
|
|
var pathlib = require("path"); |
|
|
|
|
|
|
|
var mimes = { |
|
|
|
txt: "text/plain", |
|
|
|
css: "text/css", |
|
|
|
html: "text/html", |
|
|
|
|
|
|
|
js: "application/javascript", |
|
|
|
json: "application/json", |
|
|
|
xml: "application/xml", |
|
|
|
zip: "application/zip", |
|
|
|
pdf: "application/pdf", |
|
|
|
|
|
|
|
png: "image/png", |
|
|
|
jpeg: "image/jpeg", |
|
|
|
jpg: "image/jpeg", |
|
|
|
gif: "image/gif", |
|
|
|
svg: "image/svg", |
|
|
|
} |
|
|
|
|
|
|
|
function mimetype(path) { |
|
|
|
var unknown = "application/octet-stream"; |
|
|
|
|
|
|
|
var ext = pathlib.extname(path); |
|
|
|
if (ext) |
|
|
|
return mimes[ext.substr(1)] || unknown; |
|
|
|
else |
|
|
|
return unknown; |
|
|
|
} |
|
|
|
|
|
|
|
function sendfile(path, app, pathname, res) { |
|
|
|
fs.open(path, "r", (err, fd) => { |
|
|
|
if (err) { |
|
|
|
app.notice(err); |
|
|
|
res.writeHead(404); |
|
|
|
res.end(app.template(app.res404, { pathname: pathname })); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
res.writeHead(200, { |
|
|
|
"Content-Type": mimetype(path) |
|
|
|
}); |
|
|
|
|
|
|
|
var rs = fs.createReadStream(null, { fd: fd }); |
|
|
|
rs.on("error", err => { |
|
|
|
app.warning(err); |
|
|
|
}); |
|
|
|
rs.on("data", d => res.write(d)); |
|
|
|
rs.on("end", () => res.end()); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = function(root, before) { |
|
|
@@ -11,21 +58,13 @@ module.exports = function(root, before) { |
|
|
|
|
|
|
|
// 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()); |
|
|
|
sendfile(path, app, pn, res); |
|
|
|
} |
|
|
|
|
|
|
|
res.responded = true; |
|
|
|
|
|
|
|
// Prevent leaking information |
|
|
|
if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") { |
|
|
|
res.writeHead(403); |
|
|
|
res.end(template(app.res403, { pathname: pn })); |
|
|
|
res.end(app.template(app.res403, { pathname: pn })); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@@ -38,7 +77,7 @@ module.exports = function(root, before) { |
|
|
|
if (err) { |
|
|
|
app.notice(err); |
|
|
|
res.writeHead(404); |
|
|
|
res.end(template(app.res404, { pathname: pn })); |
|
|
|
res.end(app.template(app.res404, { pathname: pn })); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@@ -47,7 +86,7 @@ module.exports = function(root, before) { |
|
|
|
path = pathlib.join(path, "index.html"); |
|
|
|
|
|
|
|
// Send the file |
|
|
|
send(path, pn, res, app); |
|
|
|
send(path); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |