<commit_before>-- Code based on https://github.com/lipp/lua-websockets

local tools = require 'lluv.websocket.tools'
local sha1, base64 = tools.sha1, tools.base64

local guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

local function tappend(t, v)
  t[#t+1]=v
  return t
end

local function trim(s)
  return string.match(s, "^%s*(.-)%s*$")
end

local function unquote(s)
  if string.sub(s, 1, 1) == '"' then
    s = string.sub(s, 2, -2)
    s = string.gsub(s, "\\(.)", "%1")
  end
  return s
end

local function enqute(s)
  if string.find(s, '[ ",;]') then
    s = '"' .. string.gsub(s, '"', '\\"') .. '"'
  end
  return s
end

------------------------------------------------------------------
local split = {} do
function split.iter(str, sep, plain)
  local b, eol = 0
  return function()
    if b > #str then
      if eol then eol = nil return "" end
      return
    end

    local e, e2 = string.find(str, sep, b, plain)
    if e then
      local s = string.sub(str, b, e-1)
      b = e2 + 1
      if b > #str then eol = true end
      return s
    end

    local s = string.sub(str, b)
    b = #str + 1
    return s
  end
end

function split.first(str, sep, plain)
  local e, e2 = string.find(str, sep, nil, plain)
  if e then
    return string.sub(str, 1, e - 1), string.sub(str, e2 + 1)
  end
  return str
end
end
------------------------------------------------------------------

local sec_websocket_accept = function(sec_websocket_key)
  local a = sec_websocket_key .. guid
  local sha1 = sha1(a)
  assert((#sha1 % 2) == 0)
  return base64.encode(sha1)
end

local http_headers = function(request)
  local header, request = split.first(request, '\r\n')

  local headers = {}
  if not header:match('.*HTTP/1%.1') then
    return headers
  end

  for line in split.iter(request, '\r\n') do
    if line == '' then break end<commit_msg>Add. Pass `method` and `uri` parameters.<commit_after>-- Code based on https://github.com/lipp/lua-websockets

local tools = require 'lluv.websocket.tools'
local sha1, base64 = tools.sha1, tools.base64

local guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

local function tappend(t, v)
  t[#t+1]=v
  return t
end

local function trim(s)
  return string.match(s, "^%s*(.-)%s*$")
end

local function unquote(s)
  if string.sub(s, 1, 1) == '"' then
    s = string.sub(s, 2, -2)
    s = string.gsub(s, "\\(.)", "%1")
  end
  return s
end

local function enqute(s)
  if string.find(s, '[ ",;]') then
    s = '"' .. string.gsub(s, '"', '\\"') .. '"'
  end
  return s
end

------------------------------------------------------------------
local split = {} do
function split.iter(str, sep, plain)
  local b, eol = 0
  return function()
    if b > #str then
      if eol then eol = nil return "" end
      return
    end

    local e, e2 = string.find(str, sep, b, plain)
    if e then
      local s = string.sub(str, b, e-1)
      b = e2 + 1
      if b > #str then eol = true end
      return s
    end

    local s = string.sub(str, b)
    b = #str + 1
    return s
  end
end

function split.first(str, sep, plain)
  local e, e2 = string.find(str, sep, nil, plain)
  if e then
    return string.sub(str, 1, e - 1), string.sub(str, e2 + 1)
  end
  return str
end
end
------------------------------------------------------------------

local sec_websocket_accept = function(sec_websocket_key)
  local a = sec_websocket_key .. guid
  local sha1 = sha1(a)
  assert((#sha1 % 2) == 0)
  return base64.encode(sha1)
end

local http_headers = function(request)
  local header, request = split.first(request, '\r\n')

  local headers = {}
  local method, uri = string.match(header, '^(%S+)%s*(.-)%s*HTTP/1%.1')
  if method then -- accept
    headers['@method'] = method
    headers['@uri'] = uri
  elseif not string.match(header, '^%s*HTTP/1%.1') then
    return headers
  end

  for line in split.iter(request, '\r\n') do
    if line == '' then break end
{'fraction_non_alphanumeric': 0.2114492006188757, 'fraction_numerical': 0.02630221763795771, 'mean_word_length': 2.161369193154034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '5264917', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1396, 'n_words': 416}