Skip to content

Commit 099eec9

Browse files
authored
feature: supported to match route by uri_args and nginx var. (apache#5)
* feature: supported to match route by uri_args and nginx var.
1 parent 5490e19 commit 099eec9

File tree

4 files changed

+287
-2
lines changed

4 files changed

+287
-2
lines changed

lib/resty/radixtree.lua

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ function _M.new(routes)
199199
error("missing argument metadata or handler", 2)
200200
end
201201

202+
if route.uri_args then
203+
if type(route.uri_args) ~= "table" or #route.uri_args % 2 ~= 0 then
204+
error("invalid argument uri_args", 2)
205+
end
206+
end
207+
208+
if route.vars then
209+
if type(route.vars) ~= "table" or #route.vars % 2 ~= 0 then
210+
error("invalid argument vars", 2)
211+
end
212+
end
213+
202214
local method = route.method
203215
local bit_methods
204216
if type(method) ~= "table" then
@@ -247,8 +259,10 @@ function _M.new(routes)
247259
route_opts.path = path
248260

249261
route_opts.metadata = route.metadata
250-
route_opts.handler = route.handler
251-
route_opts.method = bit_methods
262+
route_opts.handler = route.handler
263+
route_opts.method = bit_methods
264+
route_opts.uri_args = route.uri_args
265+
route_opts.vars = route.vars
252266

253267
if route.remote_addr then
254268
local remote_addr = route.remote_addr
@@ -393,6 +407,32 @@ local function match_route_opts(route, opts)
393407
end
394408
end
395409

410+
if route.uri_args then
411+
if type(opts.uri_args) ~= "table" then
412+
return false
413+
end
414+
415+
for i = 1, #route.uri_args, 2 do
416+
local k, v = route.uri_args[i], route.uri_args[i + 1]
417+
if opts.uri_args[k] ~= v then
418+
return false
419+
end
420+
end
421+
end
422+
423+
if route.vars then
424+
if type(opts.vars) ~= "table" then
425+
return false
426+
end
427+
428+
for i = 1, #route.vars, 2 do
429+
local k, v = route.vars[i], route.vars[i + 1]
430+
if opts.vars[k] ~= v then
431+
return false
432+
end
433+
end
434+
end
435+
396436
return true
397437
end
398438

t/args.t

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use t::RX 'no_plan';
4+
5+
repeat_each(1);
6+
run_tests();
7+
8+
__DATA__
9+
10+
=== TEST 1: sanity
11+
--- config
12+
location /t {
13+
content_by_lua_block {
14+
local radix = require("resty.radixtree")
15+
local rx = radix.new({
16+
{
17+
path = "/aa",
18+
metadata = "metadata /aa",
19+
uri_args = {"k", "v"},
20+
}
21+
})
22+
23+
ngx.say(rx:match("/aa", {uri_args = {k="v"}}))
24+
ngx.say(rx:match("/aa", {uri_args = {}}))
25+
ngx.say(rx:match("/aa", {}))
26+
}
27+
}
28+
--- request
29+
GET /t
30+
--- no_error_log
31+
[error]
32+
--- response_body
33+
metadata /aa
34+
nil
35+
nil
36+
37+
38+
39+
=== TEST 2: invalid uri_args
40+
--- config
41+
location /t {
42+
content_by_lua_block {
43+
local radix = require("resty.radixtree")
44+
radix.new({
45+
{
46+
path = "/aa",
47+
metadata = "metadata /aa",
48+
uri_args = "xxx",
49+
}
50+
})
51+
}
52+
}
53+
--- request
54+
GET /t
55+
--- error_code: 500
56+
--- error_log
57+
invalid argument uri_args
58+
59+
60+
61+
=== TEST 3: invalid uri_args
62+
--- config
63+
location /t {
64+
content_by_lua_block {
65+
local radix = require("resty.radixtree")
66+
radix.new({
67+
{
68+
path = "/aa",
69+
metadata = "metadata /aa",
70+
uri_args = {"xxx"},
71+
}
72+
})
73+
}
74+
}
75+
--- request
76+
GET /t
77+
--- error_code: 500
78+
--- error_log
79+
invalid argument uri_args

t/remote.t

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ nil
9696
ngx.say(rx:match("/aa", {remote_addr = "::2"}))
9797
ngx.say(rx:match("/bb", {remote_addr = "::1"}))
9898
ngx.say(rx:match("/bb", {remote_addr = "::2"}))
99+
ngx.say(rx:match("/aa", {remote_addr = "127.0.0.1"}))
99100
}
100101
}
101102
--- request
@@ -107,6 +108,7 @@ metadata /aa -> ::1
107108
nil
108109
nil
109110
metadata /aa -> ::2
111+
nil
110112
111113
112114

t/vars.t

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use t::RX 'no_plan';
4+
5+
repeat_each(1);
6+
run_tests();
7+
8+
__DATA__
9+
10+
=== TEST 1: uri args
11+
--- config
12+
location /t {
13+
content_by_lua_block {
14+
local radix = require("resty.radixtree")
15+
local rx = radix.new({
16+
{
17+
path = "/aa",
18+
metadata = "metadata /aa",
19+
vars = {"arg_k", "v"},
20+
}
21+
})
22+
23+
ngx.say(rx:match("/aa", {vars = ngx.var}))
24+
}
25+
}
26+
--- request
27+
GET /t?k=v
28+
--- no_error_log
29+
[error]
30+
--- response_body
31+
metadata /aa
32+
33+
34+
35+
=== TEST 2: uri args(not hit)
36+
--- config
37+
location /t {
38+
content_by_lua_block {
39+
local radix = require("resty.radixtree")
40+
local rx = radix.new({
41+
{
42+
path = "/aa",
43+
metadata = "metadata /aa",
44+
vars = {"arg_k", "v"},
45+
}
46+
})
47+
48+
ngx.say(rx:match("/aa", {vars = ngx.var}))
49+
}
50+
}
51+
--- request
52+
GET /t?k=not_hit
53+
--- no_error_log
54+
[error]
55+
--- response_body
56+
nil
57+
58+
59+
60+
=== TEST 3: http header
61+
--- config
62+
location /t {
63+
content_by_lua_block {
64+
local radix = require("resty.radixtree")
65+
local rx = radix.new({
66+
{
67+
path = "/aa",
68+
metadata = "metadata /aa",
69+
vars = {"http_test", "v"},
70+
}
71+
})
72+
73+
ngx.say(rx:match("/aa", {vars = ngx.var}))
74+
}
75+
}
76+
--- more_headers
77+
test: v
78+
--- request
79+
GET /t
80+
--- no_error_log
81+
[error]
82+
--- response_body
83+
metadata /aa
84+
85+
86+
87+
=== TEST 4: http header(not hit)
88+
--- config
89+
location /t {
90+
content_by_lua_block {
91+
local radix = require("resty.radixtree")
92+
local rx = radix.new({
93+
{
94+
path = "/aa",
95+
metadata = "metadata /aa",
96+
vars = {"http_test", "v"},
97+
}
98+
})
99+
100+
ngx.say(rx:match("/aa", {vars = ngx.var}))
101+
}
102+
}
103+
--- more_headers
104+
test: not-hit
105+
--- request
106+
GET /t
107+
--- no_error_log
108+
[error]
109+
--- response_body
110+
nil
111+
112+
113+
114+
=== TEST 5: uri args + header + server_port
115+
--- config
116+
location /t {
117+
content_by_lua_block {
118+
local radix = require("resty.radixtree")
119+
local rx = radix.new({
120+
{
121+
path = "/aa",
122+
metadata = "metadata /aa",
123+
vars = {"arg_k", "v",
124+
"host", "localhost",
125+
"server_port", "1984"},
126+
}
127+
})
128+
129+
ngx.say(rx:match("/aa", {vars = ngx.var}))
130+
}
131+
}
132+
--- request
133+
GET /t?k=v
134+
--- no_error_log
135+
[error]
136+
--- response_body
137+
metadata /aa
138+
139+
140+
141+
=== TEST 6: uri args + header + server_port (not hit)
142+
--- config
143+
location /t {
144+
content_by_lua_block {
145+
local radix = require("resty.radixtree")
146+
local rx = radix.new({
147+
{
148+
path = "/aa",
149+
metadata = "metadata /aa",
150+
vars = {"arg_k", "v",
151+
"host", "localhost",
152+
"server_port", "1984-not"},
153+
}
154+
})
155+
156+
ngx.say(rx:match("/aa", {vars = ngx.var}))
157+
}
158+
}
159+
--- request
160+
GET /t?k=v
161+
--- no_error_log
162+
[error]
163+
--- response_body
164+
nil

0 commit comments

Comments
 (0)