<commit_before>/*
  node-http-proxy.js: http proxy for node.js

  Copyright (c) 2010 <NAME>, <NAME>, <NAME>, <NAME>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

var util = require('util'),
    http = require('http'),
    https = require('https'),
    events = require('events'),
    ProxyTable = require('./proxy-table').ProxyTable,
    maxSockets = 100;

//
// Expose version information through `pkginfo`.
//
require('pkginfo')(module, 'version');

//
// Track our own list of agents internal to `node-http-proxy`
//
var _agents = {};

//
// ### function _getAgent (host, port, secure)
// #### @host {string} Host of the agent to get
// #### @port {number} Port of the agent to get
// #### @secure {boolean} Value indicating whether or not to use HTTPS
// Retreives an agent from the `http` or `https` module
// and sets the `maxSockets` property appropriately.
//
function _getAgent (host, port, secure) {
  var Agent, id = [host, port].join(':');

  if (!port) {
    port = secure ? 443 : 80;
  }

  if (!_agents[id]) {
    Agent = secure ? https.Agent : http.Agent;

    _agents[id] = new Agent({ 
      host: host, 
      port: port
    });

    _agents[id].maxSockets = maxSockets;
  }

  return _agents[id];
}

//
// ### function _getProtocol (secure, outgoing)
// #### @secure {Object|boolean} Settings for `https`
// #### @outgoing {Object} Outgoing request options
// Returns the appropriate protocol based on the settings in
// `secure`. If the protocol is `https` this function will update
// the options in `outgoing` as appropriate by adding `ca`, `key`,
// and `cert` if they exist in `secure`.
//
function _getProtocol (secure, outgoing) {
  var protocol = secure ? https : http;

  if (typeof secure === 'object') {
    outgoing = outgoing || {};
    ['ca', 'cert', 'key'].forEach(function (prop) {
      if (secure[prop]) {
        outgoing[prop] = secure[prop];
      }
    })
  }

  return protocol;
}

//
// ### function getMaxSockets ()
// Returns the maximum number of sockets
// allowed on __every__ outgoing request
// made by __all__ instances of `HttpProxy`
//
exports.getMaxSockets = function () {
  return maxSockets;
};

//
// ### function setMaxSockets ()
// Sets the maximum number of sockets
// allowed on __every__ outgoing request
// made by __all__ instances of `HttpProxy`
//
exports.setMaxSockets = function (value) {
  maxSockets = value;
};

//
// ### function stack (middlewares, proxy)
// adapted from https://github.com/creationix/stack
//
exports.stack = function stack (middlewares, proxy) {
  var handle;
  middlewares.reverse().forEach(function (layer) {
    var child = handle;
    handle = function (req, res) {
      var next = function (err) {
        if (err) {
          throw err;
          //
          // TODO: figure out where to send errors.
          // return error(req, res, err);
          //
        }
        child(req, res);
      }

      next.__proto__ = proxy;
      layer(req, res, next);
    };
  });

  return handle;
}<commit_msg>Fixed large DoS vector in the middleware implementation<commit_after>/*
  node-http-proxy.js: http proxy for node.js

  Copyright (c) 2010 <NAME>, <NAME>, <NAME>, <NAME>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

var util = require('util'),
    http = require('http'),
    https = require('https'),
    events = require('events'),
    ProxyTable = require('./proxy-table').ProxyTable,
    maxSockets = 100;

//
// Expose version information through `pkginfo`.
//
require('pkginfo')(module, 'version');

//
// Track our own list of agents internal to `node-http-proxy`
//
var _agents = {};

//
// ### function _getAgent (host, port, secure)
// #### @host {string} Host of the agent to get
// #### @port {number} Port of the agent to get
// #### @secure {boolean} Value indicating whether or not to use HTTPS
// Retreives an agent from the `http` or `https` module
// and sets the `maxSockets` property appropriately.
//
function _getAgent (host, port, secure) {
  var Agent, id = [host, port].join(':');

  if (!port) {
    port = secure ? 443 : 80;
  }

  if (!_agents[id]) {
    Agent = secure ? https.Agent : http.Agent;

    _agents[id] = new Agent({ 
      host: host, 
      port: port
    });

    _agents[id].maxSockets = maxSockets;
  }

  return _agents[id];
}

//
// ### function _getProtocol (secure, outgoing)
// #### @secure {Object|boolean} Settings for `https`
// #### @outgoing {Object} Outgoing request options
// Returns the appropriate protocol based on the settings in
// `secure`. If the protocol is `https` this function will update
// the options in `outgoing` as appropriate by adding `ca`, `key`,
// and `cert` if they exist in `secure`.
//
function _getProtocol (secure, outgoing) {
  var protocol = secure ? https : http;

  if (typeof secure === 'object') {
    outgoing = outgoing || {};
    ['ca', 'cert', 'key'].forEach(function (prop) {
      if (secure[prop]) {
        outgoing[prop] = secure[prop];
      }
    })
  }

  return protocol;
}

//
// ### function getMaxSockets ()
// Returns the maximum number of sockets
// allowed on __every__ outgoing request
// made by __all__ instances of `HttpProxy`
//
exports.getMaxSockets = function () {
  return maxSockets;
};

//
// ### function setMaxSockets ()
// Sets the maximum number of sockets
// allowed on __every__ outgoing request
// made by __all__ instances of `HttpProxy`
//
exports.setMaxSockets = function (value) {
  maxSockets = value;
};

//
// ### function stack (middlewares, proxy)
// adapted from https://github.com/creationix/stack
//
exports.stack = function stack (middlewares, proxy) {
  var handle;
  middlewares.reverse().forEach(function (layer) {
    var child = handle;
    handle = function (req, res) {
      var next = function (err) {
        if (err) {
          console.error(err.stack);
          if (res._headerSent) {
            res.destroy();
          } else {
            res.statusCode = 500;
            res.setHeader('Content-Type', 'text/plain');
            res.end('Internal Server Error');
          }
          return;
        }
        child(req, res);
      }

      next.__proto__ = proxy;
      layer(req, res, next);
    };
  });

  return handle;
}
{'fraction_non_alphanumeric': 0.12200462230872157, 'fraction_numerical': 0.003284271986376353, 'mean_word_length': 3.056240749876665, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '7183594', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2324, 'n_words': 923}