<commit_before>
var utils = require('./utils')
var response = require('./response')
var modules = require('./modules')


function request (req, options) {
  if (process.env.DEBUG) {
    var log = require('@http/log')
    log(req)
  }

  req.on('onresponse', response(req, options))

  if (options.parse === undefined) {
    options.parse = {}
  }
  if (options.stringify === undefined) {
    options.stringify = {}
  }

  if (options.redirect) {
    var redirect = modules('redirect')
    redirect(req, options)
  }

  if (options.gzip) {
    var gzip = modules('gzip')
    gzip(req, options)
  }

  if (options.multipart) {
    var multipart = modules('multipart')
    multipart(req, options)
  }

  if (options.qs) {
    var qs = modules('qs')
    qs(req, options)
  }
  if (options.form) {
    var form = modules('form')
    form(req, options)
  }
  if (options.json) {
    var json = modules('json')
    json(req, options)
  }

  if (options.cookie) {
    var cookie = modules('cookie')
    cookie(req, options)
  }

  if (options.callback) {
    if (typeof options.callback === 'function') {
      var callback = modules('callback')
      callback(req, options)
    }
    else {
      throw new Error('calback should be a function')
    }
  }

  if (options.auth || options.url.auth || options.oauth || options.hawk || options.httpSignature || options.aws) {
    var auth = modules('auth')
    auth(req, options)
  }

  req.once('init', init)
  process.nextTick(init)

  function init () {
    if (req._initialized) return

    if (options.length) {
      var length = require('./options/length')
      length(req, options, function (length) {
        if (length) {
          options.headers.set('content-length', length)
        }
        _init()
      })
    }
    else {
      _init()
    }

    function _init () {
      if (options.parse) {
        var parse = modules('parse')
        parse(req, options)
      }
      if (options.body || req._src || !options.end) {
        if (options.headers.get('content-length') === undefined) {
          options.headers.set('transfer-encoding', 'chunked')
        }
      }

      req.emit('options', options)
      req.init(utils.filter(options))

      if (options.body) {
        var body = modules('body')
        body(req, options)
      }

      if (options.end) {
        var end = modules('end')
        end(req, options)
      }
    }
  }
}

module.exports = request
<commit_msg>Handle content length errors and aborted request<commit_after>
var utils = require('./utils')
var response = require('./response')
var modules = require('./modules')


function request (req, options) {
  if (process.env.DEBUG) {
    var log = require('@http/log')
    log(req)
  }

  req.on('onresponse', response(req, options))

  if (options.parse === undefined) {
    options.parse = {}
  }
  if (options.stringify === undefined) {
    options.stringify = {}
  }

  if (options.redirect) {
    var redirect = modules('redirect')
    redirect(req, options)
  }

  if (options.gzip) {
    var gzip = modules('gzip')
    gzip(req, options)
  }

  if (options.multipart) {
    var multipart = modules('multipart')
    multipart(req, options)
  }

  if (options.qs) {
    var qs = modules('qs')
    qs(req, options)
  }
  if (options.form) {
    var form = modules('form')
    form(req, options)
  }
  if (options.json) {
    var json = modules('json')
    json(req, options)
  }

  if (options.cookie) {
    var cookie = modules('cookie')
    cookie(req, options)
  }

  if (options.callback) {
    if (typeof options.callback === 'function') {
      var callback = modules('callback')
      callback(req, options)
    }
    else {
      throw new Error('calback should be a function')
    }
  }

  if (options.auth || options.url.auth || options.oauth || options.hawk || options.httpSignature || options.aws) {
    var auth = modules('auth')
    auth(req, options)
  }

  req.once('init', init)
  process.nextTick(init)

  function init () {
    if (req._initialized) return
    if (req._aborted) return

    if (options.length) {
      var length = require('./options/length')
      length(req, options, function (err, length) {
        if (!err && length) {
          options.headers.set('content-length', length)
        }
        _init()
      })
    }
    else {
      _init()
    }

    function _init () {
      if (options.parse) {
        var parse = modules('parse')
        parse(req, options)
      }
      if (options.body || req._src || !options.end) {
        if (options.headers.get('content-length') === undefined) {
          options.headers.set('transfer-encoding', 'chunked')
        }
      }

      req.emit('options', options)
      req.init(utils.filter(options))

      if (options.body) {
        var body = modules('body')
        body(req, options)
      }

      if (options.end) {
        var end = modules('end')
        end(req, options)
      }
    }
  }
}

module.exports = request

{'fraction_non_alphanumeric': 0.15237134207870837, 'fraction_numerical': 0.0, 'mean_word_length': 2.3714285714285714, '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': '5581066', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1557, 'n_words': 355}