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.8KB

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