<commit_before>
/**
 * options.reverseProxy = [{
 *   proxy: 'http://localhost:3000/foo/bar',
 *   host: 'foo.example.com',
 *   pathname: '/foo'
 * }]
 */

module.exports = createHandler;

var request = require('request');

function createHandler(options) {
//  console.log('Creating reverseProxy', options);
  var reverseProxy = options.reverseProxy || [];
  
  function onRequest(req, res, next) {
    execute(0, reverseProxy.length, req, res, next);
  }
  
  function execute(index, pending, req, res, next) {
    if(index === pending) return next(true);
    
    var proxiedServer = reverseProxy[index++];
    
    if(proxiedServer.proxy == null) {
      return execute(index, pending, req, res, next);
    }
    
    if(proxiedServer.host != null && proxiedServer.host !== req.headers.host) {
      return execute(index, pending, req, res, next);
    }
    
    if(proxiedServer.pathname != null && req.urlParts.pathname.slice(0, proxiedServer.pathname.length) !== proxiedServer.pathname) {
      return execute(index, pending, req, res, next);
    }
    
//    console.log('proxiedServer', proxiedServer, proxiedServer.proxy + req.url);
    req.pipe(request(proxiedServer.proxy + req.url)).pipe(res);
    req.resume();
    
    next(false);
  }
  
  return onRequest;
}<commit_msg>Disables the cookie-jar of request.<commit_after>
/**
 * options.reverseProxy = [{
 *   proxy: 'http://localhost:3000/foo/bar',
 *   host: 'foo.example.com',
 *   pathname: '/foo'
 * }]
 */

module.exports = createHandler;

var request = require('request').defaults({jar: false});

function createHandler(options) {
//  console.log('Creating reverseProxy', options);
  var reverseProxy = options.reverseProxy || [];
  
  function onRequest(req, res, next) {
    execute(0, reverseProxy.length, req, res, next);
  }
  
  function execute(index, pending, req, res, next) {
    if(index === pending) return next(true);
    
    var proxiedServer = reverseProxy[index++];
    
    if(proxiedServer.proxy == null) {
      return execute(index, pending, req, res, next);
    }
    
    if(proxiedServer.host != null && proxiedServer.host !== req.headers.host) {
      return execute(index, pending, req, res, next);
    }
    
    if(proxiedServer.pathname != null && req.urlParts.pathname.slice(0, proxiedServer.pathname.length) !== proxiedServer.pathname) {
      return execute(index, pending, req, res, next);
    }
    
//    console.log('proxiedServer', proxiedServer, proxiedServer.proxy + req.url);
    var subreq = request(proxiedServer.proxy + req.url);
//    
//    subreq.on('request', function() {
//      console.log('request');
//    });
//    subreq.on('response', function() {
//      console.log('response');
//      next(false);
//    });
//    subreq.on('complete', function() {
//      console.log('complete');
//    });
//    subreq.on('abort', function() {
//      console.log('abort');
//    });
//    subreq.on('error', function(err) {
//      console.error(err.message);
//      res.writeHead(500, {});
//      res.end();
//    });
    
    req.pipe(subreq).pipe(res);
    req.resume();
  }
  
  return onRequest;
}
{'fraction_non_alphanumeric': 0.1745116874799872, 'fraction_numerical': 0.004803073967339097, 'mean_word_length': 3.0309677419354837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7294045', 'n_tokens_mistral': 1034, 'n_tokens_neox': 1014, 'n_words': 189}