Web framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

static.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var mimes = {
  4. txt: "text/plain",
  5. css: "text/css",
  6. html: "text/html",
  7. js: "application/javascript",
  8. json: "application/json",
  9. xml: "application/xml",
  10. zip: "application/zip",
  11. pdf: "application/pdf",
  12. gif: "image/gif",
  13. png: "image/png",
  14. jpeg: "image/jpeg",
  15. jpg: "image/jpeg",
  16. svg: "image/svg+xml",
  17. bmp: "image/bmp",
  18. webp: "image/webp",
  19. midi: "audio/midi",
  20. mp3: "audio/mpeg",
  21. ogg: "audio/ogg",
  22. webm: "video/webm",
  23. mkv: "video/mkv",
  24. ogv: "video/ogg",
  25. }
  26. function mimetype(path) {
  27. var unknown = "application/octet-stream";
  28. var ext = pathlib.extname(path);
  29. if (ext)
  30. return mimes[ext.substr(1)] || unknown;
  31. else
  32. return unknown;
  33. }
  34. function sendfile(path, app, pathname, req, res) {
  35. fs.open(path, "r", (err, fd) => {
  36. if (err) {
  37. app.notice(err);
  38. res.writeHead(404);
  39. res.end(app.template(app.res404,
  40. { method: req.method, pathname: pathname }));
  41. return;
  42. }
  43. res.writeHead(200, {
  44. "Content-Type": mimetype(path)
  45. });
  46. var rs = fs.createReadStream(null, { fd: fd });
  47. rs.on("error", err => {
  48. app.warning(err);
  49. });
  50. rs.on("data", d => res.write(d));
  51. rs.on("end", () => res.end());
  52. });
  53. }
  54. module.exports = function(root, before) {
  55. return function(req, res, app) {
  56. var pn = req.urlobj.pathname;
  57. // Send a file
  58. function send(path) {
  59. sendfile(path, app, pn, req, res);
  60. }
  61. // Prevent leaking information
  62. if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
  63. res.writeHead(403);
  64. res.end(app.template(app.res403,
  65. { method: req.method, pathname: pn }));
  66. return;
  67. }
  68. // Join the web root with the request's path name
  69. var path = pathlib.join(root, pn.replace(before, ""));
  70. fs.stat(path, (err, stat) => {
  71. // If there's an error stat'ing, just error
  72. if (err) {
  73. app.notice(err);
  74. res.writeHead(404);
  75. res.end(app.template(app.res404,
  76. { method: req.method, pathname: pn }));
  77. return;
  78. }
  79. // If it's a directory, we want the index.html file,
  80. // or redirect to <pathname>/
  81. if (stat.isDirectory()) {
  82. if (pn[pn.length - 1] !== "/") {
  83. res.writeHead(302, { location: pn + "/" });
  84. res.end();
  85. return;
  86. }
  87. path = pathlib.join(path, "index.html");
  88. }
  89. // Send the file
  90. send(path);
  91. });
  92. }
  93. }