'use strict';

var validator = require('validator');
var request = require('request');

module.exports.proxy = function(req, res) {
  var url = req.query.url;

  if (!validator.isURL(url)) {
    res.status(400).send('URL you passed is invalid');
    return false;
  }

  try {
    req.on('error', function(error) {
      console.trace(error);
      res.sendStatus(404);
    });
    var proxy = request(url);
    proxy.on('error', function(error) {
      console.trace(error);
      res.sendStatus(404);
    });
    req.pipe(proxy).pipe(res);
  } catch (e) {
    res.sendStatus(404);
  }
};

module.exports.download = function(req, res) {
  res.status(200);
  if (req.body.name) {
    res.attachment(req.body.name);
  }
  if (req.body.mime) {
    res.set('Content-Type', req.body.mime);
  }
  res.send(req.body.data || '');
};

'use strict';

var validator = require('validator');
var request = require('request');

module.exports.proxy = function(req, res) {
  var url = req.query.url;

  if (!validator.isURL(url)) {
    res.status(400).send('URL you passed is invalid');
    return false;
  }

  url = url.replace('https://datastore.openspending.org/',
                    'https://s3.amazonaws.com/datastore.openspending.org/');

  try {
    req.on('error', function(error) {
      console.trace(error);
      res.sendStatus(404);
    });
    var proxy = request(url);
    proxy.on('error', function(error) {
      console.trace(error);
      res.sendStatus(404);
    });
    req.pipe(proxy).pipe(res);
  } catch (e) {
    res.sendStatus(404);
  }
};

module.exports.download = function(req, res) {
  res.status(200);
  if (req.body.name) {
    res.attachment(req.body.name);
  }
  if (req.body.mime) {
    res.set('Content-Type', req.body.mime);
  }
  res.send(req.body.data || '');
};

Rewrite url to be proxied.
Rewrite url to be proxied.

We can't proxy a CloudFlare url without a CloudFlare incompatiblity
error. This commit rewrites relevant urls to direct to the original aws
s3 url.

Related to openspending/openspending#1356 and
openspending/datapackage-pipelines-fiscal#12.
