<commit_before>--

local Class = assert(require"sancus.object").Class
local URLTemplateCompiler = assert(require"sancus.web.urltemplate").URLTemplateCompiler

local M = Class{
	compile = URLTemplateCompiler
}

function M:__call(wsapi_env)
	local h = self:find_handler(wsapi_env)

	if h then
		return h(wsapi_env)
	else
		return 404
	end
end

function M:find_handler(env)
	local script_name = env.headers["SCRIPT_NAME"] or ""
	local path_info = env.headers["PATH_INFO"] or ""

	for _, t in ipairs(self.patterns) do
		local regex, h, kw = unpack(t)
		local c, p = regex:match(path_info)

		if p then
			local matched_path_info = path_info:sub(1, p-1)
			local extra_path_info = path_info:sub(p)

			if #extra_path_info == 0 or
				(#extra_path_info > 0 and extra_path_info:sub(1,1) == "/") then
				-- good match

				local routing_args = env.headers["sancus.routing_args"] or {}

				-- import captures
				for k,v in pairs(c) do
					routing_args[k] = v
				end
				-- and import extra add() args
				if kw then
					for k,v in pairs(kw) do
						routing_args[k] = v
					end
				end

				env.headers["sancus.routing_args"] = routing_args
				env.headers["SCRIPT_NAME"] = script_name .. matched_path_info
				env.headers["PATH_INFO"] = extra_path_info

				return h
			end
		end
	end
end

function M:add(expr, h, kw)
	p = {self.compile(expr), h, kw}
	self.patterns[#self.patterns + 1] = p
end

return {
	PathMapper = function (o)<commit_msg>web.dispatcher: assert if patterns don't compile<commit_after>--

local Class = assert(require"sancus.object").Class
local URLTemplateCompiler = assert(require"sancus.web.urltemplate").URLTemplateCompiler

local M = Class{
	compile = URLTemplateCompiler
}

function M:__call(wsapi_env)
	local h = self:find_handler(wsapi_env)

	if h then
		return h(wsapi_env)
	else
		return 404
	end
end

function M:find_handler(env)
	local script_name = env.headers["SCRIPT_NAME"] or ""
	local path_info = env.headers["PATH_INFO"] or ""

	for _, t in ipairs(self.patterns) do
		local regex, h, kw = unpack(t)
		local c, p = regex:match(path_info)

		if p then
			local matched_path_info = path_info:sub(1, p-1)
			local extra_path_info = path_info:sub(p)

			if #extra_path_info == 0 or
				(#extra_path_info > 0 and extra_path_info:sub(1,1) == "/") then
				-- good match

				local routing_args = env.headers["sancus.routing_args"] or {}

				-- import captures
				for k,v in pairs(c) do
					routing_args[k] = v
				end
				-- and import extra add() args
				if kw then
					for k,v in pairs(kw) do
						routing_args[k] = v
					end
				end

				env.headers["sancus.routing_args"] = routing_args
				env.headers["SCRIPT_NAME"] = script_name .. matched_path_info
				env.headers["PATH_INFO"] = extra_path_info

				return h
			end
		end
	end
end

function M:add(expr, h, kw)
	local p = assert(self.compile(expr))
	self.patterns[#self.patterns + 1] = {p, h, kw}
end

return {
	PathMapper = function (o)
{'fraction_non_alphanumeric': 0.11497782326850904, 'fraction_numerical': 0.006823609689525759, 'mean_word_length': 3.5527950310559007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '649211', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1057, 'n_words': 269}