<commit_before>var http = require('http');
var _ = require("underscore");

function as_array(value){
  if (value === undefined || value === null)
    return [];
  if (value.constructor == Array)
    return value;
  return [value];
}

function Redirecter(config){
  this.configure.call(this, config);
  this.create_server();
};

Redirecter.prototype.configure = function(config) {
  redirecter = this;
  
  redirecter.config = _.extend({
    port:3330,
    proxy: {
      host: "localhost",
      port: 80
    },
    before:[]
  }, config);
  
  redirecter.before = as_array(config.before);
};

Redirecter.prototype.create_server = function() {
  redirecter = this;
   
  redirecter.server = http.createServer(function(request, response){
    var options = {
      headers:request.headers,
      hostname: redirecter.config.proxy.host,  
      port: redirecter.config.proxy.port,
      method: request.method,
      path:request.url
    };
    
    if (redirecter.apply_before(request, response)) {
      
    var req = http.request(options, function(res) {
      response.writeHead(res.statusCode, res.headers);

      res.on('data', function (chunk) {
        response.write(chunk);
      });

      res.on('end', function() {
        response.end();
      });
    });
    
    req.end();
    }
  });
};
<commit_msg>Use request pipe<commit_after>var http = require('http');
var _ = require("underscore");

function as_array(value){
  if (value === undefined || value === null)
    return [];
  if (value.constructor == Array)
    return value;
  return [value];
}

function Redirecter(config){
  this.configure.call(this, config);
  this.create_server();
};

Redirecter.prototype.configure = function(config) {
  redirecter = this;
  
  redirecter.config = _.extend({
    port:3330,
    proxy: {
      host: "localhost",
      port: 80
    },
    before:[]
  }, config);
  
  redirecter.before = as_array(config.before);
};

Redirecter.prototype.create_server = function() {
  redirecter = this;
   
  redirecter.server = http.createServer(function(request, response){
    var options = {
      //headers:request.headers,
      hostname: redirecter.config.proxy.host,  
      port: redirecter.config.proxy.port,
      method: request.method,
      path:request.url
    };
    
    if (redirecter.apply_before(request, response)) {
      var req = http.request(options, function (res) {
        res.pipe(response, {
          end: true
        });
      });
    
      request.pipe(req);
    }
  });
};

{'fraction_non_alphanumeric': 0.14662405113863364, 'fraction_numerical': 0.004794246903715541, 'mean_word_length': 2.811263318112633, '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': '3599861', 'n_tokens_mistral': 791, 'n_tokens_neox': 778, 'n_words': 147}