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

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