<commit_before>-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

require("protocol/tcp-connection")

local module = {}

local utils = require("protocol/http_utils")
table.merge(module, utils)


--
-- HTTP dissector
--

local HeaderResult = class("HeaderResult", haka.grammar.ArrayResult)

function HeaderResult.method:__init()
	rawset(self, '_cache', {})
end

function HeaderResult.method:__index(key)
	local key = key:lower()

	if self._cache[key] then
		return self._cache[key].value
	end

	for i, header in ipairs(self) do
		-- TODO avoid getting last empty header
		if header.name and header.name:lower() == key then
			self._cache[key] = header
			return header.value
		end
	end
end

function HeaderResult:__pairs()
        local i = 0
        local function headernext(headerresult, index)
                i = i + 1
                if rawget(headerresult, i) then
                        return rawget(headerresult, i).name, rawget(headerresult, i).value
                else
                        return nil
                end
        end
        return headernext, self, nil
end

function HeaderResult.method:__newindex(key, value)
	local lowerkey = key:lower()

	-- Try to update existing header
	for i, header in ipairs(self) do
		if header.name and header.name:lower() == lowerkey then
			if value then
				header.value = value
			else<commit_msg>Fix indent<commit_after>-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

require("protocol/tcp-connection")

local module = {}

local utils = require("protocol/http_utils")
table.merge(module, utils)


--
-- HTTP dissector
--

local HeaderResult = class("HeaderResult", haka.grammar.ArrayResult)

function HeaderResult.method:__init()
	rawset(self, '_cache', {})
end

function HeaderResult.method:__index(key)
	local key = key:lower()

	if self._cache[key] then
		return self._cache[key].value
	end

	for i, header in ipairs(self) do
		-- TODO avoid getting last empty header
		if header.name and header.name:lower() == key then
			self._cache[key] = header
			return header.value
		end
	end
end

function HeaderResult:__pairs()
	local i = 0
	local function headernext(headerresult, index)
		i = i + 1
		if rawget(headerresult, i) then
			return rawget(headerresult, i).name, rawget(headerresult, i).value
		else
			return nil
		end
	end
	return headernext, self, nil
end

function HeaderResult.method:__newindex(key, value)
	local lowerkey = key:lower()

	-- Try to update existing header
	for i, header in ipairs(self) do
		if header.name and header.name:lower() == lowerkey then
			if value then
				header.value = value
			else
{'fraction_non_alphanumeric': 0.10072438771990341, 'fraction_numerical': 0.004139358399448086, 'mean_word_length': 3.1666666666666665, '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': '220071', 'n_tokens_mistral': 940, 'n_tokens_neox': 879, 'n_words': 300}