Cgi 工具

lua-users home
wiki

CGI 头部

print"Content-type: text/html; charset=iso-8859-1\r\n\r\n"

解析 POST 变量

-- Written by RiciLake.
-- The author places the code into the public domain, renouncing all rights and responsibilities.
-- Don't use this in a real application, see notes below.
function parsecgi(str)
  local rv = {}
  for pair in str:gmatch"[^&]+" do
    local key, val = pair:match"([^=]*)=(.*)"
    if key then rv[key] = val end
  end
  return rv
end

此实现存在多种安全隐患

所以这里有一个稍微好一点的版本,它仍然没有处理“搜索”情况。您必须提供一个新创建的合法键表;如果一个键可以有多个值,您提供一个表作为关联值(它会被填充),否则您提供默认值(或 false)。非法键会被忽略或抛出错误。没有 = 的段落会抛出错误。抛出错误是因为无效的查询/发布字符串很可能是攻击企图,并且应该(在我看来)被拒绝;因此,您应该将 CGI 处理程序包装在 pcall 中,并向浏览器返回 403404 错误。

该函数故意不尝试对键进行 %-解码,因为有效的键永远不需要 %-编码,因此这种形式的 URL 很可能是一种模糊的攻击服务器的企图。

一个可能的改进是,如果初始表中默认值为数字,则还检查提供的 value 是否为数字。

-- Written by RiciLake.
-- The author places the code into the public domain, renouncing all rights and responsibilities.
-- Replace + with space and %xx with the corresponding character.
local function cgidecode(str)
  return (str:gsub('+', ' '):gsub("%%(%x%x)", function(xx) return string.char(tonumber(xx, 16)) end))
end

-- Main function
-- Sample invocation:  cgivals = parsecgi(str, {count = 10, start = 1, names = {}})
function parsecgi(str, keys, ignore_invalid)
  local keyfound = {}
  for pair in str:gmatch"[^&]+" do
    local key, val = pair:match"([^=]*)=(.*)"
    if not key then error"Invalid query string" end
    local default = keys[key]
    if default == nil then
      if not ignore_invalid then error"Invalid query string" end
    else
      if type(default) == "table" then default[#default+1] = cgidecode(val)
      elseif keyfound[key] then error"Invalid query string"
      else
        keyfound[key] = true
        keys[key] = cgidecode(val)
      end
    end
  end
  return keys
end

解析 GET 变量

str = os.getenv("QUERY_STRING")
...

处理 SCGI 请求

-- Assigns header information to variable "l", and returns "Hello!" back as the webpage content.
-- This could use some cleaning up - error checking, removal of repetition, improved scoping.

-- The SCGI protocol has the HTTP requests forwarded to a specified port (default 4000), with
-- headers passed directly through TCP (and an ASCII string length prefix followed by a ":", 
-- with the key-value pairs of the header seperated by null characters "\0")

-- With no parsing, this code handles about 950 requests per second on a 2 year old laptop.

local socket = require("socket")
local host = host or "*"
local port = port or 4000
local s = assert(socket.bind(host, port))
local i, p   = s:getsockname()
assert(i, p)
print("Waiting on " .. i .. ":" .. p .. "...")
while 1 do
  c = assert(s:accept())
  print("Connection requested.")
  len = ""
  l, e = c:receive(1)
  while not e do
    if l == ":" then header_len = tonumber(len) ; break end
    len = len .. l
    l, e = c:receive(1)
  end
  l,e = c:receive(header_len)
  c:send("Status: 200 OK\r\n")
  c:send("Content-Type: text/plain\r\n")
  c:send("\r\n")
  c:send("Hello!")
  c:close()
end

以下 httpd.conf (Apache 2) 中的代码行将允许此 SCGI 示例正常工作 - 尽管 127.0.0.1 需要更改为更合适的 IP 地址。

LoadModule scgi_module modules/mod_scgi.so
SCGIMount /dynamic 127.0.0.1:4000

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2007 年 11 月 13 日凌晨 12:13 GMT (差异)