<commit_before>-- Copyright 2008 <NAME> <<EMAIL>>
-- Copyright 2008-2015 <NAME> <<EMAIL>>
-- Licensed to the public under the Apache License 2.0.

module("luci.dispatcher", package.seeall)

local http = _G.L.http

context = setmetatable({
	request = _G.L.ctx.request_path;
	requested = _G.L.node;
	dispatched = _G.L.node;
}, {
	__index = function(t, k)
		if k == "requestpath" then
			return _G.L.ctx.request_path
		elseif k == "requestargs" then
			return _G.L.ctx.request_args
		else
			return _G.L.ctx[k]
		end
	end
})

uci = require "luci.model.uci"
uci:set_session_id(_G.L.ctx.authsession)

i18n = require "luci.i18n"
i18n.setlanguage(_G.L.dispatcher.lang)

build_url = _G.L.dispatcher.build_url
menu_json = _G.L.dispatcher.menu_json
error404 = _G.L.dispatcher.error404
error500 = _G.L.dispatcher.error500

function is_authenticated(auth)
	local session = _G.L.dispatcher.is_authenticated(auth)
	if session then
		return session.sid, session.data, session.acls
	end
end

function assign(path, clone, title, order)
	local obj  = node(unpack(path))

	obj.title = title
	obj.order = order

	setmetatable(obj, {__index = node(unpack(clone))})

	return obj
end

function entry(path, target, title, order)
	local c = node(unpack(path))

	c.title  = title
	c.order  = order
	c.action = target

	return c
end

-- enabling the node.
function get(...)
	return node(...)
end

function node(...)
	local p = table.concat({ ... }, "/")

	if not __entries[p] then
		__entries[p] = {}
	end

	return __entries[p]
end

function lookup(...)
	local i, path = nil, {}
	for i = 1, select('#', ...) do
		local name, arg = nil, tostring(select(i, ...))
		for name in arg:gmatch("[^/]+") do
			path[#path+1] = name
		end
	end

	local node = menu_json()
	for i = 1, #path do
		node = node.children[path[i]]

		if not node then
			return nil
		elseif node.leaf then
			break
		end
	end

	return node, build_url(unpack(path))
end


function process_lua_controller(path)
	local base = "/usr/lib/lua/luci/controller/"
	local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
	local mod = require(modname)
	assert(mod ~= true,
	       "Invalid controller file found\n" ..
	       "The file '" .. path .. "' contains an invalid module line.\n" ..
	       "Please verify whether the module name is set to '" .. modname ..
	       "' - It must correspond to the file path!")

	local idx = mod.index
	if type(idx) ~= "function" then
		return nil
	end

	local entries = {}

	__entries = entries
	__controller = modname

	setfenv(idx,  setmetatable({}, { __index = luci.dispatcher }))()

	__entries = nil
	__controller = nil

	-- fixup gathered node specs
	for path, entry in pairs(entries) do
		if entry.leaf then
			entry.wildcard = true
		end

		if type(entry.file_depends) == "table" then
			for _, v in ipairs(entry.file_depends) do
				entry.depends = entry.depends or {}
				entry.depends.fs = entry.depends.fs or {}

				local ft = fs.stat(v, "type")
				if ft == "dir" then
					entry.depends.fs[v] = "directory"
				elseif v:match("/s?bin/") then
					entry.depends.fs[v] = "executable"
				else
					entry.depends.fs[v] = "file"
				end
			end
		end

		if type(entry.uci_depends) == "table" then
			for k, v in pairs(entry.uci_depends) do
				entry.depends = entry.depends or {}
				entry.depends.uci = entry.depends.uci or {}
				entry.depends.uci[k] = v
			end
		end

		if type(entry.acl_depends) == "table" then
			for _, acl in ipairs(entry.acl_depends) do
				entry.depends = entry.depends or {}
				entry.depends.acl = entry.depends.acl or {}
				entry.depends.acl[#entry.depends.acl + 1] = acl
			end
		end

		if (entry.sysauth_authenticator ~= nil) or
		   (entry.sysauth ~= nil and entry.sysauth ~= false)
		then
			if entry.sysauth_authenticator == "htmlauth" then
				entry.auth = {
					login = true,
					methods = { "cookie:sysauth_https", "cookie:sysauth_http" }
				}
			elseif subname == "rpc" and entry.module == "luci.controller.rpc" then
				entry.auth = {
					login = false,
					methods = { "query:auth", "cookie:sysauth_https", "cookie:sysauth_http" }
				}
			elseif entry.module == "luci.controller.admin.uci" then
				entry.auth = {
					login = false,
					methods = { "param:sid" }
				}
			end
		elseif entry.sysauth == false then
			entry.auth = {}
		end

		entry.leaf = nil

		entry.file_depends = nil
		entry.uci_depends = nil
		entry.acl_depends = nil

		entry.sysauth = nil<commit_msg>luci-lua-runtime: dispatcher.lua: translate legacy node "target" property<commit_after>-- Copyright 2008 <NAME> <<EMAIL>>
-- Copyright 2008-2015 <NAME> <<EMAIL>>
-- Licensed to the public under the Apache License 2.0.

module("luci.dispatcher", package.seeall)

local http = _G.L.http

context = setmetatable({
	request = _G.L.ctx.request_path;
	requested = _G.L.node;
	dispatched = _G.L.node;
}, {
	__index = function(t, k)
		if k == "requestpath" then
			return _G.L.ctx.request_path
		elseif k == "requestargs" then
			return _G.L.ctx.request_args
		else
			return _G.L.ctx[k]
		end
	end
})

uci = require "luci.model.uci"
uci:set_session_id(_G.L.ctx.authsession)

i18n = require "luci.i18n"
i18n.setlanguage(_G.L.dispatcher.lang)

build_url = _G.L.dispatcher.build_url
menu_json = _G.L.dispatcher.menu_json
error404 = _G.L.dispatcher.error404
error500 = _G.L.dispatcher.error500

function is_authenticated(auth)
	local session = _G.L.dispatcher.is_authenticated(auth)
	if session then
		return session.sid, session.data, session.acls
	end
end

function assign(path, clone, title, order)
	local obj  = node(unpack(path))

	obj.title = title
	obj.order = order

	setmetatable(obj, {__index = node(unpack(clone))})

	return obj
end

function entry(path, target, title, order)
	local c = node(unpack(path))

	c.title  = title
	c.order  = order
	c.action = target

	return c
end

-- enabling the node.
function get(...)
	return node(...)
end

function node(...)
	local p = table.concat({ ... }, "/")

	if not __entries[p] then
		__entries[p] = {}
	end

	return __entries[p]
end

function lookup(...)
	local i, path = nil, {}
	for i = 1, select('#', ...) do
		local name, arg = nil, tostring(select(i, ...))
		for name in arg:gmatch("[^/]+") do
			path[#path+1] = name
		end
	end

	local node = menu_json()
	for i = 1, #path do
		node = node.children[path[i]]

		if not node then
			return nil
		elseif node.leaf then
			break
		end
	end

	return node, build_url(unpack(path))
end


function process_lua_controller(path)
	local base = "/usr/lib/lua/luci/controller/"
	local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
	local mod = require(modname)
	assert(mod ~= true,
	       "Invalid controller file found\n" ..
	       "The file '" .. path .. "' contains an invalid module line.\n" ..
	       "Please verify whether the module name is set to '" .. modname ..
	       "' - It must correspond to the file path!")

	local idx = mod.index
	if type(idx) ~= "function" then
		return nil
	end

	local entries = {}

	__entries = entries
	__controller = modname

	setfenv(idx,  setmetatable({}, { __index = luci.dispatcher }))()

	__entries = nil
	__controller = nil

	-- fixup gathered node specs
	for path, entry in pairs(entries) do
		if entry.leaf then
			entry.wildcard = true
		end

		if type(entry.file_depends) == "table" then
			for _, v in ipairs(entry.file_depends) do
				entry.depends = entry.depends or {}
				entry.depends.fs = entry.depends.fs or {}

				local ft = fs.stat(v, "type")
				if ft == "dir" then
					entry.depends.fs[v] = "directory"
				elseif v:match("/s?bin/") then
					entry.depends.fs[v] = "executable"
				else
					entry.depends.fs[v] = "file"
				end
			end
		end

		if type(entry.uci_depends) == "table" then
			for k, v in pairs(entry.uci_depends) do
				entry.depends = entry.depends or {}
				entry.depends.uci = entry.depends.uci or {}
				entry.depends.uci[k] = v
			end
		end

		if type(entry.acl_depends) == "table" then
			for _, acl in ipairs(entry.acl_depends) do
				entry.depends = entry.depends or {}
				entry.depends.acl = entry.depends.acl or {}
				entry.depends.acl[#entry.depends.acl + 1] = acl
			end
		end

		if (entry.sysauth_authenticator ~= nil) or
		   (entry.sysauth ~= nil and entry.sysauth ~= false)
		then
			if entry.sysauth_authenticator == "htmlauth" then
				entry.auth = {
					login = true,
					methods = { "cookie:sysauth_https", "cookie:sysauth_http" }
				}
			elseif subname == "rpc" and entry.module == "luci.controller.rpc" then
				entry.auth = {
					login = false,
					methods = { "query:auth", "cookie:sysauth_https", "cookie:sysauth_http" }
				}
			elseif entry.module == "luci.controller.admin.uci" then
				entry.auth = {
					login = false,
					methods = { "param:sid" }
				}
			end
		elseif entry.sysauth == false then
			entry.auth = {}
		end

		if entry.action == nil and type(entry.target) == "table" then
			entry.action = entry.target
			entry.target = nil
		end

		entry.leaf = nil

		entry.file_depends = nil
		entry.uci_depends = nil
		entry.acl_depends = nil

		entry.sysauth = nil
{'fraction_non_alphanumeric': 0.13732004429678848, 'fraction_numerical': 0.008416389811738648, 'mean_word_length': 3.0643564356435644, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2971350', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3271, 'n_words': 835}