Skip to content

Commit 5240adc

Browse files
committed
use jsonp
1 parent 849736d commit 5240adc

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

‎v4-cokapi/cokapi.js‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,45 @@ var PYTHON3_BIN = '/usr/local/bin/python3.3';
5353
var TIMEOUT_SECS = 3;
5454

5555

56-
function postExecHandler(res, err, stdout, stderr) {
56+
// bind() res and useJSONP before using
57+
function postExecHandler(res, useJSONP, err, stdout, stderr) {
5758
if (err) {
5859
console.log('postExecHandler', util.inspect(err, {depth: null}));
5960
var errTrace;
6061
if (err.killed) {
6162
// timeout!
6263
errTrace = {code: '', trace: [{'event': 'uncaught_exception',
6364
'exception_msg': 'Timeout error. Your code ran for more than ' + TIMEOUT_SECS + ' seconds. Please shorten and try again.'}]};
64-
res.send(JSON.stringify(errTrace));
65+
if (useJSONP) {
66+
res.jsonp(errTrace /* return an actual object, not a string */);
67+
} else {
68+
res.send(JSON.stringify(errTrace));
69+
}
6570
} else {
6671
errTrace = {code: '', trace: [{'event': 'uncaught_exception',
6772
'exception_msg': "Unknown error. Report a bug to philip@pgbovine.net by clicking on the\n'Generate URL' button at the bottom and including a URL in your email."}]};
68-
res.send(JSON.stringify(errTrace));
73+
if (useJSONP) {
74+
res.jsonp(errTrace /* return an actual object, not a string */);
75+
} else {
76+
res.send(JSON.stringify(errTrace));
77+
}
6978
}
7079
} else {
71-
res.send(stdout);
80+
if (useJSONP) {
81+
// stdout better be real JSON!!!
82+
res.jsonp(JSON.parse(stdout) /* return an actual object, not a string */);
83+
} else {
84+
res.send(stdout);
85+
}
7286
}
7387
}
7488

7589

7690
var app = express();
7791

92+
// http://ilee.co.uk/jsonp-in-express-nodejs/
93+
app.set("jsonp callback", true);
94+
7895
app.use(serveStatic('frontends/')); // put all static files in here
7996

8097
app.get('/exec_py2', function(req, res) {
@@ -85,7 +102,10 @@ app.get('/exec_py3', function(req, res) {
85102
executePython('py3', req, res);
86103
});
87104

88-
app.get('/exec_js', function(req, res) {
105+
app.get('/exec_js', exec_js_handler.bind(null, false));
106+
app.get('/exec_js_jsonp', exec_js_handler.bind(null, true));
107+
108+
function exec_js_handler(useJSONP /* use bind first */, req, res) {
89109
var usrCod = req.query.user_script;
90110

91111
var exeFile;
@@ -114,8 +134,8 @@ app.get('/exec_js', function(req, res) {
114134
// up after itself to --rm the container, but
115135
// double-check with 'docker ps -a'
116136
killSignal: 'SIGINT'},
117-
postExecHandler.bind(null, res));
118-
});
137+
postExecHandler.bind(null, res, useJSONP));
138+
}
119139

120140
function executePython(pyVer, req, res) {
121141
var parsedOptions = JSON.parse(req.query.options_json);
@@ -158,7 +178,7 @@ function executePython(pyVer, req, res) {
158178
// up after itself to --rm the container, but
159179
// double-check with 'docker ps -a'
160180
killSignal: 'SIGINT'},
161-
postExecHandler.bind(null, res));
181+
postExecHandler.bind(null, res, false));
162182
}
163183

164184

0 commit comments

Comments
 (0)