Browse Source

added method to 404 and 403 templates

master
mortie 7 years ago
parent
commit
95baa0fcae
3 changed files with 12 additions and 9 deletions
  1. 3
    3
      index.js
  2. 8
    5
      js/static.js
  3. 1
    1
      package.json

+ 3
- 3
index.js View File

var urllib = require("url"); var urllib = require("url");
var fs = require("fs"); var fs = require("fs");


var res404 = "404 not found: {{pathname}}";
var res403 = "403 forbidden: {{pathname}}";
var res404 = "404 not found: {{method}} {{pathname}}";
var res403 = "403 forbidden: {{method}} {{pathname}}";


function template(tpml, args) { function template(tpml, args) {
for (var i in args) { for (var i in args) {
if (route === null) { if (route === null) {
res.writeHead(404); res.writeHead(404);
res.end(template(this.res404, res.end(template(this.res404,
{ pathname: req.urlobj.pathname }));
{ method: req.method, pathname: req.urlobj.pathname }));
return; return;
} }



+ 8
- 5
js/static.js View File

return unknown; return unknown;
} }


function sendfile(path, app, pathname, res) {
function sendfile(path, app, pathname, req, res) {
fs.open(path, "r", (err, fd) => { fs.open(path, "r", (err, fd) => {
if (err) { if (err) {
app.notice(err); app.notice(err);
res.writeHead(404); res.writeHead(404);
res.end(app.template(app.res404, { pathname: pathname }));
res.end(app.template(app.res404,
{ method: req.method, pathname: pathname }));
return; return;
} }




// Send a file // Send a file
function send(path) { function send(path) {
sendfile(path, app, pn, res);
sendfile(path, app, pn, req, res);
} }


// 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(app.template(app.res403, { pathname: pn }));
res.end(app.template(app.res403,
{ method: req.method, pathname: pn }));
return; return;
} }


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



+ 1
- 1
package.json View File

{ {
"name": "webframe", "name": "webframe",
"version": "0.3.0",
"version": "0.3.1",
"description": "Web server.", "description": "Web server.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

Loading…
Cancel
Save