<commit_before>local socket = require 'socket'
local Request = require 'pegasus.request'
local Response = require 'pegasus.response'


local Pegasus = {}

function Pegasus:new(port)
  self.port = port or '9090'
  return self
end

function Pegasus:start(callback)
  local server = assert(socket.bind('*', self.port))
  local ip, port = server:getsockname()
  print('Pegasus is up on port ' .. self.port)

  while 1 do
    local client = server:accept()
    client:settimeout(1, 'b')
    self:processRequest(client, callback)
    client:close()
  end
end

function Pegasus:processRequest(client, callback)
  local request = Request:new(client)
  local response =  Response:new(client)

  if request:path() then
    response:processes(request)
  end

  if callback then
    self:executeCallback(callback, request, response, client)
  else
    client:send(response.body)
  end
end

Pegasus.wasFinishCalled = false

function Pegasus:executeCallback(callback, request, response, client)
  local req = self:makeResponse(request)
  local rep = self:makeResponse(response, client)

  callback(req, rep)

  if not self.wasFinishCalled then
    client:send(response.body)
  end
end

function Pegasus:makeResponse(request)
  return {
    path = request:path(),
    headers = request:headers(),
    method = request:method(),
    querystring = request:params(),
    post = request:post()
  }
end

function Pegasus:makeResponse(response, client)
  rep = {
    statusCode = nil,
    head = nil,

    writeHead = function(statusCode)
      rep.head = response:makeHead(statusCode)
      rep.statusCode = statusCode

      return rep
    end,

    finish = function(body)
      local body = response:createBody(rep.head, body, rep.statusCode)
      client:send(body)<commit_msg>change request method makeResponse to makeRequest<commit_after>local socket = require 'socket'
local Request = require 'pegasus.request'
local Response = require 'pegasus.response'


local Pegasus = {}

function Pegasus:new(port)
  self.port = port or '9090'
  return self
end

function Pegasus:start(callback)
  local server = assert(socket.bind('*', self.port))
  local ip, port = server:getsockname()
  print('Pegasus is up on port ' .. self.port)

  while 1 do
    local client = server:accept()
    client:settimeout(1, 'b')
    self:processRequest(client, callback)
    client:close()
  end
end

function Pegasus:processRequest(client, callback)
  local request = Request:new(client)
  local response =  Response:new(client)

  if request:path() then
    response:processes(request)
  end

  if callback then
    self:executeCallback(callback, request, response, client)
  else
    client:send(response.body)
  end
end

Pegasus.wasFinishCalled = false

function Pegasus:executeCallback(callback, request, response, client)
  local req = self:makeRequest(request)
  local rep = self:makeResponse(response, client)

  callback(req, rep)

  if not self.wasFinishCalled then
    client:send(response.body)
  end
end

function Pegasus:makeRequest(request)
  return {
    path = request:path(),
    headers = request:headers(),
    method = request:method(),
    querystring = request:params(),
    post = request:post()
  }
end

function Pegasus:makeResponse(response, client)
  rep = {
    statusCode = nil,
    head = nil,

    writeHead = function(statusCode)
      rep.head = response:makeHead(statusCode)
      rep.statusCode = statusCode

      return rep
    end,

    finish = function(body)
      local body = response:createBody(rep.head, body, rep.statusCode)
      client:send(body)
{'fraction_non_alphanumeric': 0.10348706411698538, 'fraction_numerical': 0.003374578177727784, 'mean_word_length': 4.038243626062323, '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': '766247', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1066, 'n_words': 282}