Skip to content

Commit

Permalink
bugfix: only accept ternary operator for vars. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis authored Jun 18, 2020
1 parent 11f7b4e commit c6764c7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
34 changes: 18 additions & 16 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local re_find = ngx.re.find
local re_match = ngx.re.match
local sort_tab = table.sort
local ngx_re = require("ngx.re")
local ngx_null = ngx.null
local empty_table = {}


Expand Down Expand Up @@ -482,22 +483,29 @@ end
local compare_funcs = {
["=="] = function (l_v, r_v)
if type(r_v) == "number" then
return tonumber(l_v) == r_v
l_v = tonumber(l_v)
if not l_v then
return false
end
end
return l_v == r_v
end,
["~="] = function (l_v, r_v)
return l_v ~= r_v
end,
[">"] = function (l_v, r_v)
if type(r_v) == "number" then
return tonumber(l_v) > r_v
l_v = tonumber(l_v)
r_v = tonumber(r_v)
if not l_v or not r_v then
return false
end
return l_v > r_v
end,
["<"] = function (l_v, r_v)
if type(r_v) == "number" then
return tonumber(l_v) < r_v
l_v = tonumber(l_v)
r_v = tonumber(r_v)
if not l_v or not r_v then
return false
end
return l_v < r_v
end,
Expand All @@ -512,6 +520,10 @@ local compare_funcs = {


local function compare_val(l_v, op, r_v, opts)
if r_v == ngx_null then
r_v = nil
end

local com_fun = compare_funcs[op or "=="]
if not com_fun then
return false
Expand Down Expand Up @@ -589,19 +601,9 @@ local function match_route_opts(route, opts, ...)
end

for _, route_var in ipairs(route.vars) do
local l_v, op, r_v
if #route_var == 2 then
l_v, r_v = route_var[1], route_var[2]
op = "=="
else
l_v, op, r_v = route_var[1], route_var[2], route_var[3]
end
local l_v, op, r_v = route_var[1], route_var[2], route_var[3]
l_v = vars[l_v]

-- ngx.log(ngx.INFO, l_v, op, r_v)
if l_v == nil or r_v == nil then
return false
end
if not compare_val(l_v, op, r_v, opts) then
return false
end
Expand Down
44 changes: 35 additions & 9 deletions t/vars.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ __DATA__
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "v"},
{"arg_k", "==", "v"},
},
}
})
Expand Down Expand Up @@ -71,7 +71,7 @@ nil
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"http_test", "v"},
{"http_test", "==", "v"},
}
}
})
Expand Down Expand Up @@ -129,9 +129,9 @@ nil
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "v"},
{"host", "localhost"},
{"server_port", "1984"},
{"arg_k", "==", "v"},
{"host", "==", "localhost"},
{"server_port", "==", "1984"},
}
}
})
Expand Down Expand Up @@ -187,9 +187,9 @@ nil
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "v"},
{"host", "localhost"},
{"server_port", "1984"},
{"arg_k", "==", "v"},
{"host", "==", "localhost"},
{"server_port", "==", "1984"},
}
}
})
Expand Down Expand Up @@ -351,7 +351,7 @@ nil
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "v"},
{"arg_k", "==", "v"},
},
},
{
Expand Down Expand Up @@ -472,3 +472,29 @@ GET /t
[error]
--- response_body
nil
=== TEST 17: ~= nil
--- config
location /t {
content_by_lua_block {
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = "/aa",
metadata = "metadata /aa",
vars = {
{"arg_k", "~=", nil},
},
}
})
ngx.say(rx:match("/aa", {vars = ngx.var}))
}
}
--- request
GET /t?k=v
--- no_error_log
[error]
--- response_body
metadata /aa

0 comments on commit c6764c7

Please sign in to comment.