<commit_before>'use strict'

var util = require('util')
var net = require('net')
var http = require('http')
var once = require('once')
var consume = require('consume-http-header')

module.exports = ReverseServer

util.inherits(ReverseServer, http.Server)

function ReverseServer (opts, onRequest) {
  if (!(this instanceof ReverseServer)) return new ReverseServer(opts, onRequest)

  http.Server.call(this)

  this.setTimeout(0)

  if (!opts) opts = {}
  if (!opts.method) opts.method = 'POST'
  if (!opts.headers) opts.headers = {}
  if (!opts.headers['Upgrade']) opts.headers['Upgrade'] = 'PTTH/1.0'
  if (!opts.headers['Connection']) opts.headers['Connection'] = 'Upgrade'
  if (!opts.headers['Content-Length']) opts.headers['Content-Length'] = 0
  if (onRequest) this.on('request', onRequest)

  var server = this
  var closed = false

  this.on('close', function () {
    closed = true
  })

  connect()

  function connect () {
    if (closed) return

    var onClose = once(connect)
    var socket = net.connect(opts)

    socket.on('connect', upgrade)
    socket.on('error', onError)

    function upgrade () {
      socket.on('close', onClose)
      socket.on('end', onClose)

      consume(socket, function (err, head) {
        if (err) {
          server.emit('error', err)
          socket.destroy()
        } else if (head.statusCode === 101 &&
            head.headers.upgrade === 'PTTH/1.0' &&
            head.headers.connection === 'Upgrade') {
          server.emit('connection', socket)
        } else {
          server.emit('error', new Error('Unexpected response to PTTH/1.0 Upgrade request'))
          socket.destroy()
        }
      })

      socket.write('POST / HTTP/1.1\r\n' +
                   'Upgrade: PTTH/1.0\r\n' +
                   'Connection: Upgrade\r\n' +
                   'Content-Length: 0\r\n\r\n')
    }
  }

  function onError (err) {
    server.emit('error', err)
  }
}<commit_msg>Actually use the options passed in as the 1st argument<commit_after>'use strict'

var util = require('util')
var net = require('net')
var http = require('http')
var once = require('once')
var consume = require('consume-http-header')

module.exports = ReverseServer

util.inherits(ReverseServer, http.Server)

function ReverseServer (opts, onRequest) {
  if (!(this instanceof ReverseServer)) return new ReverseServer(opts, onRequest)

  var upgradeRequest = generateRequest(opts)
  var server = this
  var closed = false

  http.Server.call(this)

  this.setTimeout(0)
  this.on('close', onClose)
  if (onRequest) this.on('request', onRequest)

  connect()

  function connect () {
    if (closed) return

    var socket = net.connect(opts.port, opts.host)

    socket.on('connect', upgrade)
    socket.on('error', onError)

    function upgrade () {
      var onClose = once(connect)
      socket.on('close', onClose)
      socket.on('end', onClose)

      consume(socket, function (err, head) {
        if (err) {
          server.emit('error', err)
          server.close()
        } else if (head.statusCode === 101 &&
            head.headers.upgrade === 'PTTH/1.0' &&
            head.headers.connection === 'Upgrade') {
          server.emit('connection', socket)
        } else {
          server.emit('error', new Error('Unexpected response to PTTH/1.0 Upgrade request'))
          server.close()
        }
      })

      socket.write(upgradeRequest)
    }
  }

  function onClose () {
    closed = true
  }

  function onError (err) {
    server.emit('error', err)
  }
}

function generateRequest (opts) {
  opts = defaultOptions(opts)

  return Object.keys(opts.headers).reduce(function (s, field) {
    var value = opts.headers[field]
    if (!Array.isArray(value)) value = [value]
    return value.reduce(function (s, value) {
      return s + field + ': ' + value + '\r\n'
    }, s)
  }, opts.method + ' ' + opts.path + ' HTTP/1.1\r\n') + '\r\n'
}

function defaultOptions (opts) {
  if (!opts) opts = {}
  if (!opts.method) opts.method = 'POST'
  if (!opts.host) opts.host = 'localhost'
  if (!opts.path) opts.path = '/'
  if (!opts.headers) opts.headers = {}
  if (!opts.headers['Host']) opts.headers['Host'] = opts.host
  if (!opts.headers['Upgrade']) opts.headers['Upgrade'] = 'PTTH/1.0'
  if (!opts.headers['Connection']) opts.headers['Connection'] = 'Upgrade'
  if (!opts.headers['Content-Length']) opts.headers['Content-Length'] = 0
  return opts
}
{'fraction_non_alphanumeric': 0.15970013630168106, 'fraction_numerical': 0.0068150840527033164, 'mean_word_length': 2.886142983230362, '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': '386748', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1371, 'n_words': 324}