<commit_before>var RequestOverrider = require('./request_overrider'),
    path             = require('path'),
    url              = require('url'),
    inherits         = require('util').inherits,
    EventEmitter     = require('events').EventEmitter,
    http             = require('http'),
    ClientRequest    = http.ClientRequest;

var allInterceptors = {};

function add(key, interceptor) {
  if (! allInterceptors.hasOwnProperty(key)) {
    allInterceptors[key] = [];
  }
  allInterceptors[key].push(interceptor);
}

function remove(interceptor) {
  var key          = interceptor._key.split(' '),
      u            = url.parse(key[1]),
      hostKey      = u.protocol + '//' + u.host,
      interceptors = allInterceptors[hostKey],
      interceptor,
      thisInterceptor;

  if (interceptors) {
    for(var i = 0; i < interceptors.length; i++) {
      thisInterceptor = interceptors[i];
      if (thisInterceptor._key === interceptor._key) {
        interceptors.splice(i, 1);
        break;
      }
    }

  }
}

function removeAll() {
  allInterceptors = {};
}

function interceptorsFor(options) {
  var basePath;

  options.proto = options.proto || 'http';

  if (!options.host) {
    options.host = options.hostname;
  }

  if (
       options.port && options.host.indexOf(':') < 0 &&
       (options.port !== 80 || options.proto !== 'http') &&
       (options.port !== 443 || options.proto !== 'https')
     ) {
    options.host += ":" + options.port;
  }

  basePath =  options.proto + '://' + options.host;

  return allInterceptors[basePath] || [];
}

// ----- Extending http.ClientRequest

function OverridenClientRequest(options, defaultPort) {
  var interceptors = interceptorsFor(options);

  if (interceptors.length) {
    var overrider = RequestOverrider(this, options, interceptors, remove);
    for(var propName in overrider) {
      this[propName] = overrider[propName];
    }
  } else {
    ClientRequest.apply(this, arguments);
  }
  
}
inherits(OverridenClientRequest, ClientRequest);

http.ClientRequest = OverridenClientRequest;

// ----- Overriding http.request and https.request:

[ 'http', 'https'].forEach(
  function(proto) {<commit_msg>fixed case when mikeal/request will append port number twice when using non-standard port<commit_after>var RequestOverrider = require('./request_overrider'),
    path             = require('path'),
    url              = require('url'),
    inherits         = require('util').inherits,
    EventEmitter     = require('events').EventEmitter,
    http             = require('http'),
    ClientRequest    = http.ClientRequest;

var allInterceptors = {};

function add(key, interceptor) {
  if (! allInterceptors.hasOwnProperty(key)) {
    allInterceptors[key] = [];
  }
  allInterceptors[key].push(interceptor);
}

function remove(interceptor) {
  var key          = interceptor._key.split(' '),
      u            = url.parse(key[1]),
      hostKey      = u.protocol + '//' + u.host,
      interceptors = allInterceptors[hostKey],
      interceptor,
      thisInterceptor;

  if (interceptors) {
    for(var i = 0; i < interceptors.length; i++) {
      thisInterceptor = interceptors[i];
      if (thisInterceptor._key === interceptor._key) {
        interceptors.splice(i, 1);
        break;
      }
    }

  }
}

function removeAll() {
  allInterceptors = {};
}

function interceptorsFor(options) {
  var basePath;

  options.proto = options.proto || 'http';

  if (!options.host) {
    options.host = options.hostname;
  }

  if (
       options.port && options.host.indexOf(':') < 0 &&
       (options.port !== 80 || options.proto !== 'http') &&
       (options.port !== 443 || options.proto !== 'https')
     ) {
    options.host += ":" + options.port;
    delete options.port;
  }

  basePath =  options.proto + '://' + options.host;

  return allInterceptors[basePath] || [];
}

// ----- Extending http.ClientRequest

function OverridenClientRequest(options, defaultPort) {
  var interceptors = interceptorsFor(options);

  if (interceptors.length) {
    var overrider = RequestOverrider(this, options, interceptors, remove);
    for(var propName in overrider) {
      this[propName] = overrider[propName];
    }
  } else {
    ClientRequest.apply(this, arguments);
  }
  
}
inherits(OverridenClientRequest, ClientRequest);

http.ClientRequest = OverridenClientRequest;

// ----- Overriding http.request and https.request:

[ 'http', 'https'].forEach(
  function(proto) {
{'fraction_non_alphanumeric': 0.14648393619411368, 'fraction_numerical': 0.004044035048303752, 'mean_word_length': 2.884816753926702, 'pattern_counts': {'":': 2, '<': 7, '<?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': '2046874', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1347, 'n_words': 260}