2017-04-05 95 views
-1

我使用lua与nginx。要访问我的ES群集,我使用lua代码进行配置。我想知道的代码是如何工作..任何人都可以解释下面的LUA代码是如何工作的?

-- get URL 
local uri = ngx.var.uri 

-- get method 
local method = ngx.req.get_method() 

local allowed = false 

for path, methods in pairs(restrictions[role]) do 
    -- path matched rules? 
    local p = string.match(uri, path) 

    -- method matched rules? 
    local m = nil 
    for _, _method in pairs(methods) do 
    m = m and m or string.match(method, _method) 
    end 

    if p and m then 
    allowed = true 
    break 
    end 
end 

if not allowed then 
    return write403Message() 
end 

让被URL: http://localhost/_GETmethod:GETpath:/_GET 然后

local p = string.match(uri, path) -->Then p variable has value GET(i.e p=GET) 

纠正我,如果我错了吗?

for _, _method in pairs(methods) do 
     m = m and m or string.match(method, _method) 
     end 

上面的代码片段会做什么?

回答

1

我建议你到Lua参考手册,如果你想知道的代码做什么:

https://www.lua.org/manual/5.3/manual.html#pdf-string.match

如果你想知道什么string.match在你的榜样回报您可以使用打印功能。

例如:

p = string.match(uri, path) 
print(p) 

对于示例:

让被URL:http://localhost/_GET,method:GET,路径:/ _ GET然后

局部p = string.match(URI,路径) - >然后p变量的值为GET(即 p = GET)

p会实际参考"/_GET"

/_GET是你的模式。 string.match将返回这个简单的dinstinct模式的匹配就是模式本身。

例如,如果您要求例如匹配任何数字,例如您会从字符串中获得实际数字。

相关问题