/*jshint node:true, onevar:false, indent:4 */

var url = require('url')
var path = require('path')
var request = require('request')
var serveStatic = require('serve-static')

function fetchData(req, res, next) {
  var dataUrl = makeAbsolute(req, req.query.url)

  if (!dataUrl) {
    res.statusCode = 400
    res.end('Bad Request: no url provided')
    return
  }

  request(dataUrl, function (err, resp, body) {
    if (err) {
      res.statusCode = 400
      res.end('Bad Request: ' + err)
      return
    }

    if (resp.statusCode != 200) {
      res.statusCode = 400
      res.end('Bad Request: response status code was not 200')
      return
    }

    res.setHeader('Content-Type', 'text/plain')
    res.statusCode = 200
    res.end(body)
  })
}

function makeAbsolute(req, uri) {
  if (!uri) {
    return null
  }

  var parsed = url.parse(uri)
  if (parsed.host) {
    return uri // url is already absolute
  }

  return url.format({
    protocol: req.protocol,
    host: req.headers.host,
    pathname: parsed.pathname,
    search: parsed.search,
    hash: parsed.hash
  })
}

module.exports = function (app, root) {
  if (!root) {
    root = '/charted/'
  }

  app.use(root + 'get', fetchData)
  app.use(root, serveStatic(path.join(__dirname, '..', 'pub')))
}

@@ -225,39 +225,22 @@
 l = 
-makeAbsolute(req, req.query.url
+getDataUrl(req
 )%0A%0A 
@@ -744,32 +744,125 @@
 ion 
-makeAbsolute(req, uri) %7B
+getDataUrl(req) %7B%0A  var uri = url.parse(req.url, true)%0A  if (!uri.query) %7B%0A    return null%0A  %7D%0A%0A  uri = uri.query.url
 %0A  i

Use req.url instead of req.query because sometimes Charted doesn't get that