aboutsummaryrefslogtreecommitdiff
path: root/lib/compile-handler.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/compile-handler.js')
-rw-r--r--lib/compile-handler.js62
1 files changed, 53 insertions, 9 deletions
diff --git a/lib/compile-handler.js b/lib/compile-handler.js
index 2f874e9c8..0f6790748 100644
--- a/lib/compile-handler.js
+++ b/lib/compile-handler.js
@@ -85,9 +85,37 @@ function CompileHandler(gccProps, compilerProps) {
var proxy = httpProxy.createProxyServer({});
this.handler = _.bind(function compile(req, res, next) {
- var compiler = this.compilersById[req.body.compiler];
- if (!compiler) return next();
-
+ var source, options, filters, compiler;
+ if (req.is('json')) {
+ // JSON-style request
+ compiler = this.compilersById[req.compiler || req.body.compiler];
+ if (!compiler) return next();
+ source = req.body.source;
+ options = req.body.options;
+ filters = req.body.filters || compiler.getDefaultFilters();
+ } else {
+ // API-style
+ compiler = this.compilersById[req.compiler];
+ if (!compiler) return next();
+ source = req.body;
+ options = req.query.options;
+ // By default we get the default filters.
+ filters = compiler.getDefaultFilters();
+ // If specified exactly, we'll take that with ?filters=a,b,c
+ if (req.query.filters) {
+ filters = _.object(_.map(req.query.filters.split(","), function (filter) {
+ return [filter, true];
+ }));
+ }
+ // Add a filter. ?addFilters=binary
+ _.each((req.query.addFilters || "").split(","), function (filter) {
+ filters[filter] = true;
+ });
+ // Remove a filter. ?removeFilter=intel
+ _.each((req.query.removeFilters || "").split(","), function (filter) {
+ delete filters[filter];
+ });
+ }
var remote = compiler.getRemote();
if (remote) {
proxy.web(req, res, {target: remote}, function (e) {
@@ -96,23 +124,39 @@ function CompileHandler(gccProps, compilerProps) {
});
return;
}
- var source = req.body.source;
- var options = req.body.options || '';
+
if (source === undefined) {
return next(new Error("Bad request"));
}
- options = _.chain(quote.parse(options)
+ options = _.chain(quote.parse(options || '')
.map(function (x) {
if (typeof(x) == "string") return x;
return x.pattern;
}))
.filter(_.identity)
.value();
- var filters = req.body.filters;
+ function textify(array) {
+ return _.pluck(array || [], 'text').join("\n");
+ }
+
compiler.compile(source, options, filters).then(
function (result) {
- res.set('Content-Type', 'application/json');
- res.end(JSON.stringify(result));
+ if (req.accepts(['text', 'json']) === 'json') {
+ res.set('Content-Type', 'application/json');
+ res.end(JSON.stringify(result));
+ } else {
+ res.set('Content-Type', 'text/plain');
+ try {
+ res.write("# Compilation provided by Compiler Explorer at " + req.get('Host') + "\n");
+ res.write(textify(result.asm));
+ if (result.code !== 0) res.write("\n# Compiler exited with result code " + result.code);
+ if (!_.isEmpty(result.stdout)) res.write("\nStandard out:\n" + textify(result.stdout));
+ if (!_.isEmpty(result.stderr)) res.write("\nStandard error:\n" + textify(result.stderr));
+ } catch (ex) {
+ re.write("Error handling request: " + ex);
+ }
+ res.end('\n');
+ }
},
function (error) {
logger.error("Error: " + error);