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

@@ -2,8 +2,8 @@ var http = require("http");
var urllib = require("url");
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) {
for (var i in args) {
@@ -88,7 +88,7 @@ class App {
if (route === null) {
res.writeHead(404);
res.end(template(this.res404,
{ pathname: req.urlobj.pathname }));
{ method: req.method, pathname: req.urlobj.pathname }));
return;
}


+ 8
- 5
js/static.js View File

@@ -39,12 +39,13 @@ function mimetype(path) {
return unknown;
}

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

@@ -67,13 +68,14 @@ module.exports = function(root, before) {

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

// Prevent leaking information
if (pn.indexOf("../") !== -1 || pn.indexOf("/..") !== -1 || pn === "..") {
res.writeHead(403);
res.end(app.template(app.res403, { pathname: pn }));
res.end(app.template(app.res403,
{ method: req.method, pathname: pn }));
return;
}

@@ -86,7 +88,8 @@ module.exports = function(root, before) {
if (err) {
app.notice(err);
res.writeHead(404);
res.end(app.template(app.res404, { pathname: pn }));
res.end(app.template(app.res404,
{ method: req.method, pathname: pn }));
return;
}


+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
{
"name": "webframe",
"version": "0.3.0",
"version": "0.3.1",
"description": "Web server.",
"main": "index.js",
"scripts": {

Loading…
Cancel
Save