⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgilua.lua

📁 cgi for lua, you can build your personal web site by lua
💻 LUA
📖 第 1 页 / 共 2 页
字号:
------------------------------------------------------------------------------ CGILua library.---- @release $Id: cgilua.lua,v 1.46 2007/08/21 20:15:55 carregal Exp $----------------------------------------------------------------------------local _G, SAPI = _G, SAPIlocal urlcode = require"cgilua.urlcode"local lp = require"cgilua.lp"local post = require"cgilua.post"local lfs = require"lfs"local debug = require"debug"local assert, error, ipairs, select, tostring, type, unpack, xpcall = assert, error, ipairs, select, tostring, type, unpack, xpcalllocal gsub, format, strfind, strlower, strsub = string.gsub, string.format, string.find, string.lower, string.sublocal setmetatable = setmetatablelocal _open = io.openlocal tinsert, tremove = table.insert, table.removelocal date = os.datelocal seeall = package.seealllp.setoutfunc ("cgilua.put")lp.setcompatmode (true)module ("cgilua")---- Internal state variables.local default_errorhandler = debug.tracebacklocal errorhandler = default_errorhandlerlocal default_erroroutput = function (msg)    if type(msg) ~= "string" and type(msg) ~= "number" then        msg = format ("bad argument #1 to 'error' (string expected, got %s)", type(msg))    end  	-- Logging error	SAPI.Response.errorlog (msg)	SAPI.Response.errorlog (" ")	SAPI.Response.errorlog (SAPI.Request.servervariable"REMOTE_ADDR")	SAPI.Response.errorlog (" ")	SAPI.Response.errorlog (date())	SAPI.Response.errorlog ("\n")	-- Building user message	SAPI.Response.contenttype ("text/html")	msg = gsub (gsub (msg, "\n", "<br>\n"), "\t", "&nbsp;&nbsp;")	SAPI.Response.write (msg)endlocal erroroutput = default_erroroutputlocal default_maxfilesize = 512 * 1024local maxfilesize = default_maxfilesizelocal default_maxinput = 1024 * 1024local maxinput = default_maxinputscript_path = false_COPYRIGHT = "Copyright (C) 2003-2007 Kepler Project"_DESCRIPTION = "CGILua is a tool for creating dynamic Web pages and manipulating input data from forms"_VERSION = "CGILua 5.1.0"---- Header functions------------------------------------------------------------------------------ Sends a header.-- @param header String with the header.-- @param value String with the corresponding value.----------------------------------------------------------------------------header = SAPI.Response.header------------------------------------------------------------------------------ Sends a Content-type header.-- @param type String with the type of the header.-- @param subtype String with the subtype of the header.----------------------------------------------------------------------------function contentheader (type, subtype)	SAPI.Response.contenttype (type..'/'..subtype)end------------------------------------------------------------------------------ Sends the HTTP header "text/html".----------------------------------------------------------------------------function htmlheader()	SAPI.Response.contenttype ("text/html")endlocal htmlheader = htmlheader------------------------------------------------------------------------------ Sends an HTTP header redirecting the browser to another URL-- @param url String with the URL.-- @param args Table with the arguments (optional).----------------------------------------------------------------------------function redirect (url, args)	if strfind(url,"^https?:") then		local params=""		if args then			params = "?"..urlcode.encodetable(args)		end		return SAPI.Response.redirect(url..params)	else		return SAPI.Response.redirect(mkabsoluteurl(mkurlpath(url,args)))	endend------------------------------------------------------------------------------ Returns a server variable-- @param name String with the name of the server variable.-- @return String with the value of the server variable.----------------------------------------------------------------------------servervariable = SAPI.Request.servervariable------------------------------------------------------------------------------ Primitive error output function-- @param msg String (or number) with the message.-- @param level String with the error level (optional).----------------------------------------------------------------------------function errorlog (msg, level)	local t = type(msg)	if t == "string" or t == "number" then		SAPI.Response.errorlog (msg, level)	else		error ("bad argument #1 to `cgilua.errorlog' (string expected, got "..t..")", 2)	endend------------------------------------------------------------------------------ Converts all its arguments to strings before sending them to the server.----------------------------------------------------------------------------function print (...)	local args = { ... }	for i = 1, select("#",...) do		args[i] = tostring(args[i])	end	SAPI.Response.write (unpack(args))end------------------------------------------------------------------------------ Function 'put' sends its arguments (basically strings of HTML text)--  to the server-- Its basic implementation is to use Lua function 'write', which writes--  each of its arguments (strings or numbers) to file _OUTPUT (a file--  handle initialized with the file descriptor for stdout)-- @param s String (or number) with output.----------------------------------------------------------------------------put = SAPI.Response.write------------------------------------------------------------------------------ Remove globals not allowed in CGILua scripts.-- @param notallowed Array of Strings with the names to be removed.----------------------------------------------------------------------------function removeglobals (notallowed)	for _, g in ipairs(notallowed) do		if type(_G[g]) ~= "function" then			_G[g] = nil		else			_G[g] = function()				 error("Function '"..g.."' is not allowed in CGILua scripts.")			end		end	endend------------------------------------------------------------------------------ Execute a script--  If an error is found, Lua's error handler is called and this function--  does not return-- @param filename String with the name of the file to be processed.-- @return The result of the execution of the file.----------------------------------------------------------------------------function doscript (filename)	local res, err = _G.loadfile(filename)	if not res then		error (format ("Cannot execute `%s'. Exiting.\n%s", filename, err))	else		return res ()	endend------------------------------------------------------------------------------ Execute the file if there is no "file error".--  If an error is found, and it is not a "file error", Lua 'error'--  is called and this function does not return-- @param filename String with the name of the file to be processed.-- @return The result of the execution of the file or nil (in case the--	file does not exists or if it cannot be opened).-- @return It could return an error message if the file cannot be opened.----------------------------------------------------------------------------function doif (filename)	if not filename then return end    -- no file	local f, err = _open(filename)	if not f then return nil, err end    -- no file (or unreadable file)	f:close()	return doscript (filename)end----------------------------------------------------------------------------- Set the maximum "total" input size allowed (in bytes)-- @param nbytes Number of the maximum size (in bytes) of the whole POST data.---------------------------------------------------------------------------function setmaxinput(nbytes)	maxinput = nbytesend----------------------------------------------------------------------------- Set the maximum size for an "uploaded" file (in bytes)-- Might be less or equal than maxinput.-- @param nbytes Number of the maximum size (in bytes) of a file.---------------------------------------------------------------------------function setmaxfilesize(nbytes)	maxfilesize = nbytesend------------------------------------------------------------------------------ Preprocess the content of a mixed HTML file and output a complete--   HTML document ( a 'Content-type' header is inserted before the--   preprocessed HTML )-- @param filename String with the name of the file to be processed.----------------------------------------------------------------------------function handlelp (filename)	htmlheader ()	lp.include (filename)end------------------------------------------------------------------------------ Builds a handler that sends a header and the contents of the given file.-- Sends the contents of the file to the output without processing it.-- @param type String with the type of the header.-- @param subtype String with the subtype of the header.-- @return Function (which receives a filename as argument) that produces--	the header and copies the content of the given file.----------------------------------------------------------------------------function buildplainhandler (type, subtype)	return function (filename)		contentheader (type, subtype)		local fh = assert (_open (filename))		local prog = fh:read("*a")		fh:close()		put (prog)	endend------------------------------------------------------------------------------ Builds a handler that sends a header and the processed file.-- Processes the file as a Lua Page.-- @param type String with the type of the header.-- @param subtype String with the subtype of the header.-- @return Function (which receives a filename as argument) that produces--	the header and processes the given file.----------------------------------------------------------------------------function buildprocesshandler (type, subtype)	return function (filename)		contentheader (type, subtype)		lp.include (filename)	endend------------------------------------------------------------------------------ Create an URL path to be used as a link to a CGILua script-- @param script String with the name of the script.-- @param args Table with arguments to script (optional).-- @return String in URL format.----------------------------------------------------------------------------function mkurlpath (script, args)	-- URL-encode the parameters to be passed do the script	local params = ""	if args then		params = "?"..urlcode.encodetable(args)	end	if strsub(script,1,1) == "/" then		return urlpath .. script .. params	else		return urlpath .. script_vdir .. script .. params	endend------------------------------------------------------------------------------ Create an absolute URL containing the given URL path

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -