以下代碼按預期工作,但只能使用一次。Openresty Lua腳本只能使用一次
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 entry thread aborted: runtime error: /home/myname/code/workbench/src/handler.lua:3: attempt to call global 'loadCheckfile' (a nil value)
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 –
將lua_code_cache關閉添加到nginx.conf中解決了這個問題。但我非常懷疑這是對工作環境的渴望。 – Mathias