mortie 8 лет назад
Родитель
Сommit
6413b9c515
6 измененных файлов: 56 добавлений и 12 удалений
  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
client/utils.js Просмотреть файл


+ 5
- 0
index.js Просмотреть файл

this.clientScript += start + str + end; this.clientScript += start + str + end;
} }


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

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

+ 0
- 0
js/middleware.js Просмотреть файл


+ 51
- 12
js/static.js Просмотреть файл

var fs = require("fs"); var fs = require("fs");
var pathlib = require("path"); 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) { 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) { module.exports = function(root, before) {


// Send a file // Send a file
function send(path) { 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 // Prevent leaking information
if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") { if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
res.writeHead(403); res.writeHead(403);
res.end(template(app.res403, { pathname: pn }));
res.end(app.template(app.res403, { pathname: pn }));
return; return;
} }


if (err) { if (err) {
app.notice(err); app.notice(err);
res.writeHead(404); res.writeHead(404);
res.end(template(app.res404, { pathname: pn }));
res.end(app.template(app.res404, { pathname: pn }));
return; return;
} }


path = pathlib.join(path, "index.html"); path = pathlib.join(path, "index.html");


// Send the file // Send the file
send(path, pn, res, app);
send(path);
}); });
} }
} }

+ 0
- 0
package.json Просмотреть файл


Загрузка…
Отмена
Сохранить