// Generated by CoffeeScript 1.6.3
var http, httpProxy, url;

url = require('url');

http = require('http');

httpProxy = require('http-proxy');

module.exports = function(req, res, proxy) {
  var cors_headers, header, headers, host, hostname, ignore, key, path, port, target, target0, value, _i, _len, _ref, _ref1, _ref2;
  if (req.headers['access-control-request-headers']) {
    headers = req.headers['access-control-request-headers'];
  } else {
    headers = 'accept, accept-charset, accept-encoding, accept-language, authorization, content-length, content-type, host, origin, proxy-connection, referer, user-agent, x-requested-with';
    _ref = req.headers;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      header = _ref[_i];
      if (req.indexOf('x-') === 0) {
        headers += ", " + header;
      }
    }
  }
  cors_headers = {
    'access-control-allow-methods': 'HEAD, POST, GET, PUT, PATCH, DELETE',
    'access-control-max-age': '86400',
    'access-control-allow-headers': headers,
    'access-control-allow-credentials': 'true',
    'access-control-allow-origin': req.headers.origin || '*'
  };
  if (req.method === 'OPTIONS') {
    res.writeHead(200, cors_headers);
    res.end();
  } else {
    module.exports.options = module.exports.options || {};
    if (module.exports.options.target) {
      target0 = url.parse(module.exports.options.target);
      target = {
        host: target0.hostname,
        port: target0.port || 80
      };
      path = req.url;
      req.headers.host = target0.hostname;
    } else {
      _ref1 = req.url.match(/\/([^\/]*)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2];
      _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1];
      target = {
        host: host,
        port: port || 80
      };
      req.headers.host = hostname;
      if (target0.auth) {
        req.headers.authorization =
          'Basic ' + new Buffer(target0.auth).toString('base64');
      }
    }
    if (!(target && target.host && target.port)) {
      res.write("Cannot determine target host\n");
      res.end();
      return;
    }
    for (key in cors_headers) {
      value = cors_headers[key];
      res.setHeader(key, value);
    }
    req.url = path;
    return proxy.proxyRequest(req, res, target);
  }
};

// Generated by CoffeeScript 1.6.3
var http, httpProxy, url;

url = require('url');

http = require('http');

httpProxy = require('http-proxy');

module.exports = function(req, res, proxy) {
  var cors_headers, header, headers, host, hostname, ignore, key, path, port, target, target0, value, _i, _len, _ref, _ref1, _ref2;
  if (req.headers['access-control-request-headers']) {
    headers = req.headers['access-control-request-headers'];
  } else {
    headers = 'accept, accept-charset, accept-encoding, accept-language, authorization, content-length, content-type, host, origin, proxy-connection, referer, user-agent, x-requested-with';
    _ref = req.headers;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      header = _ref[_i];
      if (req.indexOf('x-') === 0) {
        headers += ", " + header;
      }
    }
  }
  cors_headers = {
    'access-control-allow-methods': 'HEAD, POST, GET, PUT, PATCH, DELETE',
    'access-control-max-age': '86400',
    'access-control-allow-headers': headers,
    'access-control-allow-credentials': 'true',
    'access-control-allow-origin': req.headers.origin || '*'
  };
  if (req.method === 'OPTIONS') {
    res.writeHead(200, cors_headers);
    res.end();
  } else {
    module.exports.options = module.exports.options || {};
    if (module.exports.options.target) {
      target0 = url.parse(module.exports.options.target);
      target = {
        host: target0.hostname,
        port: target0.port || 80
      };
      path = req.url;
      req.headers.host = target0.hostname;
      if (target0.auth) {
        req.headers.authorization =
          'Basic ' + new Buffer(target0.auth).toString('base64');
      }
    } else {
      _ref1 = req.url.match(/\/([^\/]*)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2];
      _ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1];
      target = {
        host: host,
        port: port || 80
      };
      req.headers.host = hostname;
    }
    if (!(target && target.host && target.port)) {
      res.write("Cannot determine target host\n");
      res.end();
      return;
    }
    for (key in cors_headers) {
      value = cors_headers[key];
      res.setHeader(key, value);
    }
    req.url = path;
    return proxy.proxyRequest(req, res, target);
  }
};

Add auth header when specifying credentials via target url param