【发布时间】:2018-01-30 06:55:06
【问题描述】:
以下代码按预期工作,但只有一次。
require("loadCheckfile")
require("checkValPairs")
local checklist = loadCheckfile("/home/myname/code/workbench/src/check.lst")
local keyList = {}
local valList = {}
-- Load GET arguments into tables
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
table.insert(keyList, key)
table.insert(valList, val)
end
-- show values in table (just for testing)
ngx.say(unpack(keyList))
ngx.say(unpack(valList))
local key1
-- search for keywords and look them up in the checklist
for i = 1, table.maxn(keyList) do
if keyList[i] == "user" then
key1 = i
for j = 1, table.maxn(keyList) do
if keyList[j] == "pass" then
doesFit = checkValPairs(checklist, keyList[key1], valList[key1], keyList[j], valList[j])
end
end
end
end
-- Show wether the combination fits or not
ngx.say(doesFit)
在第二次运行时,即使是从新的浏览器窗口,我也会收到以下错误:
*1 lua 入口线程中止:运行时错误:/home/myname/code/workbench/src/handler.lua:3: 尝试调用全局“loadCheckfile”(一个零值)
nginx.conf(仅用于开发,不是最终版):
user root;
worker_processes 1;
daemon off;
error_log /dev/stdout warn;
events {
worker_connections 32;
}
http {
default_type text/html;
access_log off;
lua_package_path '/home/myname/code/workbench/src/?.lua;;';
server {
listen 80;
location / {
content_by_lua_file /home/myname/code/workbench/src/handler.lua;
}
}
}
loadCheckfile.lua:
local function fillChecklistTable(checklist, valueLine)
repeat
firstValLength = string.find(valueLine,"=")
firstVal = string.sub(valueLine, 1, firstValLength-1)
valueLine = string.sub(valueLine, firstValLength+1)
secondValLength = string.find(valueLine, ",")
if secondValLength ~= nil then
secondVal = string.sub(valueLine, 1, secondValLength-1)
else
secondVal = valueLine
end
if checklist[firstVal] == nil then
checklist[firstVal] = {secondVal}
else
table.insert(checklist[firstVal], secondVal)
end
if secondValLength ~= nil then
valueLine = string.sub(valueLine, secondValLength+1)
else
valueLine = nil
end
until valueLine == nil
end
checklist = {}
function loadCheckfile(checkfile)
local values = io.open(checkfile)
local valueLine = values:read()
while valueLine ~= nil do
fillChecklistTable(checklist, valueLine)
valueLine = values:read()
end
return checklist
end
有人知道这个菜鸟又做错了什么吗?提前致谢!
更新:
handler.lua
checklist = {}
local checkFile = require("loadCheckfile")
local checkPairs = require("checkValPairs")
local checklist = checkFile.loadCheckfile("/home/myname/code/workbench/src/pw_list.txt")
local keyList = {}
local valList = {}
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
table.insert(keyList, key)
table.insert(valList, val)
end
ngx.say(unpack(keyList))
ngx.say(unpack(valList))
local key1
for i = 1, table.maxn(keyList) do
if keyList[i] == "user" then
key1 = i
for j = 1, table.maxn(keyList) do
if keyList[j] == "pass" then
doesFit = checkValPairs(checklist, keyList[key1], valList[key1], keyList[j], valList[j])
end
end
end
end
ngx.say(doesFit)
loadCheckfile.lua
module("loadCheckfile", package.seeall)
local function fillChecklistTable(checklist, valueLine)
repeat
firstValLength = string.find(valueLine,"=")
firstVal = string.sub(valueLine, 1, firstValLength-1)
valueLine = string.sub(valueLine, firstValLength+1)
secondValLength = string.find(valueLine, ",")
if secondValLength ~= nil then
secondVal = string.sub(valueLine, 1, secondValLength-1)
else
secondVal = valueLine
end
if checklist[firstVal] == nil then
checklist[firstVal] = {secondVal}
else
table.insert(checklist[firstVal], secondVal)
end
if secondValLength ~= nil then
valueLine = string.sub(valueLine, secondValLength+1)
else
valueLine = nil
end
until valueLine == nil
end
checklist = {}
function loadCheckfile.loadCheckfile(checkfile)
local values = io.open(checkfile)
local valueLine = values:read()
while valueLine ~= nil do
fillChecklistTable(checklist, valueLine)
valueLine = values:read()
end
return checklist
end
根据this source,我只将模块放入了loadCheckfile.lua 和checkValPairs.lua。然而,即使把它放进 handler.lua 没有工作(只是不得不尝试)。
【问题讨论】:
-
同时发布 nginx 配置和 loadCheckfile
-
在 nginx.conf 中添加 lua_code_cache off 解决了这个问题。然而,我非常怀疑这是否适合工作环境。