📄 cgilua.lua
字号:
-- @param path String with the path.-- @param protocol String with the name of the protocol (default = "http").-- @return String in URL format.----------------------------------------------------------------------------function mkabsoluteurl (path, protocol) protocol = protocol or "http" if path:sub(1,1) ~= '/' then path = '/'..path end return format("%s://%s:%s%s", protocol, servervariable"SERVER_NAME", servervariable"SERVER_PORT", path)end------------------------------------------------------------------------------ Extract the "directory" and "file" parts of a path-- @param path String with a path.-- @return String with the directory part.-- @return String with the file part.----------------------------------------------------------------------------function splitpath (path) local _,_,dir,file = strfind(path,"^(.-)([^:/\\]*)$") return dir,fileend---- Define variables and build the cgilua.POST, cgilua.GET and the global `cgi' table.-- @param args Table where to store the parameters (the actual `cgi' table).--local function getparams () -- Define variables. script_path = script_path or servervariable"PATH_TRANSLATED" if not script_path then script_path = servervariable"DOCUMENT_ROOT" .. servervariable"SCRIPT_NAME" end script_pdir, script_file = splitpath (script_path) local vpath = servervariable"PATH_INFO" script_vpath = vpath if vpath and vpath ~= "" then script_vdir = splitpath (vpath) urlpath = servervariable"SCRIPT_NAME" else script_vdir = splitpath (servervariable"SCRIPT_NAME") urlpath = "" end -- Fill in the POST table. POST = {} if servervariable"REQUEST_METHOD" == "POST" then post.parsedata { read = SAPI.Request.getpostdata, discardinput = ap and ap.discard_request_body, content_type = servervariable"CONTENT_TYPE", content_length = servervariable"CONTENT_LENGTH", maxinput = maxinput, maxfilesize = maxfilesize, args = POST, } end -- Fill in the QUERY table. QUERY = {} urlcode.parsequery (servervariable"QUERY_STRING", QUERY) -- Links POST and QUERY data to the CGI table for backward compatibility local mt = {} mt.__index = function(t,v) return POST[v] or QUERY[v] end setmetatable(CGI, mt)end---- Stores all script handlers and the file extensions used to identify-- them.local script_handlers = {}---- Default handler.-- Sends the contents of the file to the output without processing it.-- @param filename String with the name of the file.--local function default_handler (filename) htmlheader () local fh = assert (_open (filename)) local prog = fh:read("*a") fh:close() put (prog)end------------------------------------------------------------------------------ Add a script handler.-- @param file_extension String with the lower-case extension of the script.-- @param func Function to handle this kind of scripts.----------------------------------------------------------------------------function addscripthandler (file_extension, func) assert (type(file_extension) == "string", "File extension must be a string") if strfind (file_extension, '%.', 1) then file_extension = strsub (file_extension, 2) end file_extension = strlower(file_extension) assert (type(func) == "function", "Handler must be a function") script_handlers[file_extension] = funcend----------------------------------------------------------------------------- Obtains the handler corresponding to the given script path.-- @param path String with a script path.-- @return Function that handles it or nil.----------------------------------------------------------------------------function getscripthandler (path) local i,f, ext = strfind (path, "%.([^.]+)$") return script_handlers[strlower(ext or '')]end----------------------------------------------------------------------------- Execute the given path with the corresponding handler.-- @param path String with a script path.-- @return The returned values from the script.---------------------------------------------------------------------------function handle (path) local h = assert (getscripthandler (path), "There is no handler defined to process this kind of file ("..path..")") return h (path)end----------------------------------------------------------------------------- Sets "errorhandler" function-- This function is called by Lua when an error occurs.-- It receives the error message generated by Lua and it is resposible-- for the final message which should be returned.-- @param Function.---------------------------------------------------------------------------function seterrorhandler (f) local tf = type(f) if tf == "function" then errorhandler = f else error (format ("Invalid type: expected `function', got `%s'", tf)) endend----------------------------------------------------------------------------- Defines the "erroroutput" function-- This function is called to generate the error output.-- @param Function.---------------------------------------------------------------------------function seterroroutput (f) local tf = type(f) if tf == "function" then erroroutput = f else error (format ("Invalid type: expected `function', got `%s'", tf)) endend---- Executes a function with an error handler.-- @param f Function to be called.--local function _xpcall (f) local ok, result = xpcall (f, errorhandler) if ok then return result else erroroutput (result) endend---- Stores all close functions in order they are set.local close_functions = {}----------------------------------------------------------------------------- Adds a function to be executed after the script.-- @param f Function to be registered.---------------------------------------------------------------------------function addclosefunction (f) local tf = type(f) if tf == "function" then tinsert (close_functions, f) else error (format ("Invalid type: expected `function', got `%s'", tf)) endend---- Close function.--local function close() for i = #close_functions, 1, -1 do close_functions[i]() endend---- Stores all open functions in order they are set.local open_functions = {}----------------------------------------------------------------------------- Adds a function to be executed before the script.-- @param f Function to be registered.---------------------------------------------------------------------------function addopenfunction (f) local tf = type(f) if tf == "function" then tinsert (open_functions, f) else error (format ("Invalid type: expected `function', got `%s'", tf)) endend---- Open function.-- Call all defined open-functions in the order they were created.--local function open() for i = #open_functions, 1, -1 do open_functions[i]() endend---- Resets CGILua's state.--local function reset () script_path = false maxfilesize = default_maxfilesize maxinput = default_maxinput -- Error Handling errorhandler = default_errorhandler erroroutput = default_erroroutput -- Handlers script_handlers = {} open_function = {} close_functions = {}end----------------------------------------------------------------------------- Request processing.---------------------------------------------------------------------------function main () -- Default values addscripthandler ("lua", doscript) addscripthandler ("lp", handlelp) CGI = {} -- Configuring CGILua (trying to load cgilua/config.lua) _xpcall (function () _G.require"cgilua.config" end) -- Cleaning environment removeglobals { "os.execute", "loadlib", "package", "debug", } -- Build fake package _G.package = { seeall = seeall, } _xpcall (getparams) -- Changing curent directory to the script's "physical" dir local curr_dir = lfs.currentdir () _xpcall (function () lfs.chdir (script_pdir) end) -- Opening function _xpcall (open) -- Executing script local result = _xpcall (function () return handle (script_file) end) -- Closing function _xpcall (close) -- Cleanup reset () -- Changing to original directory _xpcall (function () lfs.chdir (curr_dir) end) if result then -- script executed ok! return result endend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -