Browse Source

files

master
mortie 7 years ago
parent
commit
6413b9c515
6 changed files with 56 additions and 12 deletions
  1. 0
    0
      README.md
  2. 0
    0
      client/utils.js
  3. 5
    0
      index.js
  4. 0
    0
      js/middleware.js
  5. 51
    12
      js/static.js
  6. 0
    0
      package.json

+ 0
- 0
README.md View File


+ 0
- 0
client/utils.js View File


+ 5
- 0
index.js View File

@@ -158,6 +158,11 @@ class App {
this.clientScript += start + str + end;
}

/*
* Template string
*/
template(tmpl, args) { return template(tmpl, args); }

get(path, before, func) { this.route("GET", path, before, func); }
post(path, before, func) { this.route("POST", path, before, func); }
put(path, before, func) { this.route("PUT", path, before, func); }

+ 0
- 0
js/middleware.js View File


+ 51
- 12
js/static.js View File

@@ -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);
});
}
}

+ 0
- 0
package.json View File


Loading…
Cancel
Save