<commit_before>
var url = require('url')
var Headers = require('./headers')


function redirect (req, options) {
  req._redirect = true

  req.on('pipe', function (src) {
    req._src = src
    req._src.on('data', function (chunk) {
      if (req._redirect) {
        req._chunk = chunk
        req._src.pause()
      }
    })
  })
  req.on('response', function (res) {
    res._headers = new Headers(res.headers)

    var loc = location(res, options)

    if (loc) {
      options.path = loc
      req.emit('redirect')

      req.start(options)
      req.write(req._chunk)
    }
    else {
      req._redirect = false
      req._src.resume()
    }
  })
}

function location (res, options) {
  var location = res._headers.get('location')

  if (res.statusCode >= 300 && res.statusCode < 400 && location) {

    if (options.followAllRedirects) {
      return location
    }
    else if (options.followRedirects) {
      switch (req.method) {
        case 'PATCH':
        case 'PUT':
        case 'POST':
        case 'DELETE':
          // Do not follow redirects<commit_msg>Check if there is a src stream piped<commit_after>
var url = require('url')
var Headers = require('./headers')


function redirect (req, options) {
  req._redirect = true

  req.on('pipe', function (src) {
    req._src = src
    req._src.on('data', function (chunk) {
      if (req._redirect) {
        req._chunk = chunk
        req._src.pause()
      }
    })
  })
  req.on('response', function (res) {
    res._headers = new Headers(res.headers)

    var loc = location(res, options)

    if (loc) {
      options.path = loc
      req.emit('redirect')

      req.start(options)
      if (req._src) {
        req.write(req._chunk)
      } else {
        req.end()
      }
    }
    else {
      req._redirect = false
      if (req._src) {
        req._src.resume()
      }
    }
  })
}

function location (res, options) {
  var location = res._headers.get('location')

  if (res.statusCode >= 300 && res.statusCode < 400 && location) {

    if (options.followAllRedirects) {
      return location
    }
    else if (options.followRedirects) {
      switch (req.method) {
        case 'PATCH':
        case 'PUT':
        case 'POST':
        case 'DELETE':
          // Do not follow redirects
{'fraction_non_alphanumeric': 0.13964757709251102, 'fraction_numerical': 0.0052863436123348016, 'mean_word_length': 2.089795918367347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6310557', 'n_tokens_mistral': 740, 'n_tokens_neox': 715, 'n_words': 174}