Skip to content

Commit 8981fb3

Browse files
committed
Merge branch 'master' into record-matched
2 parents bfc5daf + 437a02d commit 8981fb3

File tree

3 files changed

+89
-25
lines changed

3 files changed

+89
-25
lines changed

lib/resty/radixtree.lua

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ local re_find = ngx.re.find
3030
local re_match = ngx.re.match
3131
local sort_tab = table.sort
3232
local ngx_re = require("ngx.re")
33+
local ngx_null = ngx.null
3334
local empty_table = {}
3435

3536

@@ -482,22 +483,29 @@ end
482483
local compare_funcs = {
483484
["=="] = function (l_v, r_v)
484485
if type(r_v) == "number" then
485-
return tonumber(l_v) == r_v
486+
l_v = tonumber(l_v)
487+
if not l_v then
488+
return false
489+
end
486490
end
487491
return l_v == r_v
488492
end,
489493
["~="] = function (l_v, r_v)
490494
return l_v ~= r_v
491495
end,
492496
[">"] = function (l_v, r_v)
493-
if type(r_v) == "number" then
494-
return tonumber(l_v) > r_v
497+
l_v = tonumber(l_v)
498+
r_v = tonumber(r_v)
499+
if not l_v or not r_v then
500+
return false
495501
end
496502
return l_v > r_v
497503
end,
498504
["<"] = function (l_v, r_v)
499-
if type(r_v) == "number" then
500-
return tonumber(l_v) < r_v
505+
l_v = tonumber(l_v)
506+
r_v = tonumber(r_v)
507+
if not l_v or not r_v then
508+
return false
501509
end
502510
return l_v < r_v
503511
end,
@@ -512,6 +520,10 @@ local compare_funcs = {
512520

513521

514522
local function compare_val(l_v, op, r_v, opts)
523+
if r_v == ngx_null then
524+
r_v = nil
525+
end
526+
515527
local com_fun = compare_funcs[op or "=="]
516528
if not com_fun then
517529
return false
@@ -596,19 +608,9 @@ local function match_route_opts(route, opts, ...)
596608
end
597609

598610
for _, route_var in ipairs(route.vars) do
599-
local l_v, op, r_v
600-
if #route_var == 2 then
601-
l_v, r_v = route_var[1], route_var[2]
602-
op = "=="
603-
else
604-
l_v, op, r_v = route_var[1], route_var[2], route_var[3]
605-
end
611+
local l_v, op, r_v = route_var[1], route_var[2], route_var[3]
606612
l_v = vars[l_v]
607613

608-
-- ngx.log(ngx.INFO, l_v, op, r_v)
609-
if l_v == nil or r_v == nil then
610-
return false
611-
end
612614
if not compare_val(l_v, op, r_v, opts) then
613615
return false
614616
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package = "lua-resty-radixtree"
2+
version = "1.9-0"
3+
source = {
4+
url = "git://github.com/api7/lua-resty-radixtree",
5+
tag = "v1.9",
6+
}
7+
8+
description = {
9+
summary = "Adaptive Radix Trees implemented in Lua for OpenResty",
10+
homepage = "https://github.com/api7/lua-resty-radixtree",
11+
license = "Apache License 2.0",
12+
maintainer = "Yuansheng Wang <[email protected]>"
13+
}
14+
15+
dependencies = {
16+
"lua-resty-ipmatcher",
17+
}
18+
19+
build = {
20+
type = "make",
21+
build_variables = {
22+
CFLAGS="$(CFLAGS) -std=c99 -g -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast",
23+
LIBFLAG="$(LIBFLAG)",
24+
LUA_LIBDIR="$(LUA_LIBDIR)",
25+
LUA_BINDIR="$(LUA_BINDIR)",
26+
LUA_INCDIR="$(LUA_INCDIR)",
27+
LUA="$(LUA)",
28+
},
29+
install_variables = {
30+
INST_PREFIX="$(PREFIX)",
31+
INST_BINDIR="$(BINDIR)",
32+
INST_LIBDIR="$(LIBDIR)",
33+
INST_LUADIR="$(LUADIR)",
34+
INST_CONFDIR="$(CONFDIR)",
35+
},
36+
}

t/vars.t

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __DATA__
1717
paths = "/aa",
1818
metadata = "metadata /aa",
1919
vars = {
20-
{"arg_k", "v"},
20+
{"arg_k", "==", "v"},
2121
},
2222
}
2323
})
@@ -71,7 +71,7 @@ nil
7171
paths = "/aa",
7272
metadata = "metadata /aa",
7373
vars = {
74-
{"http_test", "v"},
74+
{"http_test", "==", "v"},
7575
}
7676
}
7777
})
@@ -129,9 +129,9 @@ nil
129129
paths = "/aa",
130130
metadata = "metadata /aa",
131131
vars = {
132-
{"arg_k", "v"},
133-
{"host", "localhost"},
134-
{"server_port", "1984"},
132+
{"arg_k", "==", "v"},
133+
{"host", "==", "localhost"},
134+
{"server_port", "==", "1984"},
135135
}
136136
}
137137
})
@@ -187,9 +187,9 @@ nil
187187
paths = "/aa",
188188
metadata = "metadata /aa",
189189
vars = {
190-
{"arg_k", "v"},
191-
{"host", "localhost"},
192-
{"server_port", "1984"},
190+
{"arg_k", "==", "v"},
191+
{"host", "==", "localhost"},
192+
{"server_port", "==", "1984"},
193193
}
194194
}
195195
})
@@ -351,7 +351,7 @@ nil
351351
paths = "/aa",
352352
metadata = "metadata /aa",
353353
vars = {
354-
{"arg_k", "v"},
354+
{"arg_k", "==", "v"},
355355
},
356356
},
357357
{
@@ -472,3 +472,29 @@ GET /t
472472
[error]
473473
--- response_body
474474
nil
475+
476+
477+
478+
=== TEST 17: ~= nil
479+
--- config
480+
location /t {
481+
content_by_lua_block {
482+
local radix = require("resty.radixtree")
483+
local rx = radix.new({
484+
{
485+
paths = "/aa",
486+
metadata = "metadata /aa",
487+
vars = {
488+
{"arg_k", "~=", nil},
489+
},
490+
}
491+
})
492+
ngx.say(rx:match("/aa", {vars = ngx.var}))
493+
}
494+
}
495+
--- request
496+
GET /t?k=v
497+
--- no_error_log
498+
[error]
499+
--- response_body
500+
metadata /aa

0 commit comments

Comments
 (0)