<commit_before>'use strict'

var util = require('util')
var stream = require('stream')
var STATUS_CODES = require('./status-codes')

var ServerResponse = module.exports = function (socket, opts) {
  if (!(this instanceof ServerResponse)) return new ServerResponse(socket, opts)
  stream.Writable.call(this, opts)
  this.socket = socket
  this.statusCode = 200
  this.headersSent = false
  this._headers = {}

  this.once('finish', function () {
    if (!this.headersSent) this.writeHead()
    this.socket.end()
  }.bind(this))
}

util.inherits(ServerResponse, stream.Writable)

ServerResponse.prototype._write = function (chunk, encoding, cb) {
  if (!this.headersSent) this.writeHead()
  this.socket.write(chunk, encoding, cb) // TODO: Implement back-preasure
}

ServerResponse.prototype.setHeader = function (name, value) {
  if (this.headersSent) throw new Error('Headers already sent!')
  this._headers[name] = value
}

ServerResponse.prototype.getHeader = function (name) {
  if (this.headersSent) throw new Error('Headers already sent!')
  return this._headers[name]
}

ServerResponse.prototype.removeHeader = function (name) {
  if (this.headersSent) throw new Error('Headers already sent!')
  delete this._headers[name]
}

ServerResponse.prototype.writeHead = function (statusCode, statusMessage, headers) {
  if (this.headersSent) throw new Error('Headers already sent!')
  if (typeof statusMessage === 'object') {
    headers = statusMessage
    statusMessage = null
  }

  if (statusCode) this.statusCode = statusCode

  if (headers) this._headers = headers
  this.statusMessage = statusMessage || this.statusMessage || STATUS_CODES[String(this.statusCode)]

  var statusLine = util.format('RTSP/1.0 %s %s\r\n', this.statusCode, this.statusMessage)
  this.socket.write(statusLine, 'utf8')

  var self = this
  Object.keys(this._headers).forEach(function (name) {
    var value = self._headers[name]
    if (!Array.isArray(value)) value = [value]
    value.forEach(function (value) {
      self.socket.write(util.format('%s: %s\r\n', name, value), 'utf8')
    })
  })

  this.socket.write('\r\n', 'utf8')
  this.headersSent = true
}
<commit_msg>Make get/set/removeHeader case insensitive<commit_after>'use strict'

var util = require('util')
var stream = require('stream')
var STATUS_CODES = require('./status-codes')

var ServerResponse = module.exports = function (socket, opts) {
  if (!(this instanceof ServerResponse)) return new ServerResponse(socket, opts)
  stream.Writable.call(this, opts)
  this.socket = socket
  this.statusCode = 200
  this.headersSent = false
  this._headers = {}

  this.once('finish', function () {
    if (!this.headersSent) this.writeHead()
    this.socket.end()
  }.bind(this))
}

util.inherits(ServerResponse, stream.Writable)

ServerResponse.prototype._write = function (chunk, encoding, cb) {
  if (!this.headersSent) this.writeHead()
  this.socket.write(chunk, encoding, cb) // TODO: Implement back-preasure
}

ServerResponse.prototype.setHeader = function (name, value) {
  if (this.headersSent) throw new Error('Headers already sent!')
  this._headers[name.toLowerCase()] = [name, value]
}

ServerResponse.prototype.getHeader = function (name) {
  if (this.headersSent) throw new Error('Headers already sent!')
  var header = this._headers[name.toLowerCase()]
  return header ? header[1] : undefined
}

ServerResponse.prototype.removeHeader = function (name) {
  if (this.headersSent) throw new Error('Headers already sent!')
  delete this._headers[name.toLowerCase()]
}

ServerResponse.prototype.writeHead = function (statusCode, statusMessage, headers) {
  var self = this

  if (this.headersSent) throw new Error('Headers already sent!')
  if (typeof statusMessage === 'object') {
    headers = statusMessage
    statusMessage = null
  }

  if (statusCode) this.statusCode = statusCode
  if (headers) {
    Object.keys(headers).forEach(function (name) {
      self.setHeader(name, headers[name])
    })
  }

  this.statusMessage = statusMessage || this.statusMessage || STATUS_CODES[String(this.statusCode)]

  var statusLine = util.format('RTSP/1.0 %s %s\r\n', this.statusCode, this.statusMessage)
  this.socket.write(statusLine, 'utf8')

  Object.keys(this._headers).forEach(function (key) {
    var header = self._headers[key]
    var name = header[0]
    var value = header[1]
    if (!Array.isArray(value)) value = [value]
    value.forEach(function (value) {
      self.socket.write(util.format('%s: %s\r\n', name, value), 'utf8')
    })
  })

  this.socket.write('\r\n', 'utf8')
  this.headersSent = true
}

{'fraction_non_alphanumeric': 0.14032399299474607, 'fraction_numerical': 0.004159369527145359, 'mean_word_length': 4.439285714285714, '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': '3357093', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1418, 'n_words': 333}