-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
74 lines (68 loc) · 2.3 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const handlerFactory = require('./handler');
const fs = require('fs');
const parser = require('url');
let handlers = [];
exports.clear = function () {
handlers = [];
};
exports.register = function (url, method) {
handlers.push({method: handlerFactory.createHandler(method), url: url});
};
exports.route = function (req) {
const url = parser.parse(req.url, true);
const handler = {method: null, urlOptions: []};
for (let i = 0, j = handlers.length; i < j; i++) {
// console.log(url.pathname);
// console.log(handlers[i].url);
const regexMatch = url.pathname.match(new RegExp('^' + handlers[i].url + '$'));
// console.log(regexMatch);
if (regexMatch === null) continue;
handler.method = handlers[i].method;
handler.urlOptions = regexMatch;
break;
}
if (handler.method === null) {
handler.method = this.missing(req);
}
return handler;
};
exports.missing = function (req) {
// Try to read the file locally, this is a security hole, yo /../../etc/passwd
const url = parser.parse(req.url, true);
const path = __dirname + "/public" + decodeURIComponent(url.pathname.replace(/\.\.\//g, ''));
try {
data = fs.readFileSync(path);
let mimeAddition;
const fileType = path.split('.').pop();
switch(fileType) {
case 'css':
mimeAddition = 'text/css';
break;
case 'jpg':
mimeAddition = 'image';
break;
case 'pdf':
mimeAddition = 'application/pdf';
break;
case 'doc':
case 'docx':
mimeAddition = 'application/msword';
break;
default:
mimeAddition = 'text/html';
break;
}
mime = req.headers.accepts || mimeAddition;
return handlerFactory.createHandler(function (req, res) {
res.writeHead(200, {'Content-Type': mime});
res.write(data);
res.end();
});
} catch (e) {
return handlerFactory.createHandler(function (req, res) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write("No route registered for " + url.pathname);
res.end();
});
}
};