square/cube — Issue #34: GET requests broken with complex expressions
Author: marklubi
Created: 2012-02-05 20:59:51+00:00
State: open

Complex expressions being passed to the /1.0/metric/get endpoint are not being decoded properly. A simple example would be any expression containing a + symbol.

> sum(connections(clients))+sum(connections(servers))

Within a querystring, the + is treated as a space so it results in the incorrect expression:

> sum(connections(clients)) sum(connections(servers))

The proper URI encoding of the expression results in an error message:

> sum(connections(clients))%2Bsum(connections(servers))

> invalid expression: SyntaxError: Expected "*", "+", "-" or "/" but "%" found.

Quick and dirty fix for evaluator.js:

```javascript

function metricGet(request, response) {
var values = [],
query = url.parse(request.url, true).query;

//decode expression
query.expression = decodeURIComponent(query.expression);

var expected = metric(query, callback);

response.writeHead(expected < 0 ? 400 : 200, headers);
if (expected <= 0) response.end(JSON.stringify([]));

function callback(value) {
if (values.push(value) === expected) {
response.end(JSON.stringify(values.sort(chronological)));
}
}
}

```

It seems to be an issue somewhere within Cube as Node (v0.6.10) is decoding querystring parameters correctly:

```javascript
var url = require("url");
console.log(url.parse('http://localhost:1081/1.0/metric/get?expression=sum(connections(clients))%2Bsum(connections(servers))', true).query);
```

> { expression: 'sum(connections(clients))+sum(connections(servers))' }