-
Notifications
You must be signed in to change notification settings - Fork 0
/
Callback.js
111 lines (93 loc) · 2.85 KB
/
Callback.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* This script is called on every request.
* It will return a static ressource, a list with all tests or the test specific response
*/
const fs = require("fs");
const path = require("path");
const formidable = require('formidable'); // for receiving JSON for the DB
const config = require(__dirname + "/config.json");
const {requestHandler} = require(__dirname + "/requestHandler.js");
const {UserAgent} = require(__dirname + "/UserAgent.js");
const {SaveToDB} = require(__dirname + "/SaveToDB.js");
const {Results} = require(__dirname + "/Results.js");
const {TestParser} = require(__dirname + "/TestParser.js");
class Callback {
constructor() {
};
start (req, res) {
this.req = req;
this.res = res;
this.action();
};
action() {
// return all tests
if(this.req.url.indexOf("getTest.json") !== -1) {
this.getTests();
}
// not implemented yet
else if(this.req.url.indexOf("result.json") !== -1) {
new Results(this.req);
}
// Callback for a test
else if(this.req.url.indexOf("test=") !== -1) {
requestHandler(this.req, this.res);
}
// Save results
else if(this.req.url.indexOf("save.json") !== -1) {
if(!this.db) {
this.db = new SaveToDB();
}
var form = new formidable.IncomingForm();
form.parse(this.req, function(err, fields, files) {
try {
var data = Object.assign(JSON.parse(fields['data']), UserAgent.parseUserAgent(this.req.headers['user-agent']));
data.https = (this.req.headers.referer.substr(0, 8) == "https://") ? true : false;
try {
this.db.save(data);
this.res.writeHead(200, {'content-type': 'text/plain'});
} catch(e) {
console.dir(e);
this.res.writeHead(500, {'content-type': 'text/plain'});
}
this.res.end();
} catch(e) {
console.error(e);
}
}.bind(this));
} else {
this.showFile(this.req.url);
}
};
// Response static files
showFile(url) {
if(url === "/") {
url = "/index.html";
}
var file = __dirname + '/static' + url;
try {
fs.accessSync(file, fs.F_OK);
this.res.writeHead(200, {'Content-Type' : config.mimeTypes[path.extname(file).split(".")[1]]});
fs.createReadStream(file).pipe(this.res);
} catch(e) {
console.dir(e);
console.log('File not exists: ' + file);
this.res.writeHead(404, {'Content-Type' : 'text/plain'});
this.res.write('404 Not Found\n');
this.res.end();
return;
}
};
// Read all testcases
getTests() {
this.res.writeHead(200, {
'Content-Type' : 'application/json',
'Cache-Control' : 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Expires' : new Date(Date.now()).toUTCString()
});
let data = TestParser.getTests();
data['user-agent'] = this.req.headers['user-agent'];
data.UA = UserAgent.parseUserAgent(this.req.headers['user-agent']);
this.res.end(JSON.stringify(data));
};
};
exports.Callback = Callback;