-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
108 lines (93 loc) · 3.14 KB
/
server.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
// Generated by CoffeeScript 1.8.0
var Insight2png, TESTER_REGEX, TU_REGEX, close, domain, fourOhFour, fs, handleImageResponse, hashCode, logTime, port, server, system, writeImageToClient;
system = require("system");
server = require("webserver").create();
Insight2png = require('./insight2png');
fs = require('fs');
TU_REGEX = /^tu=.+&u=.+&n=.+&d=.+&s=.+&share=1$/;
TESTER_REGEX = /^headline=.+&body=.+&preview=1$/;
if (system.args.length < 2 || system.args.length > 3) {
domain = "0.0.0.0";
port = 8080;
} else {
domain = system.args[1];
port = system.args[2];
}
console.log("Server is running on " + domain + ":" + port + "\n");
server.listen("" + domain + ":" + port, function(request, response) {
var filename, insight2png, params, tuUser, url;
if (request.url === '/favicon.ico') {
return;
}
response.start = new Date();
response.log = '';
if (request.url.match(/^\/insight/)) {
params = request.url.split("?")[1];
if (!((params != null) && ((params.match(TU_REGEX) != null) || (params.match(TESTER_REGEX) != null)))) {
return fourOhFour(response, "Not a valid request");
}
if (params.match(TU_REGEX)) {
tuUser = params.substr(3).split('&')[0];
if (tuUser === 'shares' || tuUser === '') {
return fourOhFour(response, "\"" + tuUser + "\" is not a valid user");
}
url = "https://" + tuUser + ".thinkup.com/?" + (params.split("tu=" + tuUser + "&")[1]);
} else if (params.match(TESTER_REGEX)) {
url = "https://thinkup.thinkup.com/insight_tester.php?" + params;
}
response.log += url + "\n";
filename = "" + (hashCode(url)) + ".png";
insight2png = new Insight2png(url, filename, response, handleImageResponse);
if (fs.exists("screenshots/" + filename)) {
response.log += "Screenshot exists; returning image " + filename;
return insight2png.readFile();
} else {
response.log += "First request; generating " + filename + "\n";
return insight2png.run();
}
} else {
return fourOhFour(response);
}
});
handleImageResponse = {
success: function(imgData, response) {
return writeImageToClient(response, imgData);
},
error: function(error, response) {
var msg;
msg = error;
return fourOhFour(response, msg);
}
};
writeImageToClient = function(response, imgData) {
response.writeHead(200, {
'Content-Type': 'image/png'
});
response.setEncoding('binary');
response.write(atob(imgData));
return close(response);
};
fourOhFour = function(response, msg) {
if (msg == null) {
msg = "No url requested";
}
response.log += "404:\n";
response.log += "" + msg;
response.statusCode = 404;
response.write("<!DOCTYPE html>\n<html><head><meta charset=\"utf-8\"><title>404</title></head><body>404: " + msg + "</body></html>");
return close(response);
};
close = function(response) {
console.log(response.log);
logTime(response.start);
return response.close();
};
logTime = function(start) {
return console.log("" + ((new Date() - start) / 1000) + " seconds\n");
};
hashCode = function(s) {
return s.split("").reduce(function(a, b) {
a = ((a << 5) - a) + b.charCodeAt(0);
return a & a;
}, 0);
};