<commit_before>import url from 'url'
import http from 'http'
import https from 'https'

import { assign } from '../utils'
import Gateway from '../gateway'
import Response from '../response'

function HTTP (request) {
  Gateway.apply(this, arguments)
}

HTTP.prototype = Gateway.extends({
  get () {
    this.performRequest('get')
  },

  post () {
    this.performRequest('post')
  },

  put () {
    this.performRequest('put')
  },

  patch () {
    this.performRequest('patch')
  },

  delete () {
    this.performRequest('delete')
  },

  performRequest (method) {
    const headers = {}
    const defaults = url.parse(this.request.url())
    const requestMethod = this.shouldEmulateHTTP() ? 'post' : method
    const body = this.prepareBody(method, headers)
    const timeout = this.request.timeout()

    this.canceled = false

    if (body && typeof body.length === 'number') {
      headers['content-length'] = Buffer.byteLength(body)
    }

    const handler = (defaults.protocol === 'https:') ? https : http

    const requestParams = assign(defaults, {
      method: requestMethod,
      headers: assign(headers, this.request.headers())
    })

    const auth = this.request.auth()
    if (auth) {
      const username = auth.username || ''
      const password = auth.password || ''
      requestParams['auth'] = `${username}:${password}`
    }

    const httpOptions = this.options().HTTP

    if (httpOptions.configure) {
      assign(requestParams, httpOptions.configure(requestParams))
    }

    const httpRequest = handler
      .request(requestParams, (httpResponse) => this.onResponse(httpResponse))

    httpRequest.on('error', (e) => this.onError(e))
    body && httpRequest.write(body)

    if (timeout) {
      httpRequest.setTimeout(timeout, () => {
        this.canceled = true
        this.dispatchClientError(`Timeout (${timeout}ms)`)
      })
    }

    httpRequest.end()
  },

  onResponse (httpResponse) {
    const rawData = []

    httpResponse
      .on('data', (chunk) => rawData.push(chunk))
      .on('end', () => {
        if (this.canceled) {
          return
        }

        this.dispatchResponse(this.createResponse(httpResponse, rawData))
      })
  },

  onError (e) {
    if (this.canceled) {
      return
    }

    this.dispatchClientError(e.message)
  },

  createResponse (httpResponse, rawData) {
    const data = Buffer.concat(rawData)

    return new Response(
      this.request,
      httpResponse.statusCode,
      this.request.isBinary() ? data : data.toString(),
      httpResponse.headers
    )
  }
})

export default HTTP<commit_msg>Restore original behaviour when not binary<commit_after>import url from 'url'
import http from 'http'
import https from 'https'

import { assign } from '../utils'
import Gateway from '../gateway'
import Response from '../response'

function HTTP (request) {
  Gateway.apply(this, arguments)
}

HTTP.prototype = Gateway.extends({
  get () {
    this.performRequest('get')
  },

  post () {
    this.performRequest('post')
  },

  put () {
    this.performRequest('put')
  },

  patch () {
    this.performRequest('patch')
  },

  delete () {
    this.performRequest('delete')
  },

  performRequest (method) {
    const headers = {}
    const defaults = url.parse(this.request.url())
    const requestMethod = this.shouldEmulateHTTP() ? 'post' : method
    const body = this.prepareBody(method, headers)
    const timeout = this.request.timeout()

    this.canceled = false

    if (body && typeof body.length === 'number') {
      headers['content-length'] = Buffer.byteLength(body)
    }

    const handler = (defaults.protocol === 'https:') ? https : http

    const requestParams = assign(defaults, {
      method: requestMethod,
      headers: assign(headers, this.request.headers())
    })

    const auth = this.request.auth()
    if (auth) {
      const username = auth.username || ''
      const password = auth.password || ''
      requestParams['auth'] = `${username}:${password}`
    }

    const httpOptions = this.options().HTTP

    if (httpOptions.configure) {
      assign(requestParams, httpOptions.configure(requestParams))
    }

    const httpRequest = handler
      .request(requestParams, (httpResponse) => this.onResponse(httpResponse))

    httpRequest.on('error', (e) => this.onError(e))
    body && httpRequest.write(body)

    if (timeout) {
      httpRequest.setTimeout(timeout, () => {
        this.canceled = true
        this.dispatchClientError(`Timeout (${timeout}ms)`)
      })
    }

    httpRequest.end()
  },

  onResponse (httpResponse) {
    const rawData = []
    if (!this.request.isBinary()) {
      httpResponse.setEncoding('utf8')
    }

    httpResponse
      .on('data', (chunk) => rawData.push(chunk))
      .on('end', () => {
        if (this.canceled) {
          return
        }

        this.dispatchResponse(this.createResponse(httpResponse, rawData))
      })
  },

  onError (e) {
    if (this.canceled) {
      return
    }

    this.dispatchClientError(e.message)
  },

  createResponse (httpResponse, rawData) {
    return new Response(
      this.request,
      httpResponse.statusCode,
      this.request.isBinary() ? Buffer.concat(rawData) : rawData.join(''),
      httpResponse.headers
    )
  }
})

export default HTTP
{'fraction_non_alphanumeric': 0.1435343193022374, 'fraction_numerical': 0.00018960940462646946, 'mean_word_length': 2.830791575889615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2442850', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1554, 'n_words': 324}