-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
152 lines (138 loc) · 3.81 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
Evaluator = {
get: function(url, options, callback) {
rest = require('./lib/restler/restler.js');
rest.get(url, options).addListener('complete', callback).addListener('error', function(err) {console.log(err)});
}
};
var
sys = require('sys'),
path = require('path'),
http = require('http'),
url = require('url'),
paperboy = require('./lib/node-paperboy/lib/paperboy.js'),
PORT = 8003,
WEBROOT = path.join(path.dirname(__filename), 'webroot')
_ = require('./lib/underscore.js');
http.createServer(function(request, response) {
var ip = request.connection.remoteAddress;
paperboy
.deliver(WEBROOT, request, response)
.addHeader('Expires', 300)
.addHeader('X-PaperRoute', 'Node')
.before(function() {
sys.log('Received Request')
})
.after(function(statCode) {
log(statCode, request.url, ip);
})
.error(function(statCode,msg) {
response.writeHead(statCode, {'Content-Type': 'text/plain'});
response.write("Error: " + statCode);
response.end();
log(statCode, request.url, ip, msg);
})
.otherwise(function(err){
var code,
sourceUrl,
token,
username,
password,
contentType = 'text/plain',
inSandbox = false,
urlObj = url.parse(request.url, true);
// Required query string parameters
var hasErrors = false;
var errorString = '';
try {
sourceUrl = urlObj.query["url"];
if(sourceUrl == undefined) throw "Source URL not set";
} catch (err) {
errorString += 'Source URL is required\n';
hasErrors = true;
}
try {
code = urlObj.query["code"];
if(code == undefined) throw "Code not set";
} catch (err) {
errorString += 'Code block is required\n';
hasErrors = true;
}
try {
token = urlObj.query["token"];
if(token == undefined) throw "Token not set";
} catch (err) {
errorString += 'Token is required\n';
hasErrors = true;
}
// Optional query string parameters
try {
username = urlObj.query["username"];
} catch (err) {}
try {
password = urlObj.query["password"];
} catch (err) {}
try {
urlCode = urlObj.query["urlCode"];
} catch (err) {}
try {
contentType = urlObj.query["contentType"];
} catch (err) {}
try {
inSandbox = (urlObj.query["sandbox"] == "true");
} catch (err) {}
if(hasErrors) {
response.writeHead(401, {'Content-Type': 'text/plain'});
response.end(errorString);
log(400, request.url, ip);
} else {
// Transform the source URL
try {
var Script = process.binding('evals').Script;
var urlSandbox = {
URL: sourceUrl
}
if(urlCode != undefined) {
Script.runInNewContext(urlCode, urlSandbox);
}
sourceUrl = urlSandbox.URL;
console.log("Source URL " + sourceUrl);
} catch (err) {
console.log("Problem running code on sourceUrl\n" + err);
}
Evaluator.get(sourceUrl, {username: username, password: password}, function(data) {
try {
var codeSandbox = {
DATA: data,
RESULT: '',
_:_
};
if(code != undefined) {
Script.runInNewContext(code, codeSandbox);
}
response.writeHead(200, {'Content-Type': contentType});
if(inSandbox) {
var resp = {
result: codeSandbox.RESULT,
success: true,
url: request.url.replace('/','').replace(/&sandbox=true/,'')
}
//console.log(sys.inspect(resp));
response.end(JSON.stringify(resp));
} else {
response.end(codeSandbox.RESULT + '\n');
}
} catch (err) {
response.writeHead(500, {'Content-Type': 'text/plain'});
console.log("Problem running code\n" + data, " Error " + err.stack);
response.end('Error\n');
}
});
}
});
}).listen(PORT);
function log(statCode, url, ip,err) {
var logStr = statCode + ' - ' + url + ' - ' + ip
if (err)
logStr += ' - ' + err;
sys.log(logStr);
}