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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var transformCache = {};
  55. function handleTransform(path, req, res, app) {
  56. var ext = pathlib.extname(req.url).substring(1);
  57. if (ext === "")
  58. return false;
  59. var transform = app._transforms[ext];
  60. if (!transform)
  61. return false;
  62. if (transformCache[path]) {
  63. var c = transformCache[path];
  64. res.writeHead(c.status, c.headers);
  65. res.end(c.content);
  66. return true;
  67. }
  68. var data = { content: "", headers: null, status: null };
  69. var writeStream = {
  70. headersSent: false,
  71. status: 200,
  72. headers: {
  73. "content-type": transform.mime
  74. },
  75. write: function(d) {
  76. if (!writeStream.headersSent) {
  77. res.writeHead(writeStream.status, writeStream.headers);
  78. writeStream.headersSent = true;
  79. }
  80. if (d != null) {
  81. data.content += d;
  82. res.write(d);
  83. }
  84. },
  85. end: function(d, nocache) {
  86. writeStream.write(d);
  87. res.end();
  88. if (!nocache) {
  89. data.status = writeStream.status;
  90. data.headers = writeStream.headers;
  91. transformCache[path] = data;
  92. }
  93. },
  94. error: function(err) {
  95. if (writeStream.status === 200)
  96. writeStream.status = 500;
  97. app.warning("Transform for "+path+": "+err.toString());
  98. writeStream.end(null, true);
  99. }
  100. }
  101. transform.func(path, writeStream);
  102. return true;
  103. }
  104. module.exports = function(root, before) {
  105. return function(req, res, app) {
  106. // `req.urlobj.pathname` is too long.
  107. var pn = req.urlobj.pathname;
  108. // Join the web root with the request's path name
  109. var path = pathlib.join(root, pn.replace(before, ""));
  110. // If there's a transform, let that handle it
  111. if (handleTransform(path, req, res, app))
  112. return;
  113. // Send a file
  114. function send(path) {
  115. sendfile(path, app, pn, req, res);
  116. }
  117. // Prevent leaking information
  118. if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
  119. res.writeHead(403);
  120. res.end(app.template(app.res403,
  121. { method: req.method, pathname: pn }));
  122. return;
  123. }
  124. fs.stat(path, (err, stat) => {
  125. // If there's an error stat'ing, just error
  126. if (err) {
  127. app.notice(err);
  128. res.writeHead(404);
  129. res.end(app.template(app.res404,
  130. { method: req.method, pathname: pn }));
  131. return;
  132. }
  133. // If it's a directory, we want the index.html file,
  134. // or redirect to <pathname>/
  135. if (stat.isDirectory()) {
  136. if (pn[pn.length - 1] !== "/") {
  137. res.writeHead(302, { location: pn + "/" });
  138. res.end();
  139. return;
  140. }
  141. path = pathlib.join(path, "index.html");
  142. }
  143. // Send the file
  144. send(path);
  145. });
  146. }
  147. }